summaryrefslogtreecommitdiff
path: root/openmp/libompd/gdb-plugin/ompd/ompd_callbacks.py
blob: bb520e50c6c64216c38e692d606c1c7633312e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import gdb
import os
import re
import traceback
import sys

""" This module evaluates function parameters of those OMPD callbacks that need GDB API calls.
"""

""" Have the debugger print a string.
"""
def _print(*args):
	# args is a tuple with just one string element
	print_string = args[0]
	gdb.execute('printf "%s\n"' % args[0])

""" Look up the address of a global symbol in the target.
"""
def _sym_addr(*args):
	# args is a tuple consisting of thread_id and symbol_name
	thread_id = args[0]
	symbol_name = args[1]
	if(thread_id >= 0):
		gdb.execute('thread %d\n' % thread_id, to_string=True)
	return int(gdb.parse_and_eval("&"+symbol_name))

""" Read string from the target and copy it into the provided buffer.
"""
def _read_string(*args):
	# args is a tuple with just the source address
	addr = args[0]
	try:
		buf = gdb.parse_and_eval('(unsigned char*)%li' % addr).string()
	except:
		traceback.print_exc()
	return buf

""" Read memory from the target and copy it into the provided buffer.
"""
def _read(*args):
	# args is a tuple consisting of address and number of bytes to be read
	addr = args[0]
	nbytes = args[1]
#	print("_read(%i,%i)"%(addr, nbytes))
	ret_buf = bytearray()
#	try:
	buf = gdb.parse_and_eval('(unsigned char*)%li' % addr)
	for i in range(nbytes):
		ret_buf.append(int(buf[i]))
#	except:
#		traceback.print_exc()
	return ret_buf


""" Get thread-specific context.
Return -1 if no match is found.
"""
def _thread_context(*args):
	# args is a tuple consisting of thread_id and the thread kind
	thread_id = args[1]
	pthread = False
	lwp = False
	if args[0] == 0:
		pthread = True
	else:
		lwp = True
	info = gdb.execute('info threads', to_string=True).splitlines()
	
	for line in info:
		if pthread:
			m = re.search(r'(0x[a-fA-F0-9]+)', line)
		elif lwp:
			m = re.search(r'\([^)]*?(\d+)[^)]*?\)', line)
		if m == None:
			continue
		pid = int(m.group(1),0)
		if pid == thread_id:
			return int(line[2:6],0)
	return -1

""" Test info threads / list threads / how to split output to get thread id 
and its size.
"""
def _test_threads(*args):
	info = gdb.execute('info threads', to_string=True).splitlines()
	for line in info[1:]:
		content = line.split()
		thread_id = None
		# fetch pointer to id
		if(content[0].startswith('*')):
			thread_id = (content[3])
		else:
			thread_id = (content[2])
		sizeof_tid = sys.getsizeof(thread_id)
		print(sizeof_tid)
	print(info)