/
pybugger.py
executable file
·95 lines (82 loc) · 2.42 KB
/
pybugger.py
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
#!/usr/bin/python
# Author: Connor Zanin
# Input: name of C/C++ code file
# Output: C/C++ code file with debugging printf statements
import sys
import string
#################
### FUNCTIONS ###
#################
def create_print(string, num):
return "printf(\"" + sys.argv[1] + "_" + \
string + str(num) + "\\n\");"
def mode_main(lines, i):
global debug_string
global debug_alph
global debug_num
global mode
if lines[i].find('{') > -1:
index = lines[i].find('{') + 1
debug_string += alphabet[debug_alph[debug_string]]
debug_alph[debug_string] = 0
debug_num[debug_string] = 0
if "switch" in lines[i]:
mode = _switch
print lines[i]
return
print lines[i][:index] + \
create_print(debug_string, debug_num[debug_string]) + \
lines[i][index:]
elif lines[i].find('}') > -1:
index = lines[i].find('}') + 1
debug_string = debug_string[:-1]
if debug_string != "":
debug_alph[debug_string] += 1
debug_num[debug_string] += 1
print lines[i][:index] + \
create_print(debug_string, debug_num[debug_string]) + \
lines[i][index:]
else:
print lines[i]
else:
print lines[i]
def mode_switch(lines, i):
global debug_string
global debug_alph
global debug_num
global mode
if "case" in lines[i] or "default" in lines[i]:
print lines[i] + create_print(debug_string, debug_num[debug_string])
debug_alph[debug_string] += 1
debug_num[debug_string] += 1
else:
print lines[i]
if lines[i+1].find('}') > -1:
mode = _main
##############
### SCRIPT ###
##############
# miscellaneous variables
_main = 0
_switch = 1
mode = _main
# open and read lines of code file
inf = open(sys.argv[1])
lines = inf.readlines()
inf.close()
# pre-process file
for i in xrange(len(lines)):
lines[i] = lines[i].strip('\n')
# generate identifiers
alphabet = string.ascii_lowercase
# initialize stack
debug_string = "" # current location in stack
debug_alph = dict() # index of next char to use
debug_num = dict() # number associated with each location in stack
debug_alph[""] = 0
# insert printf statements
for i in xrange(len(lines)):
if mode == _main:
mode_main(lines, i)
elif mode == _switch:
mode_switch(lines, i)