GDB does not load symbols from libraries - c

I try to debug some native code on Android with GBD. The code wasn't created by me and is not in an Android project, so I can't use the ndk-gdb tool. I use gdbserver on the android machine and connect to it from my mac with the normal GDB program. I try to load all the libraries (which should have symbols according to objdump tool), but gdb tells me that it does not load the symbols (according to the gdb command “info sharedLibrary”). These are the steps I took:
start gdbserver on Android machine
start GDB with the debug version of the binary
gdb symbols/system/bin/mediaserver
the following commands are executed in gdb itself
tell gdb where to look for the libraries with symbols
(gdb) set solib-search-path symbols/system/lib
tell gdb where to find the source files
(gdb) directory /sources
connect to remote target (Android machine)
(gdb) target remote 192.168.1.10:5039
GDB connects successfully to the running binary and I can pause and continue the execution. But it does not show me any debug information like function names or line numbers. It only shows adresses. When I check the status of the used libraries, I see that gdb thinks, they don’t have any symbols:
command in gdb:
(gdb) info sharedLibrary
From To Syms Read Shared Object Library
0x00003700 0x0000ff0c Yes /symbols/system/bin/linker
No libc.so
No libstdc++.so
No libm.so
No liblog.so
No libcutils.so
No libspeexresampler.so
No libaudioutils.so
No libgccdemangle.so
No libamplayer.so
Now for example the last library. When I check with the file command (not in gdb), it tells me that it is a not stripped library (the library is located in the "symbols/system/lib" folder).
file libamplayer.so
Output:
libamplayer.so: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked, not stripped
objdump command shows a lot of symbols in it (I don’t show the output because it’s very long). So why does gdb not recognise the symbols in this libraries? I thought, at least line numbers and function names are present in not stripped versions of libraries. Or am I wrong? It would be very nice if someone could give me more insight.
Thanks!
System info:
GDB Version: 7.3.1-gg2 on Mac OS X Mavericks

The code wasn't created by me and is not in an Android project, so I can't use the ndk-gdb tool.
Your conclusion does not at all follow. ndk-gdb should be able to debug any Android program, whether created as a "project" or via other means.
I use gdbserver on the android machine and connect to it from my mac with the normal GDB program.
The normal GDB is likely not configured for cross-debugging, and thus doesn't understand ARM binaries at all. I am surprised you get as far using it as you do.

Related

How do I debug Android native code into kernel?

I compiled goldfish kernel with:
[ ] Optimize for size,
[*] Kernel hacking
[*] Compile the kernel with debug info
[*] KGDB: kernel debugging with remote gdb —>
[*] Enable dynamic printk() call support
I started the AVD with the compiled kernel.
emulator -kernel goldfish/arch/arm/boot/zImage -avd TestAVD
I pushed a compiled c program onto AVD.
And I downloaded the libs (I'm not sure if it's the proper way)
adb pull /system/lib ./debuginfo/lib
adb pull /system/bin/linker ./debuginfo/lib
run gdbserver:
gdbserver 127.0.0.1:7777 ./a
forward port:
adb forward tcp:7777 tcp:7777
run gdb
gdb-multiarch ./a
specify the search directory:
set solib-search-path ./debuginfo/lib
connect to device
target remote :7777
I breaked at, for example, close.
0xaf0ae228 in close () from /home/wuyihao/android_sec/debuginfo/lib/libc.so
1: x/i $pc
=> 0xaf0ae228 <close+8>: svc 0x00000000
(gdb) list
No symbol table is loaded. Use the "file" command.
I couln'd get the source.
So I tried to recompile the kernel with variable CFLAG="-g"
Nothing different.
Thank you!
ps: I noticed the downloaded libs are all stripped.
libc.so is not the Linux kernel. It is C standard library.
It is extremely unlikely you'll need to look for a problem there. If your close() call doesn't work, it's almost certain something is wrong with you using it, not its implementation.

Unable to debug lInux on gdb

I am trying to debug Linux using gdb, I compiled kernel with "compile kernel with debug info".
once kernel is compiled I am trying to give
GDB file vmlinux
(running above command gives me below error)
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Reading symbols from file...(no debugging symbols found)...done.
"/home/Linux/vmlinux" is not a core dump: File format not recognized
Even I am setting debug con-figs it is giving me "no debugging symbols found"
kernel debugging is a whole different ballgame from debugging a user process and is not as simple as firing up GDB. For this you will need support of a JTAG.
QEMU allows you to virtualize a machine and therefore use a virtual JTAG.
For simple stuff you will probably find printk much easier.

Enable debugging of kernel module from user-space process

I am working with openvswitch on ubuntu 14.04 server. I can easily attach gdb with any of its binary files for debugging its various features but when it comes to its kernel module, I am not able to debug it as per my requirement.
I am using following steps to attach linux kernel with gdb:
1. gdb /tmp/vmlinux /proc/kcore
2. Adding a symbol File to GDB:
cd /sys/module/openvswitch/sections/
ls -A1
.cat .text .data .bss
3. in gdb - add-symbol-file datapath/linux/openvswitch.ko 0xf87a2000 -s .data 0xf87b4000 -s .bss 0xf87b4560
4. b vxlan_udp_encap_recv
but when I generate packets for testing ovs kernel module and step over it says "The program is not being run."
Note: I have confirmed all module symbols by running this command: nm root/ovs/_build-gcc/datapath/linux/openvswitch.ko = which prints all symbols. and lsmod also confirms the existence of ovs kernel module.
I want to make ovs module stop at specified break point after it receives a message from its user-space application on netlink socket for its detailed debugging as conveniently as it allows me to debug a user-space process. Please suggest me how do I resolve this problem or if there is any alternative. I'll be really grateful for any help or suggestion. Thank you!
To debug the kernel you need to use KGDB / KDB.
one possibility:
run the gdb server on the target machine. run gdb on another machine. recompile the target machine kernel with the -ggdb parameter on gcc. start both machines with the target machine running the kernel with all the -ggdb info. have all the source available on both machines. connect from the testing machine to the target machine. have the gdb server connect to the kernel ....

How to use tracing in GDB

In the documentation for gdb:
The tracepoint facility is currently available only for remote
targets. See section Specifying a Debugging Target. In addition, your
remote target must know how to collect trace data. This functionality
is implemented in the remote stub; however, none of the stubs
distributed with GDB support tracepoints as of this writing.
Emphasis mine. Where can I get such a stub (for C/C++ code compiled with GCC on Debian x86 or x64)? Or how do I go about making one? The documentation on writing stubs only mentions implementing functions to communicate with the serial ports. Thanks!
I don't know much about remotes but some targets in gdb now do support tracepoints
there is possibly a way to get at this using a 'normal' gdb info or show command, I could not find it. in the output below tracepoints are supported due to the 'supported=1',
this may not be limited to the gdb stub, but also the kernel the stub is running on.
$ gdbserver/gdbserver :12345 ~/tests/test &
$ gdb -quiet
(gdb) file ~/tests/test
Reading symbols from /home/ratmice/tests/test...done.
(gdb) target remote :12345
Remote debugging using :12345
Remote debugging from host 127.0.0.1
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00000035dd600b20 in _start () from /lib64/ld-linux-x86-64.so.2
Created trace state variable $trace_timestamp for target's variable 1.
Missing separate debuginfos, use: debuginfo-install glibc-2.13-2.x86_64
(gdb) interpreter-exec mi2 -trace-status
^done,supported="1",running="0",frames="0",frames-created="0",buffer-size="5242880",buffer-free="5242880",disconnected="0",circular="0"

gdb fails to run ELF 64-bit program with "File format not recognized"

I'm trying to use GDB to debug (to find an annoying segfault). When I run:
gdb ./filename
from the command line, I get the following error:
This GDB was configured as "i686-pc-linux-
gnu"..."/path/exec": not in executable
format: File format not recognized
When I execute:
file /path/executable/
I get the following info:
ELF 64-bit LSB executable, AMD x86-64,
version 1 (SYSV), for GNU/Linux 2.4.0,
dynamically linked (uses shared libs), not stripped
I am using GDB 6.1, and the executable is compiled with gcc version 3.4.6.
I'm a little out of my water in terms of using gdb, but as far as I can tell it should be working in this instance. Any ideas what's going wrong?
The executable is 64-bit (x86-64) and the debugger is a 32 bit (i686-pc-linux) build. You may need to install a 64-bit (x86-64) version of the debugger.
I'm not sure if this is your problem, but I faced this situation very often. The executable in the build tree, build by make/automake is not a binary, but a script, so you cannot use gdb with it. Try to install the application and change the directory, because else gdb tries to debug the script.
The question refers to "./filename" and to "/path/executable". Are these the same file?
If you are doing a post-mortem analysis, you would run:
gdb executable-file core-file
If you are going to ignore the core file, you would run:
gdb executable-file
In both cases, 'executable-file' means a pathname to the binary you want to debug. Most usually, that is actually a simple filename in the current directory, since you have the source code from your debug build there.
On Solaris, a 64-bit build of GDB is supposed to be able to debug both 32-bit and 64-bit executables (though I've had some issues with recent versions of GDB). I'm not sure of the converse - that a 32-bit GDB can necessarily debug 64-bit executables.
What you need to be checking, is really the bfd library. The binary file descriptor library is what binutils / gdb uses to actually parse and handle binaries (ELF/a.out etc..).
You can see the current supported platforms via objdump;
# objdump -H
objdump: supported targets: elf32-powerpc aixcoff-rs6000 elf32-powerpcle ppcboot elf64-powerpc elf64-powerpcle elf64-little elf64-big elf32-little elf32-big srec symbolsrec tekhex binary ihex
objdump: supported architectures: rs6000:6000 rs6000:rs1 rs6000:rsc rs6000:rs2 powerpc:common powerpc:common64 powerpc:603 powerpc:EC603e powerpc:604 powerpc:403 powerpc:601 powerpc:620 powerpc:630 powerpc:a35 powerpc:rs64ii powerpc:rs64iii powerpc:7400 powerpc:e500 powerpc:MPC8XX powerpc:750
The following PPC specific disassembler options are supported for use with
the -M switch:
booke|booke32|booke64 Disassemble the BookE instructions
e300 Disassemble the e300 instructions
e500|e500x2 Disassemble the e500 instructions
efs Disassemble the EFS instructions
power4 Disassemble the Power4 instructions
power5 Disassemble the Power5 instructions
power6 Disassemble the Power6 instructions
32 Do not disassemble 64-bit instructions
64 Allow disassembly of 64-bit instructions
It seems your GNU Debugger (gdb) doesn't support x86_64 architecture.
So try LLDB Debugger (lldb) which aims to replace it. It supports i386, x86-64 and ARM instruction sets.
It's available by default on BSD/OS X, on Linux install via: sudo apt-get install lldb (or use yum).
See: gdb to lldb command map page for more info.

Resources