Returns value through gdb user command

I am debugging the main file, so I do not have an active process to run anything.

I use custom gdb commands to check a lot of data from the main file and try to simplify the process with custom commands.

However, I cannot find a way to force user commands to return values ​​that can be used in other commands.

For instance:
(note the comment in the "return" line)

define dump_linked_list set $node = global_list->head set $count = 1 while $node != 0 printf "%p -->", $node set $node = $node->next set $count = $count + 1 end return $count ## GDB doesn't understand this return end 

Ideally, my dump_linked_list command will return the number of nodes found in the list so that it can be used in another given command:

 define higher_function set $total_nodes = dump_linked_list printf "Total Nodes is %d\n", $total_nodes end 

Is this possible in gdb commands?

I feel it should be, but I was looking for documentation and cannot find any mention of this or any examples.

+4
source share
2 answers

As far as I know, GDB does not have such functionality. You can set a variable for some name that you know and use it as a "return" value. For example, always set the retval variable as follows:

 set $retval = <whatever value> 

Then all your new functions can use it as the return value from the previously named functions. I know this is only a workaround, but it is relatively simple and works.

+1
source

I found out that gdb seems to go by name, which can be used to return the return value. A bit more flexible that uses only one global variable.

 (gdb) define foo Type commands for definition of "foo". End with a line saying just "end". >set $arg0 = 1 >end (gdb) set $retval = 0 (gdb) p $retval $3 = 0 (gdb) foo $retval (gdb) p $retval $4 = 1 
+3
source

Source: https://habr.com/ru/post/1436005/


All Articles