0

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
4
  • The "no such file or directory" message isn't related to your program's error. It just means your system doesn't have the source code for fscanf. Source code for system libraries is optional. What operating system do you have? We can tell you how to download the libc source code. (In this case, we don't need the source code to see the bug: your program called fscanf with a NULL pointer. The leading cause of that is calling fopen on a nonexistent file). Commented May 7, 2016 at 19:11
  • Hi Mark, fscanf was running fine many times before the seg fault. The curious thing is that before and after the file seg faulted, I checked whether the file is there (just looking at if the file exists in the folder that is being accessed), it is there. So I’m really puzzled. Commented May 7, 2016 at 19:29
  • That's what makes multithreaded programs more challenging: bugs can show up seemingly at random, depending on the relative timing of the threads. Anyway, type up to go to the binary_search_in_ disk function's stack frame and look at the values of its variables. Find where that FILE* pointer is being assigned NULL. Commented May 7, 2016 at 19:41
  • Thx! Found the issue, Too many open files Commented May 7, 2016 at 20:34

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.