LLDB Python scripting in Xcode

2019-03-09 14:15发布

问题:

I've just discovered this handy feature of LLDB that allows me to write Python scripts that have access to variables in the frame when I'm on a breakpoint in LLDB. However I'm having a few issues when using it in Xcode (v4.5.2). Firstly, I can't find anywhere that says where I should keep these Python scripts so that I can import them from the command line in LLDB. Secondly, after I type script into LLDB the keyboard input goes a bit wrong: backspace doesn't delete the character on the screen, but effectively deletes it from the command. So typing primt<bsp><bsp><bsp>int effectively means print, but it still comes up as primtint on the terminal. This is just aesthetic but it's quite annoying!

Can anyone point me to some Xcode-specific resources for using Python with LLDB?

EDIT: Here is another interesting link that says you can use Python to create custom summaries for variables using Python, but I can't find anything related to that.

回答1:

Between Xcode, lldb, and the Python interpreter there are some problems with the interactive console, unfortunately. Please do file a bug report at http://bugreport.apple.com/ - I don't know if there is a bug report about this specific issue already, although problems in general here are known. You may want to use the command line lldb tool if you're exploring the interactive python scripting interface; that works better.

I put all my python scripts for lldb in ~/lldb. In my ~/.lldbinit file I source them in. For instance, I have ~/lldb/stopifcaller.py which is

import lldb

# Use this like
# (lldb) command script import ~/lldb/stopifcaller.py
# (lldb) br s -n bar
# (lldb) br comm add --script-type python -o "stopifcaller.stop_if_caller(frame, 'foo')" 1

def stop_if_caller(current_frame, function_of_interest):
  thread = current_frame.GetThread()
  if thread.GetNumFrames() > 1:
    if thread.GetFrameAtIndex(1).GetFunctionName() != function_of_interest:
      thread.GetProcess().Continue()

I would put the command script import in my ~/.lldbinit file to load it automatically, if that's what I wanted. This particular example adds a python command to breakpoint #1 -- when lldb stops at the breakpoint, it will look at the calling function. If the calling function is not foo, it will automatically resume execution. In essence, breakpoint 1 will only stop if foo() calls bar(). Note that there may be a problem with Xcode 4.5 lldb in doing command script import ~/... -- you may need to type out the full path to your home directory (/Users/benwad/ or whatever). I can't remember for sure - there were a few tilde-expansion problems with Xcode 4.5 that have been fixed for a while.

You can add simple type summaries to your ~/.lldbinit directly. For instance, if I'm debugging lldb itself, ConstString objects have only one field of interest to me normally, the m_string ivar. So I have

type summary add -w lldb lldb_private::ConstString -s "${var.m_string}"

Or if it's the type addr_t, I always want that formatted as hex so I have

type format add -f x lldb::addr_t

If you want to add a new command to lldb, you would have a python file like ~/lldb/sayhello.py,

import lldb

def say_hello(debugger, command, result, dict):
  print 'hello'

def __lldb_init_module (debugger, dict):
  debugger.HandleCommand('command script add -f sayhello.say_hello hello')

and you would load it in to lldb like

(lldb) comma script import  ~/lldb/sayhello.py
(lldb) hello
hello
(lldb)

Most of the time when you're adding a command written in python you'll use the shlex and optparse libraries so the command can do option parsing, and you'll add a __doc__ string - I omitted those things to keep the example simple.