initial import
authorArjan van de Ven <arjan@linux.intel.com>
Thu, 15 Jan 2009 20:04:43 +0000 (22:04 +0200)
committerArjan van de Ven <arjan@linux.intel.com>
Thu, 15 Jan 2009 20:04:43 +0000 (22:04 +0200)
Makefile [new file with mode: 0644]
core.c [new file with mode: 0644]
coredumper.h [new file with mode: 0644]
extract_core.c [new file with mode: 0644]
find_file.c [new file with mode: 0644]
gdb.command [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..9ef5450
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,9 @@
+all: extract_core
+
+CFLAGS = -O0 -g -Wall -W -D_FORTIFY_SOURCE=2 -fstack-protector
+
+extract_core: extract_core.o find_file.o coredumper.h
+       gcc $(CFLAGS) extract_core.o find_file.o -o extract_core
+       
+clean:
+       rm -f *.o extract_core
\ No newline at end of file
diff --git a/core.c b/core.c
new file mode 100644 (file)
index 0000000..3f581cb
--- /dev/null
+++ b/core.c
@@ -0,0 +1,11 @@
+#include <stdlib.h>
+
+int bar(char *foo)
+{
+       *foo = 0;       
+}
+void main()
+{
+       char *foo = NULL;
+       bar(foo);
+}
\ No newline at end of file
diff --git a/coredumper.h b/coredumper.h
new file mode 100644 (file)
index 0000000..5b288d3
--- /dev/null
@@ -0,0 +1,2 @@
+extern char *find_executable(char *fragment);
+extern char *find_coredump(char *corefile);
diff --git a/extract_core.c b/extract_core.c
new file mode 100644 (file)
index 0000000..3f55530
--- /dev/null
@@ -0,0 +1,56 @@
+#define _GNU_SOURCE
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "coredumper.h"
+
+char *extract_core(char *corefile)
+{
+       char *command = NULL, *c1 = NULL, *c2 = NULL, *line;
+       char *appfile;
+       FILE *file;
+
+       appfile = find_executable(find_coredump(corefile));
+       if (!appfile)
+               return NULL;
+
+       asprintf(&command, "gdb --batch -f %s %s -x gdb.command 2> /dev/null", appfile, corefile);
+       file = popen(command, "r");
+       while (!feof(file)) {
+               size_t size = 0;
+               int ret;
+
+               c2 = c1;
+               line = NULL;
+               ret = getline(&line, &size, file);      
+               if (!size)
+                       break;
+               if (ret < 0)
+                       break;
+
+               if (strstr(line, "no debugging symbols found")) {
+                       free(line);
+                       continue;
+               }
+
+               if (c1) {
+                       c1 = NULL;
+                       asprintf(&c1, "%s%s", c2, line);
+                       free(c2);
+               } else {
+                       c1 = NULL;
+                       asprintf(&c1, "%s", line);
+               }
+               free(line);
+       }
+       pclose(file);
+       return c1;
+}
+
+void main()
+{
+       printf("FOO %s\n", extract_core("/var/cores/core.3824"));
+
+}
\ No newline at end of file
diff --git a/find_file.c b/find_file.c
new file mode 100644 (file)
index 0000000..af44826
--- /dev/null
@@ -0,0 +1,69 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
+#include "coredumper.h"
+
+char *find_executable(char *fragment)
+{
+       char *path, *c1, *c2;
+       path = strdup(getenv("PATH"));
+       static char filename[PATH_MAX*2];
+
+       if (strlen(fragment) < 3)
+               return NULL;
+
+       /* Deal with absolute paths first */
+       if (!access(fragment, X_OK)) {
+               strcpy(filename, fragment);
+               return filename;
+       }
+
+       c1 = path;
+       while (c1 && strlen(c1)>0) {
+               c2 = strchr(c1, ':');
+               if (c2) c2=0;
+               sprintf(filename, "%s/%s", c1, fragment);
+               if (!access(filename, X_OK))
+                       return filename;
+               c1 = c2;
+       }
+       return NULL;
+}
+
+char *find_coredump(char *corefile)
+{
+       char *line, *c, *c2;
+       size_t size = 0; 
+       FILE *file = NULL;
+       char command[PATH_MAX*2];
+       static char core[PATH_MAX];
+
+       memset(core, 0, sizeof(core));
+       sprintf(command, "file %s", corefile);
+       file = popen(command, "r");
+       if (!file)
+               return NULL;
+       if (getline(&line, &size, file) < 0)
+               goto out;;
+       c = strstr(line,"from '");
+       if (!c)
+               goto out;
+       c += 6;
+       c2 = strchr(c, '\'');
+       if (!c2)
+               goto out;
+       *c2 = 0;
+       strcpy(core, c);
+out:
+       pclose(file);
+       free(line);
+       return core;
+}
+
diff --git a/gdb.command b/gdb.command
new file mode 100644 (file)
index 0000000..f85afd9
--- /dev/null
@@ -0,0 +1,2 @@
+set width 250
+bt