Skip to content

Commit

Permalink
Merge fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Oct 16, 2021
2 parents 63ffc67 + 2becf7b commit 8074f88
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ tests/pylint3.html
_build
debug.log
.pytest_cache
.coverage
htmlcov
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ PYLINT_COMMON_PARAMETERS := --jobs=$(PYLINT_JOBS) --suggestion-mode=$(PYLINT_SUG
PYLINT_GEF_PARAMETERS := --disable=$(PYLINT_DISABLE) --enable=$(PYLINT_ENABLE) $(PYLINT_COMMON_PARAMETERS)
PYLINT_TEST_PARAMETERS := --disable=$(PYLINT_DISABLE) --enable=$(PYLINT_TEST_ENABLE) $(PYLINT_COMMON_PARAMETERS)
TARGET := $(shell lscpu | head -1 | sed -e 's/Architecture:\s*//g')
COVERAGE_DIR ?= /tmp/cov
GEF_PATH ?= $(shell readlink -f gef.py)
TMPDIR ?= /tmp
PYTEST_PARAMETERS := --verbose -n $(NB_CORES)
Expand All @@ -31,12 +32,20 @@ testbins: $(TMPDIR) $(wildcard tests/binaries/*.c)

clean:
TMPDIR=$(TMPDIR) $(MAKE) -j $(NB_CORES) -C tests/binaries clean
@rm -rf $(TMPDIR)
@rm -rf $(TMPDIR)/gef-* $(TMPDIR)/gef.py || true

lint:
python3 -m pylint $(PYLINT_GEF_PARAMETERS) $(GEF_PATH)
python3 -m pylint $(PYLINT_TEST_PARAMETERS) $(wildcard tests/*.py)

coverage:
@! ( [ -d $(COVERAGE_DIR) ] && echo "COVERAGE_DIR=$(COVERAGE_DIR) exists already")
@mkdir -p $(COVERAGE_DIR)
@COVERAGE_DIR=$(COVERAGE_DIR) $(MAKE) test
@coverage combine $(COVERAGE_DIR)/*
@coverage html --include "*/gef.py"
@rm -rf $(COVERAGE_DIR)

$(TMPDIR):
mkdir -p $@

2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mkdocs==1.0.4
mkdocs>=1.2.3
25 changes: 22 additions & 3 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -8672,7 +8672,9 @@ def __get_current_block_start_address():
pass

if not nb_argument:
if is_x86_32():
if not parameter_set:
nb_argument = 0
elif is_x86_32():
nb_argument = len(parameter_set)
else:
nb_argument = max(function_parameters.index(p)+1 for p in parameter_set)
Expand Down Expand Up @@ -8830,7 +8832,15 @@ def context_trace(self):
insn = next(gef_disassemble(pc, 1))
except gdb.MemoryError:
break
items.append(Color.redify("{} {}".format(insn.mnemonic, ", ".join(insn.operands))))

# check if the gdb symbol table may know the address
sym_found = gdb_get_location_from_symbol(pc)
symbol = ""
if sym_found:
sym_name, offset = sym_found
symbol = " <{}+{:x}> ".format(sym_name, offset)

items.append(Color.redify("{}{} {}".format(symbol, insn.mnemonic, ", ".join(insn.operands))))

gef_print("[{}] {}".format(Color.colorify("#{}".format(level), "bold green" if current_frame == orig_frame else "bold pink"),
RIGHT_ARROW.join(items)))
Expand Down Expand Up @@ -8891,7 +8901,16 @@ def reason():
line += Color.colorify("stopped", "bold red")
thread.switch()
frame = gdb.selected_frame()
line += " {:s} in {:s} ()".format(Color.colorify("{:#x}".format(frame.pc()), "blue"), Color.colorify(frame.name() or "??", "bold yellow"))
frame_name = frame.name()

# check if the gdb symbol table may know the address
if not frame_name:
sym_found = gdb_get_location_from_symbol(frame.pc())
if sym_found:
sym_name, offset = sym_found
frame_name = "<{}+{:x}>".format(sym_name, offset)

line += " {:s} in {:s} ()".format(Color.colorify("{:#x}".format(frame.pc()), "blue"), Color.colorify(frame_name or "??", "bold yellow"))
line += ", reason: {}".format(Color.colorify(reason(), "bold pink"))
elif thread.is_exited():
line += Color.colorify("exited", "bold yellow")
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ ropper
unicorn
pytest
pytest-xdist
coverage
24 changes: 17 additions & 7 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
CI_VALID_ARCHITECTURES_32B = ("i686", "armv7l")
CI_VALID_ARCHITECTURES_64B = ("x86_64", "aarch64", "mips64el", "ppc64le")
CI_VALID_ARCHITECTURES = CI_VALID_ARCHITECTURES_64B + CI_VALID_ARCHITECTURES_32B
COVERAGE_DIR = os.getenv("COVERAGE_DIR", "")

CommandType = NewType("CommandType", Union[str, Iterable[str]])

Expand All @@ -39,16 +40,25 @@ def gdb_run_cmd(cmd: CommandType, before: CommandType = (), after: CommandType =
target: Path = DEFAULT_TARGET, strip_ansi: bool = STRIP_ANSI_DEFAULT) -> str:
"""Execute a command inside GDB. `before` and `after` are lists of commands to be executed
before (resp. after) the command to test."""
command = [
"gdb", "-q", "-nx",
"-ex", f"source {GEF_PATH}",
"-ex", "gef config gef.debug True"
]

command = ["gdb", "-q", "-nx"]
if COVERAGE_DIR:
coverage_file = Path(COVERAGE_DIR) / os.getenv("PYTEST_XDIST_WORKER")
command += _add_command([
"pi from coverage import Coverage",
f"pi cov = Coverage(data_file=\"{coverage_file}\","
"auto_data=True, branch=True)",
"pi cov.start()",
])
command += _add_command([
f"source {GEF_PATH}",
"gef config gef.debug True",
])
command += _add_command(before)
command += _add_command(cmd)
command += _add_command(after)
command += ["-ex", "quit", "--", target]
if COVERAGE_DIR:
command += _add_command(["pi cov.stop()", "pi cov.save()"])
command += ["-ex", "quit", "--", str(target)]

lines = subprocess.check_output(command, stderr=subprocess.STDOUT).strip().splitlines()
output = b"\n".join(lines)
Expand Down

0 comments on commit 8074f88

Please sign in to comment.