1

Say the debugged process has a string variable as follows:

char* cmd_str = "set confirm on";

How to execute the command from cmd_str in GDB?

(gdb) $cmd = cmd_str
(gdb) ???

1 Answer 1

4

You can use gdb's eval command, which runs printf on its arguments and then evaluates the result as a command.

(gdb) list
1   #include <stdlib.h>
2   main()
3   {
4       char *a = "set confirm off";
5   
6       pause();
7   }
(gdb) break 6
Breakpoint 1 at 0x400540: file cmdtest.c, line 6.
(gdb) run
Starting program: ./cmdtest 
Breakpoint 1, main () at cmd.c:6
6       pause();

(gdb) show confirm
Whether to confirm potentially dangerous operations is on.
(gdb) printf "%s", a
set confirm off(gdb) 
(gdb) eval "%s", a
(gdb) show confirm
Whether to confirm potentially dangerous operations is off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.