简体   繁体   English

gdb 如何在匿名命名空间中找到变量的地址

[英]How does gdb find the address of a variable in anonymous namespace

I have a variable declared in a c++ file like this:我在 C++ 文件中声明了一个变量,如下所示:

namespace {
    VelocityLongitudinal vel_lgt;
}

The corresponding dwarf info is对应的矮人信息是

<1><5e08b>: Abbrev Number: 317 (DW_TAG_namespace)
    <5e08d>   DW_AT_sibling     : <0x5e09e>
 <2><5e091>: Abbrev Number: 193 (DW_TAG_variable)
    <5e093>   DW_AT_name        : (indirect string, offset: 0xa5582): vel_lgt
    <5e097>   DW_AT_decl_file   : 2
    <5e098>   DW_AT_decl_line   : 19
    <5e099>   DW_AT_type        : <0x5bed3>
    <5e09d>   DW_AT_declaration : 1
 <2><5e09d>: Abbrev Number: 0

We see that there is no DW_AT_linkage_name and no DW_AT_location我们看到没有 DW_AT_linkage_name 和 DW_AT_location

In the symbol table (readelf -s) I find.在符号表(readelf -s)中我找到了。 The address, 21a0a0, matches the address gdb reports (print &'(anonymous namespace)::vel_lgt')地址 21a0a0 与 gdb 报告的地址相匹配(print &'(anonymous namespace)::vel_lgt')

42: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS vehiclemotionstate_server
43: 000000000021a0a0    24 OBJECT  LOCAL  DEFAULT    2 _ZN12_GLOBAL__N_17vel_lgt

How does gdb find that? gdb 是如何找到的? vehiclemotionstate_serve.cpp is the name of the file in which the variable is declared. Vehiclemotionstate_serve.cpp 是声明变量的文件的名称。 It seems unlikely that gdb would search for entries with matching last characters in the symbol table entries for a certain cpp file. gdb 似乎不太可能在某个 cpp 文件的符号表条目中搜索与最后一个字符匹配的条目。 And if so, how would it distinguish between two different declarations of the same name in files with the same name?如果是这样,它如何区分同名文件中两个不同的同名声明?

How does gdb find that? gdb 是如何找到的?

By reading the symbol table (equivalent to what readelf -s does), in addition to reading the debug info.通过读取符号表(相当于readelf -s所做的),以及读取调试信息。

It seems unlikely that gdb would search for entries with matching last characters gdb 似乎不太可能搜索与最后一个字符匹配的条目

It doesn't.它没有。 The name you found demangles to (anonymous namespace)::vel_lgt (except you appear to have made a cut/paste error: the actual name should be _ZN12_GLOBAL__N_17vel_lgtE ).这个名字你发现demangles(anonymous namespace)::vel_lgt (除非你似乎已经做出了剪切/粘贴错误:实际名称应该是_ZN12_GLOBAL__N_17vel_lgtE )。

how would it distinguish between two different declarations of the same name in files with the same name?它如何区分同名文件中两个不同的同名声明?

It would tell you that there is more than one instance if you ask, eg如果你问,它会告诉你有不止一个实例,例如

(gdb) info var vel_lgt
All variables matching regular expression "vel_lgt":

File t.cc:
2:      static int (anonymous namespace)::vel_lgt;

File t1.cc:
2:      static int (anonymous namespace)::vel_lgt;

Note that it's actually quite hard to tell GDB which of the two instances to print -- print vel_lgt just prints the first one, with no easy way to ask for the other one.请注意,它实际上是相当困难告诉GDB两个实例的打印- print vel_lgt只是打印第一个,有没有简单的方法来索要另一个。 I believe there is an open GDB bug about this, but I can't find it.我相信有一个关于此的开放 GDB 错误,但我找不到它。

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

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