如何在 windbg 命令行界面中编辑局部变量值?

How to edit local variable values in windbg command line interface?

我正在将 python 与 windbg 集成,我想从脚本中编辑局部变量值。为此,我需要知道如何在 windbg 命令行中编辑局部变量值。

我知道我可以通过变量获取值,但是如何编辑同一个变量?

作为described on MSDN for ??:

?? Expression

并查找 linked C++ numbers and operators:

LValue = Value       Assign

你可以简单地

?? <variable name> = <value>

作为替代方案,您也可以使用 e* 命令来 edit values in memory,例如

eb <address> <byte1> <byte2>

我发表了评论,但评论变长了,所以将其作为答案发布

函数入口时不会初始化局部变量。
你什么时候评估当地的?? . 对本地的赋值只有在其初始初始化发生在函数范围内后才会有效 在使用之前

您可以按照 Sean Cline 的建议使用 c++ 表达式求值器分配给本地,但是您拥有准确的位置非常重要

假设你有这样的代码

:\>cat localedit.cpp
#include <stdio.h>
int addme(int a , int b) {
    int c = 25; int d = c*3;int e = a*c;int f = d*b;
    return c+d+e+f;
}
void main (void) {
    printf("%d\n" , addme( 50,50));
}
:\>

如果您在 windbg 下执行此操作并到达 addme() 并在函数入口上为局部 c 分配一些值,它将根本无效

见下文

0:000> r

localedit!addme:
003d1000 55              push    ebp
0:000> dv
              a = 0n50
              b = 0n50
              d = 0n0
              c = 0n4121264
              f = 0n1308704
              e = 0n4002235
0:000> ?? c
int 0n4121264

0:000> ?? c = 100
int 0n100

0:000> ?? c
int 0n100
0:000> g
5100   <<<<<< see the result is same as executing without modification 

0:000> q
quit:

:\>localedit.exe
5100 <<<<<<<<<<<<<<<<

功能addme的完全反汇编

0:000> uf .
localedit!addme:
01341000 55              push    ebp
01341001 8bec            mov     ebp,esp
01341003 83ec10          sub     esp,10h
01341006 c745fc19000000  mov     dword ptr [ebp-4],19h
0134100d 6b45fc03        imul    eax,dword ptr [ebp-4],3
01341011 8945f8          mov     dword ptr [ebp-8],eax
01341014 8b4d08          mov     ecx,dword ptr [ebp+8]
01341017 0faf4dfc        imul    ecx,dword ptr [ebp-4]
0134101b 894df4          mov     dword ptr [ebp-0Ch],ecx
0134101e 8b55f8          mov     edx,dword ptr [ebp-8]
01341021 0faf550c        imul    edx,dword ptr [ebp+0Ch]
01341025 8955f0          mov     dword ptr [ebp-10h],edx
01341028 8b45fc          mov     eax,dword ptr [ebp-4]
0134102b 0345f8          add     eax,dword ptr [ebp-8]
0134102e 0345f4          add     eax,dword ptr [ebp-0Ch]
01341031 0345f0          add     eax,dword ptr [ebp-10h]
01341034 8be5            mov     esp,ebp
01341036 5d              pop     ebp
01341037 c3              ret

我们在函数入口

0:000> r @eip
eip=01341000

we can check what is the address of local c 
and may be write there but unless we step until 
01341006 c745fc19000000  mov     dword ptr [ebp-4],19h

what ever we write will be overwritten again 


0:000> ?? &c
int * 0x0026f7a8

0:000> dd 26f7a8 l1
0026f7a8  0135e2b0

0:000> .enable_long_status 1
0:000> ?? c
int 0x135e2b0


0:000> ?? *(int *)&c = 45
int 0x2d
0:000> ?? c
int 0x2d
0:000> dv
              a = 0x32
              b = 0x32
              d = 0
              c = 0x2d
              f = 0x26f7c0
              e = 0x13411bb
0:000> g
5100