2

E.g.:

define mycmd
    break $arg0
    commands
        print $arg0
    end
end
mycmd myfunc
continue

Prints:

$1 = void

instead of the expected myfunc, because $arg0 is evaulated when the command is hit, and since it has not been defined shows void.

Is there a way to pass it to the commands?

I think I would be able to do it with Python easily, but it would be cool to have a quicker way. But if you have the Python ready, just paste it anyways.

Motivation: automate https://stackoverflow.com/a/5372742/895245 , where I'd like to write:

define break-stack
    break $arg0
    commands
        tbreak $arg1
        continue
    end
end
break-stack ParentFunc ChildFunc

Yes, I know that https://stackoverflow.com/a/20209911/895245 provides a Python solution.

GDB 7.11, Ubuntu 16.10.

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985

1 Answers1

1

I couldn't coax gdb's eval to evalute multi-line commands such as commands, so here's a solution that uses shell.

define break-stack
  dont-repeat
  break $arg0
  shell { echo commands; echo tbreak $arg1; echo continue; echo end; } > /tmp/gdbtmp
  source /tmp/gdbtmp
  shell rm /tmp/gdbtmp
end

If your echo accepts the -e option, it can be shortened to

define break-stack
  dont-repeat
  break $arg0
  shell echo -e "commands\ntbreak $arg1\ncontinue\nend" > /tmp/gdbtmp
  source /tmp/gdbtmp
  shell rm /tmp/gdbtmp
end

Your specific task - stopping when function b is called by function a - can be done by using the $_caller_is convenience function, which is included in recent versions of gdb.

define break-stack
  break "$arg1" if $_caller_is("$arg0")
end
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40