LLDB Python scripting in Xcode

2024/9/25 23:14:31

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.

Answer

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')" 1def 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 lldbdef 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.

https://en.xdnf.cn/q/71522.html

Related Q&A

What technologies exist to create stand alone executables for Python 3?

Other than cx_Freeze, are there any other current maintained tool suites to generate stand alone executables for Python 3k?Are there any other techniques for minimizing preinstallation requirements un…

running multiple threads in python, simultaneously - is it possible?

Im writing a little crawler that should fetch a URL multiple times, I want all of the threads to run at the same time (simultaneously).Ive written a little piece of code that should do that.import thre…

Drawing bounding rectangles around multiple objects in binary image in python

I am trying to write some easy code in python to produce bounding rectangles around objects in a binary image, where there may be 1 or more objects. This is fairly easy to achieve with cv2.boundingRec…

Replicating YEARFRAC() function from Excel in Python

So I am using python in order to automate some repetitive tasks I must do in excel. One of the calculations I need to do requires the use of yearfrac(). Has this been replicated in python?I found this…

creating a pandas dataframe from a database query that uses bind variables

Im working with an Oracle database. I can do this much:import pandas as pdimport pandas.io.sql as psqlimport cx_Oracle as odbconn = odb.connect(_user +/+ _pass +@+ _dbenv)sqlStr = "SELECT * FROM c…

Is there a docstring autocompletion tool for jupyter notebook?

I am looking for a tool/extension that helps you writing python docstrings in jupyter notebook. I normally use VS code where you have the autodocstring extension that automatically generates templates …

Long to wide data. Pandas

Im trying to take my dataframe from a long format in which I have a column with a categorical variable, into a wide format in which each category has its own price column. Currently, my data looks like…

How to wrap text in Django admin(set column width)

I have a model Itemclass Item(models.Model):id = models.IntegerField(primary_key=True)title = models.CharField(max_length=140, blank=True)description = models.TextField(blank=True)price = models.Decima…

Problems compiling mod_wsgi in virtualenv

Im trying to compile mod_wsgi (version 3.3), Python 2.6, on a CentOS server - but under virtualenv, with no success. Im getting the error:/usr/bin/ld:/home/python26/lib/libpython2.6.a(node.o):relocatio…

Python - Multiprocessing Error cannot start a process twice

I try to develop an algorithm using multiprocessing package in Python, i learn some tutorial from internet and try to develop an algorithm with this package. After looking around and try my hello world…