MCPcopy Create free account
hub / github.com/ChromeDevTools/chrome-devtools-mcp / createStackTrace

Function createStackTrace

src/devtools/DevtoolsUtils.ts:504–550  ·  view source on GitHub ↗
(
  devTools: TargetUniverse,
  rawStackTrace: StackTraceLike,
  targetId: string | undefined,
)

Source from the content-addressed store, hash-verified

502}
503
504export async function createStackTrace(
505 devTools: TargetUniverse,
506 rawStackTrace: StackTraceLike,
507 targetId: string | undefined,
508): Promise<DevTools.StackTrace.StackTrace.StackTrace> {
509 const targetManager = devTools.universe.context.get(DevTools.TargetManager);
510 const target = targetId
511 ? targetManager.targetById(targetId) || devTools.target
512 : devTools.target;
513 const model = target.model(DevTools.DebuggerModel) as DevTools.DebuggerModel;
514
515 // DevTools doesn't wait for source maps to attach before building a stack trace, rather it'll send
516 // an update event once a source map was attached and the stack trace retranslated. This doesn't
517 // work in the MCP case, so we'll collect all script IDs upfront and wait for any pending source map
518 // loads before creating the stack trace. We might also have to wait for Debugger.ScriptParsed events if
519 // the stack trace is created particularly early.
520 const scriptIds = new Set<string>();
521 for (const frame of rawStackTrace.callFrames) {
522 scriptIds.add(frame.scriptId);
523 }
524 for (
525 let asyncStack: StackTraceLike | undefined = rawStackTrace.parent;
526 asyncStack;
527 asyncStack = asyncStack.parent
528 ) {
529 for (const frame of asyncStack.callFrames) {
530 scriptIds.add(frame.scriptId);
531 }
532 }
533
534 const signal = AbortSignal.timeout(1_000);
535 const scripts = await Promise.all(
536 [...scriptIds].map(id => waitForScript(model, id, signal)),
537 );
538 await waitForSourceMaps(model, scripts, signal);
539
540 const binding = devTools.universe.context.get(
541 DevTools.DebuggerWorkspaceBinding,
542 );
543 // DevTools uses branded types for ScriptId and others. Casting the puppeteer protocol type to the DevTools protocol type is safe.
544 return await binding.createStackTraceFromProtocolRuntime(
545 rawStackTrace as Parameters<
546 DevTools.DebuggerWorkspaceBinding['createStackTraceFromProtocolRuntime']
547 >[0],
548 target,
549 );
550}
551
552/**
553 * Waits for the source maps of `scripts` to load, so that their original

Callers 2

#fromDetailsMethod · 0.85

Calls 4

waitForScriptFunction · 0.85
waitForSourceMapsFunction · 0.85
getMethod · 0.80
timeoutMethod · 0.80

Tested by

no test coverage detected