public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH] gdb: use actual DWARF version in compunit's debugformat field
Date: Thu, 18 Nov 2021 21:08:45 -0500	[thread overview]
Message-ID: <20211119020845.2606300-1-simon.marchi@efficios.com> (raw)

The "info source" command, with a DWARF-compile program, always show
that the debug info is "DWARF 2":

    (gdb) info source
    Current source file is test.c
    Compilation directory is /home/smarchi/build/binutils-gdb/gdb
    Located in /home/smarchi/build/binutils-gdb/gdb/test.c
    Contains 2 lines.
    Source language is c.
    Producer is GNU C17 9.3.0 -mtune=generic -march=x86-64 -g3 -gdwarf-5 -O0 -fasynchronous-unwind-tables -fstack-protector-strong -fstack-clash-protection -fcf-protection.
    Compiled with DWARF 2 debugging format.
    Includes preprocessor macro info.

Change it to display the actual DWARF version:

    (gdb) info source
    Current source file is test.c
    Compilation directory is /home/smarchi/build/binutils-gdb/gdb
    Located in /home/smarchi/build/binutils-gdb/gdb/test.c
    Contains 2 lines.
    Source language is c.
    Producer is GNU C17 9.3.0 -mtune=generic -march=x86-64 -g3 -gdwarf-5 -O0 -fasynchronous-unwind-tables -fstack-protector-strong -fstack-clash-protection -fcf-protection.
    Compiled with DWARF 5 debugging format.
    Includes preprocessor macro info.

The debug format strings need to live as long as the obfile itself, so
the most straightforward thing to do is to save those strings on the
objfile's obstack.  But it seems like we don't have an equivalent of
"obstack_printf".  An obstack_printf function is provided by glibc:

  https://www.gnu.org/software/libc/manual/html_node/Dynamic-Output.html

... and there's even a gnulib module for it:

  https://www.gnu.org/software/gnulib/manual/html_node/obstack_005fprintf.html

However, since GDB uses its own obstack implementation (include/obstack.h), I
don't think it would be safe to use a glibc obstack function and pass it a GDB
obstack.  The obstack struct layouts might not be the same.

Implement some simple gdb_obstack_printf/gdb_obstack_vprintf functions
to work around that.

Change-Id: I3270b7ebf5e9a17b4215405bd2e365662a4d6172
---
 gdb/dwarf2/cu.c   |  5 ++++-
 gdb/gdb_obstack.c | 32 ++++++++++++++++++++++++++++++++
 gdb/gdb_obstack.h |  8 ++++++++
 3 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/gdb/dwarf2/cu.c b/gdb/dwarf2/cu.c
index e7aed774c25..371005ab7ce 100644
--- a/gdb/dwarf2/cu.c
+++ b/gdb/dwarf2/cu.c
@@ -64,7 +64,10 @@ dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
 
   list_in_scope = get_builder ()->get_file_symbols ();
 
-  get_builder ()->record_debugformat ("DWARF 2");
+  get_builder ()->record_debugformat
+    (gdb_obstack_printf
+       (&this->per_objfile->objfile->objfile_obstack,
+	"DWARF %u", this->header.version));
   get_builder ()->record_producer (producer);
 
   processing_has_namespace_info = false;
diff --git a/gdb/gdb_obstack.c b/gdb/gdb_obstack.c
index 3833a6d69a1..d06942884e6 100644
--- a/gdb/gdb_obstack.c
+++ b/gdb/gdb_obstack.c
@@ -19,6 +19,7 @@
 
 #include "defs.h"
 #include "gdb_obstack.h"
+#include <stdio.h>
 
 /* Concatenate NULL terminated variable argument list of `const char *'
    strings; return the new string.  Space is found in the OBSTACKP.
@@ -45,3 +46,34 @@ obconcat (struct obstack *obstackp, ...)
 
   return (char *) obstack_finish (obstackp);
 }
+
+/* See gdb_obstack.h.  */
+
+const char *
+gdb_obstack_vprintf (obstack *ob, const char *fmt, va_list args)
+{
+  va_list args_copy;
+
+  va_copy (args_copy, args);
+  size_t size = vsnprintf (nullptr, 0, fmt, args_copy);
+  va_end (args_copy);
+
+  char *str = XOBNEWVEC (ob, char, size + 1);
+  vsprintf (str, fmt, args);
+
+  return str;
+}
+
+/* See gdb_obstack.h.  */
+
+const char *
+gdb_obstack_printf (obstack *ob, const char *fmt, ...)
+{
+  va_list args;
+
+  va_start (args, fmt);
+  const char *str = gdb_obstack_vprintf (ob, fmt, args);
+  va_end (args);
+
+  return str;
+}
diff --git a/gdb/gdb_obstack.h b/gdb/gdb_obstack.h
index f743bdd3eb1..dc752853dcf 100644
--- a/gdb/gdb_obstack.h
+++ b/gdb/gdb_obstack.h
@@ -21,6 +21,7 @@
 #define GDB_OBSTACK_H 1
 
 #include "obstack.h"
+#include <stdarg.h>
 
 /* Utility macros - wrap obstack alloc into something more robust.  */
 
@@ -116,6 +117,13 @@ obstack_strndup (struct obstack *obstackp, const char *string, size_t n)
   return (char *) obstack_copy0 (obstackp, string, n);
 }
 
+/* Write a formatted string to obstack OB.  */
+
+const char *gdb_obstack_vprintf (obstack *ob, const char *fmt, va_list args)
+  ATTRIBUTE_PRINTF (2, 0);
+const char *gdb_obstack_printf (obstack *ob, const char *fmt, ...)
+  ATTRIBUTE_PRINTF (2, 3);
+
 /* An obstack that frees itself on scope exit.  */
 struct auto_obstack : obstack
 {
-- 
2.33.1


             reply	other threads:[~2021-11-19  2:08 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-19  2:08 Simon Marchi [this message]
2021-11-19  2:10 ` Simon Marchi
2021-11-19 18:32 ` Pedro Alves
2021-11-19 20:41 ` Tom Tromey
2021-11-19 23:00   ` Simon Marchi
2021-11-20  1:10     ` [PATCH v2] " Simon Marchi
2021-11-23  1:57       ` [PATCH v3] " Simon Marchi
2021-12-02 15:09         ` Tom de Vries
2021-12-02 16:09           ` [PATCH] gdb/testsuite: update tests looking for "DWARF 2" debug format Simon Marchi
2021-12-02 16:14             ` Tom de Vries
2021-12-02 16:56               ` Simon Marchi
2021-11-22 19:32     ` [PATCH] gdb: use actual DWARF version in compunit's debugformat field Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211119020845.2606300-1-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).