3 * Copyright 2007, Intel Corporation
5 * This file is part of corewatcher.org
7 * This program file is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * You should have received a copy of the GNU General Public License
17 * along with this program in a file named COPYING; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA
23 * Arjan van de Ven <arjan@linux.intel.com>
24 * William Douglas <william.douglas@intel.com>
25 * Tim Pepper <timothy.c.pepper@linux.intel.com>
34 #include <asm/unistd.h>
35 #include <sys/types.h>
42 #include "corewatcher.h"
44 const char *core_folder = "/var/lib/corewatcher/";
45 const char *processed_folder = "/var/lib/corewatcher/processed/";
47 /* Always pick up the processing_mtx and then the
48 processing_queue_mtx, reverse for setting down */
49 /* Always pick up the gdb_mtx and then the
50 processing_queue_mtx, reverse for setting down */
51 /* Always pick up the processing_mtx and then the
52 gdb_mtx, reverse for setting down */
53 /* so order for pick up should be:
54 processing_mtx -> gdb_mtx -> processing_queue_mtx
55 and the reverse for setting down */
56 GMutex processing_queue_mtx;
57 static char *processing_queue[MAX_PROCESSING_OOPS];
58 static int pq_tail = 0;
59 static int pq_head = 0;
62 static char *get_release(void)
68 file = fopen("/etc/os-release", "r");
73 if (getline(&line, &dummy, file) == -1)
75 if (strstr(line, "VERSION_ID=")) {
78 c = strchr(line, '\n');
81 c = strdup(&line[11]);
96 * Strip the directories from the path
99 char *strip_directories(char *fullpath)
101 char *dfile = NULL, *c1 = NULL, *c2 = NULL, *r = NULL;
107 dfile = strdup(fullpath);
111 c1 = strtok(dfile, delim);
114 c1 = strtok(NULL, delim);
125 * Move corefile from core_folder to processed_folder subdir.
126 * If this type of core has recently been seen, unlink this more recent
127 * example in order to rate limit submissions of extremely crashy
129 * Add extension if given and attempt to create directories if needed.
131 int move_core(char *fullpath, char *extension)
133 char *corefilename = NULL, newpath[8192], *coreprefix = NULL;
137 struct dirent *entry = NULL;
142 if (!(corefilename = strip_directories(fullpath)))
145 /* if the corefile's name minus any suffixes (such as .$PID) and
146 * minus two additional characters (ie: last two digits of
147 * timestamp assuming core_%e_%t) matches another core file in the
148 * processed_folder, simply unlink it instead of processing it for
149 * submission. TODO: consider a (configurable) time delta greater
150 * than which the cores must be separated, stat'ing the files, etc.
152 if (!(coreprefix = strdup(corefilename)))
154 if (!(s = strstr(coreprefix, ".")))
157 prefix_len = strlen(coreprefix);
158 if (prefix_len > 2) {
159 s = strndup(coreprefix, prefix_len - 2);
165 dir = opendir(processed_folder);
169 entry = readdir(dir);
170 if (!entry || !entry->d_name)
172 if (entry->d_name[0] == '.')
174 if (!strstr(entry->d_name, coreprefix))
176 fprintf(stderr, "+ ...ignoring/unlinking %s\n", fullpath);
182 snprintf(newpath, 8192, "%s%s.%s", processed_folder, corefilename, extension);
184 snprintf(newpath, 8192, "%s%s", processed_folder, corefilename);
188 rename(fullpath, newpath);
198 * Finds the full path for the application that crashed,
199 * and moves file to processed_folder for processing
201 static char *get_appfile(char *fullpath)
203 char *appname = NULL, *appfile = NULL;
208 appname = find_coredump(fullpath);
212 /* don't try and do anything for rpm, gdb or corewatcher crashes */
213 if (!(strcmp(appname, "rpm") && strcmp(appname, "gdb") && strcmp(appname, "corewatcher")))
216 appfile = find_executable(appname);
217 /* appname no longer used, so free it as it was strdup'd */
222 move_core(fullpath, "to-process");
228 * Use GDB to extract backtrace information from corefile
230 static struct oops *extract_core(char *fullpath, char *appfile)
232 struct oops *oops = NULL;
234 char *command = NULL, *h1 = NULL, *c1 = NULL, *c2 = NULL, *line = NULL;
235 char *text = NULL, *coretime = NULL;
236 char *m1 = NULL, *m2 = NULL;
237 int bt_lines = 0, maps_lines = 0;
239 char *badchar = NULL;
240 char *release = get_release();
241 int parsing_maps = 0;
242 struct stat stat_buf;
244 ssize_t bytesread = 0;
246 fprintf(stderr, "+ extract_core() called for %s\n", fullpath);
248 if (asprintf(&command, "LANG=C gdb --batch -f %s %s -x /etc/corewatcher/gdb.command 2> /dev/null", appfile, fullpath) == -1)
251 file = popen(command, "r");
254 fprintf(stderr, "+ gdb failed for %s\n", fullpath);
256 if (stat(fullpath, &stat_buf) != -1) {
257 coretime = malloc(26);
259 ctime_r(&stat_buf.st_mtime, coretime);
267 release ? release : "Unknown",
268 coretime ? coretime : "Unknown");
276 while (file && !feof(file)) {
277 bytesread = getline(&line, &size, file);
283 /* try to figure out if we're onto the maps output yet */
284 if (strncmp(line, "From", 4) == 0) {
287 /* maps might not be present */
288 if (strncmp(line, "No shared libraries", 19) == 0) {
292 if (!parsing_maps) { /* parsing backtrace */
295 /* gdb's backtrace lines start with a line number */
299 /* gdb prints some initial info which may include the
300 * "#0" line of the backtrace, then prints the
301 * backtrace in its entirety, leading to a
302 * duplicate "#0" in our summary if we do do: */
303 if ((bt_lines == 1) && (strncmp(line, "#0 ", 3) == 0))
307 /* gdb outputs some 0x1a's which break XML */
309 badchar = memchr(line, 0x1a, bytesread);
316 if (asprintf(&c1, "%s %s", c2, line) == -1)
320 /* keep going even if asprintf has errors */
321 ret = asprintf(&c1, " %s", line);
323 } else { /* parsing maps */
328 if (asprintf(&m1, "%s %s", m2, line) == -1)
332 /* keep going even if asprintf has errors */
333 ret = asprintf(&m1, " %s", line);
342 ret = asprintf(&text,
349 c1 ? c1 : " Unknown",
350 m1 ? m1 : " Unknown");
360 oops = malloc(sizeof(struct oops));
365 memset(oops, 0, sizeof(struct oops));
366 oops->application = appfile;
368 oops->filename = strdup(fullpath);
373 * input filename has the form: core_$APP_$TIMESTAMP[.$PID]
374 * output filename has form of: $APP_$TIMESTAMP[.ext]
376 char *get_core_filename(char *filename, char *ext)
378 char *name = NULL, *dotpid = NULL, *stamp = NULL, *detail_filename = NULL;
383 if (!(stamp = strstr(filename, "_")))
389 if (!(name = strdup(stamp)))
392 /* strip trailing .PID if present */
393 dotpid = strstr(name, ".");
398 if ((asprintf(&detail_filename, "%s%s.%s", processed_folder, name, ext)) == -1) {
403 if ((asprintf(&detail_filename, "%s%s", processed_folder, name)) == -1) {
410 return detail_filename;
414 * Write the backtrace from the core file into a text
415 * file named as $APP_$TIMESTAMP.txt
417 static void write_core_detail_file(char *filename, char *text)
420 char *detail_filename = NULL;
422 if (!filename || !text)
425 if (!(detail_filename = get_core_filename(filename, "txt")))
428 if ((fd = open(detail_filename, O_WRONLY | O_CREAT | O_TRUNC, 0)) != -1) {
429 if(write(fd, text, strlen(text)) >= 0) {
432 fprintf(stderr, "+ ...ignoring/unlinking %s\n", detail_filename);
433 unlink(detail_filename);
437 free(detail_filename);
441 * Removes corefile (core.XXXX) from the processing_queue.
443 * Expects the processing_queue_mtx to be held.
445 static void remove_from_processing_queue(void)
447 fprintf(stderr, "+ removing processing_queue head\n");
448 free(processing_queue[pq_head]);
449 processing_queue[pq_head++] = NULL;
451 if (pq_head == MAX_PROCESSING_OOPS) {
452 fprintf(stderr, "+ wrapping processing_queue head to 0 (bugs here?)\n");
458 * Removes file from processing_oops hash based on core name,
459 * extracting that core name from a fullpath such as
460 * "/${processed_folder}/core_$APP_$TIMESTAMP.$PID"
461 * in order to get just "$APP_$TIMESTAMP"
463 * Expects the lock on the given hash to be held.
465 void remove_name_from_hash(char *fullpath, GHashTable *ht)
467 char *name = NULL, *corename = NULL, *shortname = NULL;
469 if (!(name = strip_directories(fullpath)))
472 if (!(corename = get_core_filename(name, NULL))) {
478 if (!(shortname = strip_directories(corename))) {
484 if (g_hash_table_remove(ht, shortname))
485 fprintf(stderr, "+ core %s removed from processing_oops hash table\n", shortname);
487 fprintf(stderr, "+ core %s not in processing_oops hash table\n", shortname);
493 * Common function for processing core
494 * files to generate oops structures
496 static struct oops *process_common(char *fullpath)
498 struct oops *oops = NULL;
499 char *appname = NULL, *appfile = NULL;
501 if(!(appname = find_coredump(fullpath))) {
505 if (!(appfile = find_executable(appname))) {
511 if (!(oops = extract_core(fullpath, appfile))) {
521 * Processes .to-process core files.
522 * Also creates the $APP_$TIMESTAMP.txt file and adds
523 * the oops struct to the submit queue
525 * Picks up and sets down the gdb_mtx and
526 * picks up and sets down the processing_queue_mtx.
527 * (held at the same time in that order)
529 static void *process_new(void __unused *vp)
531 struct oops *oops = NULL;
532 char *procfn = NULL, *corefn = NULL, *fullpath = NULL;
534 g_mutex_lock(&core_status.processing_mtx);
535 g_mutex_lock(&gdb_mtx);
536 g_mutex_lock(&processing_queue_mtx);
538 fprintf(stderr, "+ Entered process_new()\n");
540 if (!(fullpath = processing_queue[pq_head])) {
541 fprintf(stderr, "+ processing_queue corruption?\n");
542 g_mutex_unlock(&processing_queue_mtx);
543 g_mutex_unlock(&gdb_mtx);
544 g_mutex_unlock(&core_status.processing_mtx);
548 if (!(corefn = strip_directories(fullpath))) {
549 fprintf(stderr, "+ No corefile? (%s)\n", fullpath);
550 goto clean_process_new;
553 if (!(procfn = replace_name(fullpath, ".to-process", ".processed"))) {
554 fprintf(stderr, "+ Problems with filename manipulation for %s\n", corefn);
555 goto clean_process_new;
558 if (!(oops = process_common(fullpath))) {
559 fprintf(stderr, "+ Problems processing %s\n", procfn);
560 goto clean_process_new;
563 if (!(oops->detail_filename = get_core_filename(corefn, "txt"))) {
564 fprintf(stderr, "+ Problems with filename manipulation for %s\n", procfn);
565 goto clean_process_new;
568 if (rename(fullpath, procfn)) {
569 fprintf(stderr, "+ Unable to move %s to %s\n", fullpath, procfn);
570 goto clean_process_new;
573 free(oops->filename);
574 oops->filename = procfn;
576 remove_from_processing_queue();
578 g_mutex_unlock(&processing_queue_mtx);
579 g_mutex_unlock(&gdb_mtx);
580 g_mutex_unlock(&core_status.processing_mtx);
582 write_core_detail_file(corefn, oops->text);
584 queue_backtrace(oops);
586 fprintf(stderr, "+ Leaving process_new() with %s queued\n", oops->detail_filename);
588 /* mustn't free procfn because it was hung on oops->filename */
593 remove_name_from_hash(fullpath, core_status.processing_oops);
594 remove_from_processing_queue();
598 g_mutex_unlock(&processing_queue_mtx);
599 g_mutex_unlock(&gdb_mtx);
600 g_mutex_unlock(&core_status.processing_mtx);
605 * Reprocesses .processed core files.
607 * Picks up and sets down the gdb_mtx.
608 * Picks up and sets down the processing_queue_mtx.
609 * (held at the same time in that order)
611 static void *process_old(void __unused *vp)
613 struct oops *oops = NULL;
614 char *corefn = NULL, *fullpath = NULL;
616 g_mutex_lock(&gdb_mtx);
617 g_mutex_lock(&processing_queue_mtx);
619 fprintf(stderr, "+ Entered process_old()\n");
621 if (!(fullpath = processing_queue[pq_head])) {
622 fprintf(stderr, "+ processing_queue corruption?\n");
623 g_mutex_unlock(&processing_queue_mtx);
624 g_mutex_unlock(&gdb_mtx);
627 fprintf(stderr, "+ Reprocessing %s\n", fullpath);
629 if (!(corefn = strip_directories(fullpath))) {
630 fprintf(stderr, "+ No corefile? (%s)\n", fullpath);
631 goto clean_process_old;
634 if (!(oops = process_common(fullpath))) {
635 fprintf(stderr, "+ Problems processing %s\n", corefn);
636 goto clean_process_old;
639 if (!(oops->detail_filename = get_core_filename(corefn, "txt"))) {
640 fprintf(stderr, "+ Problems with filename manipulation for %s\n", corefn);
641 goto clean_process_old;
644 remove_from_processing_queue();
646 g_mutex_unlock(&processing_queue_mtx);
647 g_mutex_unlock(&gdb_mtx);
649 queue_backtrace(oops);
651 fprintf(stderr, "+ Leaving process_old() with %s queued\n", oops->detail_filename);
657 remove_name_from_hash(fullpath, core_status.processing_oops);
658 remove_from_processing_queue();
661 g_mutex_unlock(&processing_queue_mtx);
662 g_mutex_unlock(&gdb_mtx);
667 * Adds corefile (based on name) to the processing_oops
668 * hash table if it is not already there, then
669 * tries to add to the processing_queue.
671 * Picks up and sets down the processing_mtx.
672 * Picks up and sets down the processing_queue_mtx.
674 static int add_to_processing(char *fullpath)
676 char *c1 = NULL, *c2 = NULL, *fp = NULL;
681 if (!(fp = strdup(fullpath)))
682 goto clean_add_to_processing;
684 if (!(c1 = get_core_filename(fp, NULL)))
685 goto clean_add_to_processing;
687 if (!(c2 = strip_directories(c1)))
688 goto clean_add_to_processing;
693 g_mutex_lock(&core_status.processing_mtx);
694 if (g_hash_table_lookup(core_status.processing_oops, c2)) {
695 g_mutex_unlock(&core_status.processing_mtx);
696 fprintf(stderr, "+ ...name %s already in processing_oops hash table\n", c2);
697 goto clean_add_to_processing;
700 g_mutex_lock(&processing_queue_mtx);
701 if (processing_queue[pq_tail]) {
702 g_mutex_unlock(&processing_queue_mtx);
703 g_mutex_unlock(&core_status.processing_mtx);
704 fprintf(stderr, "+ ...processing_queue full\n");
705 goto clean_add_to_processing;
708 g_hash_table_insert(core_status.processing_oops, c2, c2);
709 processing_queue[pq_tail++] = fp;
710 if (pq_tail == MAX_PROCESSING_OOPS)
713 g_mutex_unlock(&processing_queue_mtx);
714 g_mutex_unlock(&core_status.processing_mtx);
716 clean_add_to_processing:
724 * Entry for processing new core files.
726 static void process_corefile(char *fullpath)
728 GThread *thrd = NULL;
731 r = add_to_processing(fullpath);
736 thrd = g_thread_new("process_new", process_new, NULL);
738 fprintf(stderr, "Couldn't start thread for process_new()\n");
742 * Entry for processing already seen core files.
744 static void reprocess_corefile(char *fullpath)
746 GThread *thrd = NULL;
749 r = add_to_processing(fullpath);
754 thrd = g_thread_new("process_old", process_old, NULL);
756 fprintf(stderr, "Couldn't start thread for process_old()\n");
759 static void scan_core_folder(void __unused *unused)
761 /* scan for new crash data */
763 struct dirent *entry = NULL;
764 char *fullpath = NULL, *appfile = NULL;
767 dir = opendir(core_folder);
770 fprintf(stderr, "+ Begin scanning %s...\n", core_folder);
775 entry = readdir(dir);
776 if (!entry || !entry->d_name)
778 if (entry->d_name[0] == '.')
780 if (strncmp(entry->d_name, "core_", 5))
783 /* matched core_#### */
784 r = asprintf(&fullpath, "%s%s", core_folder, entry->d_name);
788 } else if (((unsigned int)r) != strlen(core_folder) + strlen(entry->d_name)) {
792 /* If one were to prompt the user before submitting, that
793 * might happen here. */
795 fprintf(stderr, "+ Looking at %s\n", fullpath);
796 appfile = get_appfile(fullpath);
799 fprintf(stderr, "+ ...ignoring/unlinking %s\n", fullpath);
807 fprintf(stderr, "+ End scanning %s...\n", core_folder);
810 static void scan_processed_folder(void __unused *unused)
812 /* scan for partially processed crash data */
814 struct dirent *entry = NULL;
815 char *fullpath = NULL;
818 dir = opendir(processed_folder);
821 fprintf(stderr, "+ Begin scanning %s...\n", processed_folder);
826 entry = readdir(dir);
827 if (!entry || !entry->d_name)
829 if (entry->d_name[0] == '.')
831 if (!strstr(entry->d_name, "process"))
834 r = asprintf(&fullpath, "%s%s", processed_folder, entry->d_name);
838 } else if (((unsigned int)r) != strlen(processed_folder) + strlen(entry->d_name)) {
842 fprintf(stderr, "+ Looking at %s\n", fullpath);
843 if (strstr(fullpath, "to-process"))
844 process_corefile(fullpath);
846 reprocess_corefile(fullpath);
849 fprintf(stderr, "+ End scanning %s...\n", processed_folder);
852 int scan_folders(void __unused *unused)
854 scan_core_folder(NULL);
855 scan_processed_folder(NULL);