automotive-dlt
dlt_cdh_coredump.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 
32 #include <fcntl.h>
33 #include <syslog.h>
34 #include <errno.h>
35 
36 #include <sys/time.h>
37 #include <sys/resource.h>
38 #include <sys/stat.h>
39 
40 #include "dlt_cdh.h"
41 
43 {
44  int phnum = 0;
45 
46  // Read ELF header
47  stream_read(&p_proc->streamer, &p_proc->m_Ehdr, sizeof(p_proc->m_Ehdr));
48 
49  // Read until PROG position
50  stream_move_to_offest(&p_proc->streamer, p_proc->m_Ehdr.e_phoff);
51 
52  // Read and store all program headers
53  p_proc->m_pPhdr = (ELF_Phdr*) malloc(sizeof(ELF_Phdr) * p_proc->m_Ehdr.e_phnum);
54  if (p_proc->m_pPhdr == NULL)
55  {
56  syslog(LOG_ERR, "Cannot allocate Phdr memory (%d headers)", p_proc->m_Ehdr.e_phnum);
57  return CDH_NOK;
58  }
59 
60  for (phnum = 0; phnum < p_proc->m_Ehdr.e_phnum; phnum++)
61  {
62  // Read Programm header
63  stream_read(&p_proc->streamer, &p_proc->m_pPhdr[phnum], sizeof(ELF_Phdr));
64  }
65 
66  return CDH_OK;
67 }
68 
70 {
71  int i = 0;
72 
73  // Search PT_NOTE section
74  for (i = 0; i < p_proc->m_Ehdr.e_phnum; i++)
75  {
76  syslog(LOG_INFO, "==Note section prog_note:%d type:0x%X offset:0x%X size:0x%X (%dbytes)",
77  i,
78  p_proc->m_pPhdr[i].p_type,
79  p_proc->m_pPhdr[i].p_offset,
80  p_proc->m_pPhdr[i].p_filesz,
81  p_proc->m_pPhdr[i].p_filesz);
82 
83  if (p_proc->m_pPhdr[i].p_type == PT_NOTE)
84  break;
85  }
86 
87  return i == p_proc->m_Ehdr.e_phnum ? CDH_NOK : i;
88 }
89 
91 {
92  int prog_note = getNotePageIndex(p_proc);
93 // p_proc->m_note_page_size = 0;
94  p_proc->m_Nhdr = NULL;
95 
96  // note page not found, abort
97  if (prog_note < 0)
98  {
99  syslog(LOG_ERR, "Cannot find note header page index");
100  return CDH_NOK;
101  }
102 
103  // Move to NOTE header position
104  if (stream_move_to_offest(&p_proc->streamer, p_proc->m_pPhdr[prog_note].p_offset) != CDH_OK)
105  {
106  syslog(LOG_ERR, "Cannot move to note header");
107  return CDH_NOK;
108  }
109 
110  if ((p_proc->m_Nhdr = (char*) malloc(p_proc->m_pPhdr[prog_note].p_filesz)) == NULL)
111  {
112  syslog(LOG_ERR, "Cannot allocate Nhdr memory (note size %d bytes)", p_proc->m_pPhdr[prog_note].p_filesz);
113  return CDH_NOK;
114  }
115 
116  if (stream_read(&p_proc->streamer, p_proc->m_Nhdr, p_proc->m_pPhdr[prog_note].p_filesz) != CDH_OK)
117  {
118  syslog(LOG_ERR, "Cannot read note header");
119  return CDH_NOK;
120  }
121 
122  p_proc->m_note_page_size = p_proc->m_pPhdr[prog_note].p_filesz;
123 
124  return CDH_OK;
125 }
126 
128 {
129  if (p_proc == NULL)
130  return CDH_NOK;
131 
132  if (p_proc->can_create_coredump)
133  {
134  char l_dst_filename[CORE_MAX_FILENAME_LENGTH];
135 
136  snprintf(l_dst_filename, sizeof(l_dst_filename), CORE_FILE_PATTERN,
138  p_proc->timestamp,
139  p_proc->name,
140  p_proc->pid);
141 
142  stream_init(&p_proc->streamer, 0, l_dst_filename);
143  }
144  else
145  {
146  stream_init(&p_proc->streamer, 0, NULL);
147  }
148 
149  return CDH_OK;
150 }
151 
153 {
154  stream_close(&p_proc->streamer);
155 
156  return CDH_OK;
157 }
158 
160 {
161  cdh_status_t ret = CDH_OK;
162 
163  // open src and dest files, allocate read buffer
164  if (init_coredump(p_proc) != CDH_OK)
165  {
166  syslog(LOG_ERR, "cannot init coredump system");
167  ret = CDH_NOK;
168  goto finished;
169  }
170 
171  if (read_elf_headers(p_proc) == CDH_OK)
172  {
173  // TODO: No NOTES here leads to crash elsewhere!!! dlt_cdh_crashid.c: around line 76
174  if (read_notes(p_proc) != CDH_OK)
175  {
176  syslog(LOG_ERR, "cannot read NOTES");
177  ret = CDH_NOK;
178  goto finished;
179  }
180  }
181  else
182  {
183  if (read_elf_headers(p_proc) != CDH_OK)
184  {
185  syslog(LOG_ERR, "cannot read ELF header");
186  ret = CDH_NOK;
187  goto finished;
188  }
189  }
190 
191  finished:
192  // In all cases, we try to finish to read/compress the coredump until the end
193  if (stream_finish(&p_proc->streamer) != CDH_OK)
194  syslog(LOG_ERR, "cannot finish coredump compression");
195 
196  // In all cases, let's close the files
197  if (close_coredump(p_proc) != CDH_OK)
198  syslog(LOG_ERR, "cannot close coredump system");
199 
200  return ret;
201 }
202 
unsigned int m_note_page_size
Definition: dlt_cdh.h:79
ELF_Ehdr m_Ehdr
Definition: dlt_cdh.h:75
cdh_status_t treat_coredump(proc_info_t *p_proc)
char name[MAX_PROC_NAME_LENGTH]
Definition: dlt_cdh.h:65
int stream_finish(file_streamer_t *p_fs)
cdh_status_t
int stream_move_to_offest(file_streamer_t *p_fs, unsigned int p_offset)
cdh_status_t stream_read(file_streamer_t *p_fs, void *p_buf, unsigned int p_size)
#define CORE_TMP_DIRECTORY
Definition: dlt_cdh.h:40
cdh_status_t stream_close(file_streamer_t *p_fs)
int can_create_coredump
Definition: dlt_cdh.h:71
cdh_status_t stream_init(file_streamer_t *p_fs, const char *p_src_fname, const char *p_dst_fname)
file_streamer_t streamer
Definition: dlt_cdh.h:72
pid_t pid
Definition: dlt_cdh.h:67
cdh_status_t close_coredump(proc_info_t *p_proc)
cdh_status_t init_coredump(proc_info_t *p_proc)
#define ELF_Phdr
Definition: dlt_cdh.h:51
int getNotePageIndex(proc_info_t *p_proc)
#define CORE_FILE_PATTERN
Definition: dlt_cdh.h:47
cdh_status_t read_notes(proc_info_t *p_proc)
uint32_t timestamp
Definition: dlt_cdh.h:68
char * m_Nhdr
Definition: dlt_cdh.h:77
#define CORE_MAX_FILENAME_LENGTH
Definition: dlt_cdh.h:42
cdh_status_t read_elf_headers(proc_info_t *p_proc)
#define NULL
Definition: dlt_common.h:232
ELF_Phdr * m_pPhdr
Definition: dlt_cdh.h:76