gdb 'call' / 'print' commands don't work properly when attaching to R

Bug #1926815 reported by Kevin Ushey
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
gdb (Ubuntu)
New
Undecided
Unassigned

Bug Description

Sorry for the somewhat weird / vague title; hopefully the rest of this issue will make it more concrete ...

First, to set the stage; gdb and R both installed from apt repositories:

$ /usr/bin/gdb --version
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ /usr/bin/R --version
R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.

Now, I can use gdb in batch mode to introspect a bash process; e.g. print the SHELL environment variable:

$ /usr/bin/gdb -batch -p $(pgrep -nx bash) -ex 'print (char*) getenv("SHELL")'
0x00007f5623d8c1db in __pselect (nfds=1, readfds=0x7ffdaeda52f0, writefds=0x0, exceptfds=0x0, timeout=<optimized out>, sigmask=0x5615fe778140 <_rl_orig_sigset>) at ../sysdeps/unix/sysv/linux/pselect.c:48
48 ../sysdeps/unix/sysv/linux/pselect.c: No such file or directory.
$1 = 0x5615ffc825d0 "/bin/bash"
[Inferior 1 (process 640102) detached]

But, if I try to do the same with a running R process, I see:

$ R -s -e "Sys.sleep(100)" &
[1] 643585

$ /usr/bin/gdb -batch -p $(pgrep -nx R) -ex 'print (char*) getenv("SHELL")'
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
0x00007f47944050da in select () from /usr/lib/x86_64-linux-gnu/libc.so.6
Invalid character '"' in expression.
[Inferior 1 (process 643585) detached]

Note the very confusing 'Invalid character '"' in expression.' error, implying that gdb was for some reason unable to handle the double-quoted string passed to getenv().

I cannot reproduce this in a version of gdb 9.2 built from sources locally; e.g.

$ gdb --version
GNU gdb (GDB) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ gdb -batch -p $(pgrep -nx R) -ex 'print (char*) getenv("SHELL")'
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
0x00007fba31ad60da in select () from /usr/lib/x86_64-linux-gnu/libc.so.6
$1 = 0x7fff4dc9adfe "/bin/bash"
[Inferior 1 (process 701827) detached]

So it seems like something specifically is broken in the version of GDB packaged for Ubuntu 20.04.

Revision history for this message
Kevin Ushey (kevinushey) wrote :

In addition, I get a segfault if I use a single-quoted string rather than a double-quoted string. (My understanding is that single-quoted strings are normally used to reference symbols, so this should be an error but not a crash)

$ /usr/bin/gdb -batch -p $(pgrep -nx R) -ex "print (char*) getenv('oops')"
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
0x00007f69b4c870da in select () from /usr/lib/x86_64-linux-gnu/libc.so.6
Aborted (core dumped)

Kevin Ushey (kevinushey)
summary: - gdb doesn't work properly when attaching to R
+ gdb 'call' / 'print' commands don't work properly when attaching to R
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.