|
22 | 22 | Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
23 | 23 | """ |
24 | 24 |
|
| 25 | +import cProfile |
25 | 26 | import os |
26 | 27 | import random |
27 | 28 | import re |
|
54 | 55 | from lib.core.exception import sqlmapSyntaxException |
55 | 56 | from lib.core.settings import DESCRIPTION |
56 | 57 | from lib.core.settings import IS_WIN |
| 58 | +from lib.core.settings import PLATFORM |
57 | 59 | from lib.core.settings import SITE |
58 | 60 | from lib.core.settings import SQL_STATEMENTS |
59 | 61 | from lib.core.settings import SUPPORTED_DBMS |
@@ -1087,45 +1089,48 @@ def isBase64EncodedString(subject): |
1087 | 1089 | def isHexEncodedString(subject): |
1088 | 1090 | return re.match(r"\A[0-9a-fA-F]+\Z", subject) is not None |
1089 | 1091 |
|
1090 | | -def profile(profileOutputFile='sqlmap.profile', imageOutputFile='profile.png'): |
1091 | | - import cProfile |
1092 | | - cProfile.run("start()", profileOutputFile) |
| 1092 | +def profile(profileOutputFile=None, imageOutputFile=None): |
| 1093 | + if profileOutputFile is None: |
| 1094 | + profileOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.raw") |
1093 | 1095 |
|
1094 | | - graphScriptPath = os.path.join(paths.SQLMAP_EXTRAS_PATH, 'gprof2dot', 'gprof2dot.py') |
| 1096 | + if imageOutputFile is None: |
| 1097 | + imageOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.png") |
1095 | 1098 |
|
1096 | | - infoMsg = "converting profile data to a graph image." |
1097 | | - logger.info(infoMsg) |
| 1099 | + if os.path.exists(profileOutputFile): |
| 1100 | + os.remove(profileOutputFile) |
1098 | 1101 |
|
1099 | 1102 | if os.path.exists(imageOutputFile): |
1100 | 1103 | os.remove(imageOutputFile) |
1101 | 1104 |
|
1102 | | - msg = subprocess.Popen('python %s -f pstats %s | dot -Tpng -o %s' % (graphScriptPath, profileOutputFile, imageOutputFile), shell=True, stderr=subprocess.PIPE).stderr.read() |
| 1105 | + infoMsg = "profiling the execution into file %s" % profileOutputFile |
| 1106 | + logger.info(infoMsg) |
1103 | 1107 |
|
1104 | | - if msg: |
1105 | | - errMsg = "there was an error while converting ('%s'), " % msg.strip() |
1106 | | - errMsg += "but you can still find raw profile data " |
1107 | | - errMsg += "inside file '%s'" % profileOutputFile |
1108 | | - logger.error(errMsg) |
1109 | | - else: |
1110 | | - try: |
1111 | | - if os.name == 'mac': |
1112 | | - subprocess.call(('open', imageOutputFile)) |
1113 | | - elif os.name == 'posix': |
1114 | | - subprocess.call(('xdg-open', imageOutputFile)) |
1115 | | - elif os.name == 'nt': |
1116 | | - subprocess.call(('start', imageOutputFile)) |
1117 | | - except: |
1118 | | - pass |
| 1108 | + cProfile.run("start()", profileOutputFile) |
1119 | 1109 |
|
1120 | | - if os.path.exists(imageOutputFile): |
1121 | | - infoMsg = "done. you can find a graph image inside file '%s'." % imageOutputFile |
1122 | | - logger.info(infoMsg) |
1123 | | - else: |
1124 | | - errMsg = "there was an error while converting, " |
| 1110 | + infoMsg = "converting profile data into a graph image, %s" % imageOutputFile |
| 1111 | + logger.info(infoMsg) |
| 1112 | + |
| 1113 | + graphScriptPath = os.path.join(paths.SQLMAP_EXTRAS_PATH, 'gprof2dot', 'gprof2dot.py') |
| 1114 | + stderr = subprocess.Popen('python %s -f pstats %s | dot -Tpng -o %s' % (graphScriptPath, profileOutputFile, imageOutputFile), shell=True, stderr=subprocess.PIPE).stderr.read() |
| 1115 | + |
| 1116 | + if stderr or not os.path.exists(imageOutputFile): |
| 1117 | + errMsg = "there was an error while converting ('%s')" % stderr.strip() |
1125 | 1118 | errMsg += "but you can still find raw profile data " |
1126 | 1119 | errMsg += "inside file '%s'" % profileOutputFile |
1127 | 1120 | logger.error(errMsg) |
1128 | 1121 |
|
| 1122 | + return |
| 1123 | + |
| 1124 | + try: |
| 1125 | + if PLATFORM == 'mac': |
| 1126 | + subprocess.call(('open', imageOutputFile)) |
| 1127 | + elif PLATFORM == 'posix': |
| 1128 | + subprocess.call(('xdg-open', imageOutputFile)) |
| 1129 | + elif PLATFORM == 'nt': |
| 1130 | + subprocess.call(('start', imageOutputFile)) |
| 1131 | + except: |
| 1132 | + pass |
| 1133 | + |
1129 | 1134 | def getConsoleWidth(default=80): |
1130 | 1135 | width = None |
1131 | 1136 |
|
|
0 commit comments