113 lines · python
1import gdb2import os3import re4import traceback5import sys6 7""" This module evaluates function parameters of those OMPD callbacks that need GDB API calls.8"""9 10""" Have the debugger print a string.11"""12 13 14def _print(*args):15 # args is a tuple with just one string element16 print_string = args[0]17 gdb.execute('printf "%s\n"' % args[0])18 19 20""" Look up the address of a global symbol in the target.21"""22 23 24def _sym_addr(*args):25 # args is a tuple consisting of thread_id and symbol_name26 thread_id = args[0]27 symbol_name = args[1]28 if thread_id >= 0:29 gdb.execute("thread %d\n" % thread_id, to_string=True)30 return int(gdb.parse_and_eval("&" + symbol_name))31 32 33""" Read string from the target and copy it into the provided buffer.34"""35 36 37def _read_string(*args):38 # args is a tuple with just the source address39 addr = args[0]40 try:41 buf = gdb.parse_and_eval("(unsigned char*)%li" % addr).string()42 except:43 traceback.print_exc()44 return buf45 46 47""" Read memory from the target and copy it into the provided buffer.48"""49 50 51def _read(*args):52 # args is a tuple consisting of address and number of bytes to be read53 addr = args[0]54 nbytes = args[1]55 # print("_read(%i,%i)"%(addr, nbytes))56 ret_buf = bytearray()57 # try:58 buf = gdb.parse_and_eval("(unsigned char*)%li" % addr)59 for i in range(nbytes):60 ret_buf.append(int(buf[i]))61 # except:62 # traceback.print_exc()63 return ret_buf64 65 66""" Get thread-specific context.67Return -1 if no match is found.68"""69 70 71def _thread_context(*args):72 # args is a tuple consisting of thread_id and the thread kind73 thread_id = args[1]74 pthread = False75 lwp = False76 if args[0] == 0:77 pthread = True78 else:79 lwp = True80 info = gdb.execute("info threads", to_string=True).splitlines()81 82 for line in info:83 if pthread:84 m = re.search(r"(0x[a-fA-F0-9]+)", line)85 elif lwp:86 m = re.search(r"\([^)]*?(\d+)[^)]*?\)", line)87 if m is None:88 continue89 pid = int(m.group(1), 0)90 if pid == thread_id:91 return int(line[2:6], 0)92 return -193 94 95""" Test info threads / list threads / how to split output to get thread id 96and its size.97"""98 99 100def _test_threads(*args):101 info = gdb.execute("info threads", to_string=True).splitlines()102 for line in info[1:]:103 content = line.split()104 thread_id = None105 # fetch pointer to id106 if content[0].startswith("*"):107 thread_id = content[3]108 else:109 thread_id = content[2]110 sizeof_tid = sys.getsizeof(thread_id)111 print(sizeof_tid)112 print(info)113