Few GDB Commands – Debug Core, Disassemble, Load Shared Library

by Lakshmanan Ganapathy on March 11, 2014

GDB is an essential tool for programmers to debug their code.

This article explains how you can use gdb to debug a program with the core file, how to display assembly language instructions of your program, and how to load shared library programs for debugging.

Debug a Program with Core File

A core file or core dump is a file that records the memory image of a running process and its status. It is used in post-mortem debugging of a program that crashed while it ran outside a debugger.

$ gdb executable_name core_file_name


The above command will load the core file for the executable and prompts a gdb shell.

You can use gdb backtrace or other commands to examine what really happened. Note that the core_file will be ignored, if the executable is running under gdb.

Print Assembly Instructions

You can use the disassemble command to print the assembly instruction of a function. You can also specify 2 address ranges, and the instructions between them will be disassembled and printed in gdb console.

(gdb) disassemble main
Dump of assembler code for function main:
   0x00000000004004ac :	push   %rbp
   0x00000000004004ad :	mov    %rsp,%rbp
   0x00000000004004b0 :	mov    $0x0,%eax
   0x00000000004004b5 :	pop    %rbp
   0x00000000004004b6 :	retq   
End of assembler dump.

Load Shared library Symbols

Many a times, programmers will use shared libraries in their code. Sometimes, we might want to look into the shared library itself to understand what’s going on. Here I’ll show an example using GLib Library and how to obtains the debugging information for it.

By default, all distributions will strip the libraries to some extent. The complete debugging information will be stored in a separate package which they name like “package-1.0-dbg”, and only if needed user can install.

When you install the “package-1.0-dbg”, by default gdb will load all the debugging information, but to understand the concept here we will see how to manually load the symbol file.

#include <stdio.h>
#include <glib.h>
struct a {
        int a;
        int b;
void *print( struct a *obj,int as) {
int main() {
        struct a *obj;
        obj = (struct a*)malloc(sizeof(struct a));
        GList *list=NULL;
        list = g_list_append(list,obj);
$ cc  -g -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/  -lglib-2.0 glib_test.c

Note: You need to install the libglib2.0-0 to try out this example.

Now we will start the debugging.

(gdb) b 1
Breakpoint 1 at 0x4007db: file a.c, line 1.
(gdb) run
(gdb) info sharedlibrary 
From                To                  Syms Read   Shared Object Library
0x00007ffff7dddaf0  0x00007ffff7df5c83  Yes (*)     /lib64/ld-linux-x86-64.so.2
0x00007ffff7b016c0  0x00007ffff7b6e5cc  Yes (*)     /lib/x86_64-linux-gnu/libglib-2.0.so.0
0x00007ffff7779b80  0x00007ffff7890bcc  Yes (*)     /lib/x86_64-linux-gnu/libc.so.6
0x00007ffff751f9a0  0x00007ffff7546158  Yes (*)     /lib/x86_64-linux-gnu/libpcre.so.3
0x00007ffff7307690  0x00007ffff7312c78  Yes (*)     /lib/x86_64-linux-gnu/libpthread.so.0
0x00007ffff70fc190  0x00007ffff70ff4f8  Yes (*)     /lib/x86_64-linux-gnu/librt.so.1
(*): Shared library is missing debugging information.

From the above information, note that the library libglib-2.0.so.0 is having symbols, but the debuuging information like file_name, line_no etc… are missing.

Download the debug information for the package from respective distribution (libglib2.0-0-dbg in Debian – Wheezy).

(gdb) add-symbol-file /home/lakshmanan/libglib-2.0.so.0.3200.4 0x00007ffff7b016c0
add symbol table from file "/home/lakshmanan/libglib-2.0.so.0.3200.4" at
	.text_addr = 0x7ffff7b016c0
(y or n) y
Reading symbols from /home/lakshmanan/libglib-2.0.so.0.3200.4...done.

The address given in the add-symbol-file command is, the “From” address printed by “info sharedlibrary” command. Now the debugging information is loaded.

(gdb) n
g_list_foreach (list=0x0, func=0x4007cc , user_data=0x0) at /tmp/buildd/glib2.0-2.33.12+really2.32.4/./glib/glist.c:897

Sometimes the shared libraries won’t even have any symbols in it, and in those situations, the above method will be helpful.

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 4 comments… read them below or add one }

1 duskoKoscica March 12, 2014 at 12:42 am

And if I get it would be possible to get the code that assembler was goten from. It is like inverse function from math. Like you get what have you started from, the acctual code. If you know the language that has been developed, the optimizations and product that has been used. Short to say you could get like the code from the assambler.

2 duskoKoscica March 12, 2014 at 1:27 am

Meaning everything is open source! One more thing, when you have fragrance that you by in the store it is sometimes similar to other one, but that thing is legall to produce, but when one buyes the mp3 which is not as original file at all it is not legal to distribute that file. And yes there are more legal things that make no sense.

3 Jalal Hajigholamali March 12, 2014 at 9:26 pm


Thanks a lot.

4 Pratik March 26, 2014 at 3:10 am

Hi Ramesh,
I just wanted to know if there is an way to list variables of a program in gdb?


Leave a Comment

Previous post:

Next post: