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>
34 #include <asm/unistd.h>
35 #include <sys/types.h>
43 #include "corewatcher.h"
48 const char *core_folder = "/var/lib/corewatcher/";
49 const char *processed_folder = "/var/lib/corewatcher/processed/";
51 /* Always pick up the processing_mtx and then the
52 processing_queue_mtx, reverse for setting down */
53 /* Always pick up the gdb_mtx and then the
54 processing_queue_mtx, reverse for setting down */
55 /* Always pick up the processing_mtx and then the
56 gdb_mtx, reverse for setting down */
57 /* so order for pick up should be:
58 processing_mtx -> gdb_mtx -> processing_queue_mtx
59 and the reverse for setting down */
60 static pthread_mutex_t processing_queue_mtx = PTHREAD_MUTEX_INITIALIZER;
61 static char *processing_queue[MAX_PROCESSING_OOPS];
62 static pthread_mutex_t gdb_mtx = PTHREAD_MUTEX_INITIALIZER;
66 static char *get_release(void)
72 file = fopen("/etc/os-release", "r");
74 line = strdup("Unknown");
79 if (getline(&line, &dummy, file) == -1)
81 if (strstr(line, "VERSION_ID=")) {
84 c = strchr(line, '\n');
87 c = strdup(&line[11]);
98 line = strdup("Unknown");
103 static char *set_wrapped_app(char *line)
105 char *dline = NULL, *app = NULL, *appfile = NULL, *abs_path = NULL;
107 char app_folder[] = "/usr/share/";
113 dline = strdup(line);
115 app = strtok(dline, delim);
117 if (strcmp(app, "--app") == 0) {
118 app = strtok(NULL, delim);
121 app = strtok(NULL, delim);
124 goto cleanup_set_wrapped_app;
125 r = asprintf(&abs_path, "%s%s", app_folder, app);
128 goto cleanup_set_wrapped_app;
129 } else if (((unsigned int)r) != strlen(app_folder) + strlen(app)) {
130 goto cleanup_set_wrapped_app;
133 appfile = find_executable(abs_path);
135 cleanup_set_wrapped_app:
142 * Scan core dump in case a wrapper was used
143 * to run the process and get the actual binary name
145 static char *wrapper_scan(char *command)
147 char *line = NULL, *appfile = NULL;
150 file = popen(command, "r");
154 while (!feof(file)) {
158 ret = getline(&line, &size, file);
164 if (strstr(line, "Core was generated by") &&
165 strstr(line, "--app")) {
166 /* attempt to update appfile */
167 appfile = set_wrapped_app(line);
179 * Strip the directories from the path
182 char *strip_directories(char *fullpath)
184 char *dfile = NULL, *c1 = NULL, *c2 = NULL, *r = NULL;
190 dfile = strdup(fullpath);
194 c1 = strtok(dfile, delim);
197 c1 = strtok(NULL, delim);
208 * Move corefile from core_folder to processed_folder subdir.
209 * Add extension if given and attempt to create directories if needed.
211 int move_core(char *fullpath, char *extension)
213 char *corefilename = NULL, newpath[8192];
218 if (!(corefilename = strip_directories(fullpath))) {
224 snprintf(newpath, 8192, "%s%s.%s", processed_folder, corefilename, extension);
226 snprintf(newpath, 8192, "%s%s", processed_folder, corefilename);
229 rename(fullpath, newpath);
235 * Finds the full path for the application that crashed,
236 * and depending on what opted_in was configured as will:
237 * opted_in 2 (always submit) -> move file to processed_folder
238 * to be processed further
239 * opted_in 1 (ask user) -> ask user if we should submit
240 * the crash and add to asked_oops hash so we don't get
241 * called again for this corefile
242 * opted_in 0 (don't submit) -> do nothing
244 * Picks up and sets down the asked_mtx.
246 static char *get_appfile(char *fullpath)
248 char *appname = NULL, *appfile = NULL;
253 appname = find_coredump(fullpath);
257 /* don't try and do anything for rpm, gdb or corewatcher crashes */
258 if (!(strcmp(appname, "rpm") && strcmp(appname, "gdb") && strcmp(appname, "corewatcher")))
261 appfile = find_executable(appname);
262 /* appname no longer used, so free it as it was strdup'd */
267 move_core(fullpath, "to-process");
273 * Use GDB to extract backtrace information from corefile
275 static struct oops *extract_core(char *fullpath, char *appfile)
277 struct oops *oops = NULL;
279 char *command = NULL, *h1 = NULL, *c1 = NULL, *c2 = NULL, *line = NULL, *text = NULL, *at = NULL;
280 char *m1 = NULL, *m2 = NULL;
282 char *badchar = NULL;
283 char *release = get_release();
284 int parsing_maps = 0;
286 if (asprintf(&command, "LANG=C gdb --batch -f %s %s -x /etc/corewatcher/gdb.command 2> /dev/null", appfile, fullpath) == -1)
289 if ((at = wrapper_scan(command))) {
301 h1 = strdup("Unknown");
303 file = popen(command, "r");
305 while (file && !feof(file)) {
310 ret = getline(&line, &size, file);
316 if (strncmp(line, "From", 4) == 0) {
321 if (!parsing_maps) { /* parsing backtrace */
325 fixup: /* gdb outputs some 0x1a's which break XML */
326 badchar = memchr(line, 0x1a, size);
334 if (asprintf(&c1, "%s %s", c2, line) == -1)
338 /* keep going even if asprintf has errors */
339 ret = asprintf(&c1, " %s", line);
341 } else { /* parsing maps */
345 if (asprintf(&m1, "%s %s", m2, line) == -1)
349 /* keep going even if asprintf has errors */
350 ret = asprintf(&m1, " %s", line);
360 ret = asprintf(&text,
373 oops = malloc(sizeof(struct oops));
378 memset(oops, 0, sizeof(struct oops));
379 oops->application = strdup(appfile);
381 oops->filename = strdup(fullpath);
386 * filename is of the form core_XXXX[.blah]
387 * we need to get the pid out as we want
388 * output of the form XXXX[.ext]
390 char *get_core_filename(char *filename, char *ext)
392 char *pid = NULL, *c = NULL, *s = NULL, *detail_filename = NULL;
397 if (!(s = strstr(filename, "_")))
402 /* causes valgrind whining because we copy from middle of a string */
403 if (!(pid = strdup(s)))
406 c = strstr(pid, ".");
412 /* causes valgrind whining because we copy from middle of a string */
413 if ((asprintf(&detail_filename, "%s%s.%s", processed_folder, pid, ext)) == -1) {
418 /* causes valgrind whining because we copy from middle of a string */
419 if ((asprintf(&detail_filename, "%s%s", processed_folder, pid)) == -1) {
426 return detail_filename;
430 * Write the backtrace from the core file into a text
431 * file named after the pid
433 static void write_core_detail_file(char *filename, char *text)
436 char *detail_filename = NULL;
438 if (!filename || !text)
441 if (!(detail_filename = get_core_filename(filename, "txt")))
444 if ((fd = open(detail_filename, O_WRONLY | O_CREAT | O_TRUNC, 0)) != -1) {
445 if(write(fd, text, strlen(text)) >= 0)
448 unlink(detail_filename);
451 free(detail_filename);
455 * Removes corefile (core.XXXX) from the processing_queue.
457 * Expects the processing_queue_mtx to be held.
459 static void remove_from_processing_queue(void)
461 free(processing_queue[head]);
462 processing_queue[head++] = NULL;
464 if (head == MAX_PROCESSING_OOPS)
469 * Removes file from processing_oops hash based on pid.
470 * Extracts pid from the fullpath such that
471 * /home/user/core.pid will be tranformed into just the pid.
473 * Expects the lock on the given hash to be held.
475 void remove_pid_from_hash(char *fullpath, GHashTable *ht)
477 char *c1 = NULL, *c2 = NULL;
479 if (!(c1 = strip_directories(fullpath)))
482 if (!(c2 = get_core_filename(c1, NULL))) {
489 g_hash_table_remove(ht, c2);
495 * Common function for processing core
496 * files to generate oops structures
498 static struct oops *process_common(char *fullpath)
500 struct oops *oops = NULL;
501 char *appname = NULL, *appfile = NULL;
503 if(!(appname = find_coredump(fullpath))) {
507 if (!(appfile = find_executable(appname))) {
513 if (!(oops = extract_core(fullpath, appfile))) {
524 * Processes .to-process core files.
525 * Also creates the pid.txt file and adds
526 * the oops struct to the submit queue
528 * Picks up and sets down the gdb_mtx and
529 * picks up and sets down the processing_queue_mtx.
530 * (held at the same time in that order)
531 * Also will pick up and sets down the queued_mtx.
533 static void *process_new(void __unused *vp)
535 struct oops *oops = NULL;
536 char *procfn = NULL, *corefn = NULL, *fullpath = NULL;
538 pthread_mutex_lock(&core_status.processing_mtx);
539 pthread_mutex_lock(&gdb_mtx);
540 pthread_mutex_lock(&processing_queue_mtx);
542 if (!(fullpath = processing_queue[head])) {
543 /* something went quite wrong */
544 pthread_mutex_unlock(&processing_queue_mtx);
545 pthread_mutex_unlock(&gdb_mtx);
546 pthread_mutex_unlock(&core_status.processing_mtx);
550 if (!(corefn = strip_directories(fullpath)))
551 goto clean_process_new;
553 if (!(procfn = replace_name(fullpath, ".to-process", ".processed")))
554 goto clean_process_new;
556 if (!(oops = process_common(fullpath)))
557 goto clean_process_new;
559 if (!(oops->detail_filename = get_core_filename(corefn, "txt")))
560 goto clean_process_new;
562 if (rename(fullpath, procfn))
563 goto clean_process_new;
565 free(oops->filename);
566 oops->filename = procfn;
568 remove_from_processing_queue();
570 pthread_mutex_unlock(&processing_queue_mtx);
571 pthread_mutex_unlock(&gdb_mtx);
572 pthread_mutex_unlock(&core_status.processing_mtx);
574 write_core_detail_file(corefn, oops->text);
576 pthread_mutex_lock(&core_status.queued_mtx);
577 queue_backtrace(oops);
578 pthread_mutex_unlock(&core_status.queued_mtx);
580 /* don't need to free procfn because was set to oops->filename and that gets free'd */
586 remove_pid_from_hash(fullpath, core_status.processing_oops);
587 remove_from_processing_queue();
589 procfn = NULL; /* don't know if oops->filename == procfn so be safe */
592 pthread_mutex_unlock(&processing_queue_mtx);
593 pthread_mutex_unlock(&gdb_mtx);
594 pthread_mutex_unlock(&core_status.processing_mtx);
599 * Reprocesses .processed core files.
601 * Picks up and sets down the gdb_mtx.
602 * Picks up and sets down the processing_queue_mtx.
603 * (held at the same time in that order)
604 * Also will pick up and sets down the queued_mtx.
606 static void *process_old(void __unused *vp)
608 struct oops *oops = NULL;
609 char *corefn = NULL, *fullpath = NULL;
611 pthread_mutex_lock(&gdb_mtx);
612 pthread_mutex_lock(&processing_queue_mtx);
614 if (!(fullpath = processing_queue[head])) {
615 /* something went quite wrong */
616 pthread_mutex_unlock(&processing_queue_mtx);
617 pthread_mutex_unlock(&gdb_mtx);
621 if (!(corefn = strip_directories(fullpath)))
622 goto clean_process_old;
624 if (!(oops = process_common(fullpath)))
625 goto clean_process_old;
627 if (!(oops->detail_filename = get_core_filename(corefn, "txt")))
628 goto clean_process_old;
630 remove_from_processing_queue();
632 pthread_mutex_unlock(&processing_queue_mtx);
633 pthread_mutex_unlock(&gdb_mtx);
635 pthread_mutex_lock(&core_status.queued_mtx);
636 queue_backtrace(oops);
637 pthread_mutex_unlock(&core_status.queued_mtx);
644 remove_pid_from_hash(fullpath, core_status.processing_oops);
645 remove_from_processing_queue();
648 pthread_mutex_unlock(&processing_queue_mtx);
649 pthread_mutex_unlock(&gdb_mtx);
654 * Adds corefile (based on pid) to the processing_oops
655 * hash table if it is not already there, then
656 * tries to add to the processing_queue.
658 * Picks up and sets down the processing_mtx.
659 * Picks up and sets down the processing_queue_mtx.
661 static int add_to_processing(char *fullpath)
663 char *c1 = NULL, *c2 = NULL, *fp = NULL;
668 if (!(fp = strdup(fullpath)))
669 goto clean_add_to_processing;
671 if (!(c1 = get_core_filename(fp, NULL)))
672 goto clean_add_to_processing;
674 if (!(c2 = strip_directories(c1)))
675 goto clean_add_to_processing;
680 pthread_mutex_lock(&core_status.processing_mtx);
681 if (g_hash_table_lookup(core_status.processing_oops, c2)) {
682 pthread_mutex_unlock(&core_status.processing_mtx);
683 goto clean_add_to_processing;
686 pthread_mutex_lock(&processing_queue_mtx);
687 if (processing_queue[tail]) {
688 pthread_mutex_unlock(&processing_queue_mtx);
689 pthread_mutex_unlock(&core_status.processing_mtx);
690 goto clean_add_to_processing;
693 g_hash_table_insert(core_status.processing_oops, c2, c2);
694 processing_queue[tail++] = fp;
695 if (tail == MAX_PROCESSING_OOPS)
698 pthread_mutex_unlock(&processing_queue_mtx);
699 pthread_mutex_unlock(&core_status.processing_mtx);
701 clean_add_to_processing:
709 * Entry for processing new core files.
711 static void process_corefile(char *fullpath)
716 r = add_to_processing(fullpath);
721 if (pthread_create(&thrd, NULL, process_new, NULL))
722 fprintf(stderr, "Couldn't start up gdb extract core thread\n");
726 * Entry for processing already seen core files.
728 static void reprocess_corefile(char *fullpath)
733 r = add_to_processing(fullpath);
738 if (pthread_create(&thrd, NULL, process_old, NULL))
739 fprintf(stderr, "Couldn't start up gdb extract core thread\n");
742 int scan_corefolders(void __unused *unused)
745 struct dirent *entry = NULL;
746 char *fullpath = NULL, *appfile = NULL;
749 pthread_mutex_init(&core_status.processing_mtx, NULL);
750 pthread_mutex_init(&core_status.queued_mtx, NULL);
751 pthread_mutex_init(&core_status.asked_mtx, NULL);
753 /* scan for new crash data */
754 dir = opendir(core_folder);
757 fprintf(stderr, "+ scanning %s...\n", core_folder);
762 entry = readdir(dir);
763 if (!entry || !entry->d_name)
765 if (entry->d_name[0] == '.')
767 if (strncmp(entry->d_name, "core_", 5))
770 /* matched core_#### where #### is the pid of the process */
771 r = asprintf(&fullpath, "%s%s", core_folder, entry->d_name);
775 } else if (((unsigned int)r) != strlen(core_folder) + strlen(entry->d_name)) {
778 /* already found, waiting for response from user */
779 pthread_mutex_lock(&core_status.asked_mtx);
780 if (g_hash_table_lookup(core_status.asked_oops, fullpath)) {
781 pthread_mutex_unlock(&core_status.asked_mtx);
784 pthread_mutex_unlock(&core_status.asked_mtx);
785 fprintf(stderr, "+ Looking at %s\n", fullpath);
786 appfile = get_appfile(fullpath);
797 /* scan for partially processed crash data */
798 dir = opendir(processed_folder);
801 fprintf(stderr, "+ scanning %s...\n", processed_folder);
806 entry = readdir(dir);
807 if (!entry || !entry->d_name)
809 if (entry->d_name[0] == '.')
811 if (!strstr(entry->d_name, "process"))
814 r = asprintf(&fullpath, "%s%s", processed_folder, entry->d_name);
818 } else if (((unsigned int)r) != strlen(processed_folder) + strlen(entry->d_name)) {
822 fprintf(stderr, "+ Looking at %s\n", fullpath);
823 if (strstr(fullpath, "to-process"))
824 process_corefile(fullpath);
826 reprocess_corefile(fullpath);