-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DI] Improve separation between RC and breakpoint logic (#4992)
This will make it easier to mock either one or the other in tests or to add probes without relying on RC.
- Loading branch information
Showing
2 changed files
with
72 additions
and
63 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
packages/dd-trace/src/debugger/devtools_client/breakpoints.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict' | ||
|
||
const session = require('./session') | ||
const { findScriptFromPartialPath, probes, breakpoints } = require('./state') | ||
const log = require('../../log') | ||
|
||
let sessionStarted = false | ||
|
||
module.exports = { | ||
addBreakpoint, | ||
removeBreakpoint | ||
} | ||
|
||
async function addBreakpoint (probe) { | ||
if (!sessionStarted) await start() | ||
|
||
const file = probe.where.sourceFile | ||
const line = Number(probe.where.lines[0]) // Tracer doesn't support multiple-line breakpoints | ||
|
||
// Optimize for sending data to /debugger/v1/input endpoint | ||
probe.location = { file, lines: [String(line)] } | ||
delete probe.where | ||
|
||
// TODO: Inbetween `await session.post('Debugger.enable')` and here, the scripts are parsed and cached. | ||
// Maybe there's a race condition here or maybe we're guraenteed that `await session.post('Debugger.enable')` will | ||
// not continue untill all scripts have been parsed? | ||
const script = findScriptFromPartialPath(file) | ||
if (!script) throw new Error(`No loaded script found for ${file} (probe: ${probe.id}, version: ${probe.version})`) | ||
const [path, scriptId] = script | ||
|
||
log.debug(`Adding breakpoint at ${path}:${line} (probe: ${probe.id}, version: ${probe.version})`) | ||
|
||
const { breakpointId } = await session.post('Debugger.setBreakpoint', { | ||
location: { | ||
scriptId, | ||
lineNumber: line - 1 // Beware! lineNumber is zero-indexed | ||
} | ||
}) | ||
|
||
probes.set(probe.id, breakpointId) | ||
breakpoints.set(breakpointId, probe) | ||
} | ||
|
||
async function removeBreakpoint ({ id }) { | ||
if (!sessionStarted) { | ||
// We should not get in this state, but abort if we do, so the code doesn't fail unexpected | ||
throw Error(`Cannot remove probe ${id}: Debugger not started`) | ||
} | ||
if (!probes.has(id)) { | ||
throw Error(`Unknown probe id: ${id}`) | ||
} | ||
|
||
const breakpointId = probes.get(id) | ||
await session.post('Debugger.removeBreakpoint', { breakpointId }) | ||
probes.delete(id) | ||
breakpoints.delete(breakpointId) | ||
|
||
if (breakpoints.size === 0) await stop() | ||
} | ||
|
||
async function start () { | ||
sessionStarted = true | ||
return session.post('Debugger.enable') // return instead of await to reduce number of promises created | ||
} | ||
|
||
async function stop () { | ||
sessionStarted = false | ||
return session.post('Debugger.disable') // return instead of await to reduce number of promises created | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters