Redirect lldb output to file

Here is a slight modification incorporating some of the comments from above:

def toFile(debugger, command, result, dict):
    f=open("/Users/user/temp.txt","w")
    debugger.SetOutputFileHandle(f,True);
    debugger.HandleCommand(command)  
    f.close()
    debugger.SetOutputFileHandle(sys.stdout, True)

This allows the command to be supplied as an argument, and reverts the output file handle to stdout after the command is run.


You can use a Python script to do so (and much more), as explained here:

LLDB Python scripting in Xcode

Create a file named po.py in a directory of your choice (for example "~/.lldb"):

import lldb

def print_to_file(debugger, command, result, dict):
  #Change the output file to a path/name of your choice
  f=open("/Users/user/temp.txt","w")
  debugger.SetOutputFileHandle(f,True);
  #Change command to the command you want the output of
  command = "po self"
  debugger.HandleCommand(command)

def __lldb_init_module (debugger, dict):
  debugger.HandleCommand('command script add -f po.print_to_file print_to_file ')

Then in lldb write:

command script import ~/.lldb/po.py
print_to_file