Search code examples
cmultithreadinggdbscanfrace-condition

How to use GDB to debug multithreaded program?


I am writing a 2 threaded program in which one write thread and one read thread have simultaneous access to a file on disk. The write thread can (1) read from the disk and create a new file, and this, it (2) delete the old file, and rename the new file (tmp) the old file name. The new file is always larger than the old file. The read thread reads from the file during case (1).

However, fscanf in read is producing No such file or directory seg fault error when the new file is smaller than the old file. I identified the function that write thread is calling, but I wish to know which statement that write thread is executing at the moment, and the local variable values. The function is big so it’s not practical to printf every statement. How can I find this out using GDB?

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb73ffb40 (LWP 12649)]
__isoc99_fscanf (stream=0x0, format=0x804b5f1 "%d%d%d\n") at isoc99_fscanf.c:30
30  isoc99_fscanf.c: No such file or directory.
(gdb) bt
#0  __isoc99_fscanf (stream=0x0, format=0x804b5f1 "%d%d%d\n") at isoc99_fscanf.c:30
#1  0x0804ae18 in binary_search_in_disk (k_level=1, key=2) at lib.c:887
#2  0x0804abbc in search (k_level=1, key=2) at lib.c:802
#3  0x080490da in get (key=2) at lsm.c:56
#4  0x08048dc9 in run_get (args=0x804e0c8) at concurrent_main.c:181
#5  0xb7f71f70 in start_thread (arg=0xb73ffb40) at pthread_create.c:312
#6  0xb7ea7bee in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:129

Solution

  • I wish to know which statement that write thread is executing at the moment, and the local variable values

    Given your stack trace, the current statement is whatever is on line 887 of lib.c.

    The following sequence of commands will let you look at the values of local variables:

    (gdb) frame 1
    (gdb) info locals