LLDB – 打印函数内的静态变量内容

huangapple go评论57阅读模式
英文:

LLDB - printing contents of a static variable inside function

问题

I've got the following code that defines an Instance method in a static class.

namespace AAA {
class BBB {
   static BBB& Instance() {
       static BBB bbb();
       return bbb;
   }
}

I'd like to print the static object bbb from any breakpoint in the code (since it's static, it should be defined anywhere in the code).

If I look up the symbol using the following command, it gives me the symbol's offset in the binary file. However, it might not include the ASLR (Address Space Layout Randomization) offset.

The ASLR offset can be obtained by subtracting the address of a function symbol from its offset in the binary structure (assuming that the function and the static share the same ASLR).

The outcome ASLR value should be added to the symbol's offset to get its real place in the process memory.

I wonder if there's an easier way to find the memory location of this static variable?
If not, perhaps there's a way to automate this instruction set?

Thanks.

英文:

I've got the following code that define an Instance method in static class.

namepsace AAA { 
class BBB { 
   static BBB& Instance() { 
       static BBB bbb();
       return bbb;
   }
}

I'd like to print the static object bbb from any breakpoint in the code (since it's static, it should be defined anywhere in the code)

If I lookup the symbol using the following command, it gives me the symbol's offset in the binary file. However, it might not include the ASLR (Address Space Layout Randomization) offset.

The ASLR offset can be obtained by by subtracting the address of a function symbol from its offset in the binary structure (assuming that the function and the static share the same ASLR).

the outcome ASLR value should be added to the symbol's offset to get its real place in the process memory.

I wonder if there's an easier way to find the memory location of this static variable ?
if not perhaps there's way to automate this instruction set?

Thanks

答案1

得分: 1

以下是翻译好的部分:

调试器中的函数静态变量存在两个问题。

第一个问题是它们很难找到,因为您必须深入调试信息,所以“请找到一个函数静态变量”请求需要具有某种范围,否则在大型应用程序中表现不佳。

因此,我们需要某种范围限定。第二个问题是尽管符号名称可以传达范围,但函数静态变量的解码名称不是C++语言可以解析为有效代码以获取值的标识符。因此,您不能在表达式解析器中使用它们。

lldb应该在“target variable”命令中添加一些语法以指定函数范围,这将非常简单,并允许您指定范围。这不时会作为增强请求提出,但尚未实现...

然而,这些静态变量仍然具有混淆的名称,当它们出现在表达式中时,lldb的表达式解析器将通过混淆的名称查找符号。这里的一个技巧是裸符号没有类型,只有地址。所以您必须提供类型信息。但这很容易做到...

在您的示例中(我将名称从“bbb”更改为“local_bbb”以便更容易找到),首先我必须找到混淆的名称:

(lldb) image dump symtab <binary_name> -m
...
[   26]    115 D X Data            0x0000000100008000 0x0000000100008000 0x0000000000000020 0x000f0080 _ZZN3AAA3BBB8InstanceEvE9local_bbb

[   27]    116 D X Data            0x0000000100008020 0x0000000100008020 0x0000000000000008 0x000f0080 _ZGVZN3AAA3BBB8InstanceEvE9local_bbb

有两个有点奇怪,为什么会这样?

(lldb) lang cplus demangle _ZGVZN3AAA3BBB8InstanceEvE9local_bbb
_ZGVZN3AAA3BBB8InstanceEvE9local_bbb ---> AAA::BBB::Instance()::local_bbb的保护变量

啊,那是用于设置静态变量的保护变量。所以我们要另一个。然后,我可以使用强制转换来提供类型信息来访问该符号:

(lldb) expr (AAA::BBB &) _ZZN3AAA3BBB8InstanceEvE9local_bbb
(AAA::BBB) $2 = (an_int = 200, a_string = "some value")

我还添加了一些字段来显示我们获取了正确的值...因为lldb在从符号名称到地址的处理中会正确获取ASLR偏移量。

希望这对您有所帮助。如果您有其他翻译需求,请随时告诉我。

英文:

There are two problems with function statics in the debugger.

The first is that they are hard to find since you have to dig into debug information pretty deeply, so a "please find me a function static" request needs to have some kind of scope or it would behave badly in big apps.

So we need some kind of scoping here. The second problem is that though the symbol name could convey that scoping, the demangled names of function statics aren't identifiers that the C++ language can parse to valid code that fetches the value. So you can't use them in the expression parser.

lldb should add some more syntax to the target variable command to specify function scopes, that would be fairly straightforward and allow you to specify the scope. This comes up every so often as an enhancement request, but hasn't achieved activation energy yet...

However, these statics still have mangled names - and lldb's expression parser will do lookup of symbols by mangled name when they appear in an expression. The one trick here is that bare symbols don't have types, just addresses. So you have to provide the type. But that's easily done...

In your example (I changed the name from bbb to local_bbb to make it easier to find) first I have to find the mangled name:

(lldb) image dump symtab &lt;binary_name&gt; -m
...
[   26]    115 D X Data            0x0000000100008000 0x0000000100008000 0x0000000000000020 0x000f0080 _ZZN3AAA3BBB8InstanceEvE9local_bbb

[   27]    116 D X Data            0x0000000100008020 0x0000000100008020 0x0000000000000008 0x000f0080 _ZGVZN3AAA3BBB8InstanceEvE9local_bbb

It's a little odd that there are two, why is that?

(lldb) lang cplus demangle _ZGVZN3AAA3BBB8InstanceEvE9local_bbb
_ZGVZN3AAA3BBB8InstanceEvE9local_bbb ---&gt; guard variable for AAA::BBB::Instance()::local_bbb

Ah, that's the guard variable for setting the static. So we want the other one. Then I can access that symbol using a cast to provide the type info:

(lldb) expr (AAA::BBB &amp;) _ZZN3AAA3BBB8InstanceEvE9local_bbb
(AAA::BBB) $2 = (an_int = 200, a_string = &quot;some value&quot;)

I also added some fields to show we were getting the value right... Because lldb's handling going from the symbol name to the address it will get the ASLR slides right for you.

huangapple
  • 本文由 发表于 2023年6月8日 20:22:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定