1
# Copyright (c) 2013 Adrian Negreanu <groleo@gmail.com>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 3 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>
20
class LoadSymFiles(gdb.Command):
21
"""Add symbol files for files in /proc/pid/maps: load-sym-file [symbols-directory] [lib-directory]"""
25
b dlfcn.c:56 # breakpoint 2, android_dlopen
26
commands 2 # commands to run when breakpoint 2 is hit
27
source ../utils/load_sym_files.py
30
end # end of commands for bkpt 2
33
gdb.Command.__init__(self, "load-sym-files", gdb.COMMAND_FILES, gdb.COMPLETE_FILENAME, True)
35
def invoke(self, arg, from_tty):
36
arg_list = gdb.string_to_argv(arg)
37
symdir = "/system/symbols"
38
libdir = "/system/lib"
40
if len(arg_list) == 1:
42
if not os.path.isdir(symdir):
43
print "error: symbol directory is invalid"
44
print "usage: load-sym-files [symbols-dir] [lib-dir]"
47
if len(arg_list) == 2:
49
if not os.path.isdir(libdir):
50
print "error: library directory is invalid"
51
print "usage: load-sym-files [symbols-dir] [lib-dir]"
54
pid = gdb.selected_inferior().pid
56
print "error: debugee not started yet"
59
maps = open("/proc/%d/maps"%pid,"rb")
61
# b7fc9000-b7fcf000 r-xp 00000000 08:01 1311443 /system/lib/liblog.so
62
m = re.match("([0-9A-Fa-f]+)-[0-9A-Fa-f]+\s+r-xp.*(%s.*)"%libdir,line)
66
start_addr = int(m.group(1),16)
70
p = subprocess.Popen("objdump -h "+lib , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
71
for header in p.stdout.read().split("\n"):
72
#6 .text 00044ef7 00017f80 00017f80 00017f80 2**4
73
t = re.match("\s*[0-9]+\s+\.text\s+([0-9A-Fa-f]+\s+){3}([0-9A-Fa-f]+)",header)
75
text_addr = int(t.group(2),16)
77
symfile = symdir + lib
78
if os.path.isfile(symfile):
79
gdb.execute("add-symbol-file %s 0x%X" % (symfile, start_addr+text_addr))