Print /proc/<pid>/maps on segfault

master
Drew DeVault 9 years ago
parent a6e57dd7ac
commit e5bb08cc18

@ -1,5 +1,6 @@
#include "log.h" #include "log.h"
#include "sway.h" #include "sway.h"
#include "readline.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -142,6 +143,9 @@ void error_handler(int sig) {
void *array[max_lines]; void *array[max_lines];
char **bt; char **bt;
size_t bt_len; size_t bt_len;
char maps_file[256];
char maps_buffer[1024];
FILE *maps;
sway_log(L_ERROR, "Error: Signal %d. Printing backtrace", sig); sway_log(L_ERROR, "Error: Signal %d. Printing backtrace", sig);
bt_len = backtrace(array, max_lines); bt_len = backtrace(array, max_lines);
@ -155,6 +159,22 @@ void error_handler(int sig) {
for (i = 0; (size_t)i < bt_len; i++) { for (i = 0; (size_t)i < bt_len; i++) {
sway_log(L_ERROR, "Backtrace: %s", bt[i]); sway_log(L_ERROR, "Backtrace: %s", bt[i]);
} }
sway_log(L_ERROR, "Maps:");
pid_t pid = getpid();
if (snprintf(maps_file, 255, "/proc/%zd/maps", (size_t)pid) < 255) {
maps = fopen(maps_file, "r");
while (!feof(maps)) {
char *m = read_line_buffer(maps, maps_buffer, 1024);
if (!m) {
fclose(maps);
sway_log(L_ERROR, "Unable to allocate memory to show maps");
break;
}
sway_log(L_ERROR, m);
}
fclose(maps);
}
#else #else
sway_log(L_ERROR, "Error: Signal %d.", sig); sway_log(L_ERROR, "Error: Signal %d.", sig);
#endif #endif

@ -3,7 +3,7 @@
#include <stdio.h> #include <stdio.h>
char *read_line(FILE *file) { char *read_line(FILE *file) {
int length = 0, size = 128; size_t length = 0, size = 128;
char *string = malloc(size); char *string = malloc(size);
if (!string) { if (!string) {
return NULL; return NULL;
@ -37,3 +37,28 @@ char *read_line(FILE *file) {
string[length] = '\0'; string[length] = '\0';
return string; return string;
} }
char *read_line_buffer(FILE *file, char *string, size_t string_len) {
size_t length = 0;
if (!string) {
return NULL;
}
while (1) {
int c = getc(file);
if (c == EOF || c == '\n' || c == '\0') {
break;
}
if (c == '\r') {
continue;
}
string[length++] = c;
if (string_len <= length) {
return NULL;
}
}
if (length + 1 == string_len) {
return NULL;
}
string[length] = '\0';
return string;
}

@ -4,5 +4,6 @@
#include <stdio.h> #include <stdio.h>
char *read_line(FILE *file); char *read_line(FILE *file);
char *read_line_buffer(FILE *file, char *string, size_t string_len);
#endif #endif

Loading…
Cancel
Save