In response to Employed Russian's answer I verified that I do have debug packages that should provide debug info sufficient to call functions such as floor
from the gdb command-line, but something is still behaving oddly in gdb.
I'm running this version of gdb via:
gdb --version
is:
GNU gdb (Ubuntu 8.3-0ubuntu1) 8.3
Copyright (C) 2019 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.
The Linux machine operating system info via:
lsb_release -r -i
is:
Distributor ID: Ubuntu
Release: 19.10
Debug packages that I expect for gdb to use in order to find the floor
symbol via:
dpkg --listfiles libc6-dbg | grep libm
is:
/usr/lib/debug/lib/x86_64-linux-gnu/libm-2.30.so
/usr/lib/debug/lib/x86_64-linux-gnu/libmemusage.so
/usr/lib/debug/lib/x86_64-linux-gnu/libmvec-2.30.so
Compiling, running, and then running gdb via:
#!/bin/bash
exec 2>&1
set -x
rm -rf "/tmp/testdir"
mkdir -p "/tmp/testdir"
cd "/tmp/testdir"
cat > main.cpp <<'EOF'
#include <stdio.h>
#include <math.h> // double floor(double x);
int main(int argc, char *argv[], char *const envp[])
{
printf("From inside C++: floor(2.12) %g\n", floor(2.12));
return 0;
} // end main
EOF
/usr/bin/c++ -MD -DDEBUG -g -fPIC -Wall -Werror -Wsynth -Wno-comment -Wreturn-type $cxx_args main.cpp -c -o main.o
/usr/bin/c++ -MD -DDEBUG -g -fPIC -Wall -Werror -Wsynth -Wno-comment -Wreturn-type $cxx_args main.o -L. -L/usr/lib64 -lstdc++ -o main.exe
(./main.exe; exit 0)
cat > /tmp/gdb.commands <<EOF
# https://mcmap.net/q/376743/-how-to-make-gdb-emit-the-commands-it-is-executing-in-batch-mode
set trace-commands on
b main
r
info shared
p floor(2.12)
EOF
gdb -batch -x /tmp/gdb.commands main.exe
exit 0
is:
+ rm -rf /tmp/testdir
+ mkdir -p /tmp/testdir
+ cd /tmp/testdir
+ cat
+ /usr/bin/c++ -MD -DDEBUG -g -fPIC -Wall -Werror -Wsynth -Wno-comment -Wreturn-type main.cpp -c -o main.o
+ /usr/bin/c++ -MD -DDEBUG -g -fPIC -Wall -Werror -Wsynth -Wno-comment -Wreturn-type main.o -L. -L/usr/lib64 -lstdc++ -o main.exe
+ ./main.exe
From inside C++: floor(2.12) 2
+ exit 0
+ cat
+ gdb -batch -x /tmp/gdb.commands main.exe
+b main
Breakpoint 1 at 0x1149: file main.cpp, line 5.
+r
Breakpoint 1, main (argc=0, argv=0x7fffffffdbc0, envp=0x555555555060 <_start>) at main.cpp:5
5 {
+info shared
From To Syms Read Shared Object Library
0x00007ffff7fd1100 0x00007ffff7ff23f4 Yes /lib64/ld-linux-x86-64.so.2
0x00007ffff7dc9670 0x00007ffff7f3e74f Yes /lib/x86_64-linux-gnu/libc.so.6
+p floor(2.12)
/tmp/gdb.commands:6: Error in sourced command file:
No symbol "floor" in current context.
+ exit 0
Even though I do have debug info installed as shown above, why is gdb unable to allow me to call a function that the program itself is able to call?
How do I fix that?
Update 1
Per help from Employed Russian in https://mcmap.net/q/376743/-how-to-make-gdb-emit-the-commands-it-is-executing-in-batch-mode, I used set trace-command on
in the gdb commands.
Update 2
Per Employed Russian's comment, inserted in info shared
into gdb commands.
Update 3
Per Employed Russian's answer, I rebuilt using -fno-builtin
via:
#!/bin/bash
exec 2>&1
set -x
cxx_args='-fno-builtin'
rm -rf "/tmp/testdir"
mkdir -p "/tmp/testdir"
cd "/tmp/testdir"
cat > main.cpp <<'EOF'
#include <stdio.h>
#include <math.h> // double floor(double x);
int main(int argc, char *argv[], char *const envp[])
{
printf("From inside C++: floor(2.12) %g\n", floor(2.12));
return 0;
} // end main
EOF
/usr/bin/c++ -MD -DDEBUG -g -fPIC -Wall -Werror -Wsynth -Wno-comment -Wreturn-type $cxx_args main.cpp -c -o main.o
/usr/bin/c++ -MD -DDEBUG -g -fPIC -Wall -Werror -Wsynth -Wno-comment -Wreturn-type $cxx_args main.o -L. -L/usr/lib64 -lstdc++ -o main.exe
(./main.exe; exit 0)
cat > /tmp/gdb.commands <<EOF
# https://mcmap.net/q/376743/-how-to-make-gdb-emit-the-commands-it-is-executing-in-batch-mode
set trace-commands on
b main
r
info shared
p floor(2.12)
EOF
gdb -batch -x /tmp/gdb.commands main.exe
exit 0
is:
+ cxx_args=-fno-builtin
+ rm -rf /tmp/testdir
+ mkdir -p /tmp/testdir
+ cd /tmp/testdir
+ cat
+ /usr/bin/c++ -MD -DDEBUG -g -fPIC -Wall -Werror -Wsynth -Wno-comment -Wreturn-type -fno-builtin main.cpp -c -o main.o
+ /usr/bin/c++ -MD -DDEBUG -g -fPIC -Wall -Werror -Wsynth -Wno-comment -Wreturn-type -fno-builtin main.o -L. -L/usr/lib64 -lstdc++ -o main.exe
+ ./main.exe
From inside C++: floor(2.12) 2
+ exit 0
+ cat
+ gdb -batch -x /tmp/gdb.commands main.exe
+b main
Breakpoint 1 at 0x1169: file main.cpp, line 5.
+r
Breakpoint 1, main (argc=0, argv=0x7fffffffdbc0, envp=0x555555555080 <_start>) at main.cpp:5
5 {
+info shared
From To Syms Read Shared Object Library
0x00007ffff7fd1100 0x00007ffff7ff23f4 Yes /lib64/ld-linux-x86-64.so.2
0x00007ffff7e553c0 0x00007ffff7efbe78 Yes /lib/x86_64-linux-gnu/libm.so.6
0x00007ffff7c7a670 0x00007ffff7def74f Yes /lib/x86_64-linux-gnu/libc.so.6
+p floor(2.12)
$1 = 2
+ exit 0
(gdb) info shared
? – Allopatricinfo shared
gdb command. – Vamp