Skip to content

Commit

Permalink
Add c++ symbol support in xinfo. (#1028)
Browse files Browse the repository at this point in the history
This change adds c++ symbol support in xinfo.

Currently, when using xinfo to get the symbol information in C++
programs, we might get incomplete data

This is because the gdb_get_location_from_symbol function could not
handle space in the symbol name.

This change updates the gdb_get_location_from_symbol, so we can handle
the space. It makes `xinfo` produces expected result.
  • Loading branch information
r12f committed Dec 21, 2023
1 parent fbda021 commit 023b1a9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
10 changes: 6 additions & 4 deletions gef.py
Expand Up @@ -2010,11 +2010,13 @@ def gdb_get_location_from_symbol(address: int) -> Optional[Tuple[str, int]]:
if sym.startswith("No symbol matches"):
return None

# gdb outputs symbols with format: "<symbol_name> + <offset> in section <section_name> of <file>",
# here, we are only interested in symbol name and offset.
i = sym.find(" in section ")
sym = sym[:i].split()
name, offset = sym[0], 0
if len(sym) == 3 and sym[2].isdigit():
offset = int(sym[2])
sym = sym[:i].split("+")
name, offset = sym[0].strip(), 0
if len(sym) == 2 and sym[1].isdigit():
offset = int(sym[1])
return name, offset


Expand Down
6 changes: 6 additions & 0 deletions tests/binaries/Makefile
Expand Up @@ -2,7 +2,9 @@ CC = gcc
DEBUG = 1
CFLAGS += -Wall
SOURCES = $(wildcard *.c)
SOURCES += $(wildcard *.cpp)
LINKED = $(SOURCES:.c=.out)
LINKED := $(LINKED:.cpp=.out)
LDFLAGS =
EXTRA_FLAGS =
TMPDIR ?= /tmp
Expand All @@ -27,6 +29,10 @@ all: $(LINKED)
@echo "[+] Building '$(TMPDIR)/$@'"
@$(CC) $(CFLAGS) $(EXTRA_FLAGS) -o $(TMPDIR)/$@ $? $(LDFLAGS)

%.out : %.cpp
@echo "[+] Building '$(TMPDIR)/$@'"
@$(CC) $(CFLAGS) $(EXTRA_FLAGS) -o $(TMPDIR)/$@ $? $(LDFLAGS) -lstdc++

clean :
@echo "[+] Cleaning stuff"
@cd $(TMPDIR) && rm -f $(LINKED)
Expand Down
29 changes: 29 additions & 0 deletions tests/binaries/class.cpp
@@ -0,0 +1,29 @@
#include <stdio.h>

class TraitA {};
class TraitB {};

class A {
private:
int _a;

public:
virtual ~A() {}
virtual void Run() { printf("I am A\n"); }
};

template <class TA, class TB>
class B : public A {
private:
int _b;

public:
virtual void Run() { printf("I am B\n"); }
};

int main() {
A* a = new B<TraitA, TraitB>();
a->Run();
delete a;
return 0;
}
9 changes: 8 additions & 1 deletion tests/commands/xinfo.py
Expand Up @@ -3,7 +3,7 @@
"""


from tests.utils import GefUnitTestGeneric, gdb_run_cmd, gdb_start_silent_cmd
from tests.utils import GefUnitTestGeneric, gdb_run_cmd, gdb_start_silent_cmd, gdb_run_silent_cmd, _target


class XinfoCommand(GefUnitTestGeneric):
Expand All @@ -18,3 +18,10 @@ def test_cmd_xinfo(self):
res = gdb_start_silent_cmd("xinfo $sp")
self.assertNoException(res)
self.assertTrue(len(res.splitlines()) >= 7)

def test_cmd_xinfo_on_class(self):
cmd = "xinfo $pc"
target = _target("class")
res = gdb_run_silent_cmd(cmd, target=target, before=["b B<TraitA, TraitB>::Run()"])
self.assertNoException(res)
self.assertIn("Symbol: B<TraitA, TraitB>::Run", res)

0 comments on commit 023b1a9

Please sign in to comment.