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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
import os
import time
import dap_server
from lldbsuite.test.lldbtest import *
class DAPTestCaseBase(TestBase):
NO_DEBUG_INFO_TESTCASE = True
def create_debug_adaptor(self, lldbDAPEnv=None):
"""Create the Visual Studio Code debug adaptor"""
self.assertTrue(
is_exe(self.lldbDAPExec), "lldb-dap must exist and be executable"
)
log_file_path = self.getBuildArtifact("dap.txt")
self.dap_server = dap_server.DebugAdaptorServer(
executable=self.lldbDAPExec,
init_commands=self.setUpCommands(),
log_file=log_file_path,
env=lldbDAPEnv,
)
def build_and_create_debug_adaptor(self, lldbDAPEnv=None):
self.build()
self.create_debug_adaptor(lldbDAPEnv)
def set_source_breakpoints(self, source_path, lines, data=None):
"""Sets source breakpoints and returns an array of strings containing
the breakpoint IDs ("1", "2") for each breakpoint that was set.
Parameter data is array of data objects for breakpoints.
Each object in data is 1:1 mapping with the entry in lines.
It contains optional location/hitCondition/logMessage parameters.
"""
response = self.dap_server.request_setBreakpoints(source_path, lines, data)
if response is None:
return []
breakpoints = response["body"]["breakpoints"]
breakpoint_ids = []
for breakpoint in breakpoints:
breakpoint_ids.append("%i" % (breakpoint["id"]))
return breakpoint_ids
def set_function_breakpoints(self, functions, condition=None, hitCondition=None):
"""Sets breakpoints by function name given an array of function names
and returns an array of strings containing the breakpoint IDs
("1", "2") for each breakpoint that was set.
"""
response = self.dap_server.request_setFunctionBreakpoints(
functions, condition=condition, hitCondition=hitCondition
)
if response is None:
return []
breakpoints = response["body"]["breakpoints"]
breakpoint_ids = []
for breakpoint in breakpoints:
breakpoint_ids.append("%i" % (breakpoint["id"]))
return breakpoint_ids
def waitUntil(self, condition_callback):
for _ in range(20):
if condition_callback():
return True
time.sleep(0.5)
return False
def verify_breakpoint_hit(self, breakpoint_ids):
"""Wait for the process we are debugging to stop, and verify we hit
any breakpoint location in the "breakpoint_ids" array.
"breakpoint_ids" should be a list of breakpoint ID strings
(["1", "2"]). The return value from self.set_source_breakpoints()
or self.set_function_breakpoints() can be passed to this function"""
stopped_events = self.dap_server.wait_for_stopped()
for stopped_event in stopped_events:
if "body" in stopped_event:
body = stopped_event["body"]
if "reason" not in body:
continue
if body["reason"] != "breakpoint":
continue
if "description" not in body:
continue
# Descriptions for breakpoints will be in the form
# "breakpoint 1.1", so look for any description that matches
# ("breakpoint 1.") in the description field as verification
# that one of the breakpoint locations was hit. DAP doesn't
# allow breakpoints to have multiple locations, but LLDB does.
# So when looking at the description we just want to make sure
# the right breakpoint matches and not worry about the actual
# location.
description = body["description"]
for breakpoint_id in breakpoint_ids:
match_desc = "breakpoint %s." % (breakpoint_id)
if match_desc in description:
return
self.assertTrue(False, "breakpoint not hit")
def verify_stop_exception_info(self, expected_description):
"""Wait for the process we are debugging to stop, and verify the stop
reason is 'exception' and that the description matches
'expected_description'
"""
stopped_events = self.dap_server.wait_for_stopped()
for stopped_event in stopped_events:
if "body" in stopped_event:
body = stopped_event["body"]
if "reason" not in body:
continue
if body["reason"] != "exception":
continue
if "description" not in body:
continue
description = body["description"]
if expected_description == description:
return True
return False
def verify_commands(self, flavor, output, commands):
self.assertTrue(output and len(output) > 0, "expect console output")
lines = output.splitlines()
prefix = "(lldb) "
for cmd in commands:
found = False
for line in lines:
if line.startswith(prefix) and cmd in line:
found = True
break
self.assertTrue(
found, "verify '%s' found in console output for '%s'" % (cmd, flavor)
)
def get_dict_value(self, d, key_path):
"""Verify each key in the key_path array is in contained in each
dictionary within "d". Assert if any key isn't in the
corresponding dictionary. This is handy for grabbing values from VS
Code response dictionary like getting
response['body']['stackFrames']
"""
value = d
for key in key_path:
if key in value:
value = value[key]
else:
self.assertTrue(
key in value,
'key "%s" from key_path "%s" not in "%s"' % (key, key_path, d),
)
return value
def get_stackFrames_and_totalFramesCount(
self, threadId=None, startFrame=None, levels=None, dump=False
):
response = self.dap_server.request_stackTrace(
threadId=threadId, startFrame=startFrame, levels=levels, dump=dump
)
if response:
stackFrames = self.get_dict_value(response, ["body", "stackFrames"])
totalFrames = self.get_dict_value(response, ["body", "totalFrames"])
self.assertTrue(
totalFrames > 0,
"verify totalFrames count is provided by extension that supports "
"async frames loading",
)
return (stackFrames, totalFrames)
return (None, 0)
def get_stackFrames(self, threadId=None, startFrame=None, levels=None, dump=False):
(stackFrames, totalFrames) = self.get_stackFrames_and_totalFramesCount(
threadId=threadId, startFrame=startFrame, levels=levels, dump=dump
)
return stackFrames
def get_source_and_line(self, threadId=None, frameIndex=0):
stackFrames = self.get_stackFrames(
threadId=threadId, startFrame=frameIndex, levels=1
)
if stackFrames is not None:
stackFrame = stackFrames[0]
["source", "path"]
if "source" in stackFrame:
source = stackFrame["source"]
if "path" in source:
if "line" in stackFrame:
return (source["path"], stackFrame["line"])
return ("", 0)
def get_stdout(self, timeout=0.0):
return self.dap_server.get_output("stdout", timeout=timeout)
def get_console(self, timeout=0.0):
return self.dap_server.get_output("console", timeout=timeout)
def collect_console(self, duration):
return self.dap_server.collect_output("console", duration=duration)
def get_local_as_int(self, name, threadId=None):
value = self.dap_server.get_local_variable_value(name, threadId=threadId)
if value.startswith("0x"):
return int(value, 16)
elif value.startswith("0"):
return int(value, 8)
else:
return int(value)
def set_local(self, name, value, id=None):
"""Set a top level local variable only."""
return self.dap_server.request_setVariable(1, name, str(value), id=id)
def set_global(self, name, value, id=None):
"""Set a top level global variable only."""
return self.dap_server.request_setVariable(2, name, str(value), id=id)
def stepIn(self, threadId=None, waitForStop=True):
self.dap_server.request_stepIn(threadId=threadId)
if waitForStop:
return self.dap_server.wait_for_stopped()
return None
def stepOver(self, threadId=None, waitForStop=True):
self.dap_server.request_next(threadId=threadId)
if waitForStop:
return self.dap_server.wait_for_stopped()
return None
def stepOut(self, threadId=None, waitForStop=True):
self.dap_server.request_stepOut(threadId=threadId)
if waitForStop:
return self.dap_server.wait_for_stopped()
return None
def continue_to_next_stop(self):
self.dap_server.request_continue()
return self.dap_server.wait_for_stopped()
def continue_to_breakpoints(self, breakpoint_ids):
self.dap_server.request_continue()
self.verify_breakpoint_hit(breakpoint_ids)
def continue_to_exception_breakpoint(self, filter_label):
self.dap_server.request_continue()
self.assertTrue(
self.verify_stop_exception_info(filter_label),
'verify we got "%s"' % (filter_label),
)
def continue_to_exit(self, exitCode=0):
self.dap_server.request_continue()
stopped_events = self.dap_server.wait_for_stopped()
self.assertEqual(
len(stopped_events), 1, "stopped_events = {}".format(stopped_events)
)
self.assertEqual(
stopped_events[0]["event"], "exited", "make sure program ran to completion"
)
self.assertEqual(
stopped_events[0]["body"]["exitCode"],
exitCode,
"exitCode == %i" % (exitCode),
)
def disassemble(self, threadId=None, frameIndex=None):
stackFrames = self.get_stackFrames(
threadId=threadId, startFrame=frameIndex, levels=1
)
self.assertIsNotNone(stackFrames)
memoryReference = stackFrames[0]["instructionPointerReference"]
self.assertIsNotNone(memoryReference)
if memoryReference not in self.dap_server.disassembled_instructions:
self.dap_server.request_disassemble(memoryReference=memoryReference)
return self.dap_server.disassembled_instructions[memoryReference]
def attach(
self,
program=None,
pid=None,
waitFor=None,
trace=None,
initCommands=None,
preRunCommands=None,
stopCommands=None,
exitCommands=None,
attachCommands=None,
coreFile=None,
disconnectAutomatically=True,
terminateCommands=None,
postRunCommands=None,
sourceMap=None,
sourceInitFile=False,
):
"""Build the default Makefile target, create the DAP debug adaptor,
and attach to the process.
"""
# Make sure we disconnect and terminate the DAP debug adaptor even
# if we throw an exception during the test case.
def cleanup():
if disconnectAutomatically:
self.dap_server.request_disconnect(terminateDebuggee=True)
self.dap_server.terminate()
# Execute the cleanup function during test case tear down.
self.addTearDownHook(cleanup)
# Initialize and launch the program
self.dap_server.request_initialize(sourceInitFile)
response = self.dap_server.request_attach(
program=program,
pid=pid,
waitFor=waitFor,
trace=trace,
initCommands=initCommands,
preRunCommands=preRunCommands,
stopCommands=stopCommands,
exitCommands=exitCommands,
attachCommands=attachCommands,
terminateCommands=terminateCommands,
coreFile=coreFile,
postRunCommands=postRunCommands,
sourceMap=sourceMap,
)
if not (response and response["success"]):
self.assertTrue(
response["success"], "attach failed (%s)" % (response["message"])
)
def launch(
self,
program=None,
args=None,
cwd=None,
env=None,
stopOnEntry=False,
disableASLR=True,
disableSTDIO=False,
shellExpandArguments=False,
trace=False,
initCommands=None,
preRunCommands=None,
stopCommands=None,
exitCommands=None,
terminateCommands=None,
sourcePath=None,
debuggerRoot=None,
sourceInitFile=False,
launchCommands=None,
sourceMap=None,
disconnectAutomatically=True,
runInTerminal=False,
expectFailure=False,
postRunCommands=None,
enableAutoVariableSummaries=False,
enableSyntheticChildDebugging=False,
commandEscapePrefix="`",
customFrameFormat=None,
customThreadFormat=None,
):
"""Sending launch request to dap"""
# Make sure we disconnect and terminate the DAP debug adapter,
# if we throw an exception during the test case
def cleanup():
if disconnectAutomatically:
self.dap_server.request_disconnect(terminateDebuggee=True)
self.dap_server.terminate()
# Execute the cleanup function during test case tear down.
self.addTearDownHook(cleanup)
# Initialize and launch the program
self.dap_server.request_initialize(sourceInitFile)
response = self.dap_server.request_launch(
program,
args=args,
cwd=cwd,
env=env,
stopOnEntry=stopOnEntry,
disableASLR=disableASLR,
disableSTDIO=disableSTDIO,
shellExpandArguments=shellExpandArguments,
trace=trace,
initCommands=initCommands,
preRunCommands=preRunCommands,
stopCommands=stopCommands,
exitCommands=exitCommands,
terminateCommands=terminateCommands,
sourcePath=sourcePath,
debuggerRoot=debuggerRoot,
launchCommands=launchCommands,
sourceMap=sourceMap,
runInTerminal=runInTerminal,
postRunCommands=postRunCommands,
enableAutoVariableSummaries=enableAutoVariableSummaries,
enableSyntheticChildDebugging=enableSyntheticChildDebugging,
commandEscapePrefix=commandEscapePrefix,
customFrameFormat=customFrameFormat,
customThreadFormat=customThreadFormat,
)
if expectFailure:
return response
if not (response and response["success"]):
self.assertTrue(
response["success"], "launch failed (%s)" % (response["message"])
)
return response
def build_and_launch(
self,
program,
args=None,
cwd=None,
env=None,
stopOnEntry=False,
disableASLR=True,
disableSTDIO=False,
shellExpandArguments=False,
trace=False,
initCommands=None,
preRunCommands=None,
stopCommands=None,
exitCommands=None,
terminateCommands=None,
sourcePath=None,
debuggerRoot=None,
sourceInitFile=False,
runInTerminal=False,
disconnectAutomatically=True,
postRunCommands=None,
lldbDAPEnv=None,
enableAutoVariableSummaries=False,
enableSyntheticChildDebugging=False,
commandEscapePrefix="`",
customFrameFormat=None,
customThreadFormat=None,
):
"""Build the default Makefile target, create the DAP debug adaptor,
and launch the process.
"""
self.build_and_create_debug_adaptor(lldbDAPEnv)
self.assertTrue(os.path.exists(program), "executable must exist")
return self.launch(
program,
args,
cwd,
env,
stopOnEntry,
disableASLR,
disableSTDIO,
shellExpandArguments,
trace,
initCommands,
preRunCommands,
stopCommands,
exitCommands,
terminateCommands,
sourcePath,
debuggerRoot,
sourceInitFile,
runInTerminal=runInTerminal,
disconnectAutomatically=disconnectAutomatically,
postRunCommands=postRunCommands,
enableAutoVariableSummaries=enableAutoVariableSummaries,
enableSyntheticChildDebugging=enableSyntheticChildDebugging,
commandEscapePrefix=commandEscapePrefix,
customFrameFormat=customFrameFormat,
customThreadFormat=customThreadFormat,
)
|