| 1 |
|
| 2 |
|
| 3 |
import os, sys, re |
| 4 |
import subprocess |
| 5 |
import shutil |
| 6 |
import tempfile |
| 7 |
|
| 8 |
GRAPH_ON_TYPES = ['function', 'macro'] |
| 9 |
|
| 10 |
def _eintr_retry_call(func, *args): |
| 11 |
while True: |
| 12 |
try: |
| 13 |
return func(*args) |
| 14 |
except OSError as e: |
| 15 |
if e.errno == errno.EINTR: |
| 16 |
continue |
| 17 |
raise |
| 18 |
|
| 19 |
def ct_override(filename): |
| 20 |
pattern = os.getenv('SEASCOPE_CTAGS_OVERRIDE_PATTERN') |
| 21 |
delim1 = os.getenv('SEASCOPE_CTAGS_OVERRIDE_DELIM_START') |
| 22 |
delim2 = os.getenv('SEASCOPE_CTAGS_OVERRIDE_DELIM_END') |
| 23 |
if not (pattern and delim1 and delim2): |
| 24 |
return |
| 25 |
|
| 26 |
args = ['grep', '-En', "%s" % pattern, "%s" % filename] |
| 27 |
try: |
| 28 |
proc = subprocess.Popen(args, stdout=subprocess.PIPE) |
| 29 |
(out_data, err_data) = _eintr_retry_call(proc.communicate) |
| 30 |
out_data = out_data.decode() |
| 31 |
out_data = out_data.split('\n') |
| 32 |
except Exception as e: |
| 33 |
out_data = [ |
| 34 |
'Failed to run ct override cmd\tignore\t0;\t ', |
| 35 |
'cmd: %s\tignore\t0;\t ' % ' '.join(args), |
| 36 |
'error: %s\tignore\t0;\t ' % str(e), |
| 37 |
'ctags not installed ?\tignore\t0;\t ', |
| 38 |
] |
| 39 |
res = {} |
| 40 |
for line in out_data: |
| 41 |
if (line == ''): |
| 42 |
break |
| 43 |
line = line.split(':', 1) |
| 44 |
num = line[0] |
| 45 |
inx1 = line[1].find(delim1) + 1 |
| 46 |
inx2 = line[1].find(delim2) |
| 47 |
sym = line[1][inx1:inx2] |
| 48 |
res[num] = [sym, num, 'function'] |
| 49 |
return res |
| 50 |
|
| 51 |
|
| 52 |
def ct_cmdForFile(f): |
| 53 |
|
| 54 |
|
| 55 |
ct_args = 'ctags -n -u --fields=+K -f -' |
| 56 |
opt_I_file = os.getenv('SEASCOPE_CTAGS_OPT_I_FILE') |
| 57 |
if opt_I_file and os.path.isfile(opt_I_file): |
| 58 |
ct_args += ' -I ' + opt_I_file |
| 59 |
if os.path.isdir(f): |
| 60 |
cmd = ct_args + ' -R' |
| 61 |
return cmd |
| 62 |
suffix_cmd_map = [] |
| 63 |
custom_map = os.getenv('SEASCOPE_CTAGS_SUFFIX_CMD_MAP') |
| 64 |
if custom_map: |
| 65 |
custom_map = eval(custom_map) |
| 66 |
suffix_cmd_map += custom_map |
| 67 |
suffix_cmd_map.append( ['', ct_args] ) |
| 68 |
for (suffix, cmd) in suffix_cmd_map: |
| 69 |
if f.endswith(suffix): |
| 70 |
return cmd |
| 71 |
return None |
| 72 |
|
| 73 |
def ct_query(filename): |
| 74 |
override_res = ct_override(filename) |
| 75 |
|
| 76 |
args = ct_cmdForFile(filename) |
| 77 |
args = args.split() |
| 78 |
args.append(filename) |
| 79 |
try: |
| 80 |
proc = subprocess.Popen(args, stdout=subprocess.PIPE) |
| 81 |
(out_data, err_data) = _eintr_retry_call(proc.communicate) |
| 82 |
out_data = out_data.decode() |
| 83 |
out_data = out_data.split('\n') |
| 84 |
except Exception as e: |
| 85 |
out_data = [ |
| 86 |
'Failed to run ctags cmd\tignore\t0;\t ', |
| 87 |
'cmd: %s\tignore\t0;\t ' % ' '.join(args), |
| 88 |
'error: %s\tignore\t0;\t ' % str(e), |
| 89 |
'ctags not installed ?\tignore\t0;\t ', |
| 90 |
] |
| 91 |
res = [] |
| 92 |
for line in out_data: |
| 93 |
if (line == ''): |
| 94 |
break |
| 95 |
line = line.split('\t') |
| 96 |
num = line[2].split(';', 1)[0] |
| 97 |
if override_res: |
| 98 |
override_ent = override_res.pop(num, None) |
| 99 |
if override_ent: |
| 100 |
line[0] = override_ent[0] |
| 101 |
line = [line[0], num, line[3]] |
| 102 |
res.append(line) |
| 103 |
if override_res: |
| 104 |
res += override_res.values() |
| 105 |
res = sorted(res, key=lambda e: int(e[1])) |
| 106 |
return res |
| 107 |
|
| 108 |
class FFgraph: |
| 109 |
def __init__(self): |
| 110 |
self.cs_cmd_base = None |
| 111 |
|
| 112 |
def parse_cs_result(self, text): |
| 113 |
text = re.split(r'\r?\n', text) |
| 114 |
res = [] |
| 115 |
for line in text: |
| 116 |
if line == '': |
| 117 |
continue |
| 118 |
line = line.split(' ', 3) |
| 119 |
line = [line[1], line[0], line[2], line[3]] |
| 120 |
res.append(line) |
| 121 |
return res |
| 122 |
|
| 123 |
def findCalleeList(self, caller, srcFile): |
| 124 |
try: |
| 125 |
cmd = self.cs_cmd_base + [ '-2', caller ] |
| 126 |
if os.path.isdir(srcFile): |
| 127 |
cmd = cmd + [ '-s' ] |
| 128 |
cmd = cmd + [ srcFile ] |
| 129 |
|
| 130 |
|
| 131 |
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 132 |
(output, err_data) = proc.communicate() |
| 133 |
output = output.decode() |
| 134 |
|
| 135 |
|
| 136 |
res = self.parse_cs_result(output) |
| 137 |
res = set([ e[0] for e in res ]) |
| 138 |
except Exception as e: |
| 139 |
print('failed cmd:', cmd, ':', e, file=sys.stderr) |
| 140 |
res = [] |
| 141 |
return res |
| 142 |
|
| 143 |
def generateDotHeader(self, name): |
| 144 |
out = 'digraph "%s" {\n' % name |
| 145 |
out += 'rankdir="LR"\n' |
| 146 |
|
| 147 |
return out |
| 148 |
|
| 149 |
def generateDotFooter(self): |
| 150 |
out = '}\n\n' |
| 151 |
return out |
| 152 |
|
| 153 |
def generateSubgraphHeader(self, name): |
| 154 |
out = '\tsubgraph "%s" {\n' % name |
| 155 |
return out |
| 156 |
|
| 157 |
def generateSubgraphFooter(self): |
| 158 |
out = '\t}\n\n' |
| 159 |
return out |
| 160 |
|
| 161 |
def generateClusterInfoLocal(self, localSym): |
| 162 |
out = '' |
| 163 |
|
| 164 |
out += self.generateSubgraphHeader('cluster_local_sym') |
| 165 |
for sym in localSym: |
| 166 |
out += '\t\t%s;\n' % sym |
| 167 |
out += self.generateSubgraphFooter() |
| 168 |
return out |
| 169 |
|
| 170 |
def generateClusterInfoExternal(self, externalSym): |
| 171 |
out = '' |
| 172 |
|
| 173 |
out += self.generateSubgraphHeader('cluster_external_sym') |
| 174 |
for sym in externalSym: |
| 175 |
out += '\t\t%s;\n' % sym |
| 176 |
out += self.generateSubgraphFooter() |
| 177 |
return out |
| 178 |
|
| 179 |
def generateClusterInfo(self, localSym, externalSym): |
| 180 |
out = '' |
| 181 |
|
| 182 |
out += self.generateClusterInfoExternal(externalSym) |
| 183 |
return out |
| 184 |
|
| 185 |
def generateDotInput(self, f, callerCalleInfo, is_extern): |
| 186 |
dotInput = '' |
| 187 |
dotInput += self.generateDotHeader(f) |
| 188 |
(localSym, externalSym, callerCalleInfo) = callerCalleInfo |
| 189 |
if is_extern: |
| 190 |
dotInput += self.generateClusterInfo(localSym, externalSym) |
| 191 |
|
| 192 |
for (caller, localCalleeList, externCalleeList) in callerCalleInfo: |
| 193 |
for callee in localCalleeList: |
| 194 |
dotInput += '\t"%s" -> "%s";\n' % (caller, callee) |
| 195 |
if not is_extern: |
| 196 |
continue |
| 197 |
for callee in externCalleeList: |
| 198 |
style = '[style=dotted]' |
| 199 |
dotInput += '\t"%s" -> "%s" %s;\n' % (caller, callee, style) |
| 200 |
dotInput += self.generateDotFooter() |
| 201 |
return dotInput |
| 202 |
|
| 203 |
def _generateDotGraph(self, f, is_extern, gOut): |
| 204 |
try: |
| 205 |
res = self.getCallerCalleeInfo(f, is_extern) |
| 206 |
dotInput = self.generateDotInput(f, res, is_extern) |
| 207 |
except Exception as e: |
| 208 |
print(e, file=sys.stderr) |
| 209 |
|
| 210 |
if not gOut: |
| 211 |
print(dotInput) |
| 212 |
return None |
| 213 |
|
| 214 |
args = ['dot', '-Tsvg'] |
| 215 |
try: |
| 216 |
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 217 |
(svg_data, err_data) = p.communicate(dotInput.encode()) |
| 218 |
svg_data = svg_data.decode() |
| 219 |
if err_data and err_data.decode() != '': |
| 220 |
print(err_data, '\n', file=sys.stderr) |
| 221 |
|
| 222 |
return svg_data |
| 223 |
except Exception as e: |
| 224 |
print('Failed to run:', ' '.join(args), '\n', file=sys.stderr) |
| 225 |
print('dotInput:', dotInput, '\n', file=sys.stderr) |
| 226 |
print(e, '\n', file=sys.stderr) |
| 227 |
|
| 228 |
|
| 229 |
def generateDotGraph(self, f, is_extern, gOut): |
| 230 |
tmp_cs_dir = tempfile.mkdtemp(prefix='ffgraph_') |
| 231 |
if not tmp_cs_dir: |
| 232 |
print('generateDotGraph: failed to create tmp dir') |
| 233 |
return |
| 234 |
self.cs_cmd_base = [ 'cscope', '-L', '-f', os.path.join(tmp_cs_dir, 'cscope.out') ] |
| 235 |
|
| 236 |
svg_data = None |
| 237 |
|
| 238 |
try: |
| 239 |
svg_data = self._generateDotGraph(f, is_extern, gOut) |
| 240 |
except: |
| 241 |
print('generateDotGraph: failed') |
| 242 |
|
| 243 |
shutil.rmtree(tmp_cs_dir) |
| 244 |
|
| 245 |
return svg_data |
| 246 |
|
| 247 |
def getCallerCalleeInfo(self, f, is_extern): |
| 248 |
ct_out = ct_query(f) |
| 249 |
localSym = dict([ (e[0], e[2]) for e in ct_out ]) |
| 250 |
lSym = [] |
| 251 |
eSym = [] |
| 252 |
res = [] |
| 253 |
visitedCaller = dict() |
| 254 |
for e in ct_out: |
| 255 |
if e[2] in GRAPH_ON_TYPES: |
| 256 |
caller = e[0] |
| 257 |
if caller in visitedCaller: |
| 258 |
continue |
| 259 |
visitedCaller[caller] = True |
| 260 |
calleeList = self.findCalleeList(caller, f) |
| 261 |
localCalleeList = [] |
| 262 |
externCalleeList = [] |
| 263 |
for c in calleeList: |
| 264 |
if c in localSym and localSym[c] in GRAPH_ON_TYPES: |
| 265 |
localCalleeList.append(c) |
| 266 |
continue |
| 267 |
if is_extern: |
| 268 |
externCalleeList.append(c) |
| 269 |
res.append([caller, localCalleeList, externCalleeList]) |
| 270 |
if is_extern: |
| 271 |
if len(localCalleeList) or len(externCalleeList): |
| 272 |
lSym += [caller] + localCalleeList |
| 273 |
eSym += externCalleeList |
| 274 |
else: |
| 275 |
if len(localCalleeList): |
| 276 |
lSym += [caller] + localCalleeList |
| 277 |
return [lSym, eSym, res] |
| 278 |
|
| 279 |
def ff_graph(f, is_extern, gOut=True): |
| 280 |
|
| 281 |
|
| 282 |
ffg = FFgraph() |
| 283 |
|
| 284 |
svg_data = ffg.generateDotGraph(f, is_extern, gOut) |
| 285 |
return svg_data |
| 286 |
|
| 287 |
if __name__ == '__main__': |
| 288 |
import optparse |
| 289 |
usage = "usage: %prog [options] (-d <code_dir/file> | -p <idutils_proj>) (-e) [symbol]" |
| 290 |
op = optparse.OptionParser(usage=usage) |
| 291 |
op.add_option("-e", action="store_true", dest="is_extern", help="include external sym") |
| 292 |
op.add_option("-d", "--codedir", dest="code_dir", help="Code dir", metavar="CODE_DIR") |
| 293 |
op.add_option("-p", "--project", dest="id_path", help="Idutils project dir", metavar="PROJECT") |
| 294 |
(options, args) = op.parse_args() |
| 295 |
|
| 296 |
if (not any([options.code_dir, options.id_path]) or |
| 297 |
all([options.code_dir, options.id_path])): |
| 298 |
print('Specify one among -d or -p', file=sys.stderr) |
| 299 |
sys.exit(-1) |
| 300 |
|
| 301 |
sym = None |
| 302 |
dname = None |
| 303 |
is_extern = False |
| 304 |
if options.is_extern: |
| 305 |
is_extern = True |
| 306 |
if len(args): |
| 307 |
if len(args) != 1: |
| 308 |
print('Please specify only one symbol', file=sys.stderr) |
| 309 |
sys.exit(-2) |
| 310 |
sym = args[0] |
| 311 |
|
| 312 |
svg_data = None |
| 313 |
if options.code_dir: |
| 314 |
dname = options.code_dir |
| 315 |
if not os.path.exists(dname): |
| 316 |
print('"%s": does not exist' % dname, file=sys.stderr) |
| 317 |
sys.exit(-3) |
| 318 |
svg_data = ff_graph(dname, is_extern=is_extern) |
| 319 |
print(svg_data) |
| 320 |
|