1import gdb 2import os 3import re 4import traceback 5import sys 6 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""" 12def _print(*args): 13 # args is a tuple with just one string element 14 print_string = args[0] 15 gdb.execute('printf "%s\n"' % args[0]) 16 17""" Look up the address of a global symbol in the target. 18""" 19def _sym_addr(*args): 20 # args is a tuple consisting of thread_id and symbol_name 21 thread_id = args[0] 22 symbol_name = args[1] 23 if(thread_id >= 0): 24 gdb.execute('thread %d\n' % thread_id, to_string=True) 25 return int(gdb.parse_and_eval("&"+symbol_name)) 26 27""" Read string from the target and copy it into the provided buffer. 28""" 29def _read_string(*args): 30 # args is a tuple with just the source address 31 addr = args[0] 32 try: 33 buf = gdb.parse_and_eval('(unsigned char*)%li' % addr).string() 34 except: 35 traceback.print_exc() 36 return buf 37 38""" Read memory from the target and copy it into the provided buffer. 39""" 40def _read(*args): 41 # args is a tuple consisting of address and number of bytes to be read 42 addr = args[0] 43 nbytes = args[1] 44# print("_read(%i,%i)"%(addr, nbytes)) 45 ret_buf = bytearray() 46# try: 47 buf = gdb.parse_and_eval('(unsigned char*)%li' % addr) 48 for i in range(nbytes): 49 ret_buf.append(int(buf[i])) 50# except: 51# traceback.print_exc() 52 return ret_buf 53 54 55""" Get thread-specific context. 56Return -1 if no match is found. 57""" 58def _thread_context(*args): 59 # args is a tuple consisting of thread_id and the thread kind 60 thread_id = args[1] 61 pthread = False 62 lwp = False 63 if args[0] == 0: 64 pthread = True 65 else: 66 lwp = True 67 info = gdb.execute('info threads', to_string=True).splitlines() 68 69 for line in info: 70 if pthread: 71 m = re.search(r'(0x[a-fA-F0-9]+)', line) 72 elif lwp: 73 m = re.search(r'\([^)]*?(\d+)[^)]*?\)', line) 74 if m == None: 75 continue 76 pid = int(m.group(1),0) 77 if pid == thread_id: 78 return int(line[2:6],0) 79 return -1 80 81""" Test info threads / list threads / how to split output to get thread id 82and its size. 83""" 84def _test_threads(*args): 85 info = gdb.execute('info threads', to_string=True).splitlines() 86 for line in info[1:]: 87 content = line.split() 88 thread_id = None 89 # fetch pointer to id 90 if(content[0].startswith('*')): 91 thread_id = (content[3]) 92 else: 93 thread_id = (content[2]) 94 sizeof_tid = sys.getsizeof(thread_id) 95 print(sizeof_tid) 96 print(info) 97