[LLDB] Create your own command in Python

Load the original command created in Python into LLDB of Xcode.

Xcode seems to read one of the following two at startup.

Basically it seems that .lldbinit-Xcode is loaded. However, since the above is read from the command line etc., if you describe the basic settings in .lldbinit and put the process to read .lldbinit in .lldbinit-Xcode as follows It's okay.

command source ~/.lldbinit

Read the original command file at startup

After setting as above, the command will be loaded automatically by setting to read the Python file in .lldbinit.

command script import ~/.lldb/hoge.py

Define in file

After setting the reading settings described above, define the contents to be actually read. The main format and required description are as follows.

new-command.py


#!/usr/bin/env python
#coding: utf-8

import lldb
import commands
import os

def command_function(debugger, command, result, internal_dict):
	cmd = "po self"
	debugger.HandleCommand(cmd)
	
def __lldb_init_module(debugger, internal_dict):
	debugger.HandleCommand("command script add -f new-command:command_function commandname")
	print "commandname has been installed."

After defining it, read it as follows.

(lldb) command script import /path/to/command.py

Set inline during break

It is easy to check the operation by setting it inline and executing it for a little operation test.

The following will start the python interpreter.

(lldb) script⏎

Then define as follows and exit the interpreter.

(lldb) script⏎
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> def hoge_function(debugger, command, result, internal_dict):
...	cmd = "po self"
...	debugger.HandleCommand(cmd)
...	
>>> quit⏎
(lldb) command script add hoge -f hoge_func
(lldb) hoge
<UIView...

String placeholder

You can use the placeholder by writing as follows. This is convenient when processing is performed based on the character string specified in the command.

val = "This is %s %s" % ("a", "pen")

# --- or ---

val = "This is %(unit)s %(msg)s" % {"unit": "a", "msg": "pen"}

# --- or ---

val = """This is
%s %s""" % ("a", "pen")

Split command

The command is passed as a single string, so use shlex.split to split it.

command_list = shlex.split(command)

Command to copy dummy view and ʻadd Subview:`

What I wanted to do this time was to create a command that duplicates the view using the specified view size and ʻadd Subview: `to see if the view is properly generated at the desired position. ..

I think it's a bit redundant, but for the time being, it worked if I did the following, so I'll describe it.

#!/usr/bin/env python
# -*- coding: utf-8

import lldb
import commands
import os
import shlex

def dmy_func(debugger, command, result, internal_dict):
    commands = shlex.split(command)

    count = len(commands)

    if count < 2:
        return

    cmd = "po CGRect $frame = (CGRect)[%s frame]" % commands[0]
    print(cmd)
    debugger.HandleCommand(cmd)

    cmd = "po UIView *$view = [[UIView alloc] initWithFrame:(CGRect)$frame]"
    print(cmd)
    debugger.HandleCommand(cmd)

    cmd = "po [$view setBackgroundColor:(UIColor*)[UIColor redColor]]"
    print(cmd)
    debugger.HandleCommand(cmd)

    cmd = "po [%s addSubview:(UIView*)$view]" % commands[1]
    print(cmd)
    debugger.HandleCommand(cmd)

def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand("command script add -f dmy.dmy_func dmy")
    print "dmy command has been installed."

How to use

(lldb) dmy self.hogeView self.view

Create a UIView using the frame of self.hogeView and ʻaddSubview:toself.view`.

Recommended Posts

[LLDB] Create your own command in Python
Create your own Linux commands in Python
Create your own Big Data in Python for validation
Create your own Random Dot Stereogram (RDS) in Python.
[Python] logging in your own module
Easily use your own functions in Python
Get your own IP address in Python
Create your own graph structure class and its drawing in python
Create your own exception
Create SpatiaLite in Python
Import your own modules in Grasshopper's Python development
Memo to create your own Box with Pepper's Python
Create a function in Python
Create a dictionary in Python
Try to improve your own intro quiz in Python
Create gif video in Python
Execute external command in python
[Blender × Python] Create your own function & summary so far
Use the CASA Toolkit in your own Python environment
Create your first GDSII file in Python using gdspy
Create your own Django middleware
[Road to intermediate Python] Define in in your own class
External command execution in Python
Try sorting your own objects with priority queue in Python
[Python] Make your own LINE bot
Hit a command in Python (Windows)
Create a DI Container in Python
Run shell command / python in R
How to create your own Transform
In the python command python points to python3.8
Create a binary file in Python
Create Gmail in Python without API
Create your own name resolution service
[Django] Create your own 403, 404, 500 error pages
Create Python project documentation in Sphinx
Create a Kubernetes Operator in Python
Create a random string in Python
Create and read messagepacks in Python
Try docker: Create your own container image for your Python web app
Specify your own class in class argument and return type annotation in Python
Create wordcloud from your tweet with python3
Learn the design pattern "Command" in Python
Create your own DNS server with Twisted
Create a python environment on your Mac
[Python] Package and distribute your own modules
Create a simple GUI app in Python
Create your own Composite Value with SQLAlchemy
Create a JSON object mapper in Python
Create Qt designer parts in Python (PyQt)
[Python] Register your own library on PyPI
Until you install your own Python library
Decompose command arguments in one line in Python
[GPS] Create a kml file in Python
Publish your own Python library with Homebrew
Try text mining your diary in Python
Create a shortcut to run a Python file in VScode on your terminal
Make your own NOAA / GOES familiar X-ray flux 3days plot in Python
Create your own virtual camera with Python + OpenCV and apply original effects
Template for creating command line applications in Python
Quadtree in Python --2
Python in optimization