Skip to content

Commit fd16456

Browse files
committed
rtt: allow printing defmt output with defmt-print
1 parent 441af06 commit fd16456

File tree

6 files changed

+47
-7
lines changed

6 files changed

+47
-7
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1134,10 +1134,11 @@
11341134
"type": {
11351135
"enum": [
11361136
"console",
1137-
"binary"
1137+
"binary",
1138+
"defmt"
11381139
],
11391140
"default": "console",
1140-
"description": "'console' with text input/output, 'binary' is for converting byte stream to other data types",
1141+
"description": "'console' with text input/output, 'binary' is for converting byte stream to other data types, 'defmt' uses defmt-print",
11411142
"type": "string"
11421143
},
11431144
"prompt": {

src/common.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -456,13 +456,14 @@ export class RTTServerHelper {
456456
});
457457
}
458458

459-
public emitConfigures(cfg: RTTConfiguration, obj: EventEmitter): boolean {
459+
public emitConfigures(cfg: RTTConfiguration, executable: string, obj: EventEmitter): boolean {
460460
let ret = false;
461461
if (cfg.enabled) {
462462
for (const dec of cfg.decoders) {
463463
if (dec.tcpPort || dec.tcpPorts) {
464464
obj.emit('event', new RTTConfigureEvent({
465465
type: 'socket',
466+
executable: executable,
466467
decoder: dec
467468
}));
468469
ret = true;

src/frontend/extension.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { MemoryContentProvider } from './memory_content_provider';
1212
import Reporting from '../reporting';
1313

1414
import { CortexDebugConfigurationProvider } from './configprovider';
15-
import { JLinkSocketRTTSource, SocketRTTSource, SocketSWOSource, PeMicroSocketSource } from './swo/sources/socket';
15+
import { JLinkSocketRTTSource, SocketRTTSource, SocketSWOSource, PeMicroSocketSource, DefmtSocketRTTSource } from './swo/sources/socket';
1616
import { FifoSWOSource } from './swo/sources/fifo';
1717
import { FileSWOSource } from './swo/sources/file';
1818
import { SerialSWOSource } from './swo/sources/serial';
@@ -770,7 +770,7 @@ export class CortexDebugExtension {
770770
private receivedRTTConfigureEvent(e: vscode.DebugSessionCustomEvent) {
771771
if (e.body.type === 'socket') {
772772
const decoder: RTTCommonDecoderOpts = e.body.decoder;
773-
if ((decoder.type === 'console') || (decoder.type === 'binary')) {
773+
if ((decoder.type === 'console') || (decoder.type === 'binary') || (decoder.type === 'defmt')) {
774774
Reporting.sendEvent('RTT', 'Source', 'Socket: Console');
775775
this.rttCreateTerninal(e, decoder as RTTConsoleDecoderOpts);
776776
} else {
@@ -793,6 +793,7 @@ export class CortexDebugExtension {
793793
// state.
794794
private createRTTSource(e: vscode.DebugSessionCustomEvent, tcpPort: string, channel: number): Promise<SocketRTTSource> {
795795
const mySession = CDebugSession.GetSession(e.session);
796+
const wsPath = e.session.workspaceFolder.uri.fsPath;
796797
return new Promise((resolve, reject) => {
797798
let src = mySession.rttPortMap[channel];
798799
if (src) {
@@ -801,6 +802,8 @@ export class CortexDebugExtension {
801802
}
802803
if (mySession.config.servertype === 'jlink') {
803804
src = new JLinkSocketRTTSource(tcpPort, channel);
805+
} else if (e.body.decoder.type === 'defmt') {
806+
src = new DefmtSocketRTTSource(tcpPort, channel, e.body.executable, wsPath);
804807
} else {
805808
src = new SocketRTTSource(tcpPort, channel);
806809
}

src/frontend/swo/sources/socket.ts

+35
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { parseHostPort } from '../../../common';
55
import * as vscode from 'vscode';
66
import { TextDecoder } from 'util';
77
import { setFlagsFromString } from 'v8';
8+
import * as ChildProcess from 'child_process';
9+
import * as Path from 'path';
810

911
const TimerInterval = 250;
1012
export class SocketSWOSource extends EventEmitter implements SWORTTSource {
@@ -135,6 +137,39 @@ export class SocketRTTSource extends SocketSWOSource {
135137
}
136138
}
137139

140+
export class DefmtSocketRTTSource extends SocketRTTSource {
141+
private process: ChildProcess.ChildProcess;
142+
private executable: string;
143+
private cwd: string;
144+
145+
protected processData(buffer: Buffer): void {
146+
this.process.stdin.write(buffer);
147+
}
148+
149+
public start(timeout = (1000 * 60 * 5)): Promise<void> {
150+
this.process = ChildProcess.spawn(
151+
'defmt-print',['-e', this.executable], { cwd: this.cwd }
152+
);
153+
154+
this.process.on('error', (e) => {
155+
(e as any).message = `Failed to launch defmt-print (is it installed?)`;
156+
this.emit('error', e);
157+
this.dispose();
158+
});
159+
160+
this.process.stdout.on('data', (buffer) => this.emit('data', buffer));
161+
162+
return super.start(timeout);
163+
}
164+
165+
constructor(tcpPort: string, public readonly channel: number, executable: string, wsPath: string) {
166+
super(tcpPort, channel);
167+
168+
this.cwd = wsPath;
169+
this.executable = Path.relative(wsPath, executable);
170+
}
171+
}
172+
138173
export class JLinkSocketRTTSource extends SocketRTTSource {
139174
constructor(tcpPort: string, public readonly channel: number) {
140175
super(tcpPort, channel);

src/jlink.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,6 @@ export class JLinkServerController extends EventEmitter implements GDBServerCont
222222
}
223223
public debuggerLaunchStarted(): void {}
224224
public debuggerLaunchCompleted(): void {
225-
this.rttHelper.emitConfigures(this.args.rttConfig, this);
225+
this.rttHelper.emitConfigures(this.args.rttConfig, this.args.executable, this);
226226
}
227227
}

src/openocd.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export class OpenOCDServerController extends EventEmitter implements GDBServerCo
307307
this.session = obj;
308308
}
309309
public debuggerLaunchCompleted(): void {
310-
const hasRtt = this.rttHelper.emitConfigures(this.args.rttConfig, this);
310+
const hasRtt = this.rttHelper.emitConfigures(this.args.rttConfig, this.args.executable, this);
311311
if (this.args.ctiOpenOCDConfig?.enabled) {
312312
this.ctiStopResume(CTIAction.init);
313313
}

0 commit comments

Comments
 (0)