Skip to content

Commit 81f63e2

Browse files
committed
feat: support program parameter in attach configs
1 parent 6b7cebe commit 81f63e2

File tree

4 files changed

+80
-18
lines changed

4 files changed

+80
-18
lines changed

Extension/package.json

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4597,15 +4597,8 @@
45974597
"attach": {
45984598
"type": "object",
45994599
"default": {},
4600-
"required": [
4601-
"program"
4602-
],
4600+
"required": [],
46034601
"properties": {
4604-
"program": {
4605-
"type": "string",
4606-
"description": "%c_cpp.debuggers.program.description%",
4607-
"default": "${workspaceRoot}/a.out"
4608-
},
46094602
"targetArchitecture": {
46104603
"type": "string",
46114604
"description": "%c_cpp.debuggers.targetArchitecture.description%",
@@ -4651,6 +4644,10 @@
46514644
"description": "%c_cpp.debuggers.useExtendedRemote.description%",
46524645
"default": false
46534646
},
4647+
"program": {
4648+
"type": "string",
4649+
"markdownDescription": "%c_cpp.debuggers.program.attach.markdownDescription%"
4650+
},
46544651
"processId": {
46554652
"markdownDescription": "%c_cpp.debuggers.processId.anyOf.markdownDescription%",
46564653
"anyOf": [
@@ -5618,15 +5615,17 @@
56185615
"attach": {
56195616
"type": "object",
56205617
"default": {},
5621-
"required": [
5622-
"processId"
5623-
],
5618+
"required": [],
56245619
"properties": {
56255620
"symbolSearchPath": {
56265621
"type": "string",
56275622
"description": "%c_cpp.debuggers.symbolSearchPath.description%",
56285623
"default": ""
56295624
},
5625+
"program": {
5626+
"type": "string",
5627+
"markdownDescription": "%c_cpp.debuggers.program.attach.markdownDescription%"
5628+
},
56305629
"processId": {
56315630
"markdownDescription": "%c_cpp.debuggers.processId.anyOf.markdownDescription%",
56325631
"anyOf": [

Extension/package.nls.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,13 @@
922922
"{Locked=\"`${command:pickProcess}`\"}"
923923
]
924924
},
925-
"c_cpp.debuggers.symbolSearchPath.description": "Semicolon separated list of directories to use to search for symbol (that is, pdb) files. Example: \"c:\\dir1;c:\\dir2\".",
925+
"c_cpp.debuggers.program.attach.markdownDescription": {
926+
"message": "Optional full path to program executable. When specified, the debugger will search for a running process matching this executable name and attach to it. If multiple processes match, a selection prompt will be shown. Use either `program` or `processId`, not both.",
927+
"comment": [
928+
"{Locked=\"`program`\"} {Locked=\"`processId`\"}"
929+
]
930+
},
931+
"c_cpp.debuggers.symbolSearchPath.description": "Semicolon separated list of directories to use to search for .so files. Example: \"c:\\dir1;c:\\dir2\".",
926932
"c_cpp.debuggers.dumpPath.description": "Optional full path to a dump file for the specified program. Example: \"c:\\temp\\app.dmp\". Defaults to null.",
927933
"c_cpp.debuggers.enableDebugHeap.description": "If false, the process will be launched with debug heap disabled. This sets the environment variable '_NO_DEBUG_HEAP' to '1'.",
928934
"c_cpp.debuggers.symbolLoadInfo.description": "Explicit control of symbol loading.",

Extension/src/Debugger/configurationProvider.ts

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { PlatformInformation } from '../platform';
2222
import { rsync, scp, ssh } from '../SSH/commands';
2323
import * as Telemetry from '../telemetry';
2424
import { AttachItemsProvider, AttachPicker, RemoteAttachPicker } from './attachToProcess';
25+
import { AttachItem, showQuickPick } from './attachQuickPick';
2526
import { ConfigMenu, ConfigMode, ConfigSource, CppDebugConfiguration, DebuggerEvent, DebuggerType, DebugType, IConfiguration, IConfigurationSnippet, isDebugLaunchStr, MIConfigurations, PipeTransportConfigurations, TaskStatus, WindowsConfigurations, WSLConfigurations } from './configurations';
2627
import { NativeAttachItemsProviderFactory } from './nativeAttach';
2728
import { Environment, ParsedEnvironmentFile } from './ParsedEnvironmentFile';
@@ -350,13 +351,24 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
350351
// Pick process if process id is empty
351352
if (config.request === "attach" && !config.processId) {
352353
let processId: string | undefined;
353-
if (config.pipeTransport || config.useExtendedRemote) {
354-
const remoteAttachPicker: RemoteAttachPicker = new RemoteAttachPicker();
355-
processId = await remoteAttachPicker.ShowAttachEntries(config);
354+
355+
// If program is specified, try to find matching process by name
356+
if (config.program) {
357+
processId = await this.findProcessByProgramName(config.program, config, token);
358+
if (!processId) {
359+
void logger.getOutputChannelLogger().showErrorMessage(`No running process found matching "${config.program}".`);
360+
return undefined;
361+
}
356362
} else {
357-
const attachItemsProvider: AttachItemsProvider = NativeAttachItemsProviderFactory.Get();
358-
const attacher: AttachPicker = new AttachPicker(attachItemsProvider);
359-
processId = await attacher.ShowAttachEntries(token);
363+
// Show process picker if no program specified
364+
if (config.pipeTransport || config.useExtendedRemote) {
365+
const remoteAttachPicker: RemoteAttachPicker = new RemoteAttachPicker();
366+
processId = await remoteAttachPicker.ShowAttachEntries(config);
367+
} else {
368+
const attachItemsProvider: AttachItemsProvider = NativeAttachItemsProviderFactory.Get();
369+
const attacher: AttachPicker = new AttachPicker(attachItemsProvider);
370+
processId = await attacher.ShowAttachEntries(token);
371+
}
360372
}
361373

362374
if (processId) {
@@ -1278,4 +1290,48 @@ export class ConfigurationSnippetProvider implements vscode.CompletionItemProvid
12781290

12791291
return Promise.resolve(new vscode.CompletionList(items, true));
12801292
}
1293+
1294+
private async findProcessByProgramName(
1295+
programPath: string,
1296+
config: CppDebugConfiguration,
1297+
token?: vscode.CancellationToken
1298+
): Promise<string | undefined> {
1299+
const programBaseName: string = path.basename(programPath);
1300+
let processes: AttachItem[];
1301+
1302+
// Get process list using the same logic as interactive attach
1303+
if (config.pipeTransport || config.useExtendedRemote) {
1304+
// For remote attach, we need to use RemoteAttachPicker's methods
1305+
void logger.getOutputChannelLogger().showErrorMessage(
1306+
"Finding process by program name is not yet supported for remote attach. Please use processId instead."
1307+
);
1308+
return undefined;
1309+
} else {
1310+
const attachItemsProvider: AttachItemsProvider = NativeAttachItemsProviderFactory.Get();
1311+
processes = await attachItemsProvider.getAttachItems(token);
1312+
}
1313+
1314+
// Find processes matching the program name
1315+
const matchingProcesses: AttachItem[] = processes.filter(p => {
1316+
const processName: string = p.label.toLowerCase();
1317+
const targetName: string = programBaseName.toLowerCase();
1318+
// Match if the process name exactly matches or starts with the target name
1319+
return processName === targetName || processName.startsWith(targetName);
1320+
});
1321+
1322+
if (matchingProcesses.length === 0) {
1323+
return undefined;
1324+
} else if (matchingProcesses.length === 1) {
1325+
void logger.getOutputChannelLogger().appendLine(
1326+
`Found process "${matchingProcesses[0].label}" with PID ${matchingProcesses[0].id}`
1327+
);
1328+
return matchingProcesses[0].id;
1329+
} else {
1330+
// Multiple matches - let user choose
1331+
void logger.getOutputChannelLogger().appendLine(
1332+
`Multiple processes found matching "${programBaseName}". Please select one:`
1333+
);
1334+
return showQuickPick(() => Promise.resolve(matchingProcesses));
1335+
}
1336+
}
12811337
}

MIEngine_Debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit bc13df50d03df838843dbbc1ca15639d8ae42801

0 commit comments

Comments
 (0)