aboutsummaryrefslogtreecommitdiff
path: root/tools/autodebugger_c.py
blob: 0876e05412ceca075ed3de36e795491a9e5f01ec (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
'''
Processes a C source file, adding debugging information.

Similar to autodebugger.py, but runs on .c files
'''

import os, sys, re

filename, ofilename = sys.argv[1], sys.argv[2]
f = open(filename, 'r')
data = f.read()
f.close()

lines = data.split('\n')
for i in range(len(lines)):
  m = re.match('^  [ ]*([\w\d \[\]]+) = +([^;]+);$', lines[i])
  if m and (' if ' not in lines[i-1] or '{' in lines[i-1]) and \
           (' if ' not in lines[i+1] or '{' in lines[i+1]) and \
           (' else' not in lines[i-1] or '{' in lines[i-1]) and \
           (' else' not in lines[i+1] or '{' in lines[i+1]):
    var = m.groups(1)[0].rstrip().split(' ')[-1]
    lines[i] += ''' printf("%d:%s=%%d\\n", %s);''' % (i+1, var, var)

f = open(ofilename, 'w')
f.write('\n'.join(lines))
f.close()

print 'Success.'