sqlmap/lib/core/profiling.py

92 lines
3.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python2
2010-09-15 16:59:51 +04:00
"""
2013-01-18 18:07:51 +04:00
Copyright (c) 2006-2013 sqlmap developers (http://sqlmap.org/)
2010-10-15 03:18:29 +04:00
See the file 'doc/COPYING' for copying permission
2010-09-15 16:59:51 +04:00
"""
import codecs
import os
import cProfile
2010-10-11 18:38:04 +04:00
from lib.core.common import getUnicode
2010-09-15 16:59:51 +04:00
from lib.core.data import logger
from lib.core.data import paths
2011-01-30 14:36:03 +03:00
from lib.core.settings import UNICODE_ENCODING
2010-09-15 16:59:51 +04:00
def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
2010-09-15 17:28:56 +04:00
"""
This will run the program and present profiling data in a nice looking graph
"""
2011-06-08 19:31:27 +04:00
2010-09-15 16:59:51 +04:00
try:
2012-07-14 19:01:04 +04:00
from thirdparty.gprof2dot import gprof2dot
from thirdparty.xdot import xdot
2010-09-15 16:59:51 +04:00
import gobject
import gtk
import pydot
except ImportError, e:
2011-05-30 12:33:01 +04:00
errMsg = "profiling requires third-party libraries (%s). " % getUnicode(e, UNICODE_ENCODING)
2011-06-08 19:31:27 +04:00
errMsg += "Quick steps:%s" % os.linesep
2012-03-12 17:27:07 +04:00
errMsg += "1) sudo apt-get install python-pydot python-pyparsing python-profiler graphviz"
2010-09-15 16:59:51 +04:00
logger.error(errMsg)
2011-05-30 12:33:01 +04:00
2010-09-15 16:59:51 +04:00
return
if profileOutputFile is None:
profileOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.raw")
if dotOutputFile is None:
dotOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.dot")
if imageOutputFile is None:
imageOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.png")
if os.path.exists(profileOutputFile):
os.remove(profileOutputFile)
if os.path.exists(dotOutputFile):
os.remove(dotOutputFile)
if os.path.exists(imageOutputFile):
os.remove(imageOutputFile)
2011-04-30 17:20:05 +04:00
infoMsg = "profiling the execution into file %s" % profileOutputFile
2010-09-15 16:59:51 +04:00
logger.info(infoMsg)
# Start sqlmap main function and generate a raw profile file
cProfile.run("start()", profileOutputFile)
infoMsg = "converting profile data into a dot file '%s'" % dotOutputFile
logger.info(infoMsg)
# Create dot file by using extra/gprof2dot/gprof2dot.py
# http://code.google.com/p/jrfonseca/wiki/Gprof2Dot
2011-01-30 14:36:03 +03:00
dotFilePointer = codecs.open(dotOutputFile, 'wt', UNICODE_ENCODING)
2010-09-15 16:59:51 +04:00
parser = gprof2dot.PstatsParser(profileOutputFile)
profile = parser.parse()
2013-01-10 16:18:44 +04:00
profile.prune(0.5 / 100.0, 0.1 / 100.0)
2010-09-15 16:59:51 +04:00
dot = gprof2dot.DotWriter(dotFilePointer)
dot.graph(profile, gprof2dot.TEMPERATURE_COLORMAP)
dotFilePointer.close()
infoMsg = "converting dot file into a graph image '%s'" % imageOutputFile
logger.info(infoMsg)
# Create graph image (png) by using pydot (python-pydot)
# http://code.google.com/p/pydot/
pydotGraph = pydot.graph_from_dot_file(dotOutputFile)
pydotGraph.write_png(imageOutputFile)
infoMsg = "displaying interactive graph with xdot library"
logger.info(infoMsg)
# Display interactive Graphviz dot file by using extra/xdot/xdot.py
# http://code.google.com/p/jrfonseca/wiki/XDot
win = xdot.DotWindow()
win.connect('destroy', gtk.main_quit)
win.set_filter("dot")
win.open_file(dotOutputFile)
gobject.timeout_add(1000, win.update, dotOutputFile)
gtk.main()