jonmorgan
jonmorgan

Reputation: 2610

How can I exit lldb after running commands with -o

I want to run something like the following command from a script:

lldb -f /path/to/my/file -o command1 -o command2 ... -o detach

Is there any way to exit lldb after execution without entering interactive mode? Passing in -o exit or -o quit fails with "Aborting after_file command execution, command: 'quit' failed." Running the above command with or without exit/quit leaves the terminal at the lldb prompt, which prevents me from just running this command and redirecting the output to somewhere on disk.

The end goal of this is to get the output of my command on-demand when certain things happen. There isn't a Python interpreter on this platform, so that isn't an option. Any suggestions?

Upvotes: 5

Views: 4133

Answers (2)

Enrico Granata
Enrico Granata

Reputation: 3339

This seems to work for me:

$ xcrun lldb /bin/ls -o "b malloc" -o "run" -o "script import os; os._exit(1)"

(lldb) target create "/bin/ls"

Current executable set to '/bin/ls' (x86_64).

(lldb) b malloc

Breakpoint 1: 3 locations.

(lldb) run

Process 640 launched: '/bin/ls' (x86_64)

(lldb) script import os; os._exit(1)

Process 640 stopped * thread #1: tid = 0x11033, 0x00007fff9374136b libsystem_malloc.dylibmalloc, stop reason = breakpoint 1.2 frame #0: 0x00007fff9374136b libsystem_malloc.dylibmalloc libsystem_malloc.dylib`malloc: -> 0x7fff9374136b: pushq %rbp 0x7fff9374136c: movq %rsp, %rbp 0x7fff9374136f: pushq %rbx 0x7fff93741370: pushq %rax

$ (back to the prompt)

It's kind of gross, but the key to the castle is:

-o "script import os; os._exit(1)"

sys.exit(1) won't work (we catch it and stop it from exiting LLDB), but os._exit() is an open freebie. Consider this a bug at will.

Upvotes: 7

Jim Ingham
Jim Ingham

Reputation: 27238

Yeah, that's just a bug. The "-o" commands are all gathered up and given to a sub-interpreter to execute before starting up the interactive interpreter. Unfortunately, "quit" was just quitting the sub-interpreter. That's fixed in TOT lldb, should make it into an official Apple release before too long.

Upvotes: 1

Related Questions