Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support to Log Message breakpoint #427

Merged
merged 2 commits into from
May 30, 2024
Merged
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,10 @@ differently based on whether the remote system is a POSIX or a Windows system.
You may need to experiment to find the correct escaping necessary for the command to be
sent to the debugger as you intended.

### LogMessage

LogMessage will print a message in the debug console when breakpoint is hit. Expressions within {} are interpolated.

![LogMessage](images/logMessage.gif)

## [Issues](https://github.com/WebFreak001/code-debug)
Binary file added images/logMessage.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/backend/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { DebugProtocol } from "vscode-debugprotocol/lib/debugProtocol";
export type ValuesFormattingMode = "disabled" | "parseText" | "prettyPrinters";

export interface Breakpoint {
id?:number;
file?: string;
line?: number;
raw?: string;
condition: string;
countCondition?: string;
logMessage?: string;
}

export interface Thread {
Expand Down
81 changes: 80 additions & 1 deletion src/backend/mi2/mi2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,50 @@ function couldBeOutput(line: string) {

const trace = false;

class LogMessage {
protected logMsgVar = "";
protected logMsgVarProcess = "";
protected logMsgRplNum = 0;
protected logMsgRplItem: string[] = [];
protected logMsgMatch = /(^\$[0-9]*[\ ]*=[\ ]*)(.*)/;
protected logReplaceTest = /{([^}]*)}/g;
public logMsgBrkList: Breakpoint[] = [];

logMsgOutput(record){
if ((record.type === 'console')) {
if(record.content.startsWith("$")){
const content = record.content;
const variableMatch = this.logMsgMatch.exec(content);
if (variableMatch) {
const value = content.substr(variableMatch[1].length).trim();
this.logMsgRplItem.push(value);
this.logMsgRplNum--;
if(this.logMsgRplNum == 0){
for(let i = 0; i < this.logMsgRplItem.length; i++){
this.logMsgVarProcess = this.logMsgVarProcess.replace("placeHolderForVariable", this.logMsgRplItem[i]);
}
return "Log Message:" + this.logMsgVarProcess;
}
}
}
return undefined;
}
}

logMsgProcess(parsed){
this.logMsgBrkList.forEach((brk)=>{
if(parsed.outOfBandRecord[0].output[0][1] == "breakpoint-hit" && parsed.outOfBandRecord[0].output[2][1] == brk.id){
this.logMsgVar = brk?.logMessage;
const matches = this.logMsgVar.match(this.logReplaceTest);
const count = matches ? matches.length : 0;
this.logMsgRplNum = count;
this.logMsgVarProcess = this.logMsgVar.replace(this.logReplaceTest, "placeHolderForVariable");
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not a huge fan of the magic string here and would much prefer if we could instead maybe split by the regex, so it's not possible to have this value accidentally in the output message (even if very unlikely)

this.logMsgRplItem = [];
}
});
}
}

export class MI2 extends EventEmitter implements IBackend {
constructor(public application: string, public preargs: string[], public extraargs: string[], procEnv: any, public extraCommands: string[] = []) {
super();
Expand All @@ -47,6 +91,7 @@ export class MI2 extends EventEmitter implements IBackend {
this.procEnv = env;
}
}
protected logMessage:LogMessage = new LogMessage;

load(cwd: string, target: string, procArgs: string, separateConsole: string, autorun: string[]): Thenable<any> {
if (!path.isAbsolute(target))
Expand Down Expand Up @@ -354,6 +399,10 @@ export class MI2 extends EventEmitter implements IBackend {
parsed.outOfBandRecord.forEach(record => {
if (record.isStream) {
this.log(record.type, record.content);
const logOutput = this.logMessage.logMsgOutput(record);
if(logOutput){
this.log("console", logOutput);
}
} else {
if (record.type == "exec") {
this.emit("exec-async-output", parsed);
Expand All @@ -373,6 +422,7 @@ export class MI2 extends EventEmitter implements IBackend {
switch (reason) {
case "breakpoint-hit":
this.emit("breakpoint", parsed);
this.logMessage.logMsgProcess(parsed);
break;
case "watchpoint-trigger":
case "read-watchpoint-trigger":
Expand Down Expand Up @@ -576,6 +626,22 @@ export class MI2 extends EventEmitter implements IBackend {
return this.sendCommand("break-condition " + bkptNum + " " + condition);
}

setLogPoint(bkptNum, command): Thenable<any> {
const regex = /{([a-z0-9A-Z-_\.\>\&\*\[\]]*)}/gm;
let m:RegExpExecArray;
let commands:string = "";

while ((m = regex.exec(command))) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
if (m[1]) {
commands += `\"print ${m[1]}\" `;
}
}
return this.sendCommand("break-commands " + bkptNum + " " + commands);
}

setEntryBreakPoint(entryPoint: string): Thenable<any> {
return this.sendCommand("break-insert -t -f " + entryPoint);
}
Expand Down Expand Up @@ -607,10 +673,12 @@ export class MI2 extends EventEmitter implements IBackend {
if (result.resultRecords.resultClass == "done") {
const bkptNum = parseInt(result.result("bkpt.number"));
const newBrk = {
id: bkptNum,
file: breakpoint.file ? breakpoint.file : result.result("bkpt.file"),
raw: breakpoint.raw,
line: parseInt(result.result("bkpt.line")),
condition: breakpoint.condition
condition: breakpoint.condition,
logMessage: breakpoint?.logMessage,
};
if (breakpoint.condition) {
this.setBreakPointCondition(bkptNum, breakpoint.condition).then((result) => {
Expand All @@ -621,6 +689,17 @@ export class MI2 extends EventEmitter implements IBackend {
resolve([false, undefined]);
}
}, reject);
} else if (breakpoint.logMessage) {
this.setLogPoint(bkptNum, breakpoint.logMessage).then((result) => {
if (result.resultRecords.resultClass == "done") {
breakpoint.id = newBrk.id;
this.breakpoints.set(newBrk, bkptNum);
this.logMessage.logMsgBrkList.push(breakpoint);
resolve([true, newBrk]);
} else {
resolve([false, undefined]);
}
}, reject);
} else {
this.breakpoints.set(newBrk, bkptNum);
resolve([true, newBrk]);
Expand Down
1 change: 1 addition & 0 deletions src/gdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class GDBDebugSession extends MI2DebugSession {
response.body.supportsEvaluateForHovers = true;
response.body.supportsSetVariable = true;
response.body.supportsStepBack = true;
response.body.supportsLogPoints = true;
this.sendResponse(response);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mibase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class MI2DebugSession extends DebugSession {
}
this.miDebugger.clearBreakPoints(path).then(() => {
const all = args.breakpoints.map(brk => {
return this.miDebugger.addBreakPoint({ file: path, line: brk.line, condition: brk.condition, countCondition: brk.hitCondition });
return this.miDebugger.addBreakPoint({ file: path, line: brk.line, condition: brk.condition, countCondition: brk.hitCondition, logMessage: brk.logMessage });
});
Promise.all(all).then(brkpoints => {
const finalBrks = [];
Expand Down
Loading