简体   繁体   English

如何使用GDB调试多线程程序?

[英]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. 我正在编写一个2线程程序,其中一个写线程和一个读线程可以同时访问磁盘上的文件。 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. 写入线程可以(1)从磁盘读取并创建一个新文件,然后(2)删除旧文件,然后将新文件(tmp)重命名为旧文件名。 The new file is always larger than the old file. 新文件始终大于旧文件。 The read thread reads from the file during case (1). 在情况(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. 但是,当新文件小于旧文件时,读取时的fscanf不会产生No such file or directory seg错误错误。 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. 该函数很大,因此对每个语句进行printf都是不实际的。 How can I find this out using GDB? 如何使用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

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. 给定您的堆栈跟踪,当前语句就是lib.c的第887行中的内容。

The following sequence of commands will let you look at the values of local variables: 以下命令序列将使您查看局部变量的值:

(gdb) frame 1
(gdb) info locals

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM