--- /dev/null
+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
--- /dev/null
+#include <stdlib.h>
+
+int bar(char *foo)
+{
+ *foo = 0;
+}
+void main()
+{
+ char *foo = NULL;
+ bar(foo);
+}
\ No newline at end of file
--- /dev/null
+extern char *find_executable(char *fragment);
+extern char *find_coredump(char *corefile);
--- /dev/null
+#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
--- /dev/null
+#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;
+}
+
--- /dev/null
+set width 250
+bt