How to save the result of printf to a variable in GDB?

(gdb)printf "Hello %d", 7
Hello 7
(gdb)set $MyVar = printf "Hello %d", 7 // ???

How to save the result of printf "Hello %d", 7 to $MyVar?

Asked By: xmllmx

||

eval does a printf of its arguments and then runs it as a command. So you can take your printf argument list, insert set $MyVar = at the beginning, and eval it.

(gdb) eval "set $MyVar = "Hello %d"", 7
(gdb) print $MyVar
$2 = "Hello 7"
Answered By: Mark Plotnick
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.