Skip to content

Commit

Permalink
Fix for #740
Browse files Browse the repository at this point in the history
  • Loading branch information
haneefdm committed Aug 27, 2023
1 parent 6200c61 commit 578e1a1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ChangeLog
# V1.12.1
* Fix for [#923: Local variables with same name between functions not tracking or updating context](https://github.com/Marus/cortex-debug/issues/923)
* Fix for [#740: Missing RTT Timestamp in logfile](https://github.com/Marus/cortex-debug/issues/740)
* Tag VSCode generated debug lines with a timestamp when `showDevDebugTimestamps` is set to true
* For `symbolFIles`, you can simply specify a elf file or specify an object that has other options like offsets. If you do not need any special options, `symbolFiles` is now a simple array of elf-file-names

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.12.1-pre3",
"version": "1.12.1-pre4",
"preview": false,
"activationEvents": [
"onDebugResolve:cortex-debug",
Expand Down
4 changes: 4 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,10 @@ export class HrTimer {
return this.start;
}

public static getNow(): bigint {
return process.hrtime.bigint();
}

public deltaNs(): string {
return (process.hrtime.bigint() - this.start).toString();
}
Expand Down
103 changes: 76 additions & 27 deletions src/frontend/rtt_terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export class RTTTerminal {
protected binaryFormatter: BinaryFormatter;
private source: SocketRTTSource;
public inUse = true;
protected logFd: number;
protected logFd: number = -1;
private startOfNewLine = true;
protected hrTimer: HrTimer = new HrTimer();
public get terminal(): vscode.Terminal {
return this.ptyTerm ? this.ptyTerm.terminal : null;
Expand All @@ -26,7 +27,6 @@ export class RTTTerminal {
this.createTerminal();
this.sanitizeEncodings(this.options);
this.connectToSource(src);
this.openLogFile();
}

private connectToSource(src: SocketRTTSource) {
Expand All @@ -51,9 +51,11 @@ export class RTTTerminal {
magentaWrite(`${src.connError.message}\n`, this.ptyTerm);
} else if (src.connected) {
this.source = src;
this.openLogFile();
} else {
src.once('connected', () => {
this.source = src;
this.openLogFile();
});
}
}
Expand All @@ -62,19 +64,19 @@ export class RTTTerminal {
this.source = null;
this.inUse = false;
if (!this.options.noclear && (this.logFd >= 0)) {
try { fs.closeSync(this.logFd); } catch { }
this.closeLogFd(false);
} else if ((this.logFd >= 0) && !this.startOfNewLine) {
this.writeLogFile(Buffer.from('\n'));
this.startOfNewLine = true;
}
this.logFd = -1;
this.ptyTerm.write(RESET + '\n');
magentaWrite(`RTT connection on TCP port ${this.options.tcpPort} ended. Waiting for next connection...`, this.ptyTerm);
}

private onData(data: Buffer) {
try {
if (this.logFd >= 0) {
fs.writeSync(this.logFd, data);
}
if (this.options.type === 'binary') {
this.writeLogFile(data);
this.binaryFormatter.writeBinary(data);
} else {
this.writeNonBinary(data);
Expand All @@ -86,8 +88,7 @@ export class RTTTerminal {
}

private openLogFile() {
this.logFd = -1;
if (this.options.logfile) {
if ((this.logFd < 0) && this.options.logfile) {
try {
this.logFd = fs.openSync(this.options.logfile, 'w');
}
Expand All @@ -96,31 +97,75 @@ export class RTTTerminal {
console.error(msg);
magentaWrite(msg, this.ptyTerm);
}
} else if ((this.logFd >= 0) && !this.options.logfile) {
// It is already open but new connection does not want logging anymore
this.closeLogFd(false);
}
}

private writeLogFile(data: Buffer) {
if (this.logFd >= 0) {
fs.writeSync(this.logFd, data);
}
}

private writeNonBinaryChunk(data: Buffer | string, ts: string) {
if (this.logFd >= 0) {
let str: string;
if ((typeof data !== 'string') && !(data instanceof String)) {
str = data.toString('utf8');
} else {
str = data as string;
}
if (ts) {
// We emulate what the terminal does adding a timestamp after all new lines
if (this.startOfNewLine) {
this.writeLogFile(Buffer.from(ts));
}
this.startOfNewLine = false;
if (str.endsWith('\n')) {
str = str.slice(0, str.length - 1);
this.startOfNewLine = true; // Get it the next time something is written
}
str = str.replace(/\n/g, '\n' + ts);
this.writeLogFile(Buffer.from(this.startOfNewLine ? str + '\n' : str));
} else {
this.writeLogFile(Buffer.from(str));
}
}

this.ptyTerm.writeWithHeader(data, ts);
}

private lastTime: bigint = BigInt(-1);
private lastTimeStr: string = '';
private writeNonBinary(buf: Buffer) {
let start = 0;
let time = '';
if (this.options.timestamp) {
time = this.hrTimer.createDateTimestamp() + ' ';
const now = HrTimer.getNow();
if (now !== this.lastTime) {
this.lastTime = now;
this.lastTimeStr = this.hrTimer.createDateTimestamp() + ' ';
}
time = this.lastTimeStr;
}

for (let ix = 1; ix < buf.length; ix++ ) {
if (buf[ix - 1] !== 0xff) { continue; }
const chr = buf[ix];
if (((chr >= 48) && (chr <= 57)) || ((chr >= 65) && (chr <= 90))) {
if (ix >= 1) {
this.ptyTerm.writeWithHeader(buf.slice(start, ix - 1), time);
this.writeNonBinaryChunk(buf.slice(start, ix - 1), time);
}
this.ptyTerm.write(`<switch to vTerm#${String.fromCharCode(chr)}>\n`);
this.writeNonBinaryChunk(`<switch to vTerm#${String.fromCharCode(chr)}>\n`, '');
buf = buf.slice(ix + 1);
ix = 0;
start = 0;
}
}
if (buf.length > 0) {
this.ptyTerm.writeWithHeader(buf, time);
this.writeNonBinaryChunk(buf, time);
}
}

Expand Down Expand Up @@ -182,6 +227,22 @@ export class RTTTerminal {
obj.iencoding = getTextEncoding(obj.iencoding);
}

private closeLogFd(reopen: boolean) {
if (this.logFd >= 0) {
try {
fs.closeSync(this.logFd);
}
catch (e) {
magentaWrite(`Error: closing fille ${e}\n`, this.ptyTerm);
}
this.logFd = -1;
this.startOfNewLine = true;
if (reopen) {
this.openLogFile();
}
}
}

// If all goes well, this will reset the terminal options. Label for the VSCode terminal has to match
// since there no way to rename it. If successful, tt will reset the Terminal options and mark it as
// used (inUse = true) as well
Expand All @@ -193,16 +254,7 @@ export class RTTTerminal {
this.inUse = true;
if (!this.options.noclear || (this.options.type !== options.type)) {
this.ptyTerm.clearTerminalBuffer();
try {
if (this.logFd >= 0) {
fs.closeSync(this.logFd);
this.logFd = -1;
}
this.openLogFile();
}
catch (e) {
magentaWrite(`Error: closing fille ${e}\n`, this.ptyTerm);
}
this.closeLogFd(true);
}
this.options = options;
this.ptyOptions = this.createTermOptions(newTermName);
Expand All @@ -215,10 +267,7 @@ export class RTTTerminal {

public dispose() {
this.ptyTerm.dispose();
if (this.logFd >= 0) {
try { fs.closeSync(this.logFd); } catch {}
this.logFd = -1;
}
this.closeLogFd(false);
}
}

Expand Down

0 comments on commit 578e1a1

Please sign in to comment.