How can I exit lldb after running commands with -o - lldb

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?

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.

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.

Related

Using GDB Debugger to See Hidden Code

I have been given a binary file with embedded C code which I cannot see when I run it in the GDB GCC Debugger. I imagine the C code has been hidden by the compilation / formation of the binary code. I have tried the following:
gdb> file myFile
gdb> list main
The output I get is:
myFile.c: No such file or directory
I know there is code written in C in this binary file. The executable runs when I type ./myFile
I have installed 32-bit libraries as this is needed for this situation and I'm running Ubuntu 16.04
Any help is appreciated.
It could be are a compilation issue. Try to look option for debugging
$ gcc -g myFile.c -o myFile
$ gdb myFile
(gdb) list main
If you compile without "-g" option, the debugger will never show you th C code but only the assembly code.
In your case, if you run these commands, you will see the disassembled code
(gdb) info file
Then take the address of the entry point
Entry point: 0x(address)
For show disassembly code
(gdb) break 0x(address)
(gdb) run
When the breakpoint gets caught
(gdb) x/20i $pc

How to change the information printed by Shake during a build

Currently when building Shake outputs lines such as:
# gcc (for debugBuild)
But that is not informative for our application, so we'd like to print something like:
objectFileName.o[configurationFlavour]
How can you do that in Shake?
The easiest thing is to play with the Verbosity field of shakeVerbosity, or with --verbose. At Normal I get:
# ghc (for output/self/General/Intern.o output/self/General/Intern.hi)
And at Verbose I get:
ghc -c src\Development/Shake/Classes.hs -isrc -main-is Run.main -odir=output/self -hidir=output/self -i=output/self -DPORTABLE -fwarn-unused-imports
If that's not enough you can remove the output from cmd with quietly and print your own messages with putNormal:
putNormal "I'm running objectFileName.o in configuration"
quietly $ cmd "gcc -c objectFileName.o"

C programming: How to use gdb with Makefile and command line arguments?

To create the .out executable, I have to enter:
$: make
$: myprogram.out name.ged
My program incorporates a command line argument, thus the "name.ged".
Whenever I run gdb after getting a segmentation fault (core dumped), I enter:
$: gdb a.out core
(gdb): bt
I then use the back trace command, and gdb returns:
#0 0x4a145155 in ?? ()
#1 0x08a16ce0 in ?? ()
I even tried using the up command t move up the stack, but still no luck. I can't tell which line in my program is giving me the seg fault. gdb works with my other programs that do not involve a Makefile and command arguments, so I'm wondering if my commands are incorrect.
Summarizing the comments (before anyone else does :).
Your executable file is missing the symbolic information that gdb needs to display the relevant source code. You need to add the -g option to the compile command and produce a new executable. Then re-run your failing test to produce a new core file. gdb with this executable and core will be able to show you the stack of function calls using backtrace.
In a makefile, the easiest way to do this is to add (to) the CFLAGS variable which is used with the implicit .o.c rule.
CFLAGS= -g -Wall -Wextra
You can also add this directly to the command-line (assuming a decent shell :). This sets the value as an environment variable during the execution of the make command (and sub-commands).
$ CFLAGS='-g -Wall -Wextra' make
I'd actually recommend you add this to your bash .profile, so you always get the most information from the compiler.
CFLAGS='-Wall -Wextra'
Then, when you need it, put this in the makefile to make a debuggable executable:
CFLAGS+= -g

using gdb debug c

I run this command in my terminal,
gcc -g -I/usr/include -g sample_client.c lsp.o lspmessage.pb-c.o -o sample_client -L/usr/lib -lprotobuf-c
in my file directory, I can see a sample_client file. Its property is executable.
However, when I run
(gdb) sample_client
I got this,
Undefined command: "sample_client".
How can I debug?
$ gdb ./sample_client
(gdb) run
To pass command-line arguments to your program use --args:
$ gdb --args ./sample_client arg1 arg2 arg3
(gdb) run
When you start gdb, you need to tell it which binary (executable) to debug:
$ gdb ./sample_client
Then, to run the program inside gdb, use the run command:
(gdb) run
You should probably give the fine documentation some quality time.
gdb <binary file here>
run < <flags here>
Also, refer to this quick reference for future operations:
http://www.stanford.edu/class/cs107/other/gdbrefcard.pdf

How to pass arguments to my application that include the - symbol on gdb?

I need to pass some arguments to my application,that include the - symbol. The problem is if I pass gdb a.out -foo baa, the gdb understand that the arguments are for it, and not to my application. How to fix this?
gdb -q a.out
inside gdb:
run -foo baa
Tip: -q option for gdb suppresses the introductory copyright messages.
Option 1:
gdb --args ls /tmp
Option 2:
gdb ls
set args /tmp
run
Option 3 (didn't know about that, thanks Ahmed):
gdb ls
run /tmp
Maybe there are others?

Resources