Using Windbg/SOS, it possible to change value of a local varible on stack? If so how?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The short answer is: It depends.
Per default local value types are stored on the stack but due to optimization they will often be stored only in registers as needed. Reference types are stored on the heap, with a reference to the instance on the stack (or in a register).
I am going to assume that you’re looking to change a local value type. Let’s look at a simple example.
Assuming we set a breakpoint on
Methodand run until the breakpoint is hit, the stack looks like this:Notice that the local
xis listed as , but it doesn’t tell us which register. We could look at the registers and find the one with the value 2, but there could be more than one. Instead let’s look at the JIT compiled code for the method.Looking at the code, we see that the only
addinstruction uses theesiregister, so our value is stored here prior to the calculation. Unfortunately,esidoesn’t hold the correct value at this point, but looking backwards we findmov esi,ecx. I.e. the value is initially stored inecx.To change the value of
ecxuse thercommand. E.g. to set the value to 0x15 do the following:The output of the method is now:
Please keep in mind that the example above is only one of many possible scenarios. Locals are handled differently depending on debug/release build as well as 32/64 bit. Also, for complex methods it may be a bit harder tracking the exact location of the value.
To change the state of an instance, you have to locate the reference on the stack (e.g. using
!clrstackor!dso). Once located you can use the offsets to find the memory, that holds the data and use thee*commands to change the values as needed. Let me know if you want an example for that as well.