Home > Software engineering >  How to import Facebook's Chisel fbchisellldb.py depend on the chip type(M1/Intel)?
How to import Facebook's Chisel fbchisellldb.py depend on the chip type(M1/Intel)?

Time:04-22

I add the following shell code in the ~/.lldbinit.

if [[ $(uname -p) == 'arm' ]]; then
    command script import /opt/homebrew/opt/chisel/libexec/fbchisellldb.py
fi

if [[ $(uname -p) == 'i386' ]]; then
    command script import /usr/local/opt/chisel/libexec/fbchisellldb.py
fi

script fbchisellldb.loadCommandsInDirectory('/path/to/fbchisellldb.py')

but turns out .lldbinit not support shell code/syntax, what's the correct way to detect chip type and dynamically import fbchiselldb.py.

error: 'if' is not a valid command.
error: error: No value
error: error: No value
error: error: No value
error: error: No value
Error loading Chisel (errno None)
error: 'if' is not a valid command.
error: module importing failed: invalid pathname
error: error: No value
error: error: No value
error: error: No value
error: error: No value
Error loading Chisel (errno None)

CodePudding user response:

lldb delegates scripting tasks to the script interpreter. We figured the Python folks would do a better job at producing a useable language than anything we were going to come up with, and Python is pretty widely known, so we wouldn't be forcing people to learn the quirks of yet another set of shell like if tests & looping constructs. So if you need to use logical tests & looping in your interactions with lldb, you do that in Python using the script command or by importing modules.

You can either use script command one-lines in your ~/.lldbinit, or you can put the script text inline in your .lldbinit, but I generally keep a .py file in ~/.lldb that I command script import in the .lldbinit, and have it do this sort of job. It's easier to add to and debug that way.

If you put a function of the form:

def __lldb_init_module(debugger, dict):

in a Python module, lldb will run that function when the module is imported with command script import (that's one of the big differences between using command script import and script import). That allows you to add commands to the debugger that's importing this module.

So you could do:

import platform

def __lldb_init_module(debugger, dict):
    if platform.machine() == 'arm64':
        debugger.HandleCommand("command script import <ONE_CHISEL>")
    else
        debugger.HandleCommand("command script import <OTHER_CHISEL>")
  • Related