#define NELEMS(arr) (sizeof(arr)/sizeof(arr[0]))
enum {
- OPT_HELP,
- OPT_REPORT,
- OPT_SAVE_CORE,
+ OPT_HELP,
+ OPT_REPORT,
+ OPT_SAVE_CORE,
};
const struct option opts[] = {
- { "help", no_argument, 0, OPT_HELP },
- { "report", no_argument, 0, OPT_REPORT },
- { "save-core", required_argument, 0, OPT_SAVE_CORE },
- { 0, 0, 0, 0 }
+ { "help", no_argument, 0, OPT_HELP },
+ { "report", no_argument, 0, OPT_REPORT },
+ { "save-core", required_argument, 0, OPT_SAVE_CORE },
+ { 0, 0, 0, 0 }
};
static char *argv0 = "";
static void usage(void)
{
- fprintf(stderr, "usage: %s [--help] [--save-core FILE_NAME] [--report] PID UID GID SIGNAL DUMPTIME EXE\n",
- argv0);
+ fprintf(stderr, "usage: %s [--help] [--save-core FILE_NAME] [--report] PID UID GID SIGNAL DUMPTIME EXE\n",
+ argv0);
}
/* read file to buffer
*/
static int procfs_read_fileline(const char *pid, const char *filename, char *outbuf, int outsize)
{
- char *path = NULL;
- int fd;
- int n;
- int ret = 0;
-
- if (!(outsize > 0))
- return 0;
-
- if (asprintf(&path, "/proc/%s/%s", pid, filename) == -1)
- return -ENOMEM;
-
- fd = open(path, O_RDONLY);
- if (fd == -1) {
- ret = -errno;
- goto err;
- }
-
- /* XXX we are really assuming here that one read is enough */
- ret = read(fd, outbuf, outsize);
- if (ret == -1 || ret == outsize /* no place for \0 */) {
- ret = -errno;
- goto err;
- }
-
- n = ret;
- outbuf[n] = 0;
- for (; n > 0; --n) {
- if (outbuf[n] == '\n')
- outbuf[n] = 0;
- }
-
- close(fd);
-
- free(path);
- return ret;
+ char *path = NULL;
+ int fd;
+ int n;
+ int ret = 0;
+
+ if (!(outsize > 0))
+ return 0;
+
+ if (asprintf(&path, "/proc/%s/%s", pid, filename) == -1)
+ return -ENOMEM;
+
+ fd = open(path, O_RDONLY);
+ if (fd == -1) {
+ ret = -errno;
+ goto err;
+ }
+
+ /* XXX we are really assuming here that one read is enough */
+ ret = read(fd, outbuf, outsize);
+ if (ret == -1 || ret == outsize /* no place for \0 */) {
+ ret = -errno;
+ goto err;
+ }
+
+ n = ret;
+ outbuf[n] = 0;
+ for (; n > 0; --n) {
+ if (outbuf[n] == '\n')
+ outbuf[n] = 0;
+ }
+
+ close(fd);
+
+ free(path);
+ return ret;
err:
- if (fd >= 0)
- close(fd);
- free(path);
- *outbuf = 0;
- return ret;
+ if (fd >= 0)
+ close(fd);
+ free(path);
+ *outbuf = 0;
+ return ret;
}
void print_multiline(char *buf, int buf_size)
{
- int i;
- int pos;
+ int i;
+ int pos;
- for (pos = i = 0; buf[pos] && pos < buf_size; ++ i, pos += strlen(buf + pos) + 1)
- printf("%21d: %s\n", i, buf + pos);
+ for (pos = i = 0; buf[pos] && pos < buf_size; ++i, pos += strlen(buf + pos) + 1)
+ printf("%21d: %s\n", i, buf + pos);
}
static void report(int argc, char *argv[])
{
- const char *pidstr = argv[0];
- const char *uidstr = argv[1];
- const char *gidstr = argv[2];
- const char *sigstr = argv[3];
- const char *timestr = argv[4];
- const char *exestr = argv[5];
- static const struct {
- char *file;
- char *desc;
- int is_multiline;
- } proc_filedesc[] = {
- { "comm", "Comm", 0},
- { "cgroup", "CGroup", 0 },
- { "attr/current", "MAC Label", 0 },
- { "oom_score", "OOM Score", 0 },
- { "oom_score_adj", "OOM Score Adj", 0 },
- { "cmdline", "Cmdline", 1 },
- { "environ", "Environment", 1 }
- };
-
- int i;
- int n;
+ const char *pidstr = argv[0];
+ const char *uidstr = argv[1];
+ const char *gidstr = argv[2];
+ const char *sigstr = argv[3];
+ const char *timestr = argv[4];
+ const char *exestr = argv[5];
+ static const struct {
+ char *file;
+ char *desc;
+ int is_multiline;
+ } proc_filedesc[] = {
+ { "comm", "Comm", 0},
+ { "cgroup", "CGroup", 0 },
+ { "attr/current", "MAC Label", 0 },
+ { "oom_score", "OOM Score", 0 },
+ { "oom_score_adj", "OOM Score Adj", 0 },
+ { "cmdline", "Cmdline", 1 },
+ { "environ", "Environment", 1 }
+ };
+
+ int i;
+ int n;
#define PROC_READ_MAX 16384 /* 4 pages should be enough for any process */
- static char proc_readbuf[PROC_READ_MAX];
-
- printf("Crash report for: %s\n\n", exestr);
-
- printf(" - passed from kernel -\n"
- "%16s: %s\n"
- "%16s: %s\n"
- "%16s: %s\n"
- "%16s: %s\n"
- "%16s: %s\n"
- "%16s: %s\n\n",
- "PID", pidstr,
- "UID", uidstr,
- "GID", gidstr,
- "Signal number", sigstr,
- "Timestamp", timestr,
- "Executable", exestr);
-
- printf(" - procfs information -\n");
-
- for (i = 0; i < NELEMS(proc_filedesc); ++ i) {
- n = procfs_read_fileline(pidstr, proc_filedesc[i].file, proc_readbuf, sizeof(proc_readbuf));
- if (n < 0)
- snprintf(proc_readbuf, sizeof(proc_readbuf), "Error (%d)", -n);
-
- if (n < 0 || proc_filedesc[i].is_multiline == 0)
- printf("%16s: %s\n", proc_filedesc[i].desc, proc_readbuf);
- else {
- printf("%16s:\n", proc_filedesc[i].desc);
- print_multiline(proc_readbuf, n);
- }
- }
+ static char proc_readbuf[PROC_READ_MAX];
+
+ printf("Crash report for: %s\n\n", exestr);
+
+ printf(" - passed from kernel -\n"
+ "%16s: %s\n"
+ "%16s: %s\n"
+ "%16s: %s\n"
+ "%16s: %s\n"
+ "%16s: %s\n"
+ "%16s: %s\n\n",
+ "PID", pidstr,
+ "UID", uidstr,
+ "GID", gidstr,
+ "Signal number", sigstr,
+ "Timestamp", timestr,
+ "Executable", exestr);
+
+ printf(" - procfs information -\n");
+
+ for (i = 0; i < NELEMS(proc_filedesc); ++i) {
+ n = procfs_read_fileline(pidstr, proc_filedesc[i].file, proc_readbuf, sizeof(proc_readbuf));
+ if (n < 0)
+ snprintf(proc_readbuf, sizeof(proc_readbuf), "Error (%d)", -n);
+
+ if (n < 0 || proc_filedesc[i].is_multiline == 0)
+ printf("%16s: %s\n", proc_filedesc[i].desc, proc_readbuf);
+ else {
+ printf("%16s:\n", proc_filedesc[i].desc);
+ print_multiline(proc_readbuf, n);
+ }
+ }
}
static int save_core(const char *core_path)
{
- int fd;
- static char buf[4096];
- int readb, remaining;
-
- fd = open(core_path, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
- if (fd == -1) {
- syslog(LOG_ERR, "crash-pipe: Unable to save core file to %s: %m\n", core_path);
- return -1;
- }
-
- while ((readb = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
- int n;
-
- for (n = 0, remaining = readb ; remaining > 0; remaining -= n) {
- n = write(fd, buf, remaining);
- if (n == -1) {
- syslog(LOG_ERR, "crash-pipe: Error while saving core file %s: %m. Removing core.\n", core_path);
- (void)unlink(core_path); // XXX check errors here too
- goto out;
- }
- }
- }
+ int fd;
+ static char buf[4096];
+ int readb, remaining;
+
+ fd = open(core_path, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
+ if (fd == -1) {
+ syslog(LOG_ERR, "crash-pipe: Unable to save core file to %s: %m\n", core_path);
+ return -1;
+ }
+
+ while ((readb = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
+ int n;
+
+ for (n = 0, remaining = readb ; remaining > 0; remaining -= n) {
+ n = write(fd, buf, remaining);
+ if (n == -1) {
+ syslog(LOG_ERR, "crash-pipe: Error while saving core file %s: %m. Removing core.\n", core_path);
+ (void)unlink(core_path); // XXX check errors here too
+ goto out;
+ }
+ }
+ }
out:
- close(fd);
+ close(fd);
- return 0;
+ return 0;
}
int main(int argc, char *argv[])
{
- int c;
- int opt_report = 0;
- char *opt_save_core = NULL;
- int ret = 1;
+ int c;
+ int opt_report = 0;
+ char *opt_save_core = NULL;
+ int ret = 1;
- prctl(PR_SET_DUMPABLE, 0);
+ prctl(PR_SET_DUMPABLE, 0);
- argv0 = argv[0];
+ argv0 = argv[0];
+ while ((c = getopt_long_only(argc, argv, "", opts, NULL)) != -1) {
- while ((c = getopt_long_only(argc, argv, "", opts, NULL)) != -1) {
+ if (c == OPT_HELP) {
+ usage();
+ exit(EXIT_SUCCESS);
+ } else if (c == OPT_REPORT) {
+ opt_report = 1;
+ } else if (c == OPT_SAVE_CORE) {
+ opt_save_core = strdup(optarg);
+ if (!opt_save_core) {
+ syslog(LOG_CRIT, "Out of memory. Exiting.");
+ exit(EXIT_FAILURE);
+ }
+ }
+ }
- if (c == OPT_HELP) {
- usage();
- exit(EXIT_SUCCESS);
- }
- else if (c == OPT_REPORT) {
- opt_report = 1;
- }
- else if (c == OPT_SAVE_CORE) {
- opt_save_core = strdup(optarg);
- if (!opt_save_core) {
- syslog(LOG_CRIT, "Out of memory. Exiting.");
- exit(EXIT_FAILURE);
- }
- }
- }
+ argc -= optind;
+ argv += optind;
- argc -= optind;
- argv += optind;
+ if (opt_report)
+ report(argc, argv);
- if (opt_report)
- report(argc, argv);
+ if (opt_save_core)
+ ret = save_core(opt_save_core);
- if (opt_save_core)
- ret = save_core(opt_save_core);
-
- return ret >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+ return ret >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
static Mappings *g_mappings = NULL;
static pid_t g_pid = 0;
-struct Regs
-{
+struct Regs {
Dwarf_Addr regs[REGS_REGULAR_NUM];
Dwarf_Addr sp;
Dwarf_Addr lr;
typedef struct Regs Regs;
static Regs g_regs;
-void *get_place_for_register_value (const char *regname, int regnum)
+void *get_place_for_register_value(const char *regname, int regnum)
{
- if (strcmp (regname, "pc") == 0 || REG_PC == regnum)
- {
+ if (strcmp(regname, "pc") == 0 || REG_PC == regnum)
return &g_regs.pc;
- }
- else if (strcmp (regname, "sp") == 0 || REG_SP == regnum)
- {
+ else if (strcmp(regname, "sp") == 0 || REG_SP == regnum)
return &g_regs.sp;
- }
- else if (strcmp (regname, "lr") == 0 || REG_LR == regnum)
- {
+ else if (strcmp(regname, "lr") == 0 || REG_LR == regnum)
return &g_regs.lr;
- }
- else if (strcmp (regname, "spsr") == 0 || REG_SPSR == regnum)
- {
+ else if (strcmp(regname, "spsr") == 0 || REG_SPSR == regnum)
return &g_regs.spsr;
- }
else if (regnum < REGS_REGULAR_NUM)
- {
return &g_regs.regs[regnum];
- }
return NULL;
}
-static Boolean report (void *data, Int32 address)
+static Boolean report(void *data, Int32 address)
{
Callstack *callstack = (Callstack *)(data);
callstack->tab[callstack->elems++] = address;
return callstack->elems < MAX_CALLSTACK_LEN ? TRUE : FALSE;
}
-Boolean readT (Int32 a, void *v, size_t size)
+Boolean readT(Int32 a, void *v, size_t size)
{
Dwfl_Module *module = 0;
Elf_Data *data = NULL;
- int segment = dwfl_addrsegment (g_dwfl, a, &module);
+ int segment = dwfl_addrsegment(g_dwfl, a, &module);
- if (module != NULL)
- {
+ if (module != NULL) {
Dwarf_Addr start;
- dwfl_module_info (module, NULL, &start, NULL, NULL, NULL, NULL, NULL);
+ dwfl_module_info(module, NULL, &start, NULL, NULL, NULL, NULL, NULL);
GElf_Addr bias;
- Elf *elf = dwfl_module_getelf (module, &bias);
+ Elf *elf = dwfl_module_getelf(module, &bias);
- data = elf_getdata_rawchunk (elf, a-start, size, ELF_T_BYTE);
+ data = elf_getdata_rawchunk(elf, a-start, size, ELF_T_BYTE);
}
- if (NULL == data && segment != -1)
- {
+ if (NULL == data && segment != -1) {
// get data from segment
GElf_Phdr mem;
GElf_Phdr *phdr;
Dwarf_Addr offset_in_segment;
- phdr = gelf_getphdr (g_core, segment, &mem);
- if (phdr != NULL)
- {
+ phdr = gelf_getphdr(g_core, segment, &mem);
+ if (phdr != NULL) {
offset_in_segment = a - phdr->p_vaddr;
- if (offset_in_segment < phdr->p_filesz)
- {
+ if (offset_in_segment < phdr->p_filesz) {
Dwarf_Addr offset_in_file = phdr->p_offset + offset_in_segment;
- data = elf_getdata_rawchunk (g_core, offset_in_file, size, ELF_T_BYTE);
+ data = elf_getdata_rawchunk(g_core, offset_in_file, size, ELF_T_BYTE);
}
}
}
- if (NULL == data && module != NULL)
- {
- const char *name = dwfl_module_info (module, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
- if (name != NULL && name[0] == '[')
- {
+ if (NULL == data && module != NULL) {
+ const char *name = dwfl_module_info(module, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
+ if (name != NULL && name[0] == '[') {
int i;
// get module from mappings
- for (i = 0; i < g_mappings->elems; i++)
- {
- if (g_mappings->tab[i].m_start <= a && a < g_mappings->tab[i].m_end)
- {
+ for (i = 0; i < g_mappings->elems; i++) {
+ if (g_mappings->tab[i].m_start <= a && a < g_mappings->tab[i].m_end) {
// compute offset relative to the start of the mapping
Int32 offset = a - g_mappings->tab[i].m_start;
// read from the file, but also account file offset
- data = elf_getdata_rawchunk (g_mappings->tab[i].m_elf,
+ data = elf_getdata_rawchunk(g_mappings->tab[i].m_elf,
offset + g_mappings->tab[i].m_offset, size, ELF_T_BYTE);
break;
}
}
}
- if (data != NULL)
- {
- memcpy (v, data->d_buf, size);
+ if (data != NULL) {
+ memcpy(v, data->d_buf, size);
return TRUE;
}
/* Still no data, but we have a process - read memory with ptrace */
- if (NULL == data && g_pid > 1)
- {
- long val = ptrace (PTRACE_PEEKDATA, g_pid, a, NULL);
- memcpy (v, &val, size);
+ if (NULL == data && g_pid > 1) {
+ long val = ptrace(PTRACE_PEEKDATA, g_pid, a, NULL);
+ memcpy(v, &val, size);
return TRUE;
}
return FALSE;
}
-static Boolean readW (Int32 a, Int32 *v)
+static Boolean readW(Int32 a, Int32 *v)
{
- return readT(a,v,sizeof(*v));
+ return readT(a, v, sizeof(*v));
}
-static Boolean readH (Int32 a, Int16 *v)
+static Boolean readH(Int32 a, Int16 *v)
{
- return readT(a,v,sizeof(*v));
+ return readT(a, v, sizeof(*v));
}
-static Boolean readB (Int32 a, Int8 *v)
+static Boolean readB(Int32 a, Int8 *v)
{
- return readT(a,v,sizeof(*v));
+ return readT(a, v, sizeof(*v));
}
-static Int32 getProloguePC (Int32 current_pc)
+static Int32 getProloguePC(Int32 current_pc)
{
Int32 result = 0;
- Dwfl_Module *module = dwfl_addrmodule (g_dwfl, current_pc);
- if (module)
- {
+ Dwfl_Module *module = dwfl_addrmodule(g_dwfl, current_pc);
+ if (module) {
// GElf_Off offset;
GElf_Sym sym;
// dwfl_module_addrinfo (module, current_pc, &offset, &sym, NULL, NULL, NULL);
- dwfl_module_addrsym (module, current_pc, &sym, NULL);
+ dwfl_module_addrsym(module, current_pc, &sym, NULL);
// result = current_pc - offset;
result = sym.st_value;
}
- if (0 == result)
- {
+ if (0 == result) {
int i;
- for (i=0; i < g_mappings->elems; i++)
- {
- if (g_mappings->tab[i].m_start <= current_pc && current_pc < g_mappings->tab[i].m_end)
- {
+ for (i = 0; i < g_mappings->elems; i++) {
+ if (g_mappings->tab[i].m_start <= current_pc && current_pc < g_mappings->tab[i].m_end) {
/* go through symbols to find the nearest */
Elf_Scn *scn = NULL;
Elf *elf = g_mappings->tab[i].m_elf;
- while ((scn = elf_nextscn (elf, scn)) != NULL)
- {
+ while ((scn = elf_nextscn(elf, scn)) != NULL) {
GElf_Shdr shdr_mem;
- GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
- if (shdr != NULL && (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM))
- {
- Elf_Data *sdata = elf_getdata (scn, NULL);
+ GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
+ if (shdr != NULL && (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)) {
+ Elf_Data *sdata = elf_getdata(scn, NULL);
unsigned int nsyms = sdata->d_size / (gelf_getclass(elf) == ELFCLASS32 ?
- sizeof (Elf32_Sym) :
- sizeof (Elf64_Sym));
+ sizeof(Elf32_Sym) :
+ sizeof(Elf64_Sym));
unsigned int cnt;
uintptr_t address_offset = current_pc;
if (shdr->sh_type == SHT_DYNSYM)
address_offset -= g_mappings->tab[i].m_start;
- for (cnt = 0; cnt < nsyms; ++cnt)
- {
+ for (cnt = 0; cnt < nsyms; ++cnt) {
GElf_Sym sym_mem;
Elf32_Word xndx;
GElf_Sym *sym = gelf_getsymshndx(sdata, NULL, cnt, &sym_mem, &xndx);
- if (sym != NULL && sym->st_shndx != SHN_UNDEF)
- {
+ if (sym != NULL && sym->st_shndx != SHN_UNDEF) {
if (sym->st_value <= address_offset && address_offset < sym->st_value + sym->st_size)
- {
return sym->st_value;
- }
}
}
}
return result;
}
-void create_crash_stack (Dwfl *dwfl, Elf *core, pid_t pid, Mappings *mappings, Callstack *callstack)
+void create_crash_stack(Dwfl *dwfl, Elf *core, pid_t pid, Mappings *mappings, Callstack *callstack)
{
- UnwindCallbacks callbacks =
- {
+ UnwindCallbacks callbacks = {
report,
readW,
readH,
callstack->tab[0] = g_regs.pc;
callstack->elems = 1;
- UnwInitState (&state, &callbacks, callstack, g_regs.pc, g_regs.sp);
+ UnwInitState(&state, &callbacks, callstack, g_regs.pc, g_regs.sp);
int i;
- for (i = 0; i < REGS_REGULAR_NUM; i++)
- {
+ for (i = 0; i < REGS_REGULAR_NUM; i++) {
state.regData[i].v = g_regs.regs[i];
state.regData[i].o = REG_VAL_FROM_CONST;
}
state.regData[REG_SPSR].v = g_regs.spsr;
state.regData[REG_SPSR].o = REG_VAL_FROM_CONST;
- if (UnwIsAddrThumb (g_regs.pc, g_regs.spsr))
- UnwStartThumb (&state);
+ if (UnwIsAddrThumb(g_regs.pc, g_regs.spsr))
+ UnwStartThumb(&state);
else
- UnwStartArm (&state);
+ UnwStartArm(&state);
}
#include <elfutils/libdwfl.h>
#include <elfutils/version.h>
-#if _ELFUTILS_PREREQ(0,158)
-static int frame_callback (Dwfl_Frame *state, void *arg)
+#if _ELFUTILS_PREREQ(0, 158)
+static int frame_callback(Dwfl_Frame *state, void *arg)
{
Callstack *callstack = (Callstack*)arg;
Dwarf_Addr address;
- dwfl_frame_pc (state, &address, NULL);
+ dwfl_frame_pc(state, &address, NULL);
callstack->tab[callstack->elems++] = address;
return callstack->elems < MAX_CALLSTACK_LEN ? DWARF_CB_OK : DWARF_CB_ABORT;
}
-static int thread_callback (Dwfl_Thread *thread, void *thread_arg)
+static int thread_callback(Dwfl_Thread *thread, void *thread_arg)
{
- dwfl_thread_getframes (thread, frame_callback, thread_arg);
+ dwfl_thread_getframes(thread, frame_callback, thread_arg);
return DWARF_CB_ABORT;
}
#endif
-void *get_place_for_register_value (const char *regname, int regnum)
+void *get_place_for_register_value(const char *regname, int regnum)
{
return 0;
}
-void create_crash_stack (Dwfl *dwfl, Elf *core, pid_t pid, Mappings *mappings, Callstack *callstack)
+void create_crash_stack(Dwfl *dwfl, Elf *core, pid_t pid, Mappings *mappings, Callstack *callstack)
{
callstack->elems = 0;
-#if _ELFUTILS_PREREQ(0,158)
- dwfl_getthreads (dwfl, thread_callback, callstack);
+#if _ELFUTILS_PREREQ(0, 158)
+ dwfl_getthreads(dwfl, thread_callback, callstack);
#endif
}
{ 0, 0, 0, 0 }
};
-extern char *__cxa_demangle (const char *mangled_name, char *output_buffer,
+extern char *__cxa_demangle(const char *mangled_name, char *output_buffer,
size_t *length, int *status);
-static int module_callback (Dwfl_Module *module, void **userdata,
+static int module_callback(Dwfl_Module *module, void **userdata,
const char *name, Dwarf_Addr address,
void *arg)
{
- if (name != NULL && name[0] == '[')
- {
+ if (name != NULL && name[0] == '[') {
/* libdwfl couldn't get the module file - we will get it later from notes */
Mappings *mappings = arg;
- if (mappings->elems < MAX_MAPPINGS_NUM)
- {
+ if (mappings->elems < MAX_MAPPINGS_NUM) {
size_t elems = mappings->elems;
mappings->tab[elems].m_start = address;
mappings->tab[elems].m_end = 0;
return DWARF_CB_OK;
}
-static void getvalue (Elf *core, const void *from, size_t size, void *to)
+static void getvalue(Elf *core, const void *from, size_t size, void *to)
{
- Elf_Data out =
- {
+ Elf_Data out = {
.d_buf = to,
.d_type = size == 32 ? ELF_T_WORD : ELF_T_XWORD,
.d_version = EV_CURRENT,
.d_off = 0,
.d_align = 0
};
- Elf_Data in =
- {
+ Elf_Data in = {
.d_buf = (void*)(from),
.d_type = out.d_type,
.d_version = out.d_version,
.d_align = 0
};
Elf_Data *data;
- if (gelf_getclass (core) == ELFCLASS32)
- data = elf32_xlatetom (&out, &in, elf_getident (core, NULL)[EI_DATA]);
+ if (gelf_getclass(core) == ELFCLASS32)
+ data = elf32_xlatetom(&out, &in, elf_getident(core, NULL)[EI_DATA]);
else
- data = elf64_xlatetom (&out, &in, elf_getident (core, NULL)[EI_DATA]);
+ data = elf64_xlatetom(&out, &in, elf_getident(core, NULL)[EI_DATA]);
if (data == NULL)
- fprintf (errfile, "failed to get value from core file\n");
+ fprintf(errfile, "failed to get value from core file\n");
}
-static void updateMapping (Mappings *mappings, uint64_t mapping_start, uint64_t mapping_end,
+static void updateMapping(Mappings *mappings, uint64_t mapping_start, uint64_t mapping_end,
uint64_t offset, const char *name)
{
int i;
- for (i = 0; i < mappings->elems; i++)
- {
- if (mappings->tab[i].m_start == mapping_start)
- {
+ for (i = 0; i < mappings->elems; i++) {
+ if (mappings->tab[i].m_start == mapping_start) {
mappings->tab[i].m_end = mapping_end;
mappings->tab[i].m_name = name;
mappings->tab[i].m_offset = offset;
}
}
-static void parse_note_file (Elf *elf, const char *desc, uint64_t *values_cnt, uint64_t *page_size,
+static void parse_note_file(Elf *elf, const char *desc, uint64_t *values_cnt, uint64_t *page_size,
size_t *addr_size, const char **values, const char **filenames)
{
- *addr_size = gelf_fsize (elf, ELF_T_ADDR, 1, EV_CURRENT);
+ *addr_size = gelf_fsize(elf, ELF_T_ADDR, 1, EV_CURRENT);
getvalue(elf, desc, *addr_size*8, values_cnt);
getvalue(elf, desc + *addr_size, *addr_size*8, page_size);
/* First: triplets of <mapping-start> <mapping-end> <offset-in-pages>
getvalue(elf, item + 2 * addr_size, addr_size*8, offset_in_pages);
}
-static char *try_symbol_from_elfs (Elf *core, Elf_Data *notes, uintptr_t address,
+static char *try_symbol_from_elfs(Elf *core, Elf_Data *notes, uintptr_t address,
const char **module_name)
{
GElf_Nhdr nhdr;
size_t name_pos;
size_t desc_pos;
- while ((new_pos = gelf_getnote (notes, pos, &nhdr, &name_pos, &desc_pos)) > 0)
- {
- if (nhdr.n_type == NT_FILE)
- {
+ while ((new_pos = gelf_getnote(notes, pos, &nhdr, &name_pos, &desc_pos)) > 0) {
+ if (nhdr.n_type == NT_FILE) {
uint64_t values_cnt = 0, page_size = 0;
const char *values;
const char *filenames;
parse_note_file(core, notes->d_buf + desc_pos, &values_cnt, &page_size, &addr_size, &values, &filenames);
int ii;
- for (ii = 0; ii < values_cnt; ii++)
- {
+ for (ii = 0; ii < values_cnt; ii++) {
uint64_t mapping_start = 0, mapping_end = 0, offset_in_pages = 0;
const char *item = values + 3 * addr_size * ii;
- get_mapping_item (core, addr_size, item, &mapping_start, &mapping_end, &offset_in_pages);
+ get_mapping_item(core, addr_size, item, &mapping_start, &mapping_end, &offset_in_pages);
- if (mapping_start <= address && address < mapping_end)
- {
+ if (mapping_start <= address && address < mapping_end) {
Elf *elf;
int fd;
fd = open(filenames, O_RDONLY);
if (-1 == fd)
return NULL;
- elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
+ elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
if (NULL == elf) {
close(fd);
Elf_Scn *scn = NULL;
*module_name = filenames;
- while ((scn = elf_nextscn (elf, scn)) != NULL)
- {
+ while ((scn = elf_nextscn(elf, scn)) != NULL) {
GElf_Shdr shdr_mem;
- GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
- if (shdr != NULL && (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM))
- {
- Elf_Data *sdata = elf_getdata (scn, NULL);
+ GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
+ if (shdr != NULL && (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)) {
+ Elf_Data *sdata = elf_getdata(scn, NULL);
unsigned int nsyms = sdata->d_size / (gelf_getclass(elf) == ELFCLASS32 ?
- sizeof (Elf32_Sym) :
- sizeof (Elf64_Sym));
+ sizeof(Elf32_Sym) :
+ sizeof(Elf64_Sym));
unsigned int cnt;
uintptr_t address_offset = address;
if (shdr->sh_type == SHT_DYNSYM)
address_offset -= mapping_start;
- for (cnt = 0; cnt < nsyms; ++cnt)
- {
+ for (cnt = 0; cnt < nsyms; ++cnt) {
GElf_Sym sym_mem;
Elf32_Word xndx;
GElf_Sym *sym = gelf_getsymshndx(sdata, NULL, cnt, &sym_mem, &xndx);
- if (sym != NULL && sym->st_shndx != SHN_UNDEF)
- {
- if (sym->st_value <= address_offset && address_offset < sym->st_value + sym->st_size)
- {
- symbol = strdup(elf_strptr (elf, shdr->sh_link, sym->st_name));
+ if (sym != NULL && sym->st_shndx != SHN_UNDEF) {
+ if (sym->st_value <= address_offset && address_offset < sym->st_value + sym->st_size) {
+ symbol = strdup(elf_strptr(elf, shdr->sh_link, sym->st_name));
break;
}
}
return NULL;
}
-static Dwfl *open_dwfl_with_pid (pid_t pid)
+static Dwfl *open_dwfl_with_pid(pid_t pid)
{
int status;
pid_t stopped_pid;
- if (ptrace (PTRACE_SEIZE, pid, NULL, PTRACE_O_TRACEEXIT) != 0)
- {
+ if (ptrace(PTRACE_SEIZE, pid, NULL, PTRACE_O_TRACEEXIT) != 0) {
fprintf(errfile, "PTRACE_SEIZE failed on PID %d: %m\n", pid);
return NULL;
}
- ptrace (PTRACE_INTERRUPT, pid, 0, 0);
+ ptrace(PTRACE_INTERRUPT, pid, 0, 0);
stopped_pid = waitpid(pid, &status, 0);
- if (stopped_pid == -1 || stopped_pid != pid || !WIFSTOPPED(status))
- {
+ if (stopped_pid == -1 || stopped_pid != pid || !WIFSTOPPED(status)) {
fprintf(errfile, "waitpid failed: %m, stopped_pid=%d, status=%d\n", stopped_pid, status);
return NULL;
}
- static const Dwfl_Callbacks proc_callbacks =
- {
+ static const Dwfl_Callbacks proc_callbacks = {
.find_elf = dwfl_linux_proc_find_elf,
.find_debuginfo = dwfl_standard_find_debuginfo,
.section_address = NULL,
.debuginfo_path = NULL
};
- Dwfl *dwfl = dwfl_begin (&proc_callbacks);
- if (dwfl == NULL)
- {
- fprintf (errfile, "process %d : Can't start dwfl (%s)\n", pid, dwfl_errmsg(-1));
+ Dwfl *dwfl = dwfl_begin(&proc_callbacks);
+ if (dwfl == NULL) {
+ fprintf(errfile, "process %d : Can't start dwfl (%s)\n", pid, dwfl_errmsg(-1));
return NULL;
}
- if (dwfl_linux_proc_report (dwfl, pid) < 0)
- {
- fprintf (errfile, "process %d : dwfl report failed (%s)\n", pid, dwfl_errmsg(-1));
- dwfl_end (dwfl);
+ if (dwfl_linux_proc_report(dwfl, pid) < 0) {
+ fprintf(errfile, "process %d : dwfl report failed (%s)\n", pid, dwfl_errmsg(-1));
+ dwfl_end(dwfl);
return NULL;
}
-#if _ELFUTILS_PREREQ(0,158)
- if (dwfl_linux_proc_attach (dwfl, pid, true) < 0)
- {
- fprintf (errfile, "process %d : dwfl attach failed (%s)\n", pid, dwfl_errmsg(-1));
- dwfl_end (dwfl);
+#if _ELFUTILS_PREREQ(0, 158)
+ if (dwfl_linux_proc_attach(dwfl, pid, true) < 0) {
+ fprintf(errfile, "process %d : dwfl attach failed (%s)\n", pid, dwfl_errmsg(-1));
+ dwfl_end(dwfl);
return NULL;
}
#endif
return dwfl;
}
-static Dwfl *open_dwfl_with_core (Elf *core, const char *core_file_name)
+static Dwfl *open_dwfl_with_core(Elf *core, const char *core_file_name)
{
- static const Dwfl_Callbacks core_callbacks =
- {
+ static const Dwfl_Callbacks core_callbacks = {
.find_elf = dwfl_build_id_find_elf,
.find_debuginfo = dwfl_standard_find_debuginfo,
.section_address = NULL,
.debuginfo_path = NULL
};
- Dwfl *dwfl = dwfl_begin (&core_callbacks);
- if (dwfl == NULL)
- {
- fprintf (errfile, "%s : Can't start dwfl (%s)\n", core_file_name, dwfl_errmsg(-1));
+ Dwfl *dwfl = dwfl_begin(&core_callbacks);
+ if (dwfl == NULL) {
+ fprintf(errfile, "%s : Can't start dwfl (%s)\n", core_file_name, dwfl_errmsg(-1));
return NULL;
}
-#if _ELFUTILS_PREREQ(0,158)
- if (dwfl_core_file_report (dwfl, core, NULL) < 0)
+#if _ELFUTILS_PREREQ(0, 158)
+ if (dwfl_core_file_report(dwfl, core, NULL) < 0)
#else
- if (dwfl_core_file_report (dwfl, core) < 0)
+ if (dwfl_core_file_report(dwfl, core) < 0)
#endif
{
- fprintf (errfile, "%s : dwfl report failed (%s)\n", core_file_name, dwfl_errmsg(-1));
- dwfl_end (dwfl);
+ fprintf(errfile, "%s : dwfl report failed (%s)\n", core_file_name, dwfl_errmsg(-1));
+ dwfl_end(dwfl);
return NULL;
}
-#if _ELFUTILS_PREREQ(0,158)
- if (dwfl_core_file_attach (dwfl, core) < 0)
- {
- fprintf (errfile, "%s : dwfl attach failed (%s)\n", core_file_name, dwfl_errmsg(-1));
- dwfl_end (dwfl);
+#if _ELFUTILS_PREREQ(0, 158)
+ if (dwfl_core_file_attach(dwfl, core) < 0) {
+ fprintf(errfile, "%s : dwfl attach failed (%s)\n", core_file_name, dwfl_errmsg(-1));
+ dwfl_end(dwfl);
return NULL;
}
#endif
return dwfl;
}
-static int get_registers_ptrace (pid_t pid)
+static int get_registers_ptrace(pid_t pid)
{
struct iovec data;
uintptr_t regbuf[20];
data.iov_base = regbuf;
- data.iov_len = sizeof (regbuf);
+ data.iov_len = sizeof(regbuf);
- if (ptrace (PTRACE_GETREGSET, pid, NT_PRSTATUS, &data) != 0)
- {
+ if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &data) != 0) {
fprintf(errfile, "PTRACE_GETREGSET failed on PID %d: %m\n", pid);
return -1;
}
size_t i;
for (i = 0;
- i * sizeof (regbuf[0]) < data.iov_len && i < sizeof (regbuf)/sizeof (regbuf[0]);
- i++)
- {
- void *reg = get_place_for_register_value ("", i);
+ i * sizeof(regbuf[0]) < data.iov_len && i < sizeof(regbuf)/sizeof(regbuf[0]);
+ i++) {
+ void *reg = get_place_for_register_value("", i);
if (NULL != reg)
- memcpy (reg, ®buf[i], sizeof (regbuf[i]));
+ memcpy(reg, ®buf[i], sizeof(regbuf[i]));
}
return 0;
}
-static Elf_Data *get_registers_core (Elf *core, const char *core_file_name, Mappings *mappings)
+static Elf_Data *get_registers_core(Elf *core, const char *core_file_name, Mappings *mappings)
{
GElf_Phdr mem;
- GElf_Phdr *phdr = gelf_getphdr (core, 0, &mem);
+ GElf_Phdr *phdr = gelf_getphdr(core, 0, &mem);
- if (phdr == NULL || phdr->p_type != PT_NOTE)
- {
- fprintf (errfile, "%s : Missing note section at the first position in core file\n",
+ if (phdr == NULL || phdr->p_type != PT_NOTE) {
+ fprintf(errfile, "%s : Missing note section at the first position in core file\n",
core_file_name);
return NULL;
}
- Elf_Data *notes = elf_getdata_rawchunk (core, phdr->p_offset, phdr->p_filesz, ELF_T_NHDR);
- if (notes == NULL)
- {
- fprintf (errfile, "%s : error getting notes (%s)\n", core_file_name, dwfl_errmsg(-1));
+ Elf_Data *notes = elf_getdata_rawchunk(core, phdr->p_offset, phdr->p_filesz, ELF_T_NHDR);
+ if (notes == NULL) {
+ fprintf(errfile, "%s : error getting notes (%s)\n", core_file_name, dwfl_errmsg(-1));
return NULL;
}
- Ebl *ebl = ebl_openbackend (core);
- if (ebl == NULL)
- {
- fprintf (errfile, "%s : Can't initialize ebl\n", core_file_name);
+ Ebl *ebl = ebl_openbackend(core);
+ if (ebl == NULL) {
+ fprintf(errfile, "%s : Can't initialize ebl\n", core_file_name);
return NULL;
}
size_t new_pos = 0;
int got_regs = 0;
/* registers should be in the first note! */
- while ((new_pos = gelf_getnote (notes, pos, &nhdr, &name_pos, &desc_pos)) > 0)
- {
- if (nhdr.n_type == NT_PRSTATUS && !got_regs)
- {
+ while ((new_pos = gelf_getnote(notes, pos, &nhdr, &name_pos, &desc_pos)) > 0) {
+ if (nhdr.n_type == NT_PRSTATUS && !got_regs) {
GElf_Word regs_offset;
size_t nregloc;
const Ebl_Register_Location *reglocs;
got_regs = 1;
- if (0 == ebl_core_note (ebl, &nhdr, "CORE", ®s_offset, &nregloc,
- ®locs, &nitems, &items))
- {
- fprintf (errfile,
+ if (0 == ebl_core_note(ebl, &nhdr, "CORE", ®s_offset, &nregloc,
+ ®locs, &nitems, &items)) {
+ fprintf(errfile,
"%s : error parsing notes (built with different build of libebl?)\n",
core_file_name);
return NULL;
+ regs_offset;
unsigned i;
- for (i = 0; i < nregloc; i++)
- {
+ for (i = 0; i < nregloc; i++) {
const char *register_location = regs_location + reglocs[i].offset;
int regnum;
for (regnum = reglocs[i].regno;
regnum < reglocs[i].regno + reglocs[i].count;
- regnum++)
- {
+ regnum++) {
char regname[5];
int bits, type;
const char *prefix = 0;
const char *setname = 0;
- ssize_t ret = ebl_register_info (ebl, regnum, regname,
+ ssize_t ret = ebl_register_info(ebl, regnum, regname,
sizeof(regname), &prefix, &setname,
&bits, &type);
- if (ret < 0)
- {
- fprintf (errfile, "%s : can't get register info\n", core_file_name);
+ if (ret < 0) {
+ fprintf(errfile, "%s : can't get register info\n", core_file_name);
return NULL;
}
- void *place_for_reg_value = get_place_for_register_value (regname, regnum);
+ void *place_for_reg_value = get_place_for_register_value(regname, regnum);
if (place_for_reg_value != NULL)
- getvalue (core, register_location, bits, place_for_reg_value);
+ getvalue(core, register_location, bits, place_for_reg_value);
register_location += bits / 8 + reglocs[i].pad;
}
}
- }
- else if (nhdr.n_type == NT_FILE)
- {
+ } else if (nhdr.n_type == NT_FILE) {
uint64_t values_cnt = 0, page_size = 0;
const char *values;
const char *filenames;
* count = values_cnt
* Then the names of files.
*/
- for (ii = 0; ii < values_cnt; ii++)
- {
+ for (ii = 0; ii < values_cnt; ii++) {
uint64_t mapping_start = 0, mapping_end = 0, offset_in_pages = 0;
const char *item = values + 3 * addr_size * ii;
- get_mapping_item (core, addr_size, item, &mapping_start, &mapping_end,
+ get_mapping_item(core, addr_size, item, &mapping_start, &mapping_end,
&offset_in_pages);
- updateMapping (mappings, mapping_start, mapping_end,
+ updateMapping(mappings, mapping_start, mapping_end,
offset_in_pages*page_size, filenames);
- filenames += strlen (filenames)+1;
+ filenames += strlen(filenames)+1;
}
}
pos = new_pos;
}
- ebl_closebackend (ebl);
+ ebl_closebackend(ebl);
return notes;
}
-static void printCallstack (Callstack *callstack, Dwfl *dwfl, Elf *core, pid_t pid,
+static void printCallstack(Callstack *callstack, Dwfl *dwfl, Elf *core, pid_t pid,
Elf_Data *notes)
{
- fprintf (outputfile, "Call stack");
- if (pid > 1) fprintf (outputfile, " for PID %d", pid);
- fprintf (outputfile, ":\n");
+ fprintf(outputfile, "Call stack");
+ if (pid > 1) fprintf(outputfile, " for PID %d", pid);
+ fprintf(outputfile, ":\n");
char *dem_buffer = NULL;
size_t it;
- for (it = 0; it != callstack->elems; ++it)
- {
- if (sizeof (callstack->tab[0]) > 4)
- fprintf (outputfile, "0x%016llx: ", (long long)callstack->tab[it]);
+ for (it = 0; it != callstack->elems; ++it) {
+ if (sizeof(callstack->tab[0]) > 4)
+ fprintf(outputfile, "0x%016llx: ", (long long)callstack->tab[it]);
else
- fprintf (outputfile, "0x%08x: ", (int32_t)callstack->tab[it]);
- Dwfl_Module *module = dwfl_addrmodule (dwfl, callstack->tab[it]);
- if (module)
- {
+ fprintf(outputfile, "0x%08x: ", (int32_t)callstack->tab[it]);
+ Dwfl_Module *module = dwfl_addrmodule(dwfl, callstack->tab[it]);
+ if (module) {
char *demangled_symbol = 0;
- const char *symbol = dwfl_module_addrname (module, callstack->tab[it]);
+ const char *symbol = dwfl_module_addrname(module, callstack->tab[it]);
const char *fname = 0;
- const char *module_name = dwfl_module_info (module, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
+ const char *module_name = dwfl_module_info(module, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
char *symbol_from_elf = 0;
if (symbol == NULL)
- {
- symbol = symbol_from_elf = try_symbol_from_elfs (core, notes, callstack->tab[it], &fname);
- }
- if (symbol != 0 && symbol[0] == '_' && symbol[1] == 'Z')
- {
+ symbol = symbol_from_elf = try_symbol_from_elfs(core, notes, callstack->tab[it], &fname);
+ if (symbol != 0 && symbol[0] == '_' && symbol[1] == 'Z') {
int status = -1;
- demangled_symbol = __cxa_demangle (symbol, dem_buffer, NULL, &status);
+ demangled_symbol = __cxa_demangle(symbol, dem_buffer, NULL, &status);
if (status == 0)
symbol = demangled_symbol;
}
if (symbol != 0)
- fprintf (outputfile, "%s()", symbol);
+ fprintf(outputfile, "%s()", symbol);
else
- fprintf (outputfile, "<unknown>");
+ fprintf(outputfile, "<unknown>");
if (demangled_symbol != 0)
- free (demangled_symbol);
+ free(demangled_symbol);
if (symbol_from_elf != 0)
- free (symbol_from_elf);
+ free(symbol_from_elf);
- fprintf (outputfile, " from %s\n", fname != NULL ? fname : module_name);
- }
- else
- {
- fprintf (outputfile, "unknown function\n");
+ fprintf(outputfile, " from %s\n", fname != NULL ? fname : module_name);
+ } else {
+ fprintf(outputfile, "unknown function\n");
}
}
}
const char *core_file_name;
- prctl (PR_SET_DUMPABLE, 0);
-
- while ((c = getopt_long_only(argc, argv, "", opts, NULL)) != -1)
- {
- switch (c)
- {
- case OPT_PID:
- pid = atoi(optarg);
- break;
- case OPT_OUTPUTFILE:
- outputfile = fopen(optarg, "w");
- break;
- case OPT_ERRFILE:
- errfile = fopen(optarg, "w");
- break;
+ prctl(PR_SET_DUMPABLE, 0);
+
+ while ((c = getopt_long_only(argc, argv, "", opts, NULL)) != -1) {
+ switch (c) {
+ case OPT_PID:
+ pid = atoi(optarg);
+ break;
+ case OPT_OUTPUTFILE:
+ outputfile = fopen(optarg, "w");
+ break;
+ case OPT_ERRFILE:
+ errfile = fopen(optarg, "w");
+ break;
}
}
core_file_name = argv[optind];
argc -= optind;
- elf_version (EV_CURRENT);
+ elf_version(EV_CURRENT);
/* First, prepare dwfl and modules */
Elf *core = NULL;
Dwfl *dwfl = NULL;
if (pid > 1)
- {
- dwfl = open_dwfl_with_pid (pid);
- }
- else
- {
- if (argc != 1)
- {
- fprintf (errfile,
+ dwfl = open_dwfl_with_pid(pid);
+ else {
+ if (argc != 1) {
+ fprintf(errfile,
"Usage: %s [--output file] [--erroutput file] [--pid <pid> | <core-file>]\n",
argv[0]);
return 1;
}
- core_fd = open (core_file_name, O_RDONLY);
- if (core_fd < 0)
- {
- perror (core_file_name);
+ core_fd = open(core_file_name, O_RDONLY);
+ if (core_fd < 0) {
+ perror(core_file_name);
return 2;
}
- core = elf_begin (core_fd, ELF_C_READ_MMAP, NULL);
- if (core == NULL)
- {
- fprintf (errfile, "%s : Can't open ELF (%s)\n", core_file_name, elf_errmsg(-1));
+ core = elf_begin(core_fd, ELF_C_READ_MMAP, NULL);
+ if (core == NULL) {
+ fprintf(errfile, "%s : Can't open ELF (%s)\n", core_file_name, elf_errmsg(-1));
return 3;
}
- dwfl = open_dwfl_with_core (core, core_file_name);
+ dwfl = open_dwfl_with_core(core, core_file_name);
}
if (NULL == dwfl)
Mappings mappings;
mappings.elems = 0;
- dwfl_getmodules (dwfl, module_callback, &mappings, 0);
+ dwfl_getmodules(dwfl, module_callback, &mappings, 0);
Elf_Data *notes = 0;
/* Now, get registers */
- if (pid > 1)
- {
- if (-1 == get_registers_ptrace (pid))
+ if (pid > 1) {
+ if (-1 == get_registers_ptrace(pid))
return 3333;
- }
- else
- {
- notes = get_registers_core (core, core_file_name, &mappings);
+ } else {
+ notes = get_registers_core(core, core_file_name, &mappings);
if (NULL == notes)
return 2222;
}
/* Unwind call stack */
Callstack callstack;
- create_crash_stack (dwfl, core, pid, &mappings, &callstack);
+ create_crash_stack(dwfl, core, pid, &mappings, &callstack);
/* Print the results */
- printCallstack (&callstack, dwfl, core, pid, notes);
+ printCallstack(&callstack, dwfl, core, pid, notes);
/* Clean up */
- dwfl_report_end (dwfl, NULL, NULL);
- dwfl_end (dwfl);
- if (NULL != core) elf_end (core);
- if (-1 != core_fd) close (core_fd);
+ dwfl_report_end(dwfl, NULL, NULL);
+ dwfl_end(dwfl);
+ if (NULL != core) elf_end(core);
+ if (-1 != core_fd) close(core_fd);
return 0;
}
#define MAX_CALLSTACK_LEN 1000
-typedef struct Callstack
-{
+typedef struct Callstack {
uintptr_t tab[MAX_CALLSTACK_LEN];
size_t elems;
} Callstack;
-typedef struct Mapping
-{
+typedef struct Mapping {
uintptr_t m_start;
uintptr_t m_end;
uintptr_t m_offset;
#define MAX_MAPPINGS_NUM 1000
-typedef struct Mappings
-{
+typedef struct Mappings {
Mapping tab[MAX_MAPPINGS_NUM];
size_t elems;
} Mappings;
-void *get_place_for_register_value (const char *regname, int regnum);
-void create_crash_stack (Dwfl *dwfl, Elf *core, pid_t pid, Mappings *mappings, Callstack *callstack);
+void *get_place_for_register_value(const char *regname, int regnum);
+void create_crash_stack(Dwfl *dwfl, Elf *core, pid_t pid, Mappings *mappings, Callstack *callstack);
#endif /* CRASH_STACK_H */
{"==== System disk I/O satistics " , "/proc/diskstats"},
};
-static void usage() {
+static void usage()
+{
fprintf(stderr, "usage: dump_systemstate [-k] [-d] [-f file]\n"
" -f: write to file (instead of stdout)\n"
" -k: dump kernel messages (only root)\n"
return percent;
}
-int main(int argc, char *argv[]) {
+int main(int argc, char *argv[])
+{
int c, ret, i, is_root, dpercent;
const char *arg_file = NULL;
int out_fd = -1;
} else {
out_fd = open(arg_file, O_WRONLY | O_TRUNC | O_CREAT, FILE_PERM);
if (out_fd < 0) {
- perror ("couldn't open output file");
+ perror("couldn't open output file");
ret = out_fd;
goto exit;
}
if (errno != EINTR)
return -1;
} else {
- if (WIFEXITED(status)) {
+ if (WIFEXITED(status))
return WEXITSTATUS(status);
- } else if (WIFSIGNALED(status)) {
+ else if (WIFSIGNALED(status))
return WTERMSIG(status);
- } else if (WIFSTOPPED(status)) {
+ else if (WIFSTOPPED(status))
return WSTOPSIG(status);
- }
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
int dfd;
char buf[PIPE_BUF];
- if(!src || !dst) {
+ if (!src || !dst) {
_E("Invalid argument\n");
return -1;
}
int dfd;
char buf[PIPE_BUF];
- if(!src || !dst) {
+ if (!src || !dst) {
_E("Invalid argument\n");
return -1;
}
int sfd;
char buf[PIPE_BUF];
- if(!src) {
+ if (!src) {
_E("Invalid argument\n");
return -1;
}
_E("Failed to popen\n");
return -1;
}
- while(fgets(buff, PIPE_BUF, fp) != NULL) {
+ while (fgets(buff, PIPE_BUF, fp) != NULL)
write_fd(dfd, buff, strlen(buff));
- }
+
ret = pclose(fp);
return ret;
}
ret = -1;
continue;
}
- if (remove_dir_internal(subfd)) {
+ if (remove_dir_internal(subfd))
ret = -1;
- }
close(subfd);
if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
_SE("Couldn't unlinkat %s: %d\n", de->d_name, errno);
static int trace_symbols(void *const *array, int size, struct addr_node *start, int fd)
{
Dl_info info_funcs;
- Elf32_Ehdr elf_h = {{0,},0,};
+ Elf32_Ehdr elf_h = {{0, }, 0, };
Elf32_Shdr *s_headers;
Elf32_Sym *symtab_entry;
int i, cnt, file, ret;
/* Both dli_sname and dli_fname is NULL, sys-assert cannot trace any information.
Thus, sys-assert skips to translate such address entry.
However, if a developer wants raw information, we need to fix the code to print raw data */
- if(info_funcs.dli_fname == NULL)
+ if (info_funcs.dli_fname == NULL)
continue;
file = open(info_funcs.dli_fname, O_RDONLY);
}
} else if (signum == SIGSEGV) {
switch (info->si_code) {
- case SEGV_MAPERR:
- fprintf_fd(fd, " address not mapped to object\n");
- break;
- case SEGV_ACCERR:
- fprintf_fd(fd,
- " invalid permissions for mapped object\n");
- break;
- default:
- fprintf_fd(fd, " illegal si_code: %d\n", info->si_code);
- break;
+ case SEGV_MAPERR:
+ fprintf_fd(fd, " address not mapped to object\n");
+ break;
+ case SEGV_ACCERR:
+ fprintf_fd(fd,
+ " invalid permissions for mapped object\n");
+ break;
+ default:
+ fprintf_fd(fd, " illegal si_code: %d\n", info->si_code);
+ break;
}
fprintf_fd(fd, " si_addr = %p\n", info->si_addr);
} else if (signum == SIGBUS) {
switch (info->si_code) {
- case BUS_ADRALN:
- fprintf_fd(fd, " invalid address alignment\n");
- break;
- case BUS_ADRERR:
- fprintf_fd(fd, " nonexistent physical address\n");
- break;
- case BUS_OBJERR:
- fprintf_fd(fd, " object-specific hardware error\n");
- break;
- default:
- fprintf_fd(fd, " illegal si_code: %d\n", info->si_code);
- break;
+ case BUS_ADRALN:
+ fprintf_fd(fd, " invalid address alignment\n");
+ break;
+ case BUS_ADRERR:
+ fprintf_fd(fd, " nonexistent physical address\n");
+ break;
+ case BUS_OBJERR:
+ fprintf_fd(fd, " object-specific hardware error\n");
+ break;
+ default:
+ fprintf_fd(fd, " illegal si_code: %d\n", info->si_code);
+ break;
}
fprintf_fd(fd, " si_addr: %p\n", info->si_addr);
}
return;
/* make crash info file name */
snprintf(timestr, sizeof(timestr), "%.10ld", cur_time);
- snprintf(crashid, sizeof(crashid), "%s_%d",processname, pid);
+ snprintf(crashid, sizeof(crashid), "%s_%d", processname, pid);
if (snprintf(filepath, PATH_LEN,
"%s/%s.info", CRASH_INFO_PATH, crashid) == 0) {
fprintf(stderr,
fprintf(stderr, "[sys-assert]can't open %s\n", TASK_PATH);
} else {
while (readdir_r(dir, &entry, &dentry) == 0) {
- if( strcmp(dentry->d_name, ".") == 0
+ if (strcmp(dentry->d_name, ".") == 0
|| strcmp(dentry->d_name, "..") == 0)
continue;
- fprintf_fd(fd_cs, "%s ",dentry->d_name);
+ fprintf_fd(fd_cs, "%s ", dentry->d_name);
}
closedir(dir);
fprintf_fd(fd_cs, "\n");