Skip to content
Navigation Menu
{{ message }}
forked from microsoft/vscode-cpptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattachToProcess.ts
More file actions
244 lines (205 loc) · 10.8 KB
/
Copy pathattachToProcess.ts
File metadata and controls
244 lines (205 loc) · 10.8 KB
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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import { PsProcessParser } from './nativeAttach';
import { AttachItem, showQuickPick } from './attachQuickPick';
import { CppSettings } from '../LanguageServer/settings';
import * as debugUtils from './utils';
import * as os from 'os';
import * as path from 'path';
import * as util from '../common';
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
export interface AttachItemsProvider {
getAttachItems(token?: vscode.CancellationToken): Promise<AttachItem[]>;
}
export class AttachPicker {
constructor(private attachItemsProvider: AttachItemsProvider) { }
// We should not await on this function.
public async ShowAttachEntries(token?: vscode.CancellationToken): Promise<string | undefined> {
return showQuickPick(() => this.attachItemsProvider.getAttachItems(token));
}
}
export class RemoteAttachPicker {
constructor() {
this._channel = vscode.window.createOutputChannel('remote-attach');
}
private _channel: vscode.OutputChannel;
public async ShowAttachEntries(config: any): Promise<string | undefined> {
this._channel.clear();
let processes: AttachItem[];
const pipeTransport: any = config ? config.pipeTransport : undefined;
const useExtendedRemote: any = config ? config.useExtendedRemote : undefined;
const miDebuggerPath: any = config ? config.miDebuggerPath : undefined;
const miDebuggerServerAddress: any = config ? config.miDebuggerServerAddress : undefined;
if (pipeTransport) {
let pipeProgram: string | undefined;
if (os.platform() === 'win32' &&
pipeTransport.pipeProgram &&
!await util.checkFileExists(pipeTransport.pipeProgram)) {
const pipeProgramStr: string = pipeTransport.pipeProgram.toLowerCase().trim();
const expectedArch: debugUtils.ArchType = debugUtils.ArchType[process.arch as keyof typeof debugUtils.ArchType];
// Check for pipeProgram
if (!await util.checkFileExists(config.pipeTransport.pipeProgram)) {
pipeProgram = debugUtils.ArchitectureReplacer.checkAndReplaceWSLPipeProgram(pipeProgramStr, expectedArch);
}
// If pipeProgram does not get replaced and there is a pipeCwd, concatenate with pipeProgramStr and attempt to replace.
if (!pipeProgram && config.pipeTransport.pipeCwd) {
const pipeCwdStr: string = config.pipeTransport.pipeCwd.toLowerCase().trim();
const newPipeProgramStr: string = path.join(pipeCwdStr, pipeProgramStr);
if (!await util.checkFileExists(newPipeProgramStr)) {
pipeProgram = debugUtils.ArchitectureReplacer.checkAndReplaceWSLPipeProgram(newPipeProgramStr, expectedArch);
}
}
}
if (!pipeProgram) {
pipeProgram = pipeTransport.pipeProgram;
}
const pipeArgs: string[] = pipeTransport.pipeArgs;
const argList: string = RemoteAttachPicker.createArgumentList(pipeArgs);
const pipeCmd: string = `"${pipeProgram}" ${argList}`;
processes = await this.getRemoteOSAndProcesses(pipeCmd);
} else if (!pipeTransport && useExtendedRemote) {
if (!miDebuggerPath || !miDebuggerServerAddress) {
throw new Error(localize("debugger.path.and.server.address.required", "{0} in debug configuration requires {1} and {2}", "useExtendedRemote", "miDebuggerPath", "miDebuggerServerAddress"));
}
processes = await this.getRemoteProcessesExtendedRemote(miDebuggerPath, miDebuggerServerAddress);
} else {
throw new Error(localize("no.pipetransport.useextendedremote", "Chosen debug configuration does not contain {0} or {1}", "pipeTransport", "useExtendedRemote"));
}
const attachPickOptions: vscode.QuickPickOptions = {
matchOnDetail: true,
matchOnDescription: true,
placeHolder: localize("select.process.attach", "Select the process to attach to")
};
const item: AttachItem | undefined = await vscode.window.showQuickPick(processes, attachPickOptions);
if (item) {
return item.id;
} else {
throw new Error(localize("process.not.selected", "Process not selected."));
}
}
// Creates a string to run on the host machine which will execute a shell script on the remote machine to retrieve OS and processes
private getRemoteProcessCommand(): string {
let innerQuote: string = `'`;
let outerQuote: string = `"`;
let parameterBegin: string = `$(`;
let parameterEnd: string = `)`;
let escapedQuote: string = `\\\"`;
const settings: CppSettings = new CppSettings();
if (settings.useBacktickCommandSubstitution) {
parameterBegin = `\``;
parameterEnd = `\``;
escapedQuote = `\"`;
}
// Must use single quotes around the whole command and double quotes for the argument to `sh -c` because Linux evaluates $() inside of double quotes.
// Having double quotes for the outerQuote will have $(uname) replaced before it is sent to the remote machine.
if (os.platform() !== "win32") {
innerQuote = `"`;
outerQuote = `'`;
}
return `${outerQuote}sh -c ${innerQuote}uname && if [ ${parameterBegin}uname${parameterEnd} = ${escapedQuote}Linux${escapedQuote} ] ; ` +
`then ${PsProcessParser.psLinuxCommand} ; elif [ ${parameterBegin}uname${parameterEnd} = ${escapedQuote}Darwin${escapedQuote} ] ; ` +
`then ${PsProcessParser.psDarwinCommand}; fi${innerQuote}${outerQuote}`;
}
private async getRemoteOSAndProcesses(pipeCmd: string): Promise<AttachItem[]> {
// Do not add any quoting in execCommand.
const execCommand: string = `${pipeCmd} ${this.getRemoteProcessCommand()}`;
const output: string = await util.execChildProcess(execCommand, undefined, this._channel);
// OS will be on first line
// Processes will follow if listed
const lines: string[] = output.split(/\r?\n/);
if (lines.length === 0) {
throw new Error(localize("pipe.failed", "Pipe transport failed to get OS and processes."));
} else {
const remoteOS: string = lines[0].replace(/[\r\n]+/g, '');
if (remoteOS !== "Linux" && remoteOS !== "Darwin") {
throw new Error(`Operating system "${remoteOS}" not supported.`);
}
// Only got OS from uname
if (lines.length === 1) {
throw new Error(localize("no.process.list", "Transport attach could not obtain processes list."));
} else {
const processes: string[] = lines.slice(1);
return PsProcessParser.ParseProcessFromPsArray(processes)
.sort((a, b) => {
if (a.name === undefined) {
if (b.name === undefined) {
return 0;
}
return 1;
}
if (b.name === undefined) {
return -1;
}
const aLower: string = a.name.toLowerCase();
const bLower: string = b.name.toLowerCase();
if (aLower === bLower) {
return 0;
}
return aLower < bLower ? -1 : 1;
})
.map(p => p.toAttachItem());
}
}
}
private async getRemoteProcessesExtendedRemote(miDebuggerPath: string, miDebuggerServerAddress: string): Promise<AttachItem[]> {
const args: string[] = [`-ex "target extended-remote ${miDebuggerServerAddress}"`, '-ex "info os processes"', '-batch'];
let processListOutput: util.ProcessReturnType = await util.spawnChildProcess(miDebuggerPath, args);
// The device may not be responsive for a while during the restart after image deploy. Retry 5 times.
for (let i: number = 0; i < 5 && !processListOutput.succeeded; i++) {
processListOutput = await util.spawnChildProcess(miDebuggerPath, args);
}
if (!processListOutput.succeeded) {
throw new Error(localize('failed.to.make.gdb.connection', 'Failed to make GDB connection: "{0}".', processListOutput.output));
}
const processes: AttachItem[] = this.parseProcessesFromInfoOsProcesses(processListOutput.output);
if (!processes || processes.length === 0) {
throw new Error(localize('failed.to.parse.processes', 'Failed to parse processes: "{0}".', processListOutput.output));
}
return processes;
}
/**
Format:
pid usr command cores
1 ?
2 ?
3 /usr/bin/sample 0,2
4 root /usr/bin/gdbserver --multi :6000 0
Returns an AttachItem array, each item contains a label of "<user >command", and a pid.
Unfortunately because the format of each line is not fixed, and everything except pid is optional, it's hard
to get a better label.
*/
private parseProcessesFromInfoOsProcesses(processList: string): AttachItem[] {
const lines: string[] = processList?.split('\n');
if (!lines?.length) {
return [];
}
const processes: AttachItem[] = [];
for (const line of lines) {
const trimmedLine: string = line.trim();
if (!trimmedLine.endsWith('?')) {
const matches: RegExpMatchArray | null = trimmedLine.match(/^(\d+)\s+(.+?)\s+(?:\d+,)*\d+$/);
if (matches?.length === 3) {
const id: string = matches[1];
const userCommand: string = matches[2];
processes.push({ label: userCommand, id, description: id });
}
}
}
return processes;
}
private static createArgumentList(args: string[]): string {
let argsString: string = "";
for (const arg of args) {
if (argsString) {
argsString += " ";
}
argsString += `"${arg}"`;
}
return argsString;
}
}
You can’t perform that action at this time.
