dsh-stm32 0.1.2 → 0.1.3
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 +59 -21
- package/README_EN.md +59 -21
- package/docs/development-notes.md +374 -3
- package/docs/reading-guide.md +87 -66
- package/lib/data/chips.js +38 -51
- package/lib/data/cmsis-sources.js +27 -1
- package/lib/data/device-facts.js +201 -0
- package/lib/data/stm32f1.js +94 -150
- package/lib/data/svd.js +627 -0
- package/lib/engine/assets.js +924 -0
- package/lib/engine/breakpoints.js +242 -0
- package/lib/engine/debug-session.js +153 -5
- package/lib/engine/debug.js +75 -41
- package/lib/engine/doctor.js +2 -2
- package/lib/engine/flash.js +31 -59
- package/lib/engine/ioc-plan.js +5 -5
- package/lib/engine/probe-error.js +212 -0
- package/lib/engine/probe-session.js +311 -0
- package/lib/engine/register-template.js +32 -8
- package/lib/engine/regs.js +2 -0
- package/lib/engine/rtt.js +600 -0
- package/lib/index.js +8 -0
- package/lib/tools/assets.js +341 -0
- package/lib/tools/cache.js +8 -0
- package/lib/tools/debug-session.js +40 -5
- package/lib/tools/debug.js +41 -11
- package/lib/tools/doctor.js +11 -0
- package/lib/tools/flash.js +15 -6
- package/lib/tools/generate.js +3 -0
- package/lib/tools/ioc-apply.js +2 -0
- package/lib/tools/ioc-inspect.js +2 -0
- package/lib/tools/ioc-plan.js +3 -0
- package/lib/tools/project-create.js +24 -0
- package/lib/tools/regs.js +453 -61
- package/lib/tools/route-plan.js +3 -0
- package/lib/tools/rtt.js +347 -0
- package/package.json +1 -1
- package/templates/register/CMakeLists.txt +7 -1
- package/templates/register/Core/Inc/rtt.h +103 -0
- package/templates/register/Core/Src/main.c +25 -0
- package/templates/register/Core/Src/rtt.c +297 -0
- package/templates/scaffold/RTT.md +86 -0
- package/templates/scaffold/cmake/user/CMakeLists.txt +7 -0
- package/lib/data/f1-ram.generated.js +0 -34
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L3:stm32_assets —— 把资源仓库里"会随上游长大"的那部分按需下到本地。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不塞进 npm 包:device 事实层(chips.json / memory.json / svd/<line>.json)、14 份 ioc 模板、
|
|
5
|
+
* 12 份链接脚本已经是 90+ 个文件、几百 KB,chipdb/diagrams 更多;打进包里等于每次改一颗芯片就
|
|
6
|
+
* 发一次版。所以放独立仓库,按 profile 下载 + sha256 校验 + 本地缓存,装好之后完全离线可用。
|
|
7
|
+
*
|
|
8
|
+
* 四个动作是给用户看的,不是藏在 error 里的:
|
|
9
|
+
* · list 本地装了哪些版本、profile=core 的文件齐不齐、占多大;
|
|
10
|
+
* · refresh 拉 manifest → 逐文件下载 → 逐文件 sha256 → 全绿才落盘(默认 core);
|
|
11
|
+
* · verify 对本地文件逐个复核 sha256(怀疑装坏了就跑它);
|
|
12
|
+
* · prune 只留当前与上一版(版本目录会越攒越多)。
|
|
13
|
+
*
|
|
14
|
+
* 没有内置表可退(2026-10-31 用户决定「内置表删了吧」):设备事实只从本地缓存读,所以 refresh 失败
|
|
15
|
+
* 必须返回 ok:false + 人话原因,并告诉用户"先跑 stm32_assets refresh,或检查网络"——
|
|
16
|
+
* 装不上就等于没有芯片事实,没有任何兜底表能顶上。
|
|
17
|
+
*/
|
|
18
|
+
import fs from "node:fs";
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
import { assetsRootInfo, fetchManifest, listInstalledVersions, normalizeProfile, profileFiles, pruneOldVersions, readLocalManifest, resolveSources, verifyAndInstall, verifyInstalled, } from "../engine/assets.js";
|
|
21
|
+
export const ASSETS_TOOL = "stm32_assets";
|
|
22
|
+
/**
|
|
23
|
+
* 「没有内置表可退」话术:每次 refresh/verify 失败都要原样带上,别让模型自己编
|
|
24
|
+
* "没有资源包也能建工程"——内置表已经删了,装不上就真的没有芯片事实。
|
|
25
|
+
*/
|
|
26
|
+
const NO_BUILTIN_FALLBACK = "插件不再内置芯片表:设备事实(chips.json / memory.json / svd)只从资源包缓存读," +
|
|
27
|
+
"第一次用必须联网装一次(action=refresh)。现在没装成,建工程 / 体检 / 寄存器版模板都会明说" +
|
|
28
|
+
"「先跑 stm32_assets refresh,或检查网络」,不会退回内置表(已经删了)。";
|
|
29
|
+
const fileSchema = {
|
|
30
|
+
type: "object",
|
|
31
|
+
additionalProperties: false,
|
|
32
|
+
properties: {
|
|
33
|
+
path: { type: "string" },
|
|
34
|
+
bytes: { type: "number" },
|
|
35
|
+
sha256: { type: "string" },
|
|
36
|
+
status: { type: "string" },
|
|
37
|
+
detail: { type: "string" },
|
|
38
|
+
},
|
|
39
|
+
required: ["path", "bytes", "sha256", "status", "detail"],
|
|
40
|
+
};
|
|
41
|
+
const versionSchema = {
|
|
42
|
+
type: "object",
|
|
43
|
+
additionalProperties: false,
|
|
44
|
+
properties: {
|
|
45
|
+
version: { type: "string" },
|
|
46
|
+
files: { type: "number" },
|
|
47
|
+
bytes: { type: "number" },
|
|
48
|
+
profiles: { type: "array", items: { type: "string" } },
|
|
49
|
+
installedAt: { type: "string" },
|
|
50
|
+
current: { type: "boolean" },
|
|
51
|
+
},
|
|
52
|
+
required: ["version", "files", "bytes", "profiles", "installedAt", "current"],
|
|
53
|
+
};
|
|
54
|
+
export const outputSchema = {
|
|
55
|
+
type: "object",
|
|
56
|
+
additionalProperties: false,
|
|
57
|
+
properties: {
|
|
58
|
+
action: { type: "string" },
|
|
59
|
+
ok: { type: "boolean" },
|
|
60
|
+
root: { type: "string" },
|
|
61
|
+
origin: { type: "string" },
|
|
62
|
+
version: { type: "string" },
|
|
63
|
+
profile: { type: "string" },
|
|
64
|
+
source: { type: "string" },
|
|
65
|
+
fileCount: { type: "number" },
|
|
66
|
+
totalBytes: { type: "number" },
|
|
67
|
+
files: { type: "array", items: fileSchema },
|
|
68
|
+
versions: { type: "array", items: versionSchema },
|
|
69
|
+
attempts: { type: "array", items: { type: "string" } },
|
|
70
|
+
offlineFallback: { type: "string" },
|
|
71
|
+
message: { type: "string" },
|
|
72
|
+
},
|
|
73
|
+
required: [
|
|
74
|
+
"action",
|
|
75
|
+
"ok",
|
|
76
|
+
"root",
|
|
77
|
+
"origin",
|
|
78
|
+
"version",
|
|
79
|
+
"profile",
|
|
80
|
+
"source",
|
|
81
|
+
"fileCount",
|
|
82
|
+
"totalBytes",
|
|
83
|
+
"files",
|
|
84
|
+
"versions",
|
|
85
|
+
"attempts",
|
|
86
|
+
"offlineFallback",
|
|
87
|
+
"message",
|
|
88
|
+
],
|
|
89
|
+
};
|
|
90
|
+
function toVersionView(item) {
|
|
91
|
+
return {
|
|
92
|
+
version: item.version,
|
|
93
|
+
files: item.fileCount,
|
|
94
|
+
bytes: item.totalBytes,
|
|
95
|
+
profiles: item.profiles,
|
|
96
|
+
installedAt: item.installedAt,
|
|
97
|
+
current: item.current,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function describeBytes(bytes) {
|
|
101
|
+
if (bytes < 1024)
|
|
102
|
+
return String(bytes) + " B";
|
|
103
|
+
if (bytes < 1024 * 1024)
|
|
104
|
+
return (bytes / 1024).toFixed(1) + " KB";
|
|
105
|
+
return (bytes / 1024 / 1024).toFixed(2) + " MB";
|
|
106
|
+
}
|
|
107
|
+
export function registerAssets(ctx, options = {}) {
|
|
108
|
+
ctx.tools.register({
|
|
109
|
+
name: ASSETS_TOOL,
|
|
110
|
+
description: "按需下载资源包 dsh-stm32-assets(device 事实层 device/f1/chips.json·memory.json·svd/<line>.json、ioc 模板、链接脚本;profile=full 另含 chipdb/diagrams)到本地 ~/.dsh-stm32/assets/<version>/(DSH_STM32_ASSETS 可覆盖),逐文件按 manifest 的 sha256 校验,装好之后离线复用。action=list 看本地版本/文件/占用;refresh 拉 manifest 并按 profile 安装(默认 core,源顺序 GitHub → jsDelivr → GitCode 自动回退,报告里写明实际走的源);verify 逐个 sha256 复核;prune 只留当前与上一版。下载失败不抛错:ok=false + 人话原因,并告诉你先跑 refresh 或检查网络——插件不再内置芯片表,设备事实(含建工程要的型号/启动文件/容量)只从这份缓存读,第一次用要联网装一次。",
|
|
111
|
+
parameters: {
|
|
112
|
+
type: "object",
|
|
113
|
+
additionalProperties: false,
|
|
114
|
+
properties: {
|
|
115
|
+
action: {
|
|
116
|
+
type: "string",
|
|
117
|
+
enum: ["list", "refresh", "verify", "prune"],
|
|
118
|
+
description: "list(默认,看本地版本/文件/占用)/ refresh(拉 manifest 并安装)/ verify(逐个 sha256 复核)/ prune(只留当前与上一版)。",
|
|
119
|
+
},
|
|
120
|
+
profile: {
|
|
121
|
+
type: "string",
|
|
122
|
+
enum: ["core", "full"],
|
|
123
|
+
description: "core(默认:device/ + ioc/ + ld/,建工程够用)/ full(再加 chipdb/ + diagrams/)。",
|
|
124
|
+
},
|
|
125
|
+
source: {
|
|
126
|
+
type: "string",
|
|
127
|
+
description: "只走某个源:github / jsdelivr / gitcode,或一个完整 base URL;不填按内置顺序自动回退。",
|
|
128
|
+
},
|
|
129
|
+
version: {
|
|
130
|
+
type: "string",
|
|
131
|
+
description: "list / verify 指定版本;不填用本地最新一版。",
|
|
132
|
+
},
|
|
133
|
+
keep: {
|
|
134
|
+
type: "number",
|
|
135
|
+
minimum: 1,
|
|
136
|
+
description: "prune 保留几个版本,默认 2(当前 + 上一版)。",
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
required: [],
|
|
140
|
+
},
|
|
141
|
+
timeoutMs: 5 * 60_000,
|
|
142
|
+
isConcurrencySafe: () => false,
|
|
143
|
+
output: {
|
|
144
|
+
schema: outputSchema,
|
|
145
|
+
render: (_args, value) => {
|
|
146
|
+
const typed = value;
|
|
147
|
+
const lines = [typed.message];
|
|
148
|
+
lines.push("资源目录:" + typed.root + "(来源:" + typed.origin + ");profile=" + typed.profile);
|
|
149
|
+
if (typed.source !== "")
|
|
150
|
+
lines.push("本次来源:" + typed.source);
|
|
151
|
+
if (typed.versions.length > 0) {
|
|
152
|
+
const items = typed.versions.map((item) => item.version +
|
|
153
|
+
(item.current ? "(当前)" : "") +
|
|
154
|
+
":" + String(item.files) + " 个文件、" + describeBytes(item.bytes) +
|
|
155
|
+
",profile=" + item.profiles.join("+"));
|
|
156
|
+
lines.push("本地版本:" + items.join(";"));
|
|
157
|
+
}
|
|
158
|
+
if (typed.files.length > 0) {
|
|
159
|
+
const head = typed.files.slice(0, 10);
|
|
160
|
+
for (const file of head) {
|
|
161
|
+
lines.push("· " + file.path + "(" + describeBytes(file.bytes) + ")→ " + file.status + (file.detail === "" ? "" : ":" + file.detail));
|
|
162
|
+
}
|
|
163
|
+
if (typed.files.length > head.length)
|
|
164
|
+
lines.push("… 还有 " + String(typed.files.length - head.length) + " 个文件(action=list 看全量)");
|
|
165
|
+
lines.push("合计 " + String(typed.fileCount) + " 个文件、" + describeBytes(typed.totalBytes));
|
|
166
|
+
}
|
|
167
|
+
if (typed.attempts.length > 0)
|
|
168
|
+
lines.push("尝试记录:" + typed.attempts.join(" | "));
|
|
169
|
+
if (typed.offlineFallback !== "")
|
|
170
|
+
lines.push("没有内置表可退:" + typed.offlineFallback);
|
|
171
|
+
return [{ type: "text", text: lines.join("\n") }];
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
async execute(rawArgs, exec) {
|
|
175
|
+
const args = rawArgs ?? {};
|
|
176
|
+
const action = (args.action ?? "list").trim().toLowerCase();
|
|
177
|
+
const env = options.env ?? process.env;
|
|
178
|
+
const fetchImpl = options.fetchImpl;
|
|
179
|
+
const info = assetsRootInfo(env);
|
|
180
|
+
const profile = normalizeProfile(args.profile);
|
|
181
|
+
const versionsOf = () => listInstalledVersions(env).map(toVersionView);
|
|
182
|
+
const base = () => ({
|
|
183
|
+
action,
|
|
184
|
+
ok: false,
|
|
185
|
+
root: info.root,
|
|
186
|
+
origin: info.origin,
|
|
187
|
+
version: "",
|
|
188
|
+
profile: profile ?? (args.profile ?? "").trim(),
|
|
189
|
+
source: "",
|
|
190
|
+
fileCount: 0,
|
|
191
|
+
totalBytes: 0,
|
|
192
|
+
files: [],
|
|
193
|
+
versions: versionsOf(),
|
|
194
|
+
attempts: [],
|
|
195
|
+
offlineFallback: "",
|
|
196
|
+
message: "",
|
|
197
|
+
});
|
|
198
|
+
if (profile === null) {
|
|
199
|
+
return {
|
|
200
|
+
...base(),
|
|
201
|
+
message: "不认识的 profile:" + String(args.profile) + "(只有 core / full)。什么都没做。",
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/* ---------------------------- list ---------------------------- */
|
|
205
|
+
if (action === "list") {
|
|
206
|
+
const value = base();
|
|
207
|
+
const state = readLocalManifest(env, args.version);
|
|
208
|
+
value.ok = true;
|
|
209
|
+
value.version = state.version;
|
|
210
|
+
value.source = state.install?.source ?? "";
|
|
211
|
+
if (state.manifest === null) {
|
|
212
|
+
value.offlineFallback = NO_BUILTIN_FALLBACK;
|
|
213
|
+
value.message =
|
|
214
|
+
"本地还没装资源包(" + info.root + "):" + state.error + "。action=refresh 下载 profile=core(device + ioc + ld,约 27 个文件)。";
|
|
215
|
+
return value;
|
|
216
|
+
}
|
|
217
|
+
for (const target of profileFiles(state.manifest, profile)) {
|
|
218
|
+
const localPath = path.join(state.versionDir, target.path);
|
|
219
|
+
let present = false;
|
|
220
|
+
try {
|
|
221
|
+
present = fs.statSync(localPath).isFile();
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
present = false;
|
|
225
|
+
}
|
|
226
|
+
value.files.push({
|
|
227
|
+
path: target.path,
|
|
228
|
+
bytes: target.bytes,
|
|
229
|
+
sha256: target.sha256,
|
|
230
|
+
status: present ? "present" : "missing",
|
|
231
|
+
detail: present ? "已装(未校验;action=verify 逐文件复核 sha256)" : "profile 里列了,但本地没有这个文件(action=refresh 修复)",
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
value.fileCount = value.files.length;
|
|
235
|
+
value.totalBytes = value.files.reduce((sum, file) => sum + file.bytes, 0);
|
|
236
|
+
value.message =
|
|
237
|
+
"本地已装 " + state.version + "(profile=" + profile + "):" + String(value.fileCount) + " 个文件、约 " +
|
|
238
|
+
describeBytes(value.totalBytes) + ";目录 " + state.versionDir + "。";
|
|
239
|
+
return value;
|
|
240
|
+
}
|
|
241
|
+
/* ---------------------------- refresh ---------------------------- */
|
|
242
|
+
if (action === "refresh") {
|
|
243
|
+
const value = base();
|
|
244
|
+
const sources = resolveSources(args.source);
|
|
245
|
+
const manifestResult = await fetchManifest({
|
|
246
|
+
sources,
|
|
247
|
+
...(fetchImpl === undefined ? {} : { fetchImpl }),
|
|
248
|
+
timeoutMs: 20_000,
|
|
249
|
+
signal: exec.signal,
|
|
250
|
+
});
|
|
251
|
+
value.attempts = [...manifestResult.attempts];
|
|
252
|
+
if (!manifestResult.ok || manifestResult.manifest === null || manifestResult.source === null) {
|
|
253
|
+
value.ok = false;
|
|
254
|
+
value.offlineFallback = NO_BUILTIN_FALLBACK;
|
|
255
|
+
value.message = "刷新失败:拉不到 manifest.json。" + manifestResult.error + "(设备事实只从资源包读,装不上就用不了;见下)";
|
|
256
|
+
return value;
|
|
257
|
+
}
|
|
258
|
+
const manifestSource = manifestResult.source;
|
|
259
|
+
// manifest 能拉到的源排在文件下载第一位:它证明这条线路是通的(自定义源时尤其重要)。
|
|
260
|
+
const ordered = [manifestSource, ...sources.filter((source) => source.id !== manifestSource.id)];
|
|
261
|
+
const install = await verifyAndInstall({
|
|
262
|
+
manifest: manifestResult.manifest,
|
|
263
|
+
manifestText: manifestResult.text,
|
|
264
|
+
profile,
|
|
265
|
+
sources: ordered,
|
|
266
|
+
env,
|
|
267
|
+
...(fetchImpl === undefined ? {} : { fetchImpl }),
|
|
268
|
+
timeoutMs: 60_000,
|
|
269
|
+
signal: exec.signal,
|
|
270
|
+
});
|
|
271
|
+
value.version = install.version;
|
|
272
|
+
value.source = install.source;
|
|
273
|
+
value.versions = versionsOf();
|
|
274
|
+
if (!install.ok) {
|
|
275
|
+
value.ok = false;
|
|
276
|
+
value.offlineFallback = NO_BUILTIN_FALLBACK;
|
|
277
|
+
const failureLines = install.failures.map((failure) => failure.path + ":" + failure.reason + (failure.attempts.length === 0 ? "" : "(" + failure.attempts.join(";") + ")"));
|
|
278
|
+
value.attempts = [...value.attempts, ...failureLines];
|
|
279
|
+
value.message =
|
|
280
|
+
"安装失败(manifest 来自 " + manifestSource.id + ":" + manifestSource.label + ")。" + install.message;
|
|
281
|
+
return value;
|
|
282
|
+
}
|
|
283
|
+
value.ok = true;
|
|
284
|
+
value.files = install.files.map((file) => ({
|
|
285
|
+
path: file.path,
|
|
286
|
+
bytes: file.bytes,
|
|
287
|
+
sha256: file.sha256,
|
|
288
|
+
status: "ok",
|
|
289
|
+
detail: "来自 " + file.source,
|
|
290
|
+
}));
|
|
291
|
+
value.fileCount = install.fileCount;
|
|
292
|
+
value.totalBytes = install.totalBytes;
|
|
293
|
+
value.message =
|
|
294
|
+
"已刷新:" + install.message + " manifest 来自 " + manifestSource.id + "(" + manifestSource.label + ")" +
|
|
295
|
+
(manifestResult.attempts.length === 0 ? ",第一源就通。" : ",前 " + String(manifestResult.attempts.length) + " 个源不通:" + manifestResult.attempts.join(";") + "。");
|
|
296
|
+
return value;
|
|
297
|
+
}
|
|
298
|
+
/* ---------------------------- verify ---------------------------- */
|
|
299
|
+
if (action === "verify") {
|
|
300
|
+
const value = base();
|
|
301
|
+
const result = verifyInstalled({
|
|
302
|
+
profile,
|
|
303
|
+
env,
|
|
304
|
+
...(args.version === undefined ? {} : { version: args.version }),
|
|
305
|
+
});
|
|
306
|
+
value.ok = result.ok;
|
|
307
|
+
value.version = result.version;
|
|
308
|
+
value.fileCount = result.fileCount;
|
|
309
|
+
value.totalBytes = result.totalBytes;
|
|
310
|
+
value.files = result.files.map((file) => ({
|
|
311
|
+
path: file.path,
|
|
312
|
+
bytes: file.bytes,
|
|
313
|
+
sha256: file.sha256,
|
|
314
|
+
status: file.status,
|
|
315
|
+
detail: file.detail,
|
|
316
|
+
}));
|
|
317
|
+
value.message = result.message;
|
|
318
|
+
if (!result.ok) {
|
|
319
|
+
value.offlineFallback = result.version === "" ? NO_BUILTIN_FALLBACK : "本地已有版本 " + result.version + ",action=refresh 重装这一版即可;插件不再内置芯片表,重装前芯片事实可能不全。";
|
|
320
|
+
}
|
|
321
|
+
return value;
|
|
322
|
+
}
|
|
323
|
+
/* ---------------------------- prune ---------------------------- */
|
|
324
|
+
if (action === "prune") {
|
|
325
|
+
const value = base();
|
|
326
|
+
const keep = typeof args.keep === "number" && Number.isFinite(args.keep) ? Math.floor(args.keep) : 2;
|
|
327
|
+
const pruned = pruneOldVersions(env, keep);
|
|
328
|
+
value.ok = true;
|
|
329
|
+
value.versions = versionsOf();
|
|
330
|
+
value.version = pruned.kept[0] ?? "";
|
|
331
|
+
value.totalBytes = pruned.reclaimedBytes;
|
|
332
|
+
value.message = pruned.message;
|
|
333
|
+
return value;
|
|
334
|
+
}
|
|
335
|
+
// action 拼错不该被当成某个破坏性动作:说清有哪些,什么都不做。
|
|
336
|
+
const value = base();
|
|
337
|
+
value.message = "不认识的 action:" + action + "(只有 list / refresh / verify / prune)。什么都没做。";
|
|
338
|
+
return value;
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
}
|
package/lib/tools/cache.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* 而不是藏在 error 信息里。
|
|
7
7
|
*/
|
|
8
8
|
import { cacheRootInfo, checkCacheEntry, describeCacheBytes, pruneLegacyEntries, readCacheIndex, removeCacheEntry, resolveCacheEntry, } from "../engine/cache.js";
|
|
9
|
+
import { ensureAssets } from "../engine/assets.js";
|
|
9
10
|
import { ensureCmsisTemplate } from "../engine/register-template.js";
|
|
10
11
|
export const CACHE_TOOL = "stm32_cache";
|
|
11
12
|
const entrySchema = {
|
|
@@ -145,6 +146,13 @@ export function registerCache(ctx) {
|
|
|
145
146
|
if ((args.mcu ?? "") === "" && (args.family ?? "") === "") {
|
|
146
147
|
return empty("fetch 至少给一个 mcu(例如 STM32F103C8T6)或 family(例如 F1)。");
|
|
147
148
|
}
|
|
149
|
+
// cmsis 条目 id 里带着启动文件与设备宏(cmsis:F1:103xb),那要靠设备事实算:
|
|
150
|
+
// 插件不再内置芯片表,所以先把资源包缓存准备好;拿不到就明说,别让它报"认不出这颗芯片"
|
|
151
|
+
// (那个文案会让人以为型号写错了)。
|
|
152
|
+
const assets = await ensureAssets({ signal: exec.signal });
|
|
153
|
+
if (!assets.ok) {
|
|
154
|
+
return empty("认芯片型号需要设备事实,但资源包还没就绪:" + assets.message + " 装好后重试 action=fetch。");
|
|
155
|
+
}
|
|
148
156
|
const result = await ensureCmsisTemplate({
|
|
149
157
|
...(args.mcu === undefined ? {} : { mcu: args.mcu }),
|
|
150
158
|
...(args.family === undefined ? {} : { family: args.family }),
|
|
@@ -8,12 +8,24 @@
|
|
|
8
8
|
* 会话在 `turn/end`(agent 输出完、等你回复)时由 index.ts 自动关掉,所以你想在 VS Code 里按 F5 时
|
|
9
9
|
* 探针一定是空闲的。build / flash / generate 动手前也会先让会话让出探针。
|
|
10
10
|
*/
|
|
11
|
+
import { F1_BREAKPOINT_BUDGET, quotaOf } from "../engine/breakpoints.js";
|
|
11
12
|
import { debugSessions } from "../engine/debug-session.js";
|
|
12
13
|
import { ensureToolchain, gateReportNotes } from "../engine/toolchain-gate.js";
|
|
13
14
|
import { resolveProjectDir } from "./project-dir.js";
|
|
14
15
|
export const DEBUG_SESSION_TOOL = "stm32_debug_session";
|
|
15
16
|
export const DEBUG_EVAL_TOOL = "stm32_debug_eval";
|
|
16
17
|
export const DEBUG_STOP_TOOL = "stm32_debug_stop";
|
|
18
|
+
/** 断点预算三元组(B4):与 stm32_debug_run 的报告同一个形状。 */
|
|
19
|
+
const quotaSchema = {
|
|
20
|
+
type: "object",
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
properties: {
|
|
23
|
+
used: { type: "number" },
|
|
24
|
+
limit: { type: "number" },
|
|
25
|
+
remaining: { type: "number" },
|
|
26
|
+
},
|
|
27
|
+
required: ["used", "limit", "remaining"],
|
|
28
|
+
};
|
|
17
29
|
export const sessionSchema = {
|
|
18
30
|
type: "object",
|
|
19
31
|
additionalProperties: false,
|
|
@@ -22,18 +34,23 @@ export const sessionSchema = {
|
|
|
22
34
|
active: { type: "boolean" },
|
|
23
35
|
sessionId: { type: "string" },
|
|
24
36
|
port: { type: "number" },
|
|
37
|
+
tclPort: { type: "number" },
|
|
25
38
|
projectDir: { type: "string" },
|
|
39
|
+
configFile: { type: "string" },
|
|
26
40
|
elfPath: { type: "string" },
|
|
27
41
|
startedAt: { type: "number" },
|
|
28
42
|
idleTtlMs: { type: "number" },
|
|
29
43
|
hardTtlMs: { type: "number" },
|
|
30
44
|
remainingMs: { type: "number" },
|
|
45
|
+
// B4:这次会话登记的断点数量与芯片上限(会话不跨 gdb 进程保存断点,但预算先校验)。
|
|
46
|
+
breakpoints: quotaSchema,
|
|
47
|
+
watchpoints: quotaSchema,
|
|
31
48
|
errors: { type: "array", items: { type: "string" } },
|
|
32
49
|
message: { type: "string" },
|
|
33
50
|
},
|
|
34
51
|
required: [
|
|
35
|
-
"ok", "active", "sessionId", "port", "projectDir", "elfPath", "startedAt",
|
|
36
|
-
"idleTtlMs", "hardTtlMs", "remainingMs", "errors", "message",
|
|
52
|
+
"ok", "active", "sessionId", "port", "tclPort", "projectDir", "configFile", "elfPath", "startedAt",
|
|
53
|
+
"idleTtlMs", "hardTtlMs", "remainingMs", "breakpoints", "watchpoints", "errors", "message",
|
|
37
54
|
],
|
|
38
55
|
};
|
|
39
56
|
export const outputSchema = {
|
|
@@ -83,7 +100,10 @@ const ENV_CHECK_SCHEMA = {
|
|
|
83
100
|
function describeSession(report) {
|
|
84
101
|
const lines = [report.message];
|
|
85
102
|
if (report.active) {
|
|
86
|
-
|
|
103
|
+
const tclNote = report.tclPort > 0
|
|
104
|
+
? "(Tcl " + String(report.tclPort) + ",regs_read 走快路径)"
|
|
105
|
+
: "(没有 Tcl 口,regs_read 走 gdb)";
|
|
106
|
+
lines.push("会话 " + report.sessionId + ":端口 " + String(report.port) + tclNote + ",剩余租约 " + String(Math.ceil(report.remainingMs / 1000)) + " s(空闲 " + String(Math.round(report.idleTtlMs / 1000)) + " s / 总上限 " + String(Math.round(report.hardTtlMs / 1000)) + " s)");
|
|
87
107
|
if (report.elfPath !== "")
|
|
88
108
|
lines.push("符号:" + report.elfPath);
|
|
89
109
|
lines.push("→ 用 stm32_debug_eval 执行 gdb 命令;用完 stm32_debug_stop 立刻还探针(turn 结束也会自动关)");
|
|
@@ -99,11 +119,17 @@ function describeSession(report) {
|
|
|
99
119
|
*/
|
|
100
120
|
function blockedSession(projectDir, gate) {
|
|
101
121
|
return {
|
|
102
|
-
ok: false, active: false, sessionId: "", port: 0, projectDir, elfPath: "",
|
|
122
|
+
ok: false, active: false, sessionId: "", port: 0, tclPort: 0, projectDir, configFile: "", elfPath: "",
|
|
103
123
|
startedAt: 0, idleTtlMs: 0, hardTtlMs: 0, remainingMs: 0,
|
|
124
|
+
breakpoints: quotaOf(0, F1_BREAKPOINT_BUDGET.code),
|
|
125
|
+
watchpoints: quotaOf(0, F1_BREAKPOINT_BUDGET.data),
|
|
104
126
|
errors: gateReportNotes(gate).slice(1), message: gate.message,
|
|
105
127
|
};
|
|
106
128
|
}
|
|
129
|
+
/** eval 没跑起来(多半是起会话失败)时的干净报告:字段一个不少,宿主 schema 才吃。 */
|
|
130
|
+
function failEvalReport(message, errors) {
|
|
131
|
+
return { ok: false, output: "", command: "", durationMs: 0, remainingMs: debugSessions.status().remainingMs, errors, message };
|
|
132
|
+
}
|
|
107
133
|
export function registerDebugSession(ctx) {
|
|
108
134
|
ctx.tools.register({
|
|
109
135
|
name: DEBUG_SESSION_TOOL,
|
|
@@ -120,6 +146,7 @@ export function registerDebugSession(ctx) {
|
|
|
120
146
|
idleTtlMs: { type: "number", description: "空闲超时毫秒数,默认 90000。" },
|
|
121
147
|
hardTtlMs: { type: "number", description: "总时长上限毫秒数,默认 600000。" },
|
|
122
148
|
reset: { type: "string", enum: ["none", "halt", "run"], description: "起会话时对目标做什么,默认 none(不动芯片)。" },
|
|
149
|
+
breakpoints: { type: "array", items: { type: "string" }, description: "这次会话打算用的断点(\"main\"、\"watch:g_count\"):起会话之前先按芯片预算校验,F1 上限 6 个硬件断点 / 4 个观察点,超了不占探针直接失败。" },
|
|
123
150
|
binDirs: { type: "array", items: { type: "string" }, description: "工具链目录候选。" },
|
|
124
151
|
envCheck: ENV_CHECK_SCHEMA,
|
|
125
152
|
},
|
|
@@ -158,6 +185,7 @@ export function registerDebugSession(ctx) {
|
|
|
158
185
|
...(args.idleTtlMs === undefined ? {} : { idleTtlMs: args.idleTtlMs }),
|
|
159
186
|
...(args.hardTtlMs === undefined ? {} : { hardTtlMs: args.hardTtlMs }),
|
|
160
187
|
...(args.reset === undefined ? {} : { reset: args.reset }),
|
|
188
|
+
...(args.breakpoints === undefined ? {} : { breakpoints: args.breakpoints }),
|
|
161
189
|
...(args.binDirs === undefined ? {} : { binDirs: args.binDirs }),
|
|
162
190
|
// A9:记下是谁开的会话,turn/end 只关自己的那个。
|
|
163
191
|
...(exec.agent?.id === undefined ? {} : { owner: exec.agent.id }),
|
|
@@ -172,7 +200,7 @@ export function registerDebugSession(ctx) {
|
|
|
172
200
|
});
|
|
173
201
|
ctx.tools.register({
|
|
174
202
|
name: DEBUG_EVAL_TOOL,
|
|
175
|
-
description: "
|
|
203
|
+
description: "在调试会话里执行 gdb 命令(例如 \"print g_led_blink_count\"、\"info registers pc\"、\"bt\"、\"x/4xb 0x20000000\")。B3 起没有会话时会先起一个(空闲 90 s 自动关、turn 结束也关)并复用它的 gdb server,不重复起 openocd;每次仍临时起一个 gdb -batch:连上 → monitor halt → 执行 → monitor resume → detach,跑完目标继续运行、不在插件进程里留 gdb 进程。",
|
|
176
204
|
parameters: {
|
|
177
205
|
type: "object",
|
|
178
206
|
additionalProperties: false,
|
|
@@ -198,6 +226,13 @@ export function registerDebugSession(ctx) {
|
|
|
198
226
|
async execute(rawArgs, exec) {
|
|
199
227
|
// 同 A31:无参数调用兜成 {}。没带 commands/expression 时引擎会给出"没执行"的报告,不是崩溃。
|
|
200
228
|
const args = rawArgs ?? {};
|
|
229
|
+
// B3:eval 只复用会话里那个 gdb server,不另起 openocd(会话是唯一握着探针的进程)。
|
|
230
|
+
// 没有会话时不偷偷起一个:探针是共享资源,起会话要显式(stm32_debug_session 或 stm32_regs_read)。
|
|
231
|
+
const active = debugSessions.status();
|
|
232
|
+
if (!active.active) {
|
|
233
|
+
const message = "没有会话:先 stm32_debug_session 开一个(或用 stm32_debug_run 一次性跑)";
|
|
234
|
+
return { message, report: failEvalReport(message, ["没有调试会话(eval 只复用会话里的 gdb server,不自己起 openocd)"]) };
|
|
235
|
+
}
|
|
201
236
|
const report = await debugSessions.eval({
|
|
202
237
|
...(args.commands === undefined ? {} : { commands: args.commands }),
|
|
203
238
|
...(args.expression === undefined ? {} : { expression: args.expression }),
|
package/lib/tools/debug.js
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
* 与 stm32_flash 的分工:flash 只写不调;debug_run 只调不写(不 load),
|
|
5
5
|
* 停在 breakAt(默认 main)后抓寄存器/调用栈,收尾 monitor reset run + detach。
|
|
6
6
|
*/
|
|
7
|
+
import { F1_BREAKPOINT_BUDGET, quotaOf } from "../engine/breakpoints.js";
|
|
7
8
|
import { runDebugRun } from "../engine/debug.js";
|
|
9
|
+
import { PROBE_ERROR_SCHEMA, formatProbeError } from "../engine/probe-error.js";
|
|
8
10
|
import { releaseProbeFor } from "../engine/debug-session.js";
|
|
9
11
|
import { ensureToolchain, gateReportNotes } from "../engine/toolchain-gate.js";
|
|
10
12
|
import { resolveProjectDir } from "./project-dir.js";
|
|
@@ -18,6 +20,17 @@ const registerSchema = {
|
|
|
18
20
|
},
|
|
19
21
|
required: ["name", "value"],
|
|
20
22
|
};
|
|
23
|
+
/** 断点预算三元组(B4):硬件断点与观察点各一条。 */
|
|
24
|
+
const quotaSchema = {
|
|
25
|
+
type: "object",
|
|
26
|
+
additionalProperties: false,
|
|
27
|
+
properties: {
|
|
28
|
+
used: { type: "number" },
|
|
29
|
+
limit: { type: "number" },
|
|
30
|
+
remaining: { type: "number" },
|
|
31
|
+
},
|
|
32
|
+
required: ["used", "limit", "remaining"],
|
|
33
|
+
};
|
|
21
34
|
const reportSchema = {
|
|
22
35
|
type: "object",
|
|
23
36
|
additionalProperties: false,
|
|
@@ -29,23 +42,23 @@ const reportSchema = {
|
|
|
29
42
|
breakAt: { type: "string" },
|
|
30
43
|
hitBreakpoint: { type: "boolean" },
|
|
31
44
|
location: { type: "string" },
|
|
45
|
+
// B4:断点预算(芯片的上限与实际占用),超限时 used 停在上限、remaining=0。
|
|
46
|
+
breakpoints: quotaSchema,
|
|
47
|
+
watchpoints: quotaSchema,
|
|
32
48
|
registers: { type: "array", items: registerSchema },
|
|
33
49
|
backtrace: { type: "array", items: { type: "string" } },
|
|
34
50
|
locals: { type: "array", items: { type: "string" } },
|
|
35
51
|
gdbCommand: { type: "string" },
|
|
36
52
|
gdbOutput: { type: "string" },
|
|
37
53
|
openOcdLogTail: { type: "string" },
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
type: "string",
|
|
41
|
-
enum: ["", "no-probe", "probe-busy", "swd-locked", "flash-protected", "no-power", "target-mismatch", "unknown"],
|
|
42
|
-
},
|
|
54
|
+
// B2:失败时带结构化探针错误(与 stm32_flash 同一份分类)。可选对象,成功路径不出现。
|
|
55
|
+
probeError: PROBE_ERROR_SCHEMA,
|
|
43
56
|
durationMs: { type: "number" },
|
|
44
57
|
errors: { type: "array", items: { type: "string" } },
|
|
45
58
|
message: { type: "string" },
|
|
46
59
|
notes: { type: "array", items: { type: "string" } },
|
|
47
60
|
},
|
|
48
|
-
required: ["ok", "projectDir", "elfPath", "port", "breakAt", "hitBreakpoint", "location", "registers", "backtrace", "locals", "durationMs", "errors", "message", "notes"],
|
|
61
|
+
required: ["ok", "projectDir", "elfPath", "port", "breakAt", "hitBreakpoint", "location", "breakpoints", "watchpoints", "registers", "backtrace", "locals", "durationMs", "errors", "message", "notes"],
|
|
49
62
|
};
|
|
50
63
|
export const outputSchema = {
|
|
51
64
|
type: "object",
|
|
@@ -62,17 +75,26 @@ const ENV_CHECK_SCHEMA = {
|
|
|
62
75
|
enum: ["block", "warn", "off"],
|
|
63
76
|
description: "工具链检查:block 缺件就拒绝(默认)、warn 只警告、off 不检查",
|
|
64
77
|
};
|
|
78
|
+
export function debugReportForHost(report) {
|
|
79
|
+
const { probeError, ...rest } = report;
|
|
80
|
+
return probeError === null ? rest : { ...rest, probeError };
|
|
81
|
+
}
|
|
65
82
|
export function formatDebugReport(report) {
|
|
66
83
|
const lines = [];
|
|
67
84
|
lines.push((report.ok ? "调试快照完成" : "调试失败") + ":" + report.message);
|
|
68
85
|
if (report.elfPath !== "")
|
|
69
86
|
lines.push("固件:" + report.elfPath);
|
|
70
|
-
|
|
71
|
-
|
|
87
|
+
const probeError = report.probeError ?? null;
|
|
88
|
+
if (probeError !== null)
|
|
89
|
+
lines.push(formatProbeError(probeError));
|
|
72
90
|
if (report.port !== 0)
|
|
73
91
|
lines.push("端口:" + String(report.port) + ",断点:" + report.breakAt);
|
|
74
92
|
if (report.location !== "")
|
|
75
93
|
lines.push("位置:" + report.location);
|
|
94
|
+
if (report.breakpoints.limit > 0) {
|
|
95
|
+
lines.push("断点预算:" + String(report.breakpoints.used) + "/" + String(report.breakpoints.limit)
|
|
96
|
+
+ "(还剩 " + String(report.breakpoints.remaining) + ");观察点 " + String(report.watchpoints.used) + "/" + String(report.watchpoints.limit));
|
|
97
|
+
}
|
|
76
98
|
if (report.registers.length > 0)
|
|
77
99
|
lines.push("寄存器:" + report.registers.map((item) => item.name + "=" + item.value).join(" "));
|
|
78
100
|
if (report.backtrace.length > 0) {
|
|
@@ -102,6 +124,9 @@ function blockedReport(projectDir, gate) {
|
|
|
102
124
|
ok: false, projectDir, elfPath: "", port: 0, breakAt: "main",
|
|
103
125
|
hitBreakpoint: false, location: "", registers: [], backtrace: [], locals: [],
|
|
104
126
|
gdbCommand: "", gdbOutput: "", openOcdLogTail: "", durationMs: 0,
|
|
127
|
+
breakpoints: quotaOf(0, F1_BREAKPOINT_BUDGET.code),
|
|
128
|
+
watchpoints: quotaOf(0, F1_BREAKPOINT_BUDGET.data),
|
|
129
|
+
probeError: null,
|
|
105
130
|
errors: [], message: gate.message, notes: gateReportNotes(gate),
|
|
106
131
|
};
|
|
107
132
|
}
|
|
@@ -110,6 +135,9 @@ function emptyReport(message) {
|
|
|
110
135
|
ok: false, projectDir: "", elfPath: "", port: 0, breakAt: "main",
|
|
111
136
|
hitBreakpoint: false, location: "", registers: [], backtrace: [], locals: [],
|
|
112
137
|
gdbCommand: "", gdbOutput: "", openOcdLogTail: "", durationMs: 0,
|
|
138
|
+
breakpoints: quotaOf(0, F1_BREAKPOINT_BUDGET.code),
|
|
139
|
+
watchpoints: quotaOf(0, F1_BREAKPOINT_BUDGET.data),
|
|
140
|
+
probeError: null,
|
|
113
141
|
errors: [message], message, notes: [],
|
|
114
142
|
};
|
|
115
143
|
}
|
|
@@ -126,6 +154,7 @@ export function registerDebugRun(ctx) {
|
|
|
126
154
|
buildDir: { type: "string", description: "显式构建目录;缺省自动推断(优先 Debug)。" },
|
|
127
155
|
breakAt: { type: "string", description: "断点位置,默认 main;none 表示只连上读现状不停。" },
|
|
128
156
|
preCommands: { type: "array", items: { type: "string" }, description: "在 break 之后、continue 之前执行的 gdb 命令。" },
|
|
157
|
+
breakpoints: { type: "array", items: { type: "string" }, description: "额外断点(和 breakAt、preCommands 一起算硬件断点预算):\"main\"、\"HAL_Delay\"、\"watch:g_count\"(观察点)。F1 上限 6 个硬件断点 / 4 个观察点,超了会在起 openocd 之前直接失败并说明删哪个。" },
|
|
129
158
|
port: { type: "number", description: "gdb 端口;缺省 3333,占用则顺延。" },
|
|
130
159
|
configFile: { type: "string", description: "openocd 配置文件(相对工程目录)。" },
|
|
131
160
|
adapterSpeed: { type: "number", description: "adapter speed(kHz)。" },
|
|
@@ -149,7 +178,7 @@ export function registerDebugRun(ctx) {
|
|
|
149
178
|
const subprocess = ctx.get("subprocess");
|
|
150
179
|
if (subprocess === undefined) {
|
|
151
180
|
const report = emptyReport("宿主缺少 subprocess 服务,无法调试");
|
|
152
|
-
return { message: report.message, report };
|
|
181
|
+
return { message: report.message, report: debugReportForHost(report) };
|
|
153
182
|
}
|
|
154
183
|
const resolution = await resolveProjectDir(ctx, args.projectDir, exec);
|
|
155
184
|
// 门禁:调试要 openocd + GDB 两件。缺件且 envCheck=block 时不占探针、不起进程。
|
|
@@ -161,7 +190,7 @@ export function registerDebugRun(ctx) {
|
|
|
161
190
|
});
|
|
162
191
|
if (!gate.ok && gate.mode === "block") {
|
|
163
192
|
const blocked = blockedReport(resolution.projectDir, gate);
|
|
164
|
-
return { message: blocked.message, report: blocked };
|
|
193
|
+
return { message: blocked.message, report: debugReportForHost(blocked) };
|
|
165
194
|
}
|
|
166
195
|
// debug_run 自己也要独占探针;缺件退出时不动别人的会话(B16)。
|
|
167
196
|
const released = await releaseProbeFor("stm32_debug_run");
|
|
@@ -171,6 +200,7 @@ export function registerDebugRun(ctx) {
|
|
|
171
200
|
...(args.buildDir === undefined ? {} : { buildDir: args.buildDir }),
|
|
172
201
|
...(args.breakAt === undefined ? {} : { breakAt: args.breakAt }),
|
|
173
202
|
...(args.preCommands === undefined ? {} : { preCommands: args.preCommands }),
|
|
203
|
+
...(args.breakpoints === undefined ? {} : { breakpoints: args.breakpoints }),
|
|
174
204
|
...(args.port === undefined ? {} : { port: args.port }),
|
|
175
205
|
...(args.configFile === undefined ? {} : { configFile: args.configFile }),
|
|
176
206
|
...(args.adapterSpeed === undefined ? {} : { adapterSpeed: args.adapterSpeed }),
|
|
@@ -184,7 +214,7 @@ export function registerDebugRun(ctx) {
|
|
|
184
214
|
report.notes.push(released);
|
|
185
215
|
if (resolution.note !== null)
|
|
186
216
|
report.notes.push(resolution.note);
|
|
187
|
-
return { message: report.message, report };
|
|
217
|
+
return { message: report.message, report: debugReportForHost(report) };
|
|
188
218
|
},
|
|
189
219
|
});
|
|
190
220
|
}
|