Set a variable to address of frame in gdb? - gdb

I need to set a local variable to the address of a particular frame obtained through backtrace. Is this possible? I haven't been able to find a way to do it.

Okay, here's a way to do it:
Select the frame you're interested in with f *n*,
then get the address from the instruction pointer: set $lal=$rip.
Enjoy.

Related

How to check current breakpoint function in TRACE32?

I'm trying to check if the program stopped in a function in a TRACE32.
I know I can see the the functions in FRAME window but no idea how to copy them to a variable inside my script.
Any idea how to do that ?
You get the name of the function, where the program counter points to with:
PRINT sYmbol.FUNCTION(PP())
(Instead of printing the result you can also assign it to a macro.)
So one approach to check if you've stopped in function myFunc() would be:
PRINT STRing.ComPare(sYmbol.FUNCTION(PP()),"*\myFunc")
Another way is to check if the program counter is inside the first and last address of your function myFunc():
PRINT (ADDRESS.OFFSET(sYmbol.BEGIN(`myFunc`))<=Register(PP))&&(Register(PP)<=ADDRESS.OFFSET(sYmbol.END(`myFunc`)))

Finding function parameters of function that is not on top of stack using windbg

Here is the part of call stack returned by kd command. I want to know what parameters were passed to myutil!myclass::somefunc. How do I get those parameters using windbg?
0a11f614 0a11f634
0a11f618 7686592c rpcrt4!Invoke+0x2a
0a11f61c 0e7b2c98
0a11f620 03edad48
0a11f624 03edaba8
0a11f628 00000206
0a11f62c 00000003
0a11f630 0a11f820
0a11f634 0a11fa38
0a11f638 768e05f1 rpcrt4!NdrStubCall2+0x2ea
0a11f63c 6dbc12b2*** WARNING: Unable to verify timestamp for myutil
myutil!myclass::somefunc
Thanks in advance,
-Neel.
about the stack being different
There is no reason that the stack shown by kd and kp would be different. They are essentially different representation on the same data.
If you paste the stack here may be we will be point out whats going on!
Are you sure you running both commands in the same place ?
how to check if symbols are loaded correctly
execute the command : "lml" to see for which binaries, symbols have been loaded.
Coming to your original question
kP is the simplest way to see the parameters that are being passed to the function.
another way to do the same thing is
a. run "kn" so that you see frame numbers in the stack trace.
b. move to the frame of interest by doing ".frame
c. now dump the variables using "dv"
If you are really passionte you can read about paramter passing. Then all you would need is the EBP + some assembly code reading to figure out which parameters we passed.

HOW to get value stored in a python variable if only its address is given?

If a variable has address '20754060', how can we get the value stored in that address?
a=20754060 #consist of address of some variable
how can we get value stored in '20754060' location
You probably cannot, and even if you can - you really shouldn't. Screwing around with memory content, ignoring or standing in way of GC is WRONG and probably will cause some weird errors.
As people already mentioned: this is so wrong ;)
However, this is how you do it (just a start).
from ctypes import *
# create c_int (only ctype addresses work)
a = c_int(423)
# get address of a
address = addressof(a)
#print address (just for fun)
print address
# print content of address (this works for size 1)
print c_char_p(address)
#check that it is the same (423 is 1a7 in hex. Note MSB vs LSB)
print hex(423)
This prints out:
35746280
c_char_p('\xa7\x01')
0x1a7
The above works only in the case of ctype objects like c_int (=ctypes.c_int).
c_char_p function gets the values one byte at the time. For more complicated objects, things get really ugly really fast and probably there is a limit at some point. But if you want, start reading here:
http://docs.python.org/3/library/ctypes.html
try this, in my case it is working.
address = id(variable)
this gives the id of the variable, and use this id to check the value of the variable
ctypes.cast(address, ctypes.py_object).value

How to convert a address to specific type variable in GDB?

I got an address such as 0x7fc9e401a02a in my log file, and I know that this address is a pointer of type Connection.
Then I start GDB, what I wanna know is: how to convert this address to a temporary variable of type Connection*, and display relevant information of Connection*?
How to do this, any hints?
Because my Connection class has a namespace, so what I should do is as bellow:
print * ('MyNameSpace::Connection' *) 0x7fc9e401a02a
Thanks JaredC and dbrank0 for your answers.

How do I set persistent and conditional watchpoints on locally scoped variables?

If I set a watchpoint for a variable local to the current scope, it will be auto deleted when going out of the scope. Is there any way to set it once and keep it auto alive whenever entering the same scope?
Is there anyway to set conditional watchpoint, like watch var1 if var1==0? In my case, the condition does't work. gdb stops whenever var1's value is changed, instead of untill var1 == 0 is true. My gdb is GNU gdb 6.8-debian.
I agree with Dave that a conditional breakpoint is the way to go.
However, to do what you asked, you can use GDB's commands command to set a list of GDB commands to execute whenever a breakpoint is hit. I find this incredibly useful.
I suggest writing your GDB commands into a file so that they are easy to edit and easy to reload with the source command. Or you can specify command files to load on the GDB command line or use .gdbinit to make them load automatically.
An example of a good use of commands:
Suppose that I have a function format that is called by a lot of other functions. I want to break on it, but only after function do_step_3 has been called.
break do_step_3
commands
break format
continue
end
You could use this for your problem with something like:
break func
commands
watch var
continue
end
You can set conditions on watchpoints in the same way that you do with breakpoints. This is in the documentation but admittedly it hardly calls attention to itself.
So watch my_var if my_var > 3 works just fine, as does the condition command.
To recreate the watchpoint if the variable it is watching goes out of scope, have gdb do this automatically using a breakpoint at the start of the function as Zan has described.
You can set a watchpoint that does not go out of scope by setting it to the memory address.
(gdb) p &var1
$1 = (int *) 0x41523c0
(gdb) watch *(int *)0x41523c0
Hardware watchpoint 1: *(int *)0x41523c0
This also works for other data types and pointers.
I'm not sure which language us are using, so the exact answer will vary, but could you change the variable to either be static, global, or dynamically allocated (and don't free it when the function returns?). This way it's raw address won't change, and gdb will be able breakpoint on it.
Instead of watching the value whe it equals a specific value; you should set a conditional break point on the line where you want to check the value of var1. This should effectively have the same effect
e.g.
(gdb) break main.c:123 if (var1 == 0)