forked from pwndbg/pwndbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwndbg-lldb.py
executable file
·117 lines (86 loc) · 3.58 KB
/
pwndbg-lldb.py
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
#!/usr/bin/env python3
from __future__ import annotations
import os
import re
import subprocess
import sys
from typing import List
def find_lldb_version() -> List[int]:
"""
Parses the version string given to us by the LLDB executable.
"""
lldb = subprocess.run(["lldb", "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if lldb.returncode != 0:
print(f"Could not find the LLDB Python Path: {lldb.stderr!r}", file=sys.stderr)
sys.exit(1)
output = lldb.stdout.decode("utf-8").strip()
output = re.sub("[^0-9.]", "", output)
return [int(component) for component in output.split(".")]
def find_lldb_python_path() -> str:
"""
Finds the Python path pointed to by the LLDB executable.
"""
lldb = subprocess.run(["lldb", "-P"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if lldb.returncode != 0:
print(f"Could not find the LLDB Python Path: {lldb.stderr!r}", file=sys.stderr)
sys.exit(1)
folder = lldb.stdout.decode("utf-8").strip()
if not os.path.exists(folder):
print(f"Path pointed to by LLDB ('{folder}') does not exist", file=sys.stderr)
sys.exit(1)
return folder
if __name__ == "__main__":
debug = "PWNDBG_LLDB_DEBUG" in os.environ
# Find the path for the LLDB Python bindings.
path = find_lldb_python_path()
sys.path.append(path)
if debug:
print(f"[-] Launcher: LLDB Python path: {path}")
# Older LLDB versions crash newer versions of CPython on import, so check
# for it, and stop early with an error message.
#
# See https://github.com/llvm/llvm-project/issues/70453
lldb_version = find_lldb_version()
if debug:
print(f"[-] Launcher: LLDB version {lldb_version[0]}.{lldb_version[1]}")
if sys.version_info.minor >= 12 and lldb_version[0] <= 18:
print("LLDB 18 and earlier is incompatible with Python 3.12 and later", file=sys.stderr)
sys.exit(1)
# Start up LLDB and create a new debugger object.
import lldb
lldb.SBDebugger.Initialize()
debugger = lldb.SBDebugger.Create()
# Resolve the location of lldbinit.py based on the environment, if needed.
lldbinit_dir = os.path.dirname(sys.argv[0])
if "PWNDBG_LLDBINIT_DIR" in os.environ:
lldbinit_dir = os.environ["PWNDBG_LLDBINIT_DIR"]
lldbinit_dir = os.path.abspath(lldbinit_dir)
lldbinit_path = os.path.join(lldbinit_dir, "lldbinit.py")
if debug:
print(f"[-] Launcher: Importing main LLDB module at '{lldbinit_path}'")
if not os.path.exists(lldbinit_path):
print(f"Could not find '{lldbinit_path}, please specify it with PWNDBG_LLDBINIT_DIR")
sys.exit(1)
if lldbinit_path not in sys.path:
sys.path.append(lldbinit_dir)
# Load the lldbinit module we just found.
debugger.HandleCommand(f"command script import {lldbinit_path}")
# Initialize the debugger, proper.
import lldbinit
if debug:
print("[-] Launcher: Initializing Pwndbg")
lldbinit.main(debugger, lldb_version[0], lldb_version[1], debug=debug)
# Run our REPL until the user decides to leave.
if len(sys.argv) > 2:
print(f"Usage: {sys.argv[0]} [filename]", file=sys.stderr)
sys.exit(1)
target = None
if len(sys.argv) == 2:
target = sys.argv[1]
from pwndbg.dbg.lldb.repl import run as run_repl
if debug:
print("[-] Launcher: Entering Pwndbg CLI")
run_repl([f"target create '{target}'"] if target else None, debug=debug)
# Dispose of our debugger and terminate LLDB.
lldb.SBDebugger.Destroy(debugger)
lldb.SBDebugger.Terminate()