0

I am trying to inspect the contents of a data structure in a core dump. There are many nodes in this. And I need to compare two nodes. I am trying to implement the same compare function that the program used to handle the data structure as a user defined gdb function. A node has multiple parameters and the compare function checks each of them and returns -1, 0 or 1.

I did the following,

define single_compare
    if $arg0 > $arg1
        set $arg2 = 1
    end
    if $arg0 < $arg1
        set $arg2 = -1
    end
    if $arg0 == $arg1
        set $arg2 = 0
    end
end

define compare_nodes
    set $obj_inst_a = (nodetype*)$arg0
    set $obj_inst_b = (nodetype*)$arg1
    set $compr = 0

    single_compare $obj_inst_a->a $obj_inst_b->a $compr
    if $compr != 0
        set $arg2 = $compr
    end

    single_compare $obj_inst_a->b $obj_inst_b->b $compr
    if $compr != 0
        set $arg2 = $compr
    end
...

The problem here is, if i find the a values in each node is different, I don't need to compare b, just return from there, and use $arg2 to take a decision. Is there a way to do that in gdb script? finish/return will cause the current program functions to return/finish. Not the script.

1 Answer 1

1

There is not a direct way to do this. gdb's CLI language is, as you've discovered, somewhat limited.

One way to sort of get what you want is to wrap your function's body in a loop that will only execute once:

set var $w = 1
while $w
  if something
    # Here's how to exit early.
    loop_break
  end
  # Make sure this only loops once.
  set var $w = 0
end

Another approach is to write more complicated scripts using Python.

Sign up to request clarification or add additional context in comments.

1 Comment

I did something like if else if else end end end And thank you very much, I need the loop part for something else :)

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.