8000 lldbinit/lldbinit.py at master · Mblakey/lldbinit · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
187 lines (146 loc) · 6.52 KB

lldbinit.py

File metadata and controls

187 lines (146 loc) · 6.52 KB
# python 2.x doesn't have the end option so we need this
from __future__ import print_function
"""
_ _ _ _ _ _ _
| | |_| | |_|_|___|_| |_
| | | . | . | | | | _|
|_|_|___|___|_|_|_|_|_|
lldbinit v4.0
A gdbinit clone for LLDB aka how to make LLDB a bit more useful and less crappy
Available at https://github.com/gdbinit/lldbinit
Original lldbinit code by Deroko @ https://github.com/deroko/lldbinit
gdbinit available @ https://github.com/gdbinit/Gdbinit
(c) Deroko 2014, 2015, 2016
(c) fG! 2017-2026 - reverser@put.as - https://reverse.put.as
No original license by Deroko.
All my modifications are under MIT license:
Copyright 2017-2026 Pedro Vilaca
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Huge thanks to Deroko for his original effort!
To list all implemented commands use 'lldbinitcmds' command.
How to install it:
------------------
$ cp lldbinit.py ~
$ echo "command script import ~/lldbinit.py" >>$HOME/.lldbinit
or
just copy it somewhere and use "command script import path_to_script" when you want to load it.
Notes:
------
Version 3.0+ drops support for ARM32 and assumes ARM64 instead
Version 3.3 drops support for lldb older than 3.5.2 (April 2015) since commands now use the SBExecutionContext
Something that I missed all these years :-]
"""
import sys
import os
import subprocess
import inspect
import importlib.util
import lldb
def is_system_python(python_bin):
system_python_paths = ("/usr/bin/", "/usr/lib/", "/System/", "/Library/")
return python_bin.startswith(system_python_paths)
def lldb_python_path():
python_bin = os.path.join(sys.prefix, "bin", f"python{sys.version_info.major}.{sys.version_info.minor}")
if os.path.isfile(python_bin) and os.access(python_bin, os.X_OK):
return python_bin
return None
def activate_extensions():
target = extensions_dir()
if os.path.isdir(target) and target not in sys.path:
sys.path.insert(0, target)
def extensions_dir():
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "extensions")
def ensure_installed(python_bin, *packages):
activate_extensions()
missing = [pkg for pkg in packages if importlib.util.find_spec(pkg) is None]
if not missing:
return
print(f"[lldbinit] installing package: {missing}", file=sys.stderr)
result = subprocess.run(
[python_bin, "-m", "pip", "install", "--target", extensions_dir(), *missing],
capture_output=True, text=True,
)
if result.returncode != 0:
print(f"[lldbinit] pip failed (exit {result.returncode}):", file=sys.stderr)
print(result.stdout, file=sys.stderr)
print(result.stderr, file=sys.stderr)
return
activate_extensions()
for pkg in missing:
if importlib.util.find_spec(pkg) is None:
print(f"[lldbinit] failed to install: {pkg}", file=sys.stderr)
def ensure_installed(python_bin, *packages):
activate_extensions()
for pkg in packages:
if importlib.util.find_spec(pkg) is None:
print(f"[lldbinit] installing missing package: {pkg}", file=sys.stderr)
result = subprocess.run(
[python_bin, "-m", "pip", "install", "--target", extensions_dir(), "--quiet", pkg],
capture_output=True, text=True,
)
if result.returncode != 0:
print(f"[lldbinit] pip failed (exit {result.returncode}):", file=sys.stderr)
print(result.stdout, file=sys.stderr)
print(result.stderr, file=sys.stderr)
globals()[f"USE_{pkg.upper()}"] = False
activate_extensions()
for pkg in packages:
found = importlib.util.find_spec(pkg) is not None
globals()[f"USE_{pkg.upper()}"] = found
if found:
print(f"[lldbinit] using package {pkg}", file=sys.stderr)
if __name__ == "__main__":
print("Run only as script from LLDB... Not as standalone program!")
import lldbext_stack
import lldbext_source
import lldbext_stream
import lldbext_trace
import lldbext_module
import lldbext_hexdump
import lldbext_register
import lldbext_thread
MODULES = [ lldbext_stack,
lldbext_source,
lldbext_stream,
lldbext_trace,
lldbext_module,
lldbext_hexdump,
lldbext_register,
lldbext_thread,
]
OPTIONAL_PKGS = ["pygments"]
def __lldb_init_module(debugger, internal_dict):
""" we can execute lldb commands using debugger.HandleCommand() which makes all output to default
lldb console. With SBDebugger.GetCommandinterpreter().HandleCommand() we can consume all output
with SBCommandReturnObject and parse data before we send it to output (eg. modify it);
in practice there is nothing here in initialization or anywhere else that we want to modify
"""
# python_bin = lldb_python_path()
# if python_bin is None:
# print("[lldbinit] could not locate a real python interpreter", file=sys.stderr)
# elif is_system_python(python_bin):
# print(f"[lldbinit] {python_bin} is system python - skipping optional installs", file=sys.stderr)
# else:
# print(f"[lldbinit] checking optional packages", file=sys.stderr)
# ensure_installed(python_bin, *OPTIONAL_PKGS)
debugger.HandleCommand("command alias r process launch -s -X true --")
for module in MODULES:
debugger.HandleCommand(f"command script import {module.__file__}")
for _name, cls in inspect.getmembers(module):
if inspect.isclass(cls) and callable(getattr(cls, "register_lldb_command", None)):
cls.register_lldb_command(debugger, module.__name__)
return
0