bcb3c1e
From 4eee77568fe24bcb9341ee9c2eaa74cb048103cb Mon Sep 17 00:00:00 2001
bcb3c1e
From: Jakub Filak <jfilak@redhat.com>
bcb3c1e
Date: Thu, 13 Nov 2014 12:08:07 +0100
bcb3c1e
Subject: [PATCH] gdb: make gdb aware of the abrt's debuginfo dir
bcb3c1e
bcb3c1e
A debuginfo package might ship an auto-loaded gdb script. If abrt
bcb3c1e
unpacks that package into the abrt's debuginfo cache dir and points gdb
bcb3c1e
to that directory, gdb refuses to auto-loaded that gdb scripts and
bcb3c1e
produces a plenty of warning messages that abrt writes to 'backtrace'
bcb3c1e
file.
bcb3c1e
bcb3c1e
The previous solution of this issue was to turn auto-load off completely
bcb3c1e
but it turned the pretty printer off too.
bcb3c1e
bcb3c1e
The correct solution is to add the abrt's debuginfo cache directory to
bcb3c1e
auto-load safe-path and auto-load scripts-dir settings.
bcb3c1e
bcb3c1e
Thanks Jan Kratochvil <jkratoch@redhat.com>
bcb3c1e
bcb3c1e
Requires: rhbz#1163335
bcb3c1e
bcb3c1e
Signed-off-by: Jakub Filak <jfilak@redhat.com>
bcb3c1e
---
bcb3c1e
 src/lib/hooklib.c | 83 ++++++++++++++++++++++++++++++++++++-------------------
bcb3c1e
 1 file changed, 55 insertions(+), 28 deletions(-)
bcb3c1e
bcb3c1e
diff --git a/src/lib/hooklib.c b/src/lib/hooklib.c
bcb3c1e
index 4a50727..1d45cdd 100644
bcb3c1e
--- a/src/lib/hooklib.c
bcb3c1e
+++ b/src/lib/hooklib.c
bcb3c1e
@@ -252,11 +252,12 @@ char *get_backtrace(const char *dump_dir_name, unsigned timeout_sec, const char
bcb3c1e
     /* Let user know what's going on */
bcb3c1e
     log(_("Generating backtrace"));
bcb3c1e
 
bcb3c1e
-    char *args[21];
bcb3c1e
-    args[0] = (char*)"gdb";
bcb3c1e
-    args[1] = (char*)"-batch";
bcb3c1e
-    args[2] = (char*)"-ex";
bcb3c1e
+    unsigned i = 0;
bcb3c1e
+    char *args[25];
bcb3c1e
+    args[i++] = (char*)"gdb";
bcb3c1e
+    args[i++] = (char*)"-batch";
bcb3c1e
     struct strbuf *set_debug_file_directory = strbuf_new();
bcb3c1e
+    unsigned auto_load_base_index = 0;
bcb3c1e
     if(debuginfo_dirs == NULL)
bcb3c1e
     {
bcb3c1e
         // set non-existent debug file directory to prevent resolving
bcb3c1e
@@ -266,6 +267,8 @@ char *get_backtrace(const char *dump_dir_name, unsigned timeout_sec, const char
bcb3c1e
     else
bcb3c1e
     {
bcb3c1e
         strbuf_append_str(set_debug_file_directory, "set debug-file-directory /usr/lib/debug");
bcb3c1e
+
bcb3c1e
+        struct strbuf *debug_directories = strbuf_new();
bcb3c1e
         const char *p = debuginfo_dirs;
bcb3c1e
         while (1)
bcb3c1e
         {
bcb3c1e
@@ -274,11 +277,25 @@ char *get_backtrace(const char *dump_dir_name, unsigned timeout_sec, const char
bcb3c1e
             if (*p == '\0')
bcb3c1e
                 break;
bcb3c1e
             const char *colon_or_nul = strchrnul(p, ':');
bcb3c1e
-            strbuf_append_strf(set_debug_file_directory, ":%.*s/usr/lib/debug", (int)(colon_or_nul - p), p);
bcb3c1e
+            strbuf_append_strf(debug_directories, "%s%.*s/usr/lib/debug", (debug_directories->len == 0 ? "" : ":"),
bcb3c1e
+                                                                          (int)(colon_or_nul - p), p);
bcb3c1e
             p = colon_or_nul;
bcb3c1e
         }
bcb3c1e
+
bcb3c1e
+        strbuf_append_strf(set_debug_file_directory, ":%s", debug_directories->buf);
bcb3c1e
+
bcb3c1e
+        args[i++] = (char*)"-iex";
bcb3c1e
+        auto_load_base_index = i;
bcb3c1e
+        args[i++] = xasprintf("add-auto-load-safe-path %s", debug_directories->buf);
bcb3c1e
+        args[i++] = (char*)"-iex";
bcb3c1e
+        args[i++] = xasprintf("add-auto-load-scripts-directory %s", debug_directories->buf);
bcb3c1e
+
bcb3c1e
+        strbuf_free(debug_directories);
bcb3c1e
     }
bcb3c1e
-    args[3] = strbuf_free_nobuf(set_debug_file_directory);
bcb3c1e
+
bcb3c1e
+    args[i++] = (char*)"-ex";
bcb3c1e
+    const unsigned debug_dir_cmd_index = i++;
bcb3c1e
+    args[debug_dir_cmd_index] = strbuf_free_nobuf(set_debug_file_directory);
bcb3c1e
 
bcb3c1e
     /* "file BINARY_FILE" is needed, without it gdb cannot properly
bcb3c1e
      * unwind the stack. Currently the unwind information is located
bcb3c1e
@@ -300,27 +317,31 @@ char *get_backtrace(const char *dump_dir_name, unsigned timeout_sec, const char
bcb3c1e
      * TODO: check mtimes on COREFILE and BINARY_FILE and not supply
bcb3c1e
      * BINARY_FILE if it is newer (to at least avoid gdb complaining).
bcb3c1e
      */
bcb3c1e
-    args[4] = (char*)"-ex";
bcb3c1e
-    args[5] = xasprintf("file %s", executable);
bcb3c1e
+    args[i++] = (char*)"-ex";
bcb3c1e
+    const unsigned file_cmd_index = i++;
bcb3c1e
+    args[file_cmd_index] = xasprintf("file %s", executable);
bcb3c1e
     free(executable);
bcb3c1e
 
bcb3c1e
-    args[6] = (char*)"-ex";
bcb3c1e
-    args[7] = xasprintf("core-file %s/"FILENAME_COREDUMP, dump_dir_name);
bcb3c1e
+    args[i++] = (char*)"-ex";
bcb3c1e
+    const unsigned core_cmd_index = i++;
bcb3c1e
+    args[core_cmd_index] = xasprintf("core-file %s/"FILENAME_COREDUMP, dump_dir_name);
bcb3c1e
 
bcb3c1e
-    args[8] = (char*)"-ex";
bcb3c1e
+    args[i++] = (char*)"-ex";
bcb3c1e
+    const unsigned bt_cmd_index = i++;
bcb3c1e
     /*args[9] = ... see below */
bcb3c1e
-    args[10] = (char*)"-ex";
bcb3c1e
-    args[11] = (char*)"info sharedlib";
bcb3c1e
+    args[i++] = (char*)"-ex";
bcb3c1e
+    args[i++] = (char*)"info sharedlib";
bcb3c1e
     /* glibc's abort() stores its message in __abort_msg variable */
bcb3c1e
-    args[12] = (char*)"-ex";
bcb3c1e
-    args[13] = (char*)"print (char*)__abort_msg";
bcb3c1e
-    args[14] = (char*)"-ex";
bcb3c1e
-    args[15] = (char*)"print (char*)__glib_assert_msg";
bcb3c1e
-    args[16] = (char*)"-ex";
bcb3c1e
-    args[17] = (char*)"info all-registers";
bcb3c1e
-    args[18] = (char*)"-ex";
bcb3c1e
-    args[19] = (char*)"disassemble";
bcb3c1e
-    args[20] = NULL;
bcb3c1e
+    args[i++] = (char*)"-ex";
bcb3c1e
+    args[i++] = (char*)"print (char*)__abort_msg";
bcb3c1e
+    args[i++] = (char*)"-ex";
bcb3c1e
+    args[i++] = (char*)"print (char*)__glib_assert_msg";
bcb3c1e
+    args[i++] = (char*)"-ex";
bcb3c1e
+    args[i++] = (char*)"info all-registers";
bcb3c1e
+    args[i++] = (char*)"-ex";
bcb3c1e
+    const unsigned dis_cmd_index = i++;
bcb3c1e
+    args[dis_cmd_index] = (char*)"disassemble";
bcb3c1e
+    args[i++] = NULL;
bcb3c1e
 
bcb3c1e
     /* Get the backtrace, but try to cap its size */
bcb3c1e
     /* Limit bt depth. With no limit, gdb sometimes OOMs the machine */
bcb3c1e
@@ -330,9 +351,9 @@ char *get_backtrace(const char *dump_dir_name, unsigned timeout_sec, const char
bcb3c1e
     char *bt = NULL;
bcb3c1e
     while (1)
bcb3c1e
     {
bcb3c1e
-        args[9] = xasprintf("%s backtrace %u%s", thread_apply_all, bt_depth, full);
bcb3c1e
+        args[bt_cmd_index] = xasprintf("%s backtrace %u%s", thread_apply_all, bt_depth, full);
bcb3c1e
         bt = exec_vp(args, /*redirect_stderr:*/ 1, timeout_sec, NULL);
bcb3c1e
-        free(args[9]);
bcb3c1e
+        free(args[bt_cmd_index]);
bcb3c1e
         if ((bt && strnlen(bt, 256*1024) < 256*1024) || bt_depth <= 32)
bcb3c1e
         {
bcb3c1e
             break;
bcb3c1e
@@ -357,7 +378,7 @@ char *get_backtrace(const char *dump_dir_name, unsigned timeout_sec, const char
bcb3c1e
          * End of assembler dump.
bcb3c1e
          * (IOW: "empty" dump)
bcb3c1e
          */
bcb3c1e
-        args[19] = (char*)"disassemble $pc-20, $pc+64";
bcb3c1e
+        args[dis_cmd_index] = (char*)"disassemble $pc-20, $pc+64";
bcb3c1e
 
bcb3c1e
         if (bt_depth <= 64 && thread_apply_all[0] != '\0')
bcb3c1e
         {
bcb3c1e
@@ -373,9 +394,15 @@ char *get_backtrace(const char *dump_dir_name, unsigned timeout_sec, const char
bcb3c1e
         }
bcb3c1e
     }
bcb3c1e
 
bcb3c1e
-    free(args[3]);
bcb3c1e
-    free(args[5]);
bcb3c1e
-    free(args[7]);
bcb3c1e
+    if (auto_load_base_index > 0)
bcb3c1e
+    {
bcb3c1e
+        free(args[auto_load_base_index]);
bcb3c1e
+        free(args[auto_load_base_index + 2]);
bcb3c1e
+    }
bcb3c1e
+
bcb3c1e
+    free(args[debug_dir_cmd_index]);
bcb3c1e
+    free(args[file_cmd_index]);
bcb3c1e
+    free(args[core_cmd_index]);
bcb3c1e
     return bt;
bcb3c1e
 }
bcb3c1e
 
bcb3c1e
-- 
bcb3c1e
2.1.0
bcb3c1e