gpt4 book ai didi

c - 使用 __thread 时 gdb 优化了值

转载 作者:行者123 更新时间:2023-11-30 19:34:16 25 4
gpt4 key购买 nike

我所有的static __thread值显示为 <optimized out>当在调试时我想观察变量的值时;即使使用 -o0 和/或 volatile .

Static没有 __thread 的变量正确显示。

即使我正在使用线程,是否仍然可以显示变量的值?

使用 Win10 ( CreateThread )、eclipse CDT、c11、mingw64-w64 和 gdb 7.11.1

最佳答案

解决方法可能是:在代码中添加一些线程局部变量的打印机,并让 gdb 调用它们。 (或者,如果您熟悉x86汇编,请编写一些hackish插件来修改可执行内存以读取fs:offset/gs:offset(线程局部变量值)并恢复内存和寄存器)

更具体地说,在 C 代码中添加一个函数,该函数除了返回有趣的 __thread 变量之外什么也不做,当您使用 gdb 中断程序时,您始终可以让 gdb 调用该函数您(假设该函数未优化)不会破坏原始程序的堆栈帧。它应该很简单:

(gdb) p rand()
$1 = 1804289383
(gdb) p rand()
$2 = 846930886
(gdb) p rand()
$3 = 1681692777

虽然rand不是一个很好的例子,因为它有副作用。 TLS 变量读取没有副作用。

示例:(在 Ubuntu 16.04 下,但由于功能非常基本,因此应该不会有太大差异)

tls.cpp:

#include <stdio.h>

__thread long thread_cand;

long what_is_thread_cand()
{
return thread_cand;
}

int main()
{
while ( !feof ( stdin ) )
{
scanf ( "%ld", &thread_cand );
printf ( "%p : %ld\n", &thread_cand, thread_cand );
}
return 0;
}

终端:

$ g++ -O2 -g3 tls.cpp -o tls
tls.cpp: In function ‘int main()’:
tls.cpp:14:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf ( "%ld", &thread_cand );
^
$ gdb tls --nh
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from tls...done.
(gdb) r
Starting program: /home/ubuntu/tls
123
0x7ffff7fcb6f8 : 123
432
0x7ffff7fcb6f8 : 432
^C
Program received signal SIGINT, Interrupt.
0x00007ffff7b04230 in __read_nocancel ()
at ../sysdeps/unix/syscall-template.S:84
84 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) p thread_cand
Cannot find thread-local storage for process 6472, executable file /home/ubuntu/tls:
Cannot find thread-local variables on this target
(gdb) p what_is_thread_cand()
$1 = 432
(gdb)

关于c - 使用 __thread 时 gdb 优化了值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44115390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com