dsh-stm32 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +256 -363
- package/README_EN.md +257 -357
- package/client/client.js +4340 -1
- package/docs/reading-guide.md +62 -0
- package/lib/client.js +4340 -1
- package/lib/engine/artifacts.js +123 -0
- package/lib/engine/breakpoint-hits.js +73 -0
- package/lib/engine/build.js +73 -3
- package/lib/engine/channel-tap.js +227 -0
- package/lib/engine/channels.js +48 -0
- package/lib/engine/clocks.js +192 -0
- package/lib/engine/debug-session.js +46 -1
- package/lib/engine/event-bus.js +158 -0
- package/lib/engine/event-log.js +335 -0
- package/lib/engine/fault-hints.js +146 -0
- package/lib/engine/interrupts.js +247 -0
- package/lib/engine/ioc-diff.js +369 -0
- package/lib/engine/ioc-sync.js +58 -0
- package/lib/engine/msys2-install.js +31 -6
- package/lib/engine/panel-actions.js +103 -0
- package/lib/engine/pinout.js +140 -0
- package/lib/engine/probe-info.js +95 -0
- package/lib/engine/progress.js +99 -0
- package/lib/engine/project-conf.js +199 -0
- package/lib/engine/session-context.js +19 -0
- package/lib/engine/source-slice.js +59 -0
- package/lib/engine/tool-events.js +119 -0
- package/lib/engine/watch-admin.js +101 -0
- package/lib/engine/watch-record.js +39 -0
- package/lib/engine/watch-recorder.js +116 -0
- package/lib/engine/web-api.js +886 -4
- package/lib/engine/web-routes.js +18 -1
- package/lib/engine/workspace-memo.js +41 -0
- package/lib/index.js +67 -24
- package/lib/tools/ioc-apply.js +9 -0
- package/lib/tools/project-create.js +28 -0
- package/lib/tools/project-dir.js +7 -1
- package/lib/tools/rtt.js +8 -0
- package/lib/tools/serial.js +22 -0
- package/lib/tools/watch.js +24 -2
- package/package.json +1 -1
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 产物与内存:读构建目录里的 elf / hex / bin,配上芯片的 Flash / RAM 容量,供调试台显示。
|
|
3
|
+
*
|
|
4
|
+
* 只读文件属性,不跑 size / objdump:内存占用来自上一次 `stm32_build` 的
|
|
5
|
+
* `--print-memory-usage` 结果(面板从事件流里取),避免为了一个数字再起一个子进程。
|
|
6
|
+
*/
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
9
|
+
import { basename, join } from "node:path";
|
|
10
|
+
import { findChipFact } from "../data/device-facts.js";
|
|
11
|
+
import { detectBuildDir, findElf } from "./build.js";
|
|
12
|
+
import { readIocCpn } from "./breakpoints.js";
|
|
13
|
+
function statFile(file) {
|
|
14
|
+
try {
|
|
15
|
+
const info = statSync(file);
|
|
16
|
+
return { name: basename(file), path: file, size: info.size, mtimeMs: info.mtimeMs, sha256: "" };
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** 给一个产物补上校验和;超过上限的文件保持空串,调用方据此提示(S62)。 */
|
|
23
|
+
function withHash(file) {
|
|
24
|
+
if (file === null)
|
|
25
|
+
return null;
|
|
26
|
+
return { ...file, sha256: hashFile(file) };
|
|
27
|
+
}
|
|
28
|
+
/** 与 elf 同名、扩展名不同的兄弟产物。 */
|
|
29
|
+
function sibling(elfPath, extension) {
|
|
30
|
+
const dot = elfPath.lastIndexOf(".");
|
|
31
|
+
const base = dot === -1 ? elfPath : elfPath.slice(0, dot);
|
|
32
|
+
return statFile(base + extension);
|
|
33
|
+
}
|
|
34
|
+
/** 超过这个大小的 elf 不算校验和:调试台每 10 秒轮询一次,不值得。 */
|
|
35
|
+
export const ARTIFACT_HASH_MAX_BYTES = 8 * 1024 * 1024;
|
|
36
|
+
/** 单个文件的 SHA-256(S68):构建摘要用它标出「这次 elf 和上次是不是同一个」。 */
|
|
37
|
+
export function sha256OfPath(filePath) {
|
|
38
|
+
const file = statFile(filePath);
|
|
39
|
+
return file === null ? "" : hashFile(file);
|
|
40
|
+
}
|
|
41
|
+
function hashFile(file) {
|
|
42
|
+
if (file.size > ARTIFACT_HASH_MAX_BYTES)
|
|
43
|
+
return "";
|
|
44
|
+
try {
|
|
45
|
+
return createHash("sha256").update(readFileSync(file.path)).digest("hex");
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return "";
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function listElves(buildDir) {
|
|
52
|
+
let names;
|
|
53
|
+
try {
|
|
54
|
+
names = readdirSync(buildDir);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
const files = [];
|
|
60
|
+
for (const name of names) {
|
|
61
|
+
if (!name.toLowerCase().endsWith(".elf"))
|
|
62
|
+
continue;
|
|
63
|
+
const file = statFile(join(buildDir, name));
|
|
64
|
+
if (file !== null)
|
|
65
|
+
files.push(file);
|
|
66
|
+
}
|
|
67
|
+
return files;
|
|
68
|
+
}
|
|
69
|
+
export function readArtifacts(projectDir, env = process.env) {
|
|
70
|
+
const dir = projectDir.trim();
|
|
71
|
+
if (dir === "")
|
|
72
|
+
return { ok: false, error: "projectDir 不能为空" };
|
|
73
|
+
const notes = [];
|
|
74
|
+
const buildDir = detectBuildDir(dir) ?? "";
|
|
75
|
+
if (buildDir === "") {
|
|
76
|
+
notes.push("还没有可识别的构建目录:先执行 stm32_build 或 cmake --preset。");
|
|
77
|
+
}
|
|
78
|
+
let elfInfo = null;
|
|
79
|
+
if (buildDir !== "")
|
|
80
|
+
elfInfo = findElf(buildDir);
|
|
81
|
+
const elf = withHash(elfInfo === null ? null : statFile(elfInfo.path));
|
|
82
|
+
const hex = withHash(elf === null ? null : sibling(elf.path, ".hex"));
|
|
83
|
+
const bin = withHash(elf === null ? null : sibling(elf.path, ".bin"));
|
|
84
|
+
const otherElves = buildDir === "" ? [] : listElves(buildDir).filter((file) => file.path !== elf?.path);
|
|
85
|
+
if (otherElves.length > 0) {
|
|
86
|
+
notes.push("构建目录里有 " + String(otherElves.length + 1) + " 个 .elf,当前显示按工程名/修改时间挑出的那个。");
|
|
87
|
+
}
|
|
88
|
+
const sha256 = elf?.sha256 ?? "";
|
|
89
|
+
if (elf !== null && sha256 === "") {
|
|
90
|
+
notes.push("elf 超过 " + String(Math.round(ARTIFACT_HASH_MAX_BYTES / 1024 / 1024)) + " MB,跳过校验和。");
|
|
91
|
+
}
|
|
92
|
+
const cpn = readIocCpn(dir);
|
|
93
|
+
let chip = null;
|
|
94
|
+
if (cpn !== "") {
|
|
95
|
+
const fact = findChipFact(cpn, env);
|
|
96
|
+
if (fact === null)
|
|
97
|
+
notes.push("资源包里没有 " + cpn + " 的容量数据,内存占用只能看构建输出。");
|
|
98
|
+
else
|
|
99
|
+
chip = { cpn, flashBytes: fact.flashBytes, ramBytes: fact.ramBytes };
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
notes.push("工程里没有 .ioc 的 Mcu.CPN,无法给出容量上限。");
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
ok: true,
|
|
106
|
+
report: {
|
|
107
|
+
projectDir: dir,
|
|
108
|
+
buildDir,
|
|
109
|
+
elf,
|
|
110
|
+
hex,
|
|
111
|
+
bin,
|
|
112
|
+
otherElves,
|
|
113
|
+
chip,
|
|
114
|
+
sha256,
|
|
115
|
+
notes,
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/** 构建目录存在但还没有产物的判断,供面板给出下一步。 */
|
|
120
|
+
export function hasBuildDir(projectDir) {
|
|
121
|
+
const dir = detectBuildDir(projectDir);
|
|
122
|
+
return dir !== null && existsSync(dir);
|
|
123
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 断点命中记录:从 gdb 的输出里认出「停在哪」,攒成一份可回看的短历史。
|
|
3
|
+
*
|
|
4
|
+
* 只解析文本,不订阅 MI 事件——eval 的两条路径(常驻 MI / gdb -batch)都产出同一种
|
|
5
|
+
* 停点描述,在这里统一处理,面板与命令行看到的是同一份记录。
|
|
6
|
+
*/
|
|
7
|
+
/** 历史长度:一次联调最多回看这么多条停点。 */
|
|
8
|
+
export const HIT_LIMIT = 50;
|
|
9
|
+
const BREAKPOINT_LINE = /^Breakpoint (\d+), (.+)$/u;
|
|
10
|
+
const WATCHPOINT_LINE = /^(?:Hardware |Software |)watchpoint (\d+): (.+)$/u;
|
|
11
|
+
const STOPPED_LINE = /已停下 —— (.+)$/u;
|
|
12
|
+
const OLD_VALUE = /^Old value = (.+)$/u;
|
|
13
|
+
const NEW_VALUE = /^New value = (.+)$/u;
|
|
14
|
+
/** 从一段 gdb 输出里抽出停点;不认识的行走掉。 */
|
|
15
|
+
export function parseStops(output) {
|
|
16
|
+
const stops = [];
|
|
17
|
+
const lines = output.split(/\r?\n/u);
|
|
18
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
19
|
+
const line = (lines[index] ?? "").trim();
|
|
20
|
+
if (line === "")
|
|
21
|
+
continue;
|
|
22
|
+
const plain = BREAKPOINT_LINE.exec(line);
|
|
23
|
+
if (plain !== null && plain[1] !== undefined && plain[2] !== undefined) {
|
|
24
|
+
stops.push({ id: Number(plain[1]), text: plain[2].trim() });
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const watched = WATCHPOINT_LINE.exec(line);
|
|
28
|
+
if (watched !== null && watched[1] !== undefined && watched[2] !== undefined) {
|
|
29
|
+
// 观察点命中后紧跟两行旧值/新值,拼进同一条更有用。
|
|
30
|
+
const parts = [watched[2].trim()];
|
|
31
|
+
const old = OLD_VALUE.exec((lines[index + 1] ?? "").trim());
|
|
32
|
+
const fresh = NEW_VALUE.exec((lines[index + 2] ?? "").trim());
|
|
33
|
+
if (old !== null && old[1] !== undefined)
|
|
34
|
+
parts.push("原值 " + old[1]);
|
|
35
|
+
if (fresh !== null && fresh[1] !== undefined)
|
|
36
|
+
parts.push("新值 " + fresh[1]);
|
|
37
|
+
stops.push({ id: Number(watched[1]), text: parts.join(",") });
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const stopped = STOPPED_LINE.exec(line);
|
|
41
|
+
if (stopped !== null && stopped[1] !== undefined) {
|
|
42
|
+
const detail = stopped[1].replace(/(本次 eval 结束会按默认再 resume.*$/u, "").trim();
|
|
43
|
+
if (detail !== "")
|
|
44
|
+
stops.push({ id: 0, text: detail });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return stops;
|
|
48
|
+
}
|
|
49
|
+
/** 环形历史:满了丢最旧的一条。 */
|
|
50
|
+
export class HitHistory {
|
|
51
|
+
items = [];
|
|
52
|
+
limit;
|
|
53
|
+
constructor(limit = HIT_LIMIT) {
|
|
54
|
+
this.limit = Math.max(1, Math.trunc(limit));
|
|
55
|
+
}
|
|
56
|
+
/** 把一段输出的停点追加进历史,返回本次新增条数。 */
|
|
57
|
+
record(output, nowMs = Date.now()) {
|
|
58
|
+
const stops = parseStops(output);
|
|
59
|
+
const time = new Date(nowMs).toISOString();
|
|
60
|
+
for (const stop of stops) {
|
|
61
|
+
this.items.push({ id: stop.id, text: stop.text, time: time });
|
|
62
|
+
if (this.items.length > this.limit)
|
|
63
|
+
this.items.splice(0, this.items.length - this.limit);
|
|
64
|
+
}
|
|
65
|
+
return stops.length;
|
|
66
|
+
}
|
|
67
|
+
list() {
|
|
68
|
+
return this.items.map((item) => ({ ...item }));
|
|
69
|
+
}
|
|
70
|
+
clear() {
|
|
71
|
+
this.items.length = 0;
|
|
72
|
+
}
|
|
73
|
+
}
|
package/lib/engine/build.js
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
9
9
|
import { join, resolve } from "node:path";
|
|
10
|
+
import { findChipFact } from "../data/device-facts.js";
|
|
10
11
|
import { resolveSearchConfig } from "../data/search-roots.js";
|
|
12
|
+
import { readIocCpn } from "./breakpoints.js";
|
|
11
13
|
import { runCapture, runStreaming } from "./subprocess-runner.js";
|
|
12
14
|
import { resolveToolIn } from "./probe.js";
|
|
13
15
|
import { envWithBinDirs } from "./tool-env.js";
|
|
@@ -35,6 +37,64 @@ export function parseMemoryUsage(text) {
|
|
|
35
37
|
}
|
|
36
38
|
return usages;
|
|
37
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* `arm-none-eabi-size` 的 berkeley 行:`text data bss dec hex filename`(S80)。
|
|
42
|
+
* 用于增量构建没链接、链接器没打印占用时,直接量一次已存在的 elf。
|
|
43
|
+
*/
|
|
44
|
+
export function parseBerkeleySize(text) {
|
|
45
|
+
for (const line of text.split(/\r?\n/u)) {
|
|
46
|
+
const parts = line.trim().split(/\s+/u);
|
|
47
|
+
if (parts.length < 5)
|
|
48
|
+
continue;
|
|
49
|
+
const [textBytes, dataBytes, bssBytes] = parts.slice(0, 3).map((item) => Number.parseInt(item, 10));
|
|
50
|
+
if (!Number.isFinite(textBytes) || !Number.isFinite(dataBytes) || !Number.isFinite(bssBytes))
|
|
51
|
+
continue;
|
|
52
|
+
return { text: textBytes, data: dataBytes, bss: bssBytes };
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 增量构建的占用兜底(S80):没有链接输出时,用同一套工具链的 size 量 elf。
|
|
58
|
+
*
|
|
59
|
+
* 只在「构建成功 + 有 elf + 日志里没有占用」时才跑;总量来自器件事实(同样只在有 .ioc 时才有)。
|
|
60
|
+
*/
|
|
61
|
+
async function measureElfUsage(subprocess, elfPath, binDirs, projectDir, signal) {
|
|
62
|
+
try {
|
|
63
|
+
const size = await resolveToolIn(subprocess, ["arm-none-eabi-size"], [...binDirs], signal);
|
|
64
|
+
if (size === null)
|
|
65
|
+
return [];
|
|
66
|
+
const captured = await runCapture(subprocess, {
|
|
67
|
+
argv: [size, elfPath],
|
|
68
|
+
cwd: projectDir,
|
|
69
|
+
env: envWithBinDirs([...binDirs]),
|
|
70
|
+
...(signal === undefined ? {} : { signal }),
|
|
71
|
+
});
|
|
72
|
+
if (captured.exitCode !== 0)
|
|
73
|
+
return [];
|
|
74
|
+
const parsed = parseBerkeleySize(captured.stdout);
|
|
75
|
+
if (parsed === null)
|
|
76
|
+
return [];
|
|
77
|
+
const chip = findChipFact(readIocCpn(projectDir));
|
|
78
|
+
const out = [];
|
|
79
|
+
const add = (region, usedBytes, totalBytes) => {
|
|
80
|
+
if (totalBytes === null || totalBytes <= 0)
|
|
81
|
+
return;
|
|
82
|
+
out.push({
|
|
83
|
+
region,
|
|
84
|
+
usedBytes,
|
|
85
|
+
totalBytes,
|
|
86
|
+
percent: Math.round((usedBytes / totalBytes) * 1000) / 10,
|
|
87
|
+
raw: region + ": " + String(usedBytes) + " B / " + String(totalBytes) + " B",
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
add("FLASH", parsed.text + parsed.data, chip?.flashBytes ?? null);
|
|
91
|
+
add("RAM", parsed.data + parsed.bss, chip?.ramBytes ?? null);
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
38
98
|
/** 从编译器/链接器输出里挑出错误行;重复行去重(同一头文件可能被多个 translation unit 报一次)。 */
|
|
39
99
|
export function parseBuildErrors(text) {
|
|
40
100
|
const errors = [];
|
|
@@ -342,8 +402,13 @@ export async function runBuild(subprocess, request) {
|
|
|
342
402
|
const durationMs = Date.now() - startedAt;
|
|
343
403
|
const log = outcome.stdout + "\n" + outcome.stderr;
|
|
344
404
|
const errors = parseBuildErrors(log);
|
|
345
|
-
const
|
|
405
|
+
const logMemory = parseMemoryUsage(log);
|
|
346
406
|
const elf = findElf(buildDir);
|
|
407
|
+
/* 增量构建不重新链接,链接器不会打印占用;用 size 现量一次(S80)。 */
|
|
408
|
+
const memoryUsage = logMemory.length === 0 && elf !== null && outcome.exitCode === 0
|
|
409
|
+
? await measureElfUsage(subprocess, elf.path, config.binDirs, projectDir, request.signal)
|
|
410
|
+
: logMemory;
|
|
411
|
+
const measured = logMemory.length === 0 && memoryUsage.length > 0;
|
|
347
412
|
const base = {
|
|
348
413
|
projectDir,
|
|
349
414
|
buildDir,
|
|
@@ -363,10 +428,15 @@ export async function runBuild(subprocess, request) {
|
|
|
363
428
|
if (selectionNote !== "")
|
|
364
429
|
notes.push(selectionNote);
|
|
365
430
|
}
|
|
366
|
-
if (memoryUsage.length > 0)
|
|
431
|
+
if (memoryUsage.length > 0) {
|
|
367
432
|
notes.push("占用:" + memoryUsage.map((item) => item.region + " " + String(item.percent) + "%").join(","));
|
|
368
|
-
|
|
433
|
+
// 占用不是链接器给的,就得说明来源,否则读者会以为这次真的链接了(S80)。
|
|
434
|
+
if (measured)
|
|
435
|
+
notes.push("本次没有重新链接;占用由 arm-none-eabi-size 直接量 elf 得到");
|
|
436
|
+
}
|
|
437
|
+
else if (elf !== null) {
|
|
369
438
|
notes.push("本次是增量构建,没有重新链接,因此没有内存占用数据(需要时用 clean 构建)");
|
|
439
|
+
}
|
|
370
440
|
if (outcome.spawnError !== null) {
|
|
371
441
|
return fail(request, "cmake 启动失败:" + outcome.spawnError, { ...base, errors: [outcome.spawnError] });
|
|
372
442
|
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 常驻通道:把串口与 RTT 的输出持续灌进事件流,而不是只在工具调用时取一段。
|
|
3
|
+
*
|
|
4
|
+
* 为什么放在服务端而不是面板轮询工具:面板一刷新就该有历史,且「一轮联调里
|
|
5
|
+
* 串口一直在冒字」不该依赖某个前台工具被调用。这里只做两件事:定时取增量、
|
|
6
|
+
* 通过 `emitChannel` 归成事件;解析与过滤全复用 serial / rtt 已有的实现。
|
|
7
|
+
*
|
|
8
|
+
* 串口 tap 自己维护文件偏移,不碰 `readSerial` 的读路径:rx.ndjson 是只追加的,
|
|
9
|
+
* 两边各读各的,互不抢游标。
|
|
10
|
+
*/
|
|
11
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { emitChannel } from "./channels.js";
|
|
14
|
+
import { currentSerialSession } from "./serial.js";
|
|
15
|
+
import { dumpRtt, rttStatus } from "./rtt.js";
|
|
16
|
+
/** 轮询间隔:串口 700 ms、RTT 2 s(每次 dump 都建一次 TCP 连接,不能太密)。 */
|
|
17
|
+
const SERIAL_POLL_MS = 700;
|
|
18
|
+
const RTT_POLL_MS = 2_000;
|
|
19
|
+
/** 单批上限:一次塞太多会把面板尾部挤满,剩下的下一拍再发。 */
|
|
20
|
+
const SERIAL_BATCH_LINES = 200;
|
|
21
|
+
/** 尾部指纹长度:桥重写文件时长度往往不变,只靠偏移判断会漏读或错读。 */
|
|
22
|
+
const SERIAL_TAIL_BYTES = 32;
|
|
23
|
+
const RTT_BATCH_RECORDS = 200;
|
|
24
|
+
function blank(name) {
|
|
25
|
+
return { active: false, lines: 0, lastAt: "", lastError: "", timer: null, offset: 0, carry: "", sourceId: "", tail: "", lineCarry: "" };
|
|
26
|
+
}
|
|
27
|
+
const taps = { serial: blank("serial"), rtt: blank("rtt") };
|
|
28
|
+
function stamp() {
|
|
29
|
+
return new Date().toISOString();
|
|
30
|
+
}
|
|
31
|
+
/** 读 rx.ndjson 里偏移之后的新增完整行;半行留到下一拍。 */
|
|
32
|
+
function readSerialIncrement(ipcDir) {
|
|
33
|
+
const file = join(ipcDir, "rx.ndjson");
|
|
34
|
+
if (!existsSync(file))
|
|
35
|
+
return { lines: [], reset: false };
|
|
36
|
+
const runtime = taps.serial;
|
|
37
|
+
const buffer = readFileSync(file);
|
|
38
|
+
let start = runtime.offset;
|
|
39
|
+
let reset = start > buffer.length;
|
|
40
|
+
// 偏移落在旧文件里但也可能「越界到别的字段」:用尾部指纹确认上一批还在原处。
|
|
41
|
+
// 桥在 open 之后重写 rx.ndjson,长度可能不变,只看长度会静默错位。
|
|
42
|
+
if (!reset && start > 0 && runtime.tail !== "") {
|
|
43
|
+
const from = Math.max(0, start - runtime.tail.length);
|
|
44
|
+
if (buffer.subarray(from, start).toString("utf8") !== runtime.tail)
|
|
45
|
+
reset = true;
|
|
46
|
+
}
|
|
47
|
+
if (reset)
|
|
48
|
+
start = 0;
|
|
49
|
+
const chunk = buffer.subarray(start);
|
|
50
|
+
const text = runtime.carry + chunk.toString("utf8");
|
|
51
|
+
const parts = text.split("\n");
|
|
52
|
+
runtime.carry = parts.pop() ?? "";
|
|
53
|
+
runtime.offset = buffer.length - Buffer.byteLength(runtime.carry, "utf8");
|
|
54
|
+
runtime.tail = buffer.subarray(Math.max(0, buffer.length - SERIAL_TAIL_BYTES)).toString("utf8");
|
|
55
|
+
const lines = [];
|
|
56
|
+
for (const part of parts) {
|
|
57
|
+
const raw = part.trim();
|
|
58
|
+
if (raw === "")
|
|
59
|
+
continue;
|
|
60
|
+
try {
|
|
61
|
+
const parsed = JSON.parse(raw);
|
|
62
|
+
const data = typeof parsed.d === "string" ? parsed.d : "";
|
|
63
|
+
if (data === "")
|
|
64
|
+
continue;
|
|
65
|
+
if (parsed.p === true) {
|
|
66
|
+
runtime.lineCarry += data;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
lines.push(runtime.lineCarry + data);
|
|
70
|
+
runtime.lineCarry = "";
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// 半行或被桥截断的行:跳过,等下一批。
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { lines, reset };
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 取一次串口增量并写事件流,返回本次发出的行数。
|
|
80
|
+
*
|
|
81
|
+
* 单独导出是为了单测能喂一个临时 ipc 目录,不必真的开串口。
|
|
82
|
+
*/
|
|
83
|
+
export function tapSerialOnce(ipcDir, sessionId, meta = {}) {
|
|
84
|
+
const runtime = taps.serial;
|
|
85
|
+
// 会话换了:偏移与残留都归零。
|
|
86
|
+
if (runtime.sourceId !== sessionId) {
|
|
87
|
+
runtime.sourceId = sessionId;
|
|
88
|
+
runtime.offset = 0;
|
|
89
|
+
runtime.carry = "";
|
|
90
|
+
runtime.tail = "";
|
|
91
|
+
runtime.lineCarry = "";
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const { lines, reset } = readSerialIncrement(ipcDir);
|
|
95
|
+
if (reset)
|
|
96
|
+
runtime.lastError = "串口输出文件被重写,从头读";
|
|
97
|
+
if (lines.length === 0)
|
|
98
|
+
return 0;
|
|
99
|
+
const batch = lines.slice(0, SERIAL_BATCH_LINES);
|
|
100
|
+
emitChannel({
|
|
101
|
+
channel: "serial",
|
|
102
|
+
lines: batch,
|
|
103
|
+
meta: { action: "stream", sessionId, ...meta },
|
|
104
|
+
source: "system",
|
|
105
|
+
});
|
|
106
|
+
runtime.lines += batch.length;
|
|
107
|
+
runtime.lastAt = stamp();
|
|
108
|
+
runtime.lastError = "";
|
|
109
|
+
return batch.length;
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
runtime.lastError = error instanceof Error ? error.message : String(error);
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function pollSerial() {
|
|
117
|
+
const runtime = taps.serial;
|
|
118
|
+
const session = currentSerialSession();
|
|
119
|
+
if (session === null) {
|
|
120
|
+
if (runtime.lastError !== "没有串口会话")
|
|
121
|
+
runtime.lastError = "没有串口会话";
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
tapSerialOnce(session.ipcDir, session.sessionId, { port: session.port });
|
|
125
|
+
}
|
|
126
|
+
async function pollRtt() {
|
|
127
|
+
const runtime = taps.rtt;
|
|
128
|
+
const snapshot = rttStatus();
|
|
129
|
+
if (!snapshot.running) {
|
|
130
|
+
if (runtime.lastError !== "RTT 没在跑")
|
|
131
|
+
runtime.lastError = "RTT 没在跑";
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
const dumped = await dumpRtt({ seconds: 0.5, maxRecords: RTT_BATCH_RECORDS });
|
|
136
|
+
if (!dumped.ok) {
|
|
137
|
+
runtime.lastError = dumped.message;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const records = dumped.records;
|
|
141
|
+
if (records.length === 0)
|
|
142
|
+
return;
|
|
143
|
+
emitChannel({
|
|
144
|
+
channel: "rtt",
|
|
145
|
+
lines: records.map((record) => "[" + record.level + "] " + record.text),
|
|
146
|
+
meta: { action: "stream", port: snapshot.port, addr: snapshot.addr },
|
|
147
|
+
source: "system",
|
|
148
|
+
});
|
|
149
|
+
runtime.lines += records.length;
|
|
150
|
+
runtime.lastAt = stamp();
|
|
151
|
+
runtime.lastError = "";
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
runtime.lastError = error instanceof Error ? error.message : String(error);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function startOne(name) {
|
|
158
|
+
const runtime = taps[name];
|
|
159
|
+
if (runtime.active)
|
|
160
|
+
return;
|
|
161
|
+
runtime.active = true;
|
|
162
|
+
runtime.lastError = "";
|
|
163
|
+
if (name === "serial") {
|
|
164
|
+
runtime.timer = setInterval(pollSerial, SERIAL_POLL_MS);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
let busy = false;
|
|
168
|
+
runtime.timer = setInterval(() => {
|
|
169
|
+
// dump 是 async 且要等窗口,重叠调用只会把 openocd 压满。
|
|
170
|
+
if (busy)
|
|
171
|
+
return;
|
|
172
|
+
busy = true;
|
|
173
|
+
void pollRtt().finally(() => {
|
|
174
|
+
busy = false;
|
|
175
|
+
});
|
|
176
|
+
}, RTT_POLL_MS);
|
|
177
|
+
}
|
|
178
|
+
const timer = runtime.timer;
|
|
179
|
+
if (timer !== null && typeof timer.unref === "function")
|
|
180
|
+
timer.unref();
|
|
181
|
+
}
|
|
182
|
+
function stopOne(name) {
|
|
183
|
+
const runtime = taps[name];
|
|
184
|
+
if (!runtime.active)
|
|
185
|
+
return;
|
|
186
|
+
runtime.active = false;
|
|
187
|
+
if (runtime.timer !== null)
|
|
188
|
+
clearInterval(runtime.timer);
|
|
189
|
+
runtime.timer = null;
|
|
190
|
+
}
|
|
191
|
+
/** 开/关常驻通道;返回操作后的状态。 */
|
|
192
|
+
export function setChannelTaps(request = {}) {
|
|
193
|
+
if (request.serial !== undefined) {
|
|
194
|
+
if (request.serial)
|
|
195
|
+
startOne("serial");
|
|
196
|
+
else
|
|
197
|
+
stopOne("serial");
|
|
198
|
+
}
|
|
199
|
+
if (request.rtt !== undefined) {
|
|
200
|
+
if (request.rtt)
|
|
201
|
+
startOne("rtt");
|
|
202
|
+
else
|
|
203
|
+
stopOne("rtt");
|
|
204
|
+
}
|
|
205
|
+
return channelTapStatus();
|
|
206
|
+
}
|
|
207
|
+
/** 当前状态(面板据此画开关)。 */
|
|
208
|
+
export function channelTapStatus() {
|
|
209
|
+
return ["serial", "rtt"].map((name) => ({
|
|
210
|
+
name,
|
|
211
|
+
active: taps[name].active,
|
|
212
|
+
lines: taps[name].lines,
|
|
213
|
+
lastAt: taps[name].lastAt,
|
|
214
|
+
lastError: taps[name].lastError,
|
|
215
|
+
}));
|
|
216
|
+
}
|
|
217
|
+
/** 停掉所有常驻通道;插件 dispose 与单测都用它。 */
|
|
218
|
+
export function stopChannelTaps() {
|
|
219
|
+
stopOne("serial");
|
|
220
|
+
stopOne("rtt");
|
|
221
|
+
}
|
|
222
|
+
/** 单测用:停掉并清空计数与偏移。 */
|
|
223
|
+
export function resetChannelTaps() {
|
|
224
|
+
stopChannelTaps();
|
|
225
|
+
taps.serial = blank("serial");
|
|
226
|
+
taps.rtt = blank("rtt");
|
|
227
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 实时通道事件:串口、RTT、变量监视的输出走同一条总线。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不另开一条 SSE:面板已经按 seq 续播,把通道输出也编进同一序号,
|
|
5
|
+
* 时间线、详情、证据目录就能共用一套断线与回放逻辑;代价是通道行不是独立流,
|
|
6
|
+
* 面板按 kind=channel:<名字> 过滤即可。
|
|
7
|
+
*
|
|
8
|
+
* 一次工具调用产生一条通道事件,最多 400 行、单行 400 字符:面板只需要看尾部,
|
|
9
|
+
* 完整原文仍在工具返回值与 rx.ndjson / RTT 记录里。
|
|
10
|
+
*/
|
|
11
|
+
import { emitEvent } from "./event-bus.js";
|
|
12
|
+
/** 中文标签写进摘要,面板与证据目录直接可读。 */
|
|
13
|
+
const CHANNEL_LABEL = {
|
|
14
|
+
serial: "串口",
|
|
15
|
+
rtt: "RTT",
|
|
16
|
+
watch: "监视",
|
|
17
|
+
};
|
|
18
|
+
export const CHANNEL_LINE_LIMIT = 400;
|
|
19
|
+
const CHANNEL_CHAR_LIMIT = 400;
|
|
20
|
+
function normalizeLines(lines) {
|
|
21
|
+
const nonEmpty = lines.map((line) => String(line)).filter((line) => line !== "");
|
|
22
|
+
const shown = [];
|
|
23
|
+
for (const text of nonEmpty) {
|
|
24
|
+
shown.push(text.length > CHANNEL_CHAR_LIMIT ? text.slice(0, CHANNEL_CHAR_LIMIT) + "…" : text);
|
|
25
|
+
if (shown.length >= CHANNEL_LINE_LIMIT)
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
return { total: nonEmpty.length, shown };
|
|
29
|
+
}
|
|
30
|
+
/** 追加一段通道输出;返回 null 表示没有内容可发。 */
|
|
31
|
+
export function emitChannel(chunk) {
|
|
32
|
+
const { total, shown } = normalizeLines(chunk.lines);
|
|
33
|
+
if (total === 0)
|
|
34
|
+
return null;
|
|
35
|
+
const label = CHANNEL_LABEL[chunk.channel];
|
|
36
|
+
const payload = { channel: chunk.channel, lines: shown };
|
|
37
|
+
if (chunk.meta !== undefined)
|
|
38
|
+
payload.meta = chunk.meta;
|
|
39
|
+
if (total > shown.length)
|
|
40
|
+
payload.omitted = total - shown.length;
|
|
41
|
+
return emitEvent({
|
|
42
|
+
source: chunk.source ?? "model",
|
|
43
|
+
kind: "channel:" + chunk.channel,
|
|
44
|
+
status: "info",
|
|
45
|
+
summary: label + " " + String(total) + " 行" + (total > shown.length ? "(面板保留前 " + String(shown.length) + " 行)" : ""),
|
|
46
|
+
payload,
|
|
47
|
+
});
|
|
48
|
+
}
|