如何打印正在用LLDB调试的C/ c++函数的内存地址和参数值

How to print the memory address and the value of the arguments of a C/C++ function that is being debugged with LLDB?

本文关键字:c++ 函数 地址 参数 内存 调试 打印 何打印 LLDB      更新时间:2023-10-16

我正在准备我的操作系统测试。我们使用的工具之一是调试器(LLDB),我的目标是检查C函数或c++方法的参数。

例如:我怎样才能看到内存地址和参数的值传递给_SMenuItemCommandID??-我尝试过不同的事情,但在尝试中死亡。

HITestBox`_SMenuItemCommandID(MenuData*, unsigned short, unsigned long):
0x9a7bfc35:  pushl  %ebp
0x9a7bfc36:  movl   %esp, %ebp
0x9a7bfc38:  pushl  %esi
0x9a7bfc39:  subl   $52, %esp
0x9a7bfc3c:  movl   8(%ebp), %esi
0x9a7bfc3f:  movl   88(%esi), %eax
0x9a7bfc42:  movl   %eax, -16(%ebp)
0x9a7bfc45:  movzwl 12(%ebp), %ecx
0x9a7bfc49:  movw   %cx, -12(%ebp)
0x9a7bfc4d:  movl   $0, -8(%ebp)
0x9a7bfc54:  leal   -8(%ebp), %edx
0x9a7bfc57:  movl   %edx, 28(%esp)
0x9a7bfc5b:  movl   %ecx, 4(%esp)
0x9a7bfc5f:  movl   %eax, (%esp)
0x9a7bfc62:  movl   $0, 24(%esp)
0x9a7bfc6a:  movl   $4, 20(%esp)
0x9a7bfc72:  movl   $0, 16(%esp)
0x9a7bfc7a:  movl   $1835232612, 12(%esp)
0x9a7bfc82:  movl   $12, 8(%esp)
0x9a7bfc8a:  calll  0x9a5f7c9b                ; elementGetDataAtIndex
0x9a7bfc8f:  movl   16(%ebp), %eax
0x9a7bfc92:  cmpl   %eax, -8(%ebp)
0x9a7bfc95:  je     0x9a7bfcae                ; _SMenuItemCommandID(MenuData*, unsigned short, unsigned long) + 121
0x9a7bfc97:  movl   %eax, 4(%esp)
0x9a7bfc9b:  leal   -16(%ebp), %eax
0x9a7bfc9e:  movl   %eax, (%esp)
0x9a7bfca1:  calll  0x9a7e2914                ; mID::SetCommandID(unsigned long)
0x9a7bfca6:  movl   %esi, (%esp)
0x9a7bfca9:  calll  0x9a5f7c65                ; invalidate(MenuData*)
0x9a7bfcae:  xorl   %eax, %eax
0x9a7bfcb0:  addl   $52, %esp
0x9a7bfcb3:  popl   %esi
0x9a7bfcb4:  popl   %ebp
0x9a7bfcb5:  ret    

编辑:假设我正在调试一个没有源代码的应用程序,但我导出了符号。例如,在某个时刻执行以下代码:

MenuData *myData = (MenuData *)0x28ff44;;
SMenuItemCommandID(myData, 3, 4);

我需要做什么(与LLDB)来获得:

arg0 = 0x28ff44   
arg1 =3  
arg2 =4  

您发布的反汇编是x86。参数在堆栈上。如果在函数prolog之前中断,则参数相对于堆栈指针%esp(在lldb中作为$esp访问):

# The return address:
x/w $esp
# The first argument:
x/w $esp+4
# The second argument:
x/w $esp+8

如果您在序言之后中断(在您的示例中是0x9a7bfc3c),这是通常放置符号断点的地方,则相对于帧指针(%ebp又称$ebp)找到参数:

# The saved frame pointer of the previous frame:
x/w $ebp
# The return address:
x/w $ebp+4
# The first argument:
x/w $ebp+8
# The second argument:
x/w $ebp+12

对于其他体系结构,实参将以不同的方式存储,通常存储在寄存器中。同样,上面的代码假设使用"cdecl"调用约定。还有其他的。您是否被告知您应该熟悉哪些架构和调用约定?