forked from jvitors23/PROFino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrument.py
More file actions
116 lines (99 loc) · 3.74 KB
/
instrument.py
File metadata and controls
116 lines (99 loc) · 3.74 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import subprocess
import os
def find_functions(filename):
out = subprocess.Popen(['ctags-universal', '--c-types=f','-o', '-', '--fields=+ne', filename],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = out.communicate()
functions = stdout.decode("utf-8").split('\n')
functions.remove('')
functions_list = []
for f in functions:
elts = f.split('\t')
start_line = 0
end_line = 0
# pega a linha de inicio e de fim na saída do c-tags universal
for item in elts:
if 'line' in item:
start_line = int(item.split(':')[1])
if 'end' in item:
end_line = int(item.split(':')[1])
functions_list.append({
'name': elts[0],
'start_line': start_line - 1,
'end_line': end_line - 1
})
with open(filename, 'r') as file:
lines = file.readlines()
for func in functions_list:
if '{' in lines[func['start_line']]:
continue
else:
for i in range(func['start_line'], len(lines)):
if '{' in lines[i]:
func['start_line'] = i
break
return functions_list
def getEntryFunctionCode():
code = 'printf("========:%d:%d:%s:%d:========' + '\\' + 'n", timer_overflow_count, timer_overflow_count_overflow, __func__, 1);'
return code
def getExitFunctionCode():
code = 'printf("========:%d:%d:%s:%d:========' + '\\' + 'n", timer_overflow_count, timer_overflow_count_overflow, __func__, 0);'
return code
def instrument(filename):
functions = find_functions(filename)
instrumented_filename = filename.split('.')[0]+'.inst.c'
instf = open(instrumented_filename, 'w')
instf.write('#include <util/delay.h>\n')
instf.write('#include <stdio.h>\n')
instf.write('#include "comm/uart.h"\n')
instf.write('#include <avr/io.h>\n')
instf.write('#include <avr/interrupt.h>\n\n')
instf.write('volatile uint16_t timer_overflow_count;\n')
instf.write('volatile uint16_t timer_overflow_count_overflow;\n\n')
instf.write('void timer0_init(){\n')
instf.write('\tTCCR0B |= (1 << CS02);\n')
instf.write('\tTCNT0 = 0;\n')
instf.write('\tTIMSK0 |= (1 << TOIE0);\n')
instf.write('\tsei();\n')
instf.write('\ttimer_overflow_count = 0;\n')
instf.write('\ttimer_overflow_count_overflow = 0;\n')
instf.write('}\n\n')
instf.write('ISR(TIMER0_OVF_vect){\n')
instf.write('\ttimer_overflow_count++;\n')
instf.write('\tif (timer_overflow_count >= 32000){\n')
instf.write('\t\ttimer_overflow_count_overflow++;\n')
instf.write('\t\ttimer_overflow_count = 0;\n')
instf.write('\t}\n')
instf.write('}\n')
with open(filename, 'r') as file:
lines = file.readlines()
for i in range(len(lines)):
change = False
pos = None
main = False
for func in functions:
if i == func['start_line']:
if func['name'] == 'main':
main = True
change = True
pos = 'start'
if i == func['end_line']:
change = True
pos = 'end'
if change:
if pos == 'start':
if main:
prepare_comm = 'uart_init();\nstdout = &uart_output;\n_delay_ms(3000);\nputs("=======:inicio:=======");\nputs("=======:inicio:=======");\nputs("=======:inicio:=======");\ntimer0_init();\n'
lines [i] = lines[i] + prepare_comm + '\n' + getEntryFunctionCode() + '\n'
else:
lines [i] = lines[i] + getEntryFunctionCode() + '\n'
if pos == 'end':
lines [i] = getExitFunctionCode() + '\n' + lines[i]
for func in functions:
for i in range(func['start_line'], func['end_line']):
if 'return' in lines[i] :
lines [i] = getExitFunctionCode() + '\n' + lines[i]
instf.writelines(lines)
instf.close()
return functions