|
14 | 14 | from lib.core.data import paths |
15 | 15 | from lib.core.settings import UNICODE_ENCODING |
16 | 16 |
|
17 | | -def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None): |
| 17 | +def profile(profileOutputFile=None): |
18 | 18 | """ |
19 | 19 | This will run the program and present profiling data in a nice looking graph |
20 | 20 | """ |
21 | 21 |
|
22 | | - try: |
23 | | - __import__("gobject") |
24 | | - from thirdparty.gprof2dot import gprof2dot |
25 | | - from thirdparty.xdot import xdot |
26 | | - import gtk |
27 | | - import pydot |
28 | | - except ImportError as ex: |
29 | | - errMsg = "profiling requires third-party libraries ('%s') " % getSafeExString(ex) |
30 | | - errMsg += "(Hint: 'sudo apt install python-pydot python-pyparsing python-profiler graphviz')" |
31 | | - logger.error(errMsg) |
32 | | - |
33 | | - return |
34 | | - |
35 | 22 | if profileOutputFile is None: |
36 | 23 | profileOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.raw") |
37 | 24 |
|
38 | | - if dotOutputFile is None: |
39 | | - dotOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.dot") |
40 | | - |
41 | | - if imageOutputFile is None: |
42 | | - imageOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.png") |
43 | | - |
44 | 25 | if os.path.exists(profileOutputFile): |
45 | 26 | os.remove(profileOutputFile) |
46 | 27 |
|
47 | | - if os.path.exists(dotOutputFile): |
48 | | - os.remove(dotOutputFile) |
49 | | - |
50 | | - if os.path.exists(imageOutputFile): |
51 | | - os.remove(imageOutputFile) |
52 | | - |
53 | | - infoMsg = "profiling the execution into file '%s'" % profileOutputFile |
54 | | - logger.info(infoMsg) |
55 | | - |
56 | 28 | # Start sqlmap main function and generate a raw profile file |
57 | 29 | cProfile.run("start()", profileOutputFile) |
58 | 30 |
|
59 | | - infoMsg = "converting profile data into a dot file '%s'" % dotOutputFile |
| 31 | + infoMsg = "execution profiled and stored into file '%s' (e.g. 'gprof2dot -f pstats %s | dot -Tpng -o /tmp/sqlmap_profile.png')" % (profileOutputFile, profileOutputFile) |
60 | 32 | logger.info(infoMsg) |
61 | | - |
62 | | - # Create dot file by using extra/gprof2dot/gprof2dot.py |
63 | | - # http://code.google.com/p/jrfonseca/wiki/Gprof2Dot |
64 | | - dotFilePointer = codecs.open(dotOutputFile, 'wt', UNICODE_ENCODING) |
65 | | - parser = gprof2dot.PstatsParser(profileOutputFile) |
66 | | - profile = parser.parse() |
67 | | - profile.prune(0.5 / 100.0, 0.1 / 100.0) |
68 | | - dot = gprof2dot.DotWriter(dotFilePointer) |
69 | | - dot.graph(profile, gprof2dot.TEMPERATURE_COLORMAP) |
70 | | - dotFilePointer.close() |
71 | | - |
72 | | - infoMsg = "converting dot file into a graph image '%s'" % imageOutputFile |
73 | | - logger.info(infoMsg) |
74 | | - |
75 | | - # Create graph image (png) by using pydot (python-pydot) |
76 | | - # http://code.google.com/p/pydot/ |
77 | | - pydotGraph = pydot.graph_from_dot_file(dotOutputFile) |
78 | | - |
79 | | - # Reference: http://stackoverflow.com/questions/38176472/graph-write-pdfiris-pdf-attributeerror-list-object-has-no-attribute-writ |
80 | | - if isinstance(pydotGraph, list): |
81 | | - pydotGraph = pydotGraph[0] |
82 | | - |
83 | | - try: |
84 | | - pydotGraph.write_png(imageOutputFile) |
85 | | - except OSError: |
86 | | - errMsg = "profiling requires graphviz installed " |
87 | | - errMsg += "(Hint: 'sudo apt install graphviz')" |
88 | | - logger.error(errMsg) |
89 | | - else: |
90 | | - infoMsg = "displaying interactive graph with xdot library" |
91 | | - logger.info(infoMsg) |
92 | | - |
93 | | - # Display interactive Graphviz dot file by using extra/xdot/xdot.py |
94 | | - # http://code.google.com/p/jrfonseca/wiki/XDot |
95 | | - win = xdot.DotWindow() |
96 | | - win.connect('destroy', gtk.main_quit) |
97 | | - win.set_filter("dot") |
98 | | - win.open_file(dotOutputFile) |
99 | | - gtk.main() |
0 commit comments