Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ npx prettier --write <file>
- `src/symbolFiles.ts` – symbol files from the debug configuration, sent to
the agent's memory map.
- `src/settings.ts` – VS Code settings passed to the debug adapter.
- `src/contextQuery.ts` – checks and completes typed TCF context queries,
including the Simics `IDList` attribute (no `vscode` dependency, also used
by the launch configurations editor's webview). The completions' contexts
and attributes come from the `getContextQueryData` request; the
`runContextQuery` request returns what the agent selects with a query.
- `src/targetMemory/` – Target Memory panels and commands (extension side).
- `src/webview/targetMemory/` – Target Memory UI (React), bundled to
`out/target-memory.{js,css}`.
Expand Down Expand Up @@ -246,6 +251,11 @@ Works like "Load symbols" in the Eclipse TCF client:
attaching, for each listed context, or for all contexts (`""`) with a
`ContextQuery`. `MemoryMap.set` takes a single context, so a file with
several contexts is set in each one's map.
- The agent matches a region's `ContextQuery` only against memory contexts
(`CONTEXT_GROUP_PROCESS`, reported as a `ProcessID` equal to the ID):
Simics processors, OS awareness processes. Threads use their process's
map, so a query that selects only threads adds the file nowhere. A query
the agent cannot parse matches every context.
- The region has `FileName`, plus `BaseAddress` for an offset (as Simics
`add-symbol-file -relative` uses) or `Addr` for an absolute load address.
The agent removes the regions when the session ends.
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ All notable changes to the Simics Target Debug extension will be documented here

### Added

- Context queries are checked as you type them in the context picker and
the launch configurations editor. The wrong parts are marked, hovering
over them tells what is wrong, and a query with problems is not saved.
The Simics `IDList` attribute is checked too.
- While a session to the agent runs, the agent checks a context query too
(`ContextQuery.query`). The launch configurations editor lists the
contexts it selects below the query, and for a symbol file which of them
get the file. The context picker tells how many.
- Context queries get suggestions as you type them: wildcards, the
attributes the agent compares (`ContextQuery.getAttrNames`), the names of
the contexts a part can match, and values such as context IDs. An
`IDList` suggests the IDs of the contexts with state.
- The Call Stack view shows the run control context hierarchy: each context
with child contexts, such as a process of the Linux tracker, is a nested
session with its threads. Turn it off with the setting
Expand Down
47 changes: 47 additions & 0 deletions docs/configuration/contexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,57 @@ A context query is evaluated by the agent. Some examples:
| `/server/**/HasState=true` | Such contexts below the top-level context named `server`. |
| `ID=cpu0` | The context with the ID `cpu0`. |
| `ID=cpu0,HasState=true` | The context with the ID `cpu0`, if it can run and be stopped. |
| `ID="board.cpu[0]"` | The context with the ID `board.cpu[0]`. |
| `IDList="[cpu0, cpu1]"` | The contexts `cpu0` and `cpu1`. Simics only. |

Names and values with other characters than letters, digits and `_`, such
as `.` and `[`, must be in double quotes. Inside them, write `"` as `\"` and
`\` as `\\`. Values are compared exactly, they
have no wildcards. `HasState` is `true` or `false`; the agent takes any
other value, such as `True`, as `false`. In `IDList`, the IDs have no
quotes, and the whole query matches nothing if one of the IDs does not
exist.

The syntax is described with the Context Query service in the
[TCF documentation](https://download.eclipse.org/tools/tcf/tcf-docs/).

The context picker and the launch configurations editor check a query as
you type it. When you stop typing for five seconds, or press Enter or leave
the box, the wrong parts are marked and the problems are listed below the
box. In the launch configurations editor, hover over a marked part to see
what is wrong with it. A query with problems is not saved.

While a session to the agent runs, the agent checks the query too, shortly
after you stop typing. A query it refuses is marked like the other
problems. The launch configurations editor shows how many contexts the
query selects below the box; open it to list their IDs and names. In the
context picker, the first item tells how many.

For a symbol file, they also tell which of the selected contexts get it.
The agent adds a symbol file only to contexts with a memory map of their
own, such as processors; threads use the memory map of their process. In
the list, these contexts come first and are marked "gets the symbol file".
A query that selects nothing, or no context that gets the symbol file, is
shown as a warning.

Both also suggest what to type next: `*` and `**`, the attributes the agent
compares, such as `HasState=` and `ID=`, the names of the contexts the part
can match, and values such as `true` and `false` for `HasState`, context IDs
for `ID` and the IDs in an `IDList`. Names and IDs that need double quotes
get them. An `IDList` suggests the IDs of the contexts that can run and be
stopped, without quotes around them. It adds the `"[` when you type an ID
without it, as in `IDList="cpu`, and the `]"` when the list is not closed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IDList context query is not documented anywhere. I would say that it's an internal / tech preview type of context-query. Same for TreeID, although that is used internally by code-coverage.

We could remove any of these at any point. Should either be made official by documenting etc or not be exposed through this extension.

One thing I noticed with this is that we have missed to include some newer context query comparators from open source, ParentName, AncestorID and AncestorName from 61d5800f.

We should include those in base and include them here. AncestorID is rather close to TreeID, with the difference that TreeID includes the context with the provided id, while Ancestor excludes that.

yet. Contexts come from a running session to the agent; without one,
only the wildcards and the attributes `ID`, `ParentID`, `ProcessID`, `Name`,
`HasState` and `IDList` are suggested.

- In the context picker, the suggestions are listed below the query. The
first item uses the query as typed; select a suggestion to put it in the
query.
- In the launch configurations editor, the suggestions show while you type,
or with Ctrl+Space. Use the arrow keys and Enter or Tab to put one in the
query, or Escape to close the list.

## The context picker

While debugging, the commands that need contexts open the same picker. This
Expand Down
3 changes: 2 additions & 1 deletion docs/configuration/launch-configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ file or rule.
marked.
- The **Contexts** column chooses what the row applies to: all contexts, a
list of contexts separated by commas, or a context query, see
[Selecting contexts](/configuration/contexts).
[Selecting contexts](/configuration/contexts). The list or query is typed
in a box below the row.
- **Pick Contexts...** next to it opens the
[context picker](/configuration/contexts#the-context-picker) with the
contexts of the target. It needs a running debug session to the agent of
Expand Down
2 changes: 1 addition & 1 deletion scripts/docs-screenshots/capture.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ try {
const table = form.locator(".object-list").first();
const th = await table.locator("th", { hasText: "Contexts" }).boundingBox();
const column = [th];
for (const cell of await table.locator(".context-scope").all()) {
for (const cell of await table.locator(".context-scope, .context-scope-text").all()) {
column.push(await cell.boundingBox());
}
remove = await callouts(
Expand Down
220 changes: 202 additions & 18 deletions src/contextPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ import {
pickedValues,
scopeMenuItems,
} from "./contextChoices";
import {
CONTEXT_QUERY_AGENT_DELAY_MS,
CONTEXT_QUERY_CHECK_DELAY_MS,
ContextQueryCompletion,
ContextQueryData,
ContextQueryResult,
DEFAULT_QUERY_ATTRIBUTES,
checkContextQuery,
contextQueryCompletionPoint,
contextQueryCompletions,
describeContextQueryResult,
formatContextQueryProblem,
} from "./contextQuery";

/**
* The contexts of a session to pick from: the cores (contexts with state),
Expand All @@ -20,6 +33,44 @@ export function listContexts(session: vscode.DebugSession, includeContainers = f
) as Promise<TargetContext[]>;
}

/**
* Returns what to complete a context query with in `session`, for a part
* after `parentQuery`. Without a session, or if it fails, only the attributes
* that most agents have are offered.
*/
export async function contextQueryData(
session: vscode.DebugSession | undefined,
parentQuery: string,
): Promise<ContextQueryData> {
try {
if (session) {
return (await session.customRequest("getContextQueryData", { parentQuery })) as ContextQueryData;
}
} catch {
// Completions are only a help, the query can still be typed.
}
return { attributes: DEFAULT_QUERY_ATTRIBUTES, contexts: [] };
}

/**
* Returns the contexts the agent of `session` selects with `query`, or why
* it refuses the query; undefined without a session or if the agent cannot
* tell.
*/
export async function runContextQuery(
session: vscode.DebugSession | undefined,
query: string,
): Promise<ContextQueryResult | undefined> {
try {
if (session) {
return (await session.customRequest("runContextQuery", { query })) as ContextQueryResult;
}
} catch {
// The query is still checked without the agent.
}
return undefined;
}

export interface PickContextsOptions {
title: string;
/** The contexts to offer. Checked entries that are not among them are kept as extra items. */
Expand All @@ -34,6 +85,10 @@ export interface PickContextsOptions {
allowTyped?: boolean;
/** Typed text is a context ID only, not a name. */
idsOnly?: boolean;
/** The session whose contexts and query attributes complete a context query. */
session?: vscode.DebugSession;
/** The contexts are those of a symbol file, so a query tells which of them get it. */
symbolFile?: boolean;
}

/** The scope picked, Back, or undefined when cancelled. */
Expand Down Expand Up @@ -180,39 +235,168 @@ function check(options: PickContextsOptions, initial: readonly string[]): Promis
});
}

type QueryItem = vscode.QuickPickItem & {
/** Accepting the item uses the typed query. */
use?: true;
/** Accepting the item puts it in the query, at `start` to `end`. */
completion?: ContextQueryCompletion & { start: number; end: number };
};

/**
* Asks for a context query. The first item uses the typed query, or shows
* its first problem; with a session it also tells what the agent selects
* with the query. The items below it complete the name or value being
* typed, from the session's contexts and the attributes its agent compares.
*/
function enterQuery(options: PickContextsOptions, initial: string): Promise<Step<string>> {
return new Promise((resolve) => {
const input = vscode.window.createInputBox();
input.title = options.title;
input.prompt = "TCF context query";
input.placeholder = "Context query";
input.value = initial;
input.buttons = [vscode.QuickInputButtons.Back];
const picker = vscode.window.createQuickPick<QueryItem>();
picker.title = options.title;
picker.placeholder = "TCF context query, for example **/HasState=true";
picker.value = initial;
picker.buttons = [vscode.QuickInputButtons.Back];
let finished = false;
let timer: ReturnType<typeof setTimeout> | undefined;
let agentTimer: ReturnType<typeof setTimeout> | undefined;
let problemsShown = false;
let emptyShown = false;
let completions: QueryItem[] = [];
/** What the agent made of the query, while it is the typed one. */
let agent: { query: string; result: ContextQueryResult } | undefined;
/** Increased for each change of the text, so that completions for older text are dropped. */
let generation = 0;
const cache = new Map<string, Promise<ContextQueryData>>();
const data = (parentQuery: string) => {
let result = cache.get(parentQuery);
if (!result) {
result = contextQueryData(options.session, parentQuery);
cache.set(parentQuery, result);
}
return result;
};

function finish(step: Step<string>): void {
if (finished) {
return;
}
finished = true;
input.hide();
input.dispose();
clearTimeout(timer);
clearTimeout(agentTimer);
picker.hide();
picker.dispose();
resolve(step);
}

input.onDidChangeValue(() => {
input.validationMessage = undefined;
});
input.onDidTriggerButton(() => finish({ back: input.value.trim() }));
input.onDidAccept(() => {
const query = input.value.trim();
/** The agent's reason to refuse the typed query, if it has answered for it. */
function agentError(query: string): string | undefined {
return agent?.query === query ? agent.result.error : undefined;
}

function showItems(): void {
const query = picker.value;
const problems = problemsShown ? checkContextQuery(query) : [];
const refused = problemsShown ? agentError(query.trim()) : undefined;
let first: QueryItem | undefined;
if (problems.length > 0) {
const more = problems.length > 1 ? ` (and ${problems.length - 1} more)` : "";
first = { label: `$(error) ${formatContextQueryProblem(query, problems[0])}${more}` };
} else if (refused !== undefined) {
first = { label: `$(error) The agent refuses the query: ${refused}` };
} else if (query.trim()) {
const result = agent?.query === query.trim() && !agent.result.error ? agent.result : undefined;
first = {
label: query.trim(),
description: "Use this query",
...(result && { detail: describeContextQueryResult(result, options.symbolFile) }),
use: true,
};
} else if (emptyShown) {
first = { label: "$(error) Enter a context query" };
}
picker.items = [...(first ? [{ ...first, alwaysShow: true }] : []), ...completions];
}

/** Asks the agent what the typed query selects once the user stops typing. */
function askAgent(): void {
clearTimeout(agentTimer);
const query = picker.value.trim();
if (!options.session || !query || checkContextQuery(query).length > 0) {
return;
}
agentTimer = setTimeout(() => {
void runContextQuery(options.session, query).then((result) => {
if (!finished && picker.value.trim() === query) {
agent = result && { query, result };
showItems();
}
});
}, CONTEXT_QUERY_AGENT_DELAY_MS);
}

async function complete(): Promise<void> {
const current = ++generation;
const text = picker.value;
const point = contextQueryCompletionPoint(text, text.length);
const found = point ? contextQueryCompletions(point, await data(point.parentQuery)) : [];
if (current !== generation || finished || !point) {
return;
}
completions = found.map((c) => ({
label: c.label,
description: c.detail,
alwaysShow: true,
completion: { ...c, start: point.start, end: point.end },
}));
showItems();
}

// Problems wait until the user stops typing, but once shown they follow the text.
function changed(): void {
clearTimeout(timer);
emptyShown = false;
completions = [];
if (!problemsShown || (checkContextQuery(picker.value).length === 0 && !agentError(picker.value.trim()))) {
problemsShown = false;
timer = setTimeout(() => {
problemsShown = true;
showItems();
}, CONTEXT_QUERY_CHECK_DELAY_MS);
}
showItems();
askAgent();
void complete();
}

picker.onDidChangeValue(changed);
picker.onDidTriggerButton(() => finish({ back: picker.value.trim() }));
picker.onDidAccept(() => {
const item = picker.activeItems[0];
const text = picker.value;
if (item?.completion) {
const { start, end, text: inserted } = item.completion;
picker.value = text.slice(0, start) + inserted + text.slice(end);
changed();
return;
}
if (item && !item.use) {
return;
}
const query = text.trim();
if (!query) {
input.validationMessage = "Enter a context query";
emptyShown = true;
} else if (checkContextQuery(query).length === 0 && agentError(query) === undefined) {
finish({ scope: { contextQuery: query } });
return;
}
finish({ scope: { contextQuery: query } });
clearTimeout(timer);
problemsShown = true;
showItems();
});
input.onDidHide(() => finish(undefined));
input.show();
picker.onDidHide(() => finish(undefined));
problemsShown = checkContextQuery(initial).length > 0;
showItems();
askAgent();
void complete();
picker.show();
});
}
Loading