Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a value via a gdb user-defined command

I'm debugging with a core-file, so I have no active process in which to run anything.

I'm using gdb user-defined commands to inspect a bunch of data from the core file, and attempting to simplify the process using user-defined commands.

However, I cannot find a way to make the user-defined commands return values which could be used in other commands.

For example:
(note the comment on 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 would return the number of nodes found in the list, so that it could be used in another defined command:

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

Is such a thing possible in gdb commands?

I feel it must be, but I've been searching documentation and cannot find a mention of it, or any examples.

like image 522
abelenky Avatar asked Sep 24 '12 20:09

abelenky


People also ask

Which command return from function in GDB?

Returning from a function You can cancel execution of a function call with the return command. If you give an expression argument, its value is used as the function's return value. When you use return , GDB discards the selected stack frame (and all frames within it).

What is RET in GDB?

A blank line as input to GDB (typing just RET ) means to repeat the previous command. Certain commands (for example, run ) will not repeat this way; these are commands whose unintentional repetition might cause trouble and which you are unlikely to want to repeat.

How do I save a GDB output?

Logging GDB's output to a file This is done by first issuing the command set logging file my-gdb-log , followed by the command set logging on . Later on, you can issue the set logging off command to stop sending GDB output to the log file.

What are GDB commands?

A GDB command is a single line of input. There is no limit on how long it can be. It starts with a command name, which is followed by arguments whose meaning depends on the command name. For example, the command step accepts an argument which is the number of times to step, as in `step 5' .


1 Answers

I found out gdb seems to pass by name which can be used to pass back a return value. A little more flexible that just using a single 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
like image 187
CanadiaDan Avatar answered Sep 19 '22 20:09

CanadiaDan