Reputation: 51
I have this file with lldb commands in it that I want to run. Is there a way to run pre-written lldb commands?
Upvotes: 4
Views: 2529
Reputation: 420
Yes, there are multiple ways.
Given a file: commands.lldb
, on the command line with -s
(or --source
):
$ lldb -s commands.lldb -- ./sum 1 2 3
or while interacting with lldb
(lldb) command source commands.lldb
Suppose the file commands.lldb
contains the following commands
b sum.c:8
breakpoint command add -o 'p total' 1
breakpoint modify --auto-continue true 1
run
On the command line we can pass the file with -s
, (or --source
) option, e.g.
$ lldb --batch -s commands.lldb -- ./sum 1 2 3
We'll see output similar to:
(lldb) target create "./sum"
Current executable set to './sum' (arm64).
(lldb) settings set -- target.run-args "1" "2" "3"
(lldb) command source -s 0 'commands.lldb'
Executing commands in './commands.lldb'.
(lldb) b sum.c:8
Breakpoint 1: where = sum`main + 64 at sum.c:8:23, address = 0x0000000100003f30
(lldb) breakpoint command add -o 'p total' 1
(lldb) breakpoint modify --auto-continue true 1
(lldb) run
(lldb) p total
(int) 0
(lldb) p total
(int) 1
(lldb) p total
(int) 3
6
Process 46592 exited with status = 0 (0x00000000)
Process 46592 launched: './sum' (arm64)
Notice that lldb
shows us the equivalent interactive command:
command source -s 0 'commands.lldb'
Supposing we had a program with a bug in it, we could use the -K
(--source-on-crash
) to run our commands after we've hit an assertion,
segfault or whatever it may be.
$ lldb -K commands.lldb -- ./badprog 1 2 3
While interacting with lldb, we can use command source
. e.g.
(lldb) command source commands.lldb
This will give us the same output.
Upvotes: 1
Reputation: 27110
lldb's help system is a good way to answer this sort of question:
(lldb) help command
Commands for managing custom LLDB commands.
Syntax: command <subcommand> [<subcommand-options>]
The following subcommands are supported:
alias -- Define a custom command in terms of an existing command. Expects 'raw' input (see 'help raw-input'.)
delete -- Delete one or more custom commands defined by 'command regex'.
history -- Dump the history of commands in this session.
Commands in the history list can be run again using "!<INDEX>". "!-<OFFSET>" will re-run the command that is <OFFSET> commands from the end of the list (counting the current command).
regex -- Define a custom command in terms of existing commands by matching regular expressions.
script -- Commands for managing custom commands implemented by interpreter scripts.
source -- Read and execute LLDB commands from the file <filename>.
unalias -- Delete one or more custom commands defined by 'command alias'.
For more help on any particular subcommand, type 'help <command> <subcommand>'.
command source
looks promising:
(lldb) help command source
Read and execute LLDB commands from the file <filename>.
Syntax: command source <cmd-options> <filename>
Command Options Usage:
command source [-e <boolean>] [-c <boolean>] [-s <boolean>] <filename>
-c <boolean> ( --stop-on-continue <boolean> )
If true, stop executing commands on continue.
-e <boolean> ( --stop-on-error <boolean> )
If true, stop executing commands on error.
-s <boolean> ( --silent-run <boolean> )
If true don't echo commands while executing.
This command takes options and free-form arguments. If your arguments resemble option specifiers (i.e., they start with a - or --), you must use ' -- ' between the end of the command options and the beginning of the arguments.
There's also an apropos
command that will search for keywords in the help. Unfortunately apropos command
returns too many hits to be particularly helpful in your case. apropos file
is a little less noisy, but just looking at the command subcommands is probably easiest. help
with no arguments will list the top level commands which might also help you get started.
Note, you can also tell lldb to source a random file of commands when you launch it from the command line (with the -s
option). To find out more about lldb's command line options, run:
> lldb --help
Upvotes: 4