ucmd_bootscan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 int cur = TAB_SERIAL;
  58. static int follow = 1;
  59. static int pending_tail;
  60. static int pressed_tab = -1;
  61. static int quit;
  62. static unsigned int count_lines(const struct logsrc *s)
  63. {
  64. unsigned int i, n = 0;
  65. if (!s->len)
  66. return 0;
  67. for (i = 0; i < s->len; i++)
  68. if (s->text[i] == '\n')
  69. n++;
  70. if (s->text[s->len - 1] != '\n')
  71. n++;
  72. return n;
  73. }
  74. static unsigned short line_color(const char *s, unsigned int n)
  75. {
  76. if (n >= 4 && s[0] == 'v' && s[1] == 'f' && s[2] == 's' &&
  77. s[3] == ':')
  78. return ui.theme.accent;
  79. if (n >= 4 && s[0] == 'b' && s[1] == 'l' && s[2] == 'k' &&
  80. s[3] == ':')
  81. return ui.theme.text_dim;
  82. if (n >= 6 && s[0] == '[' && s[1] == 'b' && s[2] == 'o' &&
  83. s[3] == 'o' && s[4] == 't' && s[5] == ']')
  84. return ui.theme.text_dim;
  85. return ui.theme.text;
  86. }
  87. /* Drop the oldest `drop` bytes, remembering how many text lines vanished so
  88. * an unfollowed view can keep its place. */
  89. static void trim_front(struct logsrc *s, unsigned int drop)
  90. {
  91. unsigned int i, lines = 0;
  92. if (drop > s->len)
  93. drop = s->len;
  94. for (i = 0; i < drop; i++)
  95. if (s->text[i] == '\n')
  96. lines++;
  97. for (i = 0; i + drop < s->len; i++)
  98. s->text[i] = s->text[i + drop];
  99. s->len -= drop;
  100. s->base += drop;
  101. s->trim_lines += (int)lines;
  102. }
  103. static void append_text(struct logsrc *s, const char *p, unsigned int n)
  104. {
  105. unsigned int i, need;
  106. if (n > s->cap) {
  107. unsigned int skip = n - s->cap;
  108. /* Not reachable with SYS_BOOTLOG_CHUNK-sized reads, but keep
  109. * the absolute mapping consistent if a buffer ever shrinks. */
  110. p += skip;
  111. s->base += skip;
  112. n = s->cap;
  113. }
  114. if (s->len + n > s->cap) {
  115. need = s->len + n - s->cap;
  116. /* Drop in quarter-buffer steps instead of copying the whole
  117. * buffer for every chunk once it is full. */
  118. if (need < s->cap / 4)
  119. need = s->cap / 4;
  120. trim_front(s, need);
  121. }
  122. for (i = 0; i < n; i++)
  123. s->text[s->len++] = p[i];
  124. }
  125. /* Fetch every byte that appeared since the last poll. Returns 1 when the
  126. * view changed. */
  127. static int pull(struct logsrc *s)
  128. {
  129. struct sys_bootlog info;
  130. int changed = 0;
  131. int passes = 0;
  132. if (u_bootlog_stat(s->kind, &info) != 0)
  133. return 0;
  134. if (!s->have_info || s->total != info.total ||
  135. s->rstart != info.start || s->rcap != info.capacity ||
  136. s->active != (int)info.active) {
  137. s->have_info = 1;
  138. s->total = info.total;
  139. s->rstart = info.start;
  140. s->rcap = info.capacity;
  141. s->active = (int)info.active;
  142. changed = 1;
  143. }
  144. if (!info.capacity)
  145. return changed;
  146. /* The source dropped bytes we still hold (the Serial window keeps the
  147. * newest bytes, the Scan ring overwrites). */
  148. if (info.start > s->base)
  149. trim_front(s, info.start - s->base);
  150. if (info.start > s->base) {
  151. s->base = info.start;
  152. s->len = 0;
  153. }
  154. s->seen = s->base + s->len;
  155. while (s->seen < info.total && passes++ < 64) {
  156. char chunk[SYS_BOOTLOG_CHUNK];
  157. int n = u_bootlog_read(s->kind, s->seen, chunk);
  158. if (n < 0) {
  159. /* The source wrapped between the two calls. */
  160. s->have_info = 0;
  161. break;
  162. }
  163. if (n == 0)
  164. break;
  165. append_text(s, chunk, (unsigned int)n);
  166. s->seen += (unsigned int)n;
  167. changed = 1;
  168. if ((unsigned int)n < SYS_BOOTLOG_CHUNK)
  169. break;
  170. }
  171. return changed;
  172. }
  173. static struct ngui_rect tab_rect(int i)
  174. {
  175. struct ngui_rect r;
  176. int x = PAD, j;
  177. r.y = 3;
  178. r.h = TAB_H - 6;
  179. for (j = 0; j < i; j++)
  180. x += (int)ngui_text_width_ui(&ui, src[j].label) + 32;
  181. r.x = x;
  182. r.w = (int)ngui_text_width_ui(&ui, src[i].label) + 30;
  183. return r;
  184. }
  185. static void draw_tabs(void)
  186. {
  187. struct ngui_rect hr = { 0, 0, ui.w, TAB_H };
  188. struct ngui_rect br = { 0, TAB_H - 1, ui.w, 1 };
  189. int i;
  190. ngui_fill_rect(&ui, hr, ui.theme.panel);
  191. ngui_fill_rect(&ui, br, ui.theme.border);
  192. for (i = 0; i < TAB_COUNT; i++) {
  193. struct ngui_rect r = tab_rect(i);
  194. int hot = ui.inside &&
  195. ngui_rect_contains(&r, ui.mouse_x, ui.mouse_y);
  196. int lh = ngui_text_line_height(&ui, LINE_PX, 0);
  197. int ty = r.y + (r.h - lh) / 2;
  198. unsigned short color;
  199. if (i == cur)
  200. ngui_fill_rect(&ui, r, ui.theme.panel_alt);
  201. else if (hot)
  202. ngui_fill_rect(&ui, r, ui.theme.button);
  203. color = (i == cur || hot) ? ui.theme.text : ui.theme.text_dim;
  204. if (ty < r.y)
  205. ty = r.y;
  206. ngui_text(&ui, r.x + 10, ty, src[i].label, color);
  207. if (i == cur)
  208. ngui_fill_rect(&ui, (struct ngui_rect){ r.x, TAB_H - 3,
  209. r.w, 2 },
  210. ui.theme.accent);
  211. if (src[i].fresh && i != cur)
  212. ngui_fill_round_rect(&ui, (struct ngui_rect){
  213. r.x + r.w - 12,
  214. r.y + r.h / 2 - 2, 4, 4 },
  215. 2, 2, 2, 2, ui.theme.accent);
  216. if (hot)
  217. ngui_set_cursor(&ui, CURSOR_DEFAULT);
  218. }
  219. }
  220. static int tab_hit(int x, int y)
  221. {
  222. int i;
  223. for (i = 0; i < TAB_COUNT; i++) {
  224. struct ngui_rect r = tab_rect(i);
  225. if (ngui_rect_contains(&r, x, y))
  226. return i;
  227. }
  228. return -1;
  229. }
  230. static void select_tab(int i)
  231. {
  232. if (i < 0 || i >= TAB_COUNT || i == cur)
  233. return;
  234. cur = i;
  235. src[i].fresh = 0;
  236. src[i].trim_lines = 0;
  237. sc.offset = 0;
  238. follow = 1;
  239. pending_tail = 1;
  240. ngui_invalidate(&ui);
  241. }
  242. static void tab_input(void)
  243. {
  244. int i;
  245. if (ui.pressed)
  246. pressed_tab = tab_hit(ui.press_x, ui.press_y);
  247. if (ui.released) {
  248. i = tab_hit(ui.release_x, ui.release_y);
  249. if (i >= 0 && i == pressed_tab)
  250. select_tab(i);
  251. pressed_tab = -1;
  252. }
  253. }
  254. static void draw_footer(struct ngui_rect r)
  255. {
  256. const char *status;
  257. const char *hint = "1/2/3 tabs | PgUp/PgDn | Esc";
  258. unsigned short color;
  259. int hint_w;
  260. struct logsrc *s = &src[cur];
  261. if (!s->have_info) {
  262. status = "waiting for the kernel log...";
  263. color = ui.theme.text_dim;
  264. } else if (!s->rcap) {
  265. status = s->kind == SYS_BOOTLOG_SCAN ?
  266. "boot scan log unavailable" : "log unavailable";
  267. color = ui.theme.warning;
  268. } else if (s->kind == SYS_BOOTLOG_BOOT) {
  269. status = s->active ? "boot console: live" :
  270. "boot console: full";
  271. color = s->active ? ui.theme.success : ui.theme.text_dim;
  272. } else if (s->kind == SYS_BOOTLOG_SERIAL) {
  273. status = s->rstart ? "serial log: live, oldest dropped" :
  274. "serial log: live";
  275. color = ui.theme.success;
  276. } else {
  277. status = s->active ? "live: boot scan running" :
  278. "boot scan finished";
  279. color = s->active ? ui.theme.success : ui.theme.text_dim;
  280. }
  281. ngui_fill_rect(&ui, r, ui.theme.panel);
  282. ngui_text(&ui, r.x + 2, r.y + 3, status, color);
  283. hint_w = (int)ngui_text_width_ui(&ui, hint);
  284. if (hint_w + 120 < r.w)
  285. ngui_text(&ui, r.x + r.w - hint_w - 2, r.y + 3, hint,
  286. ui.theme.text_dim);
  287. }
  288. static void draw(void)
  289. {
  290. struct ngui_rect body, foot;
  291. struct logsrc *s = &src[cur];
  292. int line_h = ngui_text_line_height(&ui, LINE_PX, 1);
  293. unsigned int lines = count_lines(s);
  294. unsigned int pos = 0, idx = 0;
  295. int max, y;
  296. if (line_h < 10)
  297. line_h = 10;
  298. ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
  299. ui.theme.bg);
  300. draw_tabs();
  301. body.x = PAD;
  302. body.y = TAB_H + 2;
  303. body.w = ui.w - 2 * PAD;
  304. body.h = ui.h - body.y - FOOT_H - 2;
  305. if (body.w < 20)
  306. body.w = 20;
  307. if (body.h < 20)
  308. body.h = 20;
  309. foot.x = 0;
  310. foot.y = ui.h - FOOT_H;
  311. foot.w = ui.w;
  312. foot.h = FOOT_H;
  313. sc.content_h = (int)lines * line_h;
  314. if (s->trim_lines) {
  315. if (!follow) {
  316. int off = sc.offset - s->trim_lines * line_h;
  317. sc.offset = off > 0 ? off : 0;
  318. }
  319. s->trim_lines = 0;
  320. }
  321. ngui_scroll_begin(&ui, body, &sc);
  322. ngui_scroll_wheel(&ui, body, &sc);
  323. max = sc.content_h - sc.view_h;
  324. if (max < 0)
  325. max = 0;
  326. if (pending_tail && follow)
  327. ngui_scroll_to(&sc, max);
  328. pending_tail = 0;
  329. follow = sc.offset >= max - 1;
  330. while (pos < s->len) {
  331. unsigned int start = pos, end;
  332. while (pos < s->len && s->text[pos] != '\n')
  333. pos++;
  334. end = pos;
  335. y = body.y + (int)idx * line_h - sc.offset;
  336. if (y + line_h > body.y && y < body.y + body.h) {
  337. ngui_text_px(&ui, body.x + 2, y + 1, s->text + start,
  338. end - start,
  339. line_color(s->text + start, end - start),
  340. LINE_PX, 0, 0, 1);
  341. }
  342. idx++;
  343. if (pos < s->len && s->text[pos] == '\n')
  344. pos++;
  345. }
  346. ngui_scroll_end(&ui, body, &sc);
  347. draw_footer(foot);
  348. }
  349. static void scroll_keys(void)
  350. {
  351. int k;
  352. int max = sc.content_h - sc.view_h;
  353. int page = sc.view_h - 22;
  354. if (page < 22)
  355. page = 22;
  356. if (max < 0)
  357. max = 0;
  358. while ((k = ngui_key(&ui)) != 0) {
  359. int old = sc.offset;
  360. if (k == NGUI_KEY_ESC)
  361. quit = 1;
  362. else if (k >= '1' && k <= '3')
  363. select_tab(k - '1');
  364. else if (k == NGUI_KEY_TAB)
  365. select_tab((cur + 1) % TAB_COUNT);
  366. else if (k == NGUI_KEY_PAGE_UP)
  367. ngui_scroll_to(&sc, sc.offset - page);
  368. else if (k == NGUI_KEY_PAGE_DOWN)
  369. ngui_scroll_to(&sc, sc.offset + page);
  370. else if (k == NGUI_KEY_UP)
  371. ngui_scroll_to(&sc, sc.offset - 20);
  372. else if (k == NGUI_KEY_DOWN)
  373. ngui_scroll_to(&sc, sc.offset + 20);
  374. else if (k == NGUI_KEY_HOME)
  375. ngui_scroll_to(&sc, 0);
  376. else if (k == NGUI_KEY_END)
  377. ngui_scroll_to(&sc, max);
  378. if (sc.offset != old)
  379. ngui_invalidate(&ui);
  380. }
  381. }
  382. void _start(const char *arg)
  383. {
  384. struct win_info wi;
  385. unsigned int sw = 0, sh = 0;
  386. unsigned int w = 440, h = 300;
  387. unsigned int last_poll;
  388. (void)arg;
  389. if (u_screen_size(&sw, &sh) == 0) {
  390. if (sw > 80 && w > sw - 40)
  391. w = sw - 40;
  392. if (sh > 200 && h > sh - 120)
  393. h = sh - 120;
  394. }
  395. if (w < 260)
  396. w = 260;
  397. if (h < 180)
  398. h = 180;
  399. if (u_win_create(w, h, "Boot scan", &wi) != 0) {
  400. u_puts("bootscan: no graphics window\n");
  401. u_exit(1);
  402. }
  403. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  404. ngui_font_attach(&ui);
  405. sc.hide_bar = 0;
  406. last_poll = u_ticks_ms() - POLL_MS;
  407. for (;;) {
  408. unsigned int now = u_ticks_ms();
  409. int changed = 0;
  410. int active_changed = 0;
  411. int i;
  412. if ((int)(now - (last_poll + POLL_MS)) >= 0) {
  413. last_poll = now;
  414. for (i = 0; i < TAB_COUNT; i++) {
  415. if (!pull(&src[i]))
  416. continue;
  417. changed = 1;
  418. if (i == cur)
  419. active_changed = 1;
  420. else
  421. src[i].fresh = 1;
  422. }
  423. if (changed) {
  424. if (active_changed)
  425. pending_tail = 1;
  426. ngui_invalidate(&ui);
  427. }
  428. }
  429. if (!ngui_begin(&ui)) {
  430. ngui_end_ex(&ui, 0);
  431. u_sleep_ms(POLL_MS);
  432. continue;
  433. }
  434. tab_input();
  435. scroll_keys();
  436. if (quit)
  437. break;
  438. draw();
  439. ngui_end(&ui);
  440. u_sleep_ms(POLL_MS);
  441. }
  442. u_win_close();
  443. u_exit(0);
  444. }