Repository navigation
Expand file tree
/
Copy pathlldbinit.py
More file actions
187 lines (146 loc) · 6.52 KB
/
Copy pathlldbinit.py
File metadata and controls
187 lines (146 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 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