Blame SOURCES/0077-gdb-make-gdb-aware-of-the-abrt-s-debuginfo-dir.patch

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