[PATCH v4] gdb, gdbserver: support dlmopen()

Kevin Buettner kevinb@redhat.com
Wed May 25 17:12:04 GMT 2022


On Wed, 17 Nov 2021 15:28:12 +0100
Markus Metzger via Gdb-patches <gdb-patches@sourceware.org> wrote:

> This patch was originally developed by H.J.  Last version:
> https://sourceware.org/pipermail/gdb-patches/2021-October/182353.html
> 
> Changes in v4:
> 
> 1. add gdb.base/dlmopen.exp
> 2. extend svr4_same() to compare the load offset in addition to the name
> 3. supply l_addr_inferior for default DSO to make it work with #2
> 4. fix bugs in gdbserver/linux-low.cc
> 
> Changes in v3:
> 
> 1. Fix gdbserver support.
> 
> Changes in v2:
> 
> 1. Don't check shared libraries in other namespaces when updating shared
> libraries in a new namespace.
> 
> Regression-tested on x86-64 linux.
> 
> ---
> 
> In glibc, the r_debug structure contains (amongst others) the following
> fields:
> 
>   int r_version:
>     Version number for this protocol.  It should be greater than 0.
> 
> If r_version is 2, struct r_debug is extended to struct r_debug_extended
> with one additional field:
> 
>   struct r_debug_extended *r_next;
>     Link to the next r_debug_extended structure.  Each r_debug_extended
>     structure represents a different namespace.  The first r_debug_extended
>     structure is for the default namespace.
> 
> 1. Change solib_svr4_r_map argument to take the debug base.
> 2. Add solib_svr4_r_next to find the link map in the next namespace from
> the r_next field.
> 3. Update svr4_current_sos_direct to get the link map in the next namespace
> from the r_next field.
> 4. Don't check shared libraries in other namespaces when updating shared
> libraries in a new namespace.
> 5. Update svr4_same to check the load offset in addition to the name
> 6. Update svr4_default_sos to also set l_addr_inferior
> 
> Add gdb.base/dlmopen.exp to test this.
> 
> This fixes PR 11839.

I've been playing around with this patch on Fedora 36 which uses
glibc-2.35.  I also found an LD_AUDIT library to play with.  See:

https://github.com/buildsi/ldaudit-yaml

Here are the interesting portions of a session when debugging whoami
while using the audit library on F36:

  [kev@f36-1 ldaudit-yaml]$ /mesquite2/sourceware-git/f36-dlmopen/inst/bin/gdb -q whoami
  Reading symbols from whoami...
  [...]
  (gdb) b main
  Breakpoint 1 at 0x25a0: file ../src/whoami.c, line 59.
  (gdb) set env LD_AUDIT=./auditlib.so
  (gdb) run
  Starting program: /usr/bin/whoami 
  auditlib:
    la_version: 2
    audits:
    - event: handshake
	function: la_version
	value: 2
  [lots of output from the audit library snipped]
  Breakpoint 1, main (argc=1, argv=0x7fffffffdbe8) at ../src/whoami.c:59
  59	{
  (gdb) info shared
  From                To                  Syms Read   Shared Object Library
  0x00007ffff7fc8090  0x00007ffff7feea45  Yes         /lib64/ld-linux-x86-64.so.2
  0x00007ffff7fb12a0  0x00007ffff7fb9022  Yes         ./auditlib.so
  0x00007ffff7df73f0  0x00007ffff7eff532  Yes         /lib64/libstdc++.so.6
  0x00007ffff7c873b0  0x00007ffff7cf8b58  Yes         /lib64/libm.so.6
  0x00007ffff7c5a670  0x00007ffff7c70c05  Yes         /lib64/libgcc_s.so.1
  0x00007ffff7a82740  0x00007ffff7bf371d  Yes         /lib64/libc.so.6
  0x00007ffff7fc8090  0x00007ffff7feea45  Yes         /lib64/ld-linux-x86-64.so.2
  0x00007ffff777d740  0x00007ffff78ee71d  Yes         /lib64/libc.so.6

Without this patch (or when running on an OS with a version of glibc
earlier than 2.35), I see:

  (gdb) info shared
  From                To                  Syms Read   Shared Object Library
  0x00007ffff7fc9090  0x00007ffff7fee593  Yes         /lib64/ld-linux-x86-64.so.2
  0x00007ffff778c740  0x00007ffff78fee3d  Yes         /lib64/libc.so.6

I found that I can also place breakpoints in the audit library and run
to those breakpoints.  When stopped at a breakpoint, the backtrace looks
reasonable.  E.g...

  Breakpoint 2, doPrint (output=...) at auditlib.cpp:30
  30	  char * out = getenv("LDAUDIT_OUTFILE");
  (gdb) bt
  #0  doPrint (output=...) at auditlib.cpp:30
  #1  0x00007ffff7fb4cf0 in la_activity (cookie=<optimized out>, 
	flag=<optimized out>) at /usr/include/c++/11/bits/char_traits.h:357
  #2  0x00007ffff7fdf415 in _dl_audit_activity_map (l=l@entry=0x7ffff7ffe2a0, 
	action=action@entry=1) at dl-audit.c:33
  #3  0x00007ffff7fe6c98 in dl_main (phdr=<optimized out>, 
	phnum=<optimized out>, user_entry=<optimized out>, auxv=<optimized out>)
	at rtld.c:1832
  #4  0x00007ffff7fe328f in _dl_sysdep_start (
	start_argptr=start_argptr@entry=0x7fffffffdbe0, 
	dl_main=dl_main@entry=0x7ffff7fe52a0 <dl_main>) at ../elf/dl-sysdep.c:256
  #5  0x00007ffff7fe503e in _dl_start_final (arg=0x7fffffffdbe0) at rtld.c:507
  #6  _dl_start (arg=0x7fffffffdbe0) at rtld.c:596
  #7  0x00007ffff7fe3e38 in _start () from /lib64/ld-linux-x86-64.so.2

While I'm convinced that other work will be needed to improve GDB's UI
to both display linker namespaces (e.g.  in the "info shared" command)
and accept namespace qualifiers when specifying a symbol (e.g. with a
breakpoint command), I think that this current patch is useful as
is.  I.e., I'd like to see it (or a modest update) go in as soon as
possible.

Kevin



More information about the Gdb-patches mailing list