ucmd_bootscan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* bootscan: live tail of the kernel's boot logs.
  2. *
  3. * SYS_BOOTLOG exposes three sources: the direct-LFB boot console text (the
  4. * `bootlog` command), the always-on COM1 capture (`seriallog`) and the boot
  5. * storage-scan kernel thread's own trail. Each tab keeps its own scrollback;
  6. * the Serial tab is the complete boot trail (it starts with the first kernel
  7. * line and keeps growing for later driver output, the deferred USB mounts and
  8. * the WiFi helper).
  9. *
  10. * The WM spawns this window before the first terminal, so the terminal is
  11. * created last and keeps the keyboard focus.
  12. *
  13. * Every tab follows the newest output while it is at the bottom; wheel,
  14. * PgUp/PgDn, Up/Down, Home and End scroll, and scrolling up stops the follow
  15. * until the bottom is reached again. Click a tab, press 1/2/3 or Tab to
  16. * switch; Esc closes the window.
  17. */
  18. #include "ucmd_lib.h"
  19. #include "ngui.h"
  20. #define POLL_MS 100
  21. #define LINE_PX 12
  22. #define PAD 8
  23. #define TAB_H 24
  24. #define FOOT_H 20
  25. enum { TAB_BOOT, TAB_SERIAL, TAB_SCAN, TAB_COUNT };
  26. struct logsrc {
  27. const char *label;
  28. int kind;
  29. char *text;
  30. unsigned int cap; /* text[] size (at least the kernel window) */
  31. unsigned int len; /* bytes held in text[] */
  32. unsigned int base; /* absolute offset of text[0] */
  33. unsigned int seen; /* absolute offset fetched up to */
  34. unsigned int total; /* source counters from the last poll */
  35. unsigned int rstart;
  36. unsigned int rcap;
  37. int active;
  38. int have_info;
  39. int fresh; /* new output arrived while the tab was hidden */
  40. int trim_lines; /* lines dropped from the front since the draw */
  41. };
  42. /* At least the kernel's window for each source (BOOTCON_LOG_PAGES,
  43. * BOOTCON_SERLOG_PAGES, BOOTCON_SCAN_PAGES in kernel/bootcon.h). */
  44. static char boot_text[32 * 1024];
  45. static char serial_text[128 * 1024];
  46. static char scan_text[16 * 1024];
  47. static struct logsrc src[TAB_COUNT] = {
  48. { .label = "Boot", .kind = SYS_BOOTLOG_BOOT,
  49. .text = boot_text, .cap = sizeof(boot_text) },
  50. { .label = "Serial", .kind = SYS_BOOTLOG_SERIAL,
  51. .text = serial_text, .cap = sizeof(serial_text) },
  52. { .label = "Scan", .kind = SYS_BOOTLOG_SCAN,
  53. .text = scan_text, .cap = sizeof(scan_text) },
  54. };
  55. static struct ngui ui;
  56. static struct ngui_scroll sc;
  57. static struct ngui_tabs tabbar = { .active = TAB_SERIAL };
  58. static struct ngui_tab tab_view[TAB_COUNT];
  59. static int cur = TAB_SERIAL;
  60. static int follow = 1;
  61. static int pending_tail;
  62. static int quit;
  63. static unsigned int count_lines(const struct logsrc *s)
  64. {
  65. unsigned int i, n = 0;
  66. if (!s->len)
  67. return 0;
  68. for (i = 0; i < s->len; i++)
  69. if (s->text[i] == '\n')
  70. n++;
  71. if (s->text[s->len - 1] != '\n')
  72. n++;
  73. return n;
  74. }
  75. static unsigned short line_color(const char *s, unsigned int n)
  76. {
  77. if (n >= 4 && s[0] == 'v' && s[1] == 'f' && s[2] == 's' &&
  78. s[3] == ':')
  79. return ui.theme.accent;
  80. if (n >= 4 && s[0] == 'b' && s[1] == 'l' && s[2] == 'k' &&
  81. s[3] == ':')
  82. return ui.theme.text_dim;
  83. if (n >= 6 && s[0] == '[' && s[1] == 'b' && s[2] == 'o' &&
  84. s[3] == 'o' && s[4] == 't' && s[5] == ']')
  85. return ui.theme.text_dim;
  86. return ui.theme.text;
  87. }
  88. /* Drop the oldest `drop` bytes, remembering how many text lines vanished so
  89. * an unfollowed view can keep its place. */
  90. static void trim_front(struct logsrc *s, unsigned int drop)
  91. {
  92. unsigned int i, lines = 0;
  93. if (drop > s->len)
  94. drop = s->len;
  95. for (i = 0; i < drop; i++)
  96. if (s->text[i] == '\n')
  97. lines++;
  98. for (i = 0; i + drop < s->len; i++)
  99. s->text[i] = s->text[i + drop];
  100. s->len -= drop;
  101. s->base += drop;
  102. s->trim_lines += (int)lines;
  103. }
  104. static void append_text(struct logsrc *s, const char *p, unsigned int n)
  105. {
  106. unsigned int i, need;
  107. if (n > s->cap) {
  108. unsigned int skip = n - s->cap;
  109. /* Not reachable with SYS_BOOTLOG_CHUNK-sized reads, but keep
  110. * the absolute mapping consistent if a buffer ever shrinks. */
  111. p += skip;
  112. s->base += skip;
  113. n = s->cap;
  114. }
  115. if (s->len + n > s->cap) {
  116. need = s->len + n - s->cap;
  117. /* Drop in quarter-buffer steps instead of copying the whole
  118. * buffer for every chunk once it is full. */
  119. if (need < s->cap / 4)
  120. need = s->cap / 4;
  121. trim_front(s, need);
  122. }
  123. for (i = 0; i < n; i++)
  124. s->text[s->len++] = p[i];
  125. }
  126. /* Fetch every byte that appeared since the last poll. Returns 1 when the
  127. * view changed. */
  128. static int pull(struct logsrc *s)
  129. {
  130. struct sys_bootlog info;
  131. int changed = 0;
  132. int passes = 0;
  133. if (u_bootlog_stat(s->kind, &info) != 0)
  134. return 0;
  135. if (!s->have_info || s->total != info.total ||
  136. s->rstart != info.start || s->rcap != info.capacity ||
  137. s->active != (int)info.active) {
  138. s->have_info = 1;
  139. s->total = info.total;
  140. s->rstart = info.start;
  141. s->rcap = info.capacity;
  142. s->active = (int)info.active;
  143. changed = 1;
  144. }
  145. if (!info.capacity)
  146. return changed;
  147. /* The source dropped bytes we still hold (the Serial window keeps the
  148. * newest bytes, the Scan ring overwrites). */
  149. if (info.start > s->base)
  150. trim_front(s, info.start - s->base);
  151. if (info.start > s->base) {
  152. s->base = info.start;
  153. s->len = 0;
  154. }
  155. s->seen = s->base + s->len;
  156. while (s->seen < info.total && passes++ < 64) {
  157. char chunk[SYS_BOOTLOG_CHUNK];
  158. int n = u_bootlog_read(s->kind, s->seen, chunk);
  159. if (n < 0) {
  160. /* The source wrapped between the two calls. */
  161. s->have_info = 0;
  162. break;
  163. }
  164. if (n == 0)
  165. break;
  166. append_text(s, chunk, (unsigned int)n);
  167. s->seen += (unsigned int)n;
  168. changed = 1;
  169. if ((unsigned int)n < SYS_BOOTLOG_CHUNK)
  170. break;
  171. }
  172. return changed;
  173. }
  174. static void draw_tabs(void)
  175. {
  176. struct ngui_rect hr = { 0, 0, ui.w, TAB_H };
  177. struct ngui_rect tr = { PAD, 3, ui.w - 2 * PAD, TAB_H - 6 };
  178. struct ngui_rect br = { 0, TAB_H - 1, ui.w, 1 };
  179. int i;
  180. ngui_fill_rect(&ui, hr, ui.theme.panel);
  181. ngui_fill_rect(&ui, br, ui.theme.border);
  182. tabbar.active = cur;
  183. for (i = 0; i < TAB_COUNT; i++) {
  184. tab_view[i].label = src[i].label;
  185. tab_view[i].fresh = src[i].fresh;
  186. }
  187. if (ngui_tabs(&ui, &tabbar, tr, tab_view, TAB_COUNT)) {
  188. cur = tabbar.active;
  189. src[cur].fresh = 0;
  190. src[cur].trim_lines = 0;
  191. sc.offset = 0;
  192. follow = 1;
  193. pending_tail = 1;
  194. }
  195. }
  196. static void select_tab(int i)
  197. {
  198. if (i < 0 || i >= TAB_COUNT || i == cur)
  199. return;
  200. cur = i;
  201. tabbar.active = i;
  202. src[i].fresh = 0;
  203. src[i].trim_lines = 0;
  204. sc.offset = 0;
  205. follow = 1;
  206. pending_tail = 1;
  207. ngui_invalidate(&ui);
  208. }
  209. static void draw_footer(struct ngui_rect r)
  210. {
  211. const char *status;
  212. const char *hint = "1/2/3 tabs | PgUp/PgDn | Esc";
  213. unsigned short color;
  214. int hint_w;
  215. struct logsrc *s = &src[cur];
  216. if (!s->have_info) {
  217. status = "waiting for the kernel log...";
  218. color = ui.theme.text_dim;
  219. } else if (!s->rcap) {
  220. status = s->kind == SYS_BOOTLOG_SCAN ?
  221. "boot scan log unavailable" : "log unavailable";
  222. color = ui.theme.warning;
  223. } else if (s->kind == SYS_BOOTLOG_BOOT) {
  224. status = s->active ? "boot console: live" :
  225. "boot console: full";
  226. color = s->active ? ui.theme.success : ui.theme.text_dim;
  227. } else if (s->kind == SYS_BOOTLOG_SERIAL) {
  228. status = s->rstart ? "serial log: live, oldest dropped" :
  229. "serial log: live";
  230. color = ui.theme.success;
  231. } else {
  232. status = s->active ? "live: boot scan running" :
  233. "boot scan finished";
  234. color = s->active ? ui.theme.success : ui.theme.text_dim;
  235. }
  236. ngui_fill_rect(&ui, r, ui.theme.panel);
  237. ngui_text(&ui, r.x + 2, r.y + 3, status, color);
  238. hint_w = (int)ngui_text_width_ui(&ui, hint);
  239. if (hint_w + 120 < r.w)
  240. ngui_text(&ui, r.x + r.w - hint_w - 2, r.y + 3, hint,
  241. ui.theme.text_dim);
  242. }
  243. static void draw(void)
  244. {
  245. struct ngui_rect body, foot;
  246. struct logsrc *s;
  247. int line_h = ngui_text_line_height(&ui, LINE_PX, 1);
  248. unsigned int lines;
  249. unsigned int pos = 0, idx = 0;
  250. int max, y;
  251. if (line_h < 10)
  252. line_h = 10;
  253. ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
  254. ui.theme.bg);
  255. draw_tabs();
  256. /* draw_tabs() may have switched the tab on a click this frame. */
  257. s = &src[cur];
  258. lines = count_lines(s);
  259. body.x = PAD;
  260. body.y = TAB_H + 2;
  261. body.w = ui.w - 2 * PAD;
  262. body.h = ui.h - body.y - FOOT_H - 2;
  263. if (body.w < 20)
  264. body.w = 20;
  265. if (body.h < 20)
  266. body.h = 20;
  267. foot.x = 0;
  268. foot.y = ui.h - FOOT_H;
  269. foot.w = ui.w;
  270. foot.h = FOOT_H;
  271. sc.content_h = (int)lines * line_h;
  272. if (s->trim_lines) {
  273. if (!follow) {
  274. int off = sc.offset - s->trim_lines * line_h;
  275. sc.offset = off > 0 ? off : 0;
  276. }
  277. s->trim_lines = 0;
  278. }
  279. ngui_scroll_begin(&ui, body, &sc);
  280. ngui_scroll_wheel(&ui, body, &sc);
  281. max = sc.content_h - sc.view_h;
  282. if (max < 0)
  283. max = 0;
  284. if (pending_tail && follow)
  285. ngui_scroll_to(&sc, max);
  286. pending_tail = 0;
  287. follow = sc.offset >= max - 1;
  288. while (pos < s->len) {
  289. unsigned int start = pos, end;
  290. while (pos < s->len && s->text[pos] != '\n')
  291. pos++;
  292. end = pos;
  293. y = body.y + (int)idx * line_h - sc.offset;
  294. if (y + line_h > body.y && y < body.y + body.h) {
  295. ngui_text_px(&ui, body.x + 2, y + 1, s->text + start,
  296. end - start,
  297. line_color(s->text + start, end - start),
  298. LINE_PX, 0, 0, 1);
  299. }
  300. idx++;
  301. if (pos < s->len && s->text[pos] == '\n')
  302. pos++;
  303. }
  304. ngui_scroll_end(&ui, body, &sc);
  305. draw_footer(foot);
  306. }
  307. static void scroll_keys(void)
  308. {
  309. int k;
  310. int max = sc.content_h - sc.view_h;
  311. int page = sc.view_h - 22;
  312. if (page < 22)
  313. page = 22;
  314. if (max < 0)
  315. max = 0;
  316. while ((k = ngui_key(&ui)) != 0) {
  317. int old = sc.offset;
  318. if (k == NGUI_KEY_ESC)
  319. quit = 1;
  320. else if (k >= '1' && k <= '3')
  321. select_tab(k - '1');
  322. else if (k == NGUI_KEY_TAB)
  323. select_tab((cur + 1) % TAB_COUNT);
  324. else if (k == NGUI_KEY_PAGE_UP)
  325. ngui_scroll_to(&sc, sc.offset - page);
  326. else if (k == NGUI_KEY_PAGE_DOWN)
  327. ngui_scroll_to(&sc, sc.offset + page);
  328. else if (k == NGUI_KEY_UP)
  329. ngui_scroll_to(&sc, sc.offset - 20);
  330. else if (k == NGUI_KEY_DOWN)
  331. ngui_scroll_to(&sc, sc.offset + 20);
  332. else if (k == NGUI_KEY_HOME)
  333. ngui_scroll_to(&sc, 0);
  334. else if (k == NGUI_KEY_END)
  335. ngui_scroll_to(&sc, max);
  336. if (sc.offset != old)
  337. ngui_invalidate(&ui);
  338. }
  339. }
  340. void _start(const char *arg)
  341. {
  342. struct win_info wi;
  343. unsigned int sw = 0, sh = 0;
  344. unsigned int w = 440, h = 300;
  345. unsigned int last_poll;
  346. (void)arg;
  347. if (u_screen_size(&sw, &sh) == 0) {
  348. if (sw > 80 && w > sw - 40)
  349. w = sw - 40;
  350. if (sh > 200 && h > sh - 120)
  351. h = sh - 120;
  352. }
  353. if (w < 260)
  354. w = 260;
  355. if (h < 180)
  356. h = 180;
  357. if (u_win_create(w, h, "Boot scan", &wi) != 0) {
  358. u_puts("bootscan: no graphics window\n");
  359. u_exit(1);
  360. }
  361. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  362. ngui_font_attach(&ui);
  363. sc.hide_bar = 0;
  364. last_poll = u_ticks_ms() - POLL_MS;
  365. for (;;) {
  366. unsigned int now = u_ticks_ms();
  367. int changed = 0;
  368. int active_changed = 0;
  369. int i;
  370. if ((int)(now - (last_poll + POLL_MS)) >= 0) {
  371. last_poll = now;
  372. for (i = 0; i < TAB_COUNT; i++) {
  373. if (!pull(&src[i]))
  374. continue;
  375. changed = 1;
  376. if (i == cur)
  377. active_changed = 1;
  378. else
  379. src[i].fresh = 1;
  380. }
  381. if (changed) {
  382. if (active_changed)
  383. pending_tail = 1;
  384. ngui_invalidate(&ui);
  385. }
  386. }
  387. if (!ngui_begin(&ui)) {
  388. ngui_end_ex(&ui, 0);
  389. u_sleep_ms(POLL_MS);
  390. continue;
  391. }
  392. scroll_keys();
  393. if (quit)
  394. break;
  395. draw();
  396. ngui_end(&ui);
  397. u_sleep_ms(POLL_MS);
  398. }
  399. u_win_close();
  400. u_exit(0);
  401. }