-
Notifications
You must be signed in to change notification settings - Fork 16
/
symbolicationWebService.py
executable file
·284 lines (229 loc) · 8.6 KB
/
symbolicationWebService.py
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python
from symLogging import LogDebug, LogError, LogMessage, SetLoggingOptions, SetDebug, CheckDebug
from symFileManager import SymFileManager
from symbolicationRequest import SymbolicationRequest
from concurrent.futures import ProcessPoolExecutor as Pool
import sys
import os
import json
import signal
import tempfile
import ConfigParser
from collections import OrderedDict as _default_dict
import tornado.gen
from tornado.ioloop import IOLoop, PeriodicCallback
from tornado.web import Application, RequestHandler, url
# Report errors while symLogging is not configured yet
import logging
# .SYM cache manager
gSymFileManager = None
# Pool of symbolication workers
gPool = None
# Default config options
gOptions = {
# IP address to listen on
"hostname": "0.0.0.0",
# TCP port to listen on
"portNumber": 80,
# Trace-level logging (verbose)
"enableTracing": 0,
# Fallback server if symbol is not found locally
"remoteSymbolServer": "",
# Maximum number of symbol files to keep in memory
"maxMemCacheFiles": 400,
# Paths to .SYM files
"symbolPaths": [
# Default to empty so users don't have to list anything in their config
# file to override the defaults.
],
# URLs to symbol stores
"symbolURLs": [
],
# Symbol files cache path
"diskCachePath": os.path.join(tempfile.gettempdir(), 'snappy', 'cache'),
# Maximum number of cache files
"maxDiskCacheFiles": 1500
}
# Use a new class to make defaults case-sensitive
class CaseSensitiveConfigParser(ConfigParser.SafeConfigParser):
superClass = ConfigParser.SafeConfigParser
def __init__(self, defaults=None, dict_type=_default_dict,
allow_no_value=False):
self.optionxform = str
self.superClass.__init__(self, defaults, dict_type, allow_no_value)
def items(self, section, raw=False, vars=None):
defaults = self.defaults()
if vars is not None:
defaults.update(vars)
# Remove default items from the result
return filter(
lambda item: item[0] not in defaults,
self.superClass.items(self, section, raw, vars))
def initializeSubprocess(options):
global gSymFileManager
# Ignore ctrl-c in the subprocess
signal.signal(signal.SIGINT, signal.SIG_IGN)
# Setup logging in the child process
if "logPath" in options["Log"]:
options["Log"]["logPath"] = os.path.join(options["Log"]["logPath"], "subprocess")
SetLoggingOptions(options["Log"])
# Create the .SYM cache manager singleton
gSymFileManager = SymFileManager(options)
def processSymbolicationRequest(rawRequest, remoteIp):
decodedRequest = json.loads(rawRequest)
request = SymbolicationRequest(gSymFileManager, decodedRequest, remoteIp)
if not request.isValidRequest:
LogDebug("Unable to parse request", remoteIp)
return None
response = { 'symbolicatedStacks': [] }
for stackIndex in range(len(request.stacks)):
symbolicatedStack = request.Symbolicate(stackIndex)
# Free up memory ASAP
request.stacks[stackIndex] = []
response['symbolicatedStacks'].append(symbolicatedStack)
response['knownModules'] = request.knownModules[:]
if not request.includeKnownModulesInResponse:
response = response['symbolicatedStacks']
request.Reset()
return json.dumps(response)
class DebugHandler(RequestHandler):
def get(self, path):
self.post(path)
def post(self, path):
if self.request.remote_ip == "127.0.0.1":
SetDebug(path == "debug")
self.set_status(200)
self.set_header("Content-type", "application/json")
class SymbolHandler(RequestHandler):
def LogDebug(self, string):
LogDebug(string, self.remoteIp)
def LogMessage(self, string):
LogMessage(string, self.remoteIp)
def LogError(self, string):
LogError(string, self.remoteIp)
def sendHeaders(self, errorCode):
self.set_status(errorCode)
self.set_header("Content-type", "application/json")
def prepare(self):
xForwardIp = self.request.headers.get("X-Forwarded-For")
self.remoteIp = self.request.remote_ip if not xForwardIp else xForwardIp
def head(self):
self.sendHeaders(200)
def get(self, path):
return self.post(path)
@tornado.gen.coroutine
def post(self, path):
self.LogDebug("Received request with path '{}'".format(path))
try:
CheckDebug()
requestBody = self.request.body
# vdjeric: temporary hack to stop a spammy request
if "\"Bolt\"" in requestBody:
self.sendHeaders(400)
return
self.LogDebug("Request body: " + requestBody)
response = yield gPool.submit(
processSymbolicationRequest,
requestBody,
self.remoteIp)
if response is None:
self.LogDebug("Unable to parse request")
self.sendHeaders(400)
return
except Exception as e:
self.LogDebug("Unable to parse request body: " + str(e))
# Ensure connection is back in blocking mode so rfile/wfile can be used safely
self.sendHeaders(400)
return
try:
self.sendHeaders(200)
self.LogDebug("Response: " + response)
self.write(response)
except Exception as e:
self.LogError("Exception in post: " + str(e))
def SetConfigOptions(options):
for (option, value) in options:
if option not in gOptions:
logging.error("Unknown config option '" + option + "' in the 'General' section of config file")
return False
elif type(gOptions[option]) == int:
try:
value = int(value)
except ValueError:
logging.error("Integer value expected for config option '" + option + "'")
return False
gOptions[option] = value
return True
def ReadConfigFile():
if len(sys.argv) == 1:
return True
elif len(sys.argv) > 2:
logging.error("Usage: symbolicationWebService.py [<config file>]")
return False
elif len(sys.argv) == 2:
try:
# ConfigParser uses the pattern %(<variable>)<type> for variable substitution,
# so '%' found in environment variable values will raise an error. We replace
# '%' by '%%' to make the parser understand it is literal character.
environ = {key:value.replace(r"%", r"%%") for key, value in os.environ.iteritems()}
configParser = CaseSensitiveConfigParser(environ)
configFile = open(sys.argv[1], "r")
configParser.readfp(configFile)
configFile.close()
except ConfigParser.Error as e:
logging.error("Unable to parse config file %s: %s", sys.argv[1], e)
except Exception as e:
logging.error("Unable to open config file %s: %s", sys.argv[1], e)
return False
# Check for section names
if not set(["General", "Log"]).issubset(set(configParser.sections())):
logging.error("'General' and 'Log' sections are mandatory in the config file")
return False
if not all(map(
lambda section: SetConfigOptions(configParser.items(section)),
("General", "DiskCache", "MemoryCache"))):
return False
# Get the list of symbol paths from the config file
if configParser.has_section("SymbolPaths"):
configPaths = configParser.items("SymbolPaths")
if configPaths:
# Drop defaults if config file entries exist
gOptions["symbolPaths"] = [path for name, path in configPaths]
# Get the list of symbol URLs from the config file
if configParser.has_section("SymbolURLs"):
configURLs = configParser.items("SymbolURLs")
if configURLs:
gOptions["symbolURLs"] = [url for name, url in configURLs if name not in environ]
gOptions["Log"] = dict(configParser.items("Log"))
return True
def Main():
global gSymFileManager, gOptions, gPool
if not ReadConfigFile():
return 1
# In a perfect world, we could create a process per cpu core.
# But then we'd have to deal with cache sharing
gPool = Pool(1)
gPool.submit(initializeSubprocess, gOptions)
# Setup logging in the parent process.
# Ensure this is called after the call to initializeSubprocess to
# avoid duplicate messages in Unix systems.
SetLoggingOptions(gOptions["Log"])
LogMessage("Starting server with the following options:\n" + str(gOptions))
app = Application([
url(r'/(debug)', DebugHandler),
url(r'/(nodebug)', DebugHandler),
url(r"(.*)", SymbolHandler)])
app.listen(gOptions['portNumber'], gOptions['hostname'])
try:
# select on Windows doesn't return on ctrl-c, add a periodic
# callback to make ctrl-c responsive
if sys.platform == 'win32':
PeriodicCallback(lambda: None, 100).start()
IOLoop.current().start()
except KeyboardInterrupt:
LogMessage("Received SIGINT, stopping...")
gPool.shutdown()
LogMessage("Server stopped - " + gOptions['hostname'] + ":" + str(gOptions['portNumber']))
return 0
if __name__ == '__main__':
sys.exit(Main())