-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstrace_analyser
executable file
·227 lines (195 loc) · 9.04 KB
/
strace_analyser
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import sys
import getopt
import re
import traceback
import logging
import os
import io
from optparse import OptionParser, OptionValueError
from straceParserLib.StraceParser import StraceParser
from collections import defaultdict
def parsePluginOption(pluginOptionStr):
""" Parse the plugin option str into dict
e.g. -o StatFileIO.output=/tmp/IO.out,StatProcessTree.output=/tmp/process.out
Return: {"StatFileIO": {"output": "/tmp/IO.out"},
"StatProcessTree: {"output": "/tmp/process.out"}
}
if there is no "." in the option key, we will put it in the "default".
"""
returnDict = defaultdict(dict)
if not pluginOptionStr:
return returnDict
for optionStr in pluginOptionStr.split(","):
try:
key, value = optionStr.split("=")
if "." in key:
pluginName, keyName = key.split('.')
else:
pluginName, keyName = "default", key
returnDict[pluginName][keyName] = value
except ValueError as e:
logging.warning("Cannot parse plugin option '%s', ignore." % optionStr)
return returnDict
def importPlugin(pluginname, name):
""" Import a plugin
similar to "from pluginname import name"
"""
try:
plugin = __import__(pluginname, globals(), locals(), [name])
except ImportError:
return None
return getattr(plugin, name)
def main():
# parse command line options
usage = "\n".join(["Usage: %prog [options] -e [plugin1,plugin2,...] [<filename>| - ]",
"",
"Example: %prog -e StatFileIO strace.out",
" %prog -e StatFileIO -o output=/tmp/StatFileIO.txt strace.out",
" %prog -e StatFileIO,StatFutex -o StatFileIO.output=/tmp/FileIO.txt,StatFutex.output=/tmp/Futex.txt strace.out",
" strace -o >(%prog -e StatFileIO -) ls > /dev/null"
])
optionParser = OptionParser(usage=usage)
optionParser.add_option("-t", action="count", dest="withtime",
help="have time in strace (disable autodetect, use -T, -f, -t, -tt or -ttt to match with your strace output)")
optionParser.add_option("-f", action="store_true", dest="withpid", help="have pid in strace (disable autodetect)")
optionParser.add_option("-T", action="store_true", dest="withtimespent", help="have time spent in strace (disable autodetect)")
optionParser.add_option("-l", "--list-plugins", action="store_true", dest="listplugins", help="list all plugins")
optionParser.add_option("-e", "--enable-plugins", action="store", type="string", dest="enableplugins",
help="enable these plugins only (e.g. plugin1,plugin2,plugin3)")
optionParser.add_option("-o", "--plugin-options", action="store", type="string", dest="plugin_options",
help=" ".join(["Specify plugin options by plugin_name.key=value,",
"multiple options can be separate by comma (see example above).",
"plugin_name can be omitted if only 1 plugin is enabled."]))
optionParser.add_option("--list-plugin-options", action="store_true", dest="list_plugin_options", help="Show all plugin options")
(options, args) = optionParser.parse_args()
# List plugins
if options.listplugins or options.list_plugin_options:
print "=== Stat plugin list ==="
pluginPath = os.path.join(sys.path[0], "statPlugins")
plugins = os.listdir(pluginPath)
plugins.sort()
for plug in plugins:
plugbase = plug[:-3]
if not plug[-3:] == '.py' or plugbase == "__init__" or plugbase == "StatBase":
continue
try:
pluginClass = importPlugin("statPlugins." + plugbase, plugbase)
except:
print "Warning: plugin %s does not install, skipping" % plug
print plugbase, ":", pluginClass.__doc__
# show plugin options
if options.list_plugin_options:
optionHelp = pluginClass().optionHelp()
if len(optionHelp) == 0:
print " "*8 + "(No option for this plugin)"
else:
for name, desc in optionHelp.iteritems():
print " "*8 + "%s.%s: %s" % (pluginClass.__name__, name, desc)
print ""
# just listed all plugin and then exit
exit(0)
# Not list plugin then we will need the strace file
if len(args) < 1:
print "Error: Filename is missing, exit."
optionParser.print_help()
exit(1)
straceFile = args[0]
if straceFile == '-':
reader = io.open(sys.stdin.fileno())
else:
try:
reader = io.open(straceFile)
except IOError as e:
print e
exit(1)
# init StraceParser
straceParser = StraceParser()
straceOptions = {}
if options.withpid or options.withtime or options.withtimespent:
straceOptions["havePid"] = options.withpid
# options.withtime contain the number of -t (e.g. -t = 1, -tt = 2. -ttt = 3)
# convert it back to t, tt, or ttt
if options.withtime:
straceOptions["haveTime"] = "t" * options.withtime
else:
straceOptions["haveTime"] = ""
straceOptions["haveTimeSpent"] = options.withtimespent
else:
straceOptions = straceParser.autoDetectFormat(reader)
if not straceOptions:
logging.warning("Auto detect line format failed. Suggest using -t,-f,-T to specify.")
exit(1)
enablePluginList = []
if options.enableplugins:
enablePluginList = options.enableplugins.split(",")
if len(enablePluginList) == 0:
print "No plugin is specified. Exit."
exit(0)
# parse plugin options
pluginOptions = parsePluginOption(options.plugin_options)
if "default" in pluginOptions:
if len(enablePluginList) != 1:
print "Have more than 1 plugin but plugin name doesn't specified on some plugin options."
exit(1)
pluginOptions[enablePluginList[0]] = pluginOptions["default"]
# A sensible check: all the plugin named in plugin options should be enabled, avoid typo
for key in pluginOptions:
if key is not "default" and key not in enablePluginList:
print "Plugin option is specified for plugin %s, but the plugin is not enabled. (typo?)" % key
exit(1)
# Load enabled plugins
statObjList = []
for plug in enablePluginList:
pluginClass = importPlugin("statPlugins." + plug, plug)
if not pluginClass:
print "Cannot install plugin %s, skipping" % plug
continue
statObj = pluginClass()
if not statObj.isOperational(straceOptions): # if it is operational under current strace option
print "plugin %s is not operational under this strace options, skipping" % plug
continue
# A sensible check: the plugin option should be in the dict of optionHelp
option = pluginOptions.get(plug, {})
optionHelpDict = statObj.optionHelp()
for optionName in option:
if optionName not in optionHelpDict:
print "option '%s' doesn't exist for plugin %s" % (optionName, plug)
exit(1)
# setOption for this plugin
if not statObj.setOption(option):
print "plugin %s add option failure" % plug
exit(1)
statObjList.append(statObj) # new an object from plugin class and put into list
if len(statObjList) == 0:
print "No plugin is loaded. Exit."
exit(1)
# register plugins to parser
for obj in statObjList:
hooks = obj.getSyscallHooks()
if hooks:
for syscall, func in hooks.iteritems():
straceParser.registerSyscallHook(syscall, func)
hooks = obj.getRawSyscallHooks()
if hooks:
for syscall, func in hooks.iteritems():
straceParser.registerRawSyscallHook(syscall, func)
## Go ahead and parse the file
straceParser.startParse(reader, straceOptions)
## print the result of the stat plugins
for obj in statObjList:
obj.printOutput()
reader.close()
if __name__ == "__main__":
main()