简体   繁体   中英

How to catch a random value returned by a function in gdb

In C, if you have a function that returns "always" the same result you can do:

(gdb) p foo()

But if this function always returns a random result, how to print THAT value without use a variable. Other detail, if you don't have debug information of the function, it's from a stripped lib.

(gdb) p myRandom() can't be used, it will result in something different than the value used by application.

You may ask: "Why would you use a random function and don't use its results", lets say that the return is just an extra thing of what that func does.

(gdb) p myRandom()
can't be used, it will result in something different than the value used by application.

Presumably you want to examine the value that was returned to the application in the particular place where the application called myRandom() .

You need to set a breakpoint on the instruction that immediately follows the CALL , then examine the register in which the value is returned. For example, on i*86 , the value is returned in eax register, so you'll do print $eax .

Alternatively, set a breakpoint on myRandom() , then do finish command (you don't need debug info to do that), then examine the return register.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM