Skip to content

Commit

Permalink
Improvments to build and inject
Browse files Browse the repository at this point in the history
- Copy tall the libraries needed for gdb to it's lib dir.
- Inject uses our bundled gdb
- Adds util module
  • Loading branch information
dwoz committed Oct 26, 2023
1 parent ba524a3 commit 5ef9aaf
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 39 deletions.
19 changes: 17 additions & 2 deletions src/relenv_gdb/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def build_gdb(prefix):
relenv.common.extract_archive(str(src), archive_name)
dir_name = archive_name.split(".tar")[0]
# os.environ["LDFLAGS"] = f"{os.environ['LDFLAGS']} '-Wl,-rpath=$$ORIGIN/../lib'"
# os.environ["LDFLAGS"] += f" -Wl,-rpath={os.environ['TOOLCHAIN_PATH']}/{os.environ['TRIPLET']}/sysroot/lib"
with pushd(src / dir_name):
subprocess.run(
[
Expand All @@ -88,9 +89,23 @@ def build_gdb(prefix):
]
)
subprocess.run(["make"])
subprocess.run(["patchelf", "--add-rpath", "$ORIGIN/../lib", "gdb/gdb"])
# subprocess.run(["patchelf", "--add-rpath", "$ORIGIN/../lib", "gdb/gdb"])
subprocess.run(
[
"patchelf",
"--add-rpath",
f"{os.environ['TOOLCHAIN_PATH']}/{os.environ['TRIPLET']}/sysroot/lib",
"gdb/gdb",
]
)
subprocess.run(["make", "install"])
relenv.relocate.main(os.environ["RELENV_PATH"])

# relenv.relocate.main(os.environ["RELENV_PATH"])
relenv.relocate.main(
f"{os.environ['RELENV_PATH']}/lib/python3.10/site-packages/relenv_gdb/gdb",
f"{os.environ['RELENV_PATH']}/lib/python3.10/site-packages/relenv_gdb/gdb/lib",
False,
)
shutil.copytree(
f"{os.environ['RELENV_PATH']}/lib/python3.10/site-packages/relenv_gdb/gdb",
"src/relenv_gdb/gdb",
Expand Down
32 changes: 2 additions & 30 deletions src/relenv_gdb/dbg.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import psutil

from .util import append_line, find_relenv_gdb

CMD_TPL = """
set pagination off
source {libpython}
Expand All @@ -33,36 +35,6 @@
"""


def find_dist_info():
"""Find relenv_gdb's dist-info directory."""
for name in pathlib.Path(__file__).parent.parent.rglob("relenv_gdb*dist-info"):
return name


def find_relenv_gdb():
"""Find the relenv-gdb script location."""
dist_info = find_dist_info()
text = pathlib.Path(dist_info, "RECORD").read_text()
for line in text.split("\n"):
if "bin/relenv-gdb" in line:
location, digest, size = line.rsplit(",", 2)
script = (pathlib.Path(dist_info).parent / location).resolve()
if script.exists():
return script


def append_line(path, line):
"""
Append a line to the path.
"""
if isinstance(path, (str, pathlib.Path)):
with open(path, "a") as fp:
fp.write(line + "\n")
else:
path.write(line + "\n")
path.flush()


def debug(gdb, proc, output):
"""
Run debug.
Expand Down
27 changes: 20 additions & 7 deletions src/relenv_gdb/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@
"""
import os
import pathlib
import subprocess
import sys
import tempfile

import psutil

from .util import find_relenv_gdb

INJ_TPL = """
set pagination off
source libpython.py
source {}
source {libpython}
source {s_path}
echo acquire gill\\n
call PyGILState_Ensure()
call (char *) PyGILState_Ensure()
p $SCRIPT
call PyRun_SimpleString($SCRIPT)
call (void) PyRun_SimpleString($SCRIPT)
echo release gill\\n
call PyGILState_Release($1)
call (void) PyGILState_Release($1)
quit
"""

Expand Down Expand Up @@ -67,8 +70,18 @@ def main():
fd, path = tempfile.mkstemp()
try:
with open(path, "w") as fp:
fp.write(INJ_TPL.format(s_path))
subprocess.run(["gdb", "-p", f"{pid}", "--command", path], capture_output=False)
fp.write(
INJ_TPL.format(
s_path=s_path,
libpython=(
pathlib.Path(__file__).parent / "libpython.py"
).resolve(),
)
)
subprocess.run(
[str(find_relenv_gdb()), "-p", f"{pid}", "--command", path],
capture_output=False,
)
finally:
os.close(fd)
os.remove(path)
Expand Down
36 changes: 36 additions & 0 deletions src/relenv_gdb/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2023 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
"""
Utility methods.
"""
import pathlib


def find_dist_info():
"""Find relenv_gdb's dist-info directory."""
for name in pathlib.Path(__file__).parent.parent.rglob("relenv_gdb*dist-info"):
return name


def find_relenv_gdb():
"""Find the relenv-gdb script location."""
dist_info = find_dist_info()
text = pathlib.Path(dist_info, "RECORD").read_text()
for line in text.split("\n"):
if "bin/relenv-gdb" in line:
location, digest, size = line.rsplit(",", 2)
script = (pathlib.Path(dist_info).parent / location).resolve()
if script.exists():
return script


def append_line(path, line):
"""
Append a line to the path.
"""
if isinstance(path, (str, pathlib.Path)):
with open(path, "a") as fp:
fp.write(line + "\n")
else:
path.write(line + "\n")
path.flush()

0 comments on commit 5ef9aaf

Please sign in to comment.