| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- /* bootscan: live tail of the kernel's boot logs.
- *
- * SYS_BOOTLOG exposes three sources: the direct-LFB boot console text (the
- * `bootlog` command), the always-on COM1 capture (`seriallog`) and the boot
- * storage-scan kernel thread's own trail. Each tab keeps its own scrollback;
- * the Serial tab is the complete boot trail (it starts with the first kernel
- * line and keeps growing for later driver output, the deferred USB mounts and
- * the WiFi helper).
- *
- * The WM spawns this window before the first terminal, so the terminal is
- * created last and keeps the keyboard focus.
- *
- * Every tab follows the newest output while it is at the bottom; wheel,
- * PgUp/PgDn, Up/Down, Home and End scroll, and scrolling up stops the follow
- * until the bottom is reached again. Click a tab, press 1/2/3 or Tab to
- * switch; Esc closes the window.
- */
- #include "ucmd_lib.h"
- #include "ngui.h"
- #define POLL_MS 100
- #define LINE_PX 12
- #define PAD 8
- #define TAB_H 24
- #define FOOT_H 20
- enum { TAB_BOOT, TAB_SERIAL, TAB_SCAN, TAB_COUNT };
- struct logsrc {
- const char *label;
- int kind;
- char *text;
- unsigned int cap; /* text[] size (at least the kernel window) */
- unsigned int len; /* bytes held in text[] */
- unsigned int base; /* absolute offset of text[0] */
- unsigned int seen; /* absolute offset fetched up to */
- unsigned int total; /* source counters from the last poll */
- unsigned int rstart;
- unsigned int rcap;
- int active;
- int have_info;
- int fresh; /* new output arrived while the tab was hidden */
- int trim_lines; /* lines dropped from the front since the draw */
- };
- /* At least the kernel's window for each source (BOOTCON_LOG_PAGES,
- * BOOTCON_SERLOG_PAGES, BOOTCON_SCAN_PAGES in kernel/bootcon.h). */
- static char boot_text[32 * 1024];
- static char serial_text[128 * 1024];
- static char scan_text[16 * 1024];
- static struct logsrc src[TAB_COUNT] = {
- { .label = "Boot", .kind = SYS_BOOTLOG_BOOT,
- .text = boot_text, .cap = sizeof(boot_text) },
- { .label = "Serial", .kind = SYS_BOOTLOG_SERIAL,
- .text = serial_text, .cap = sizeof(serial_text) },
- { .label = "Scan", .kind = SYS_BOOTLOG_SCAN,
- .text = scan_text, .cap = sizeof(scan_text) },
- };
- static struct ngui ui;
- static struct ngui_scroll sc;
- static int cur = TAB_SERIAL;
- static int follow = 1;
- static int pending_tail;
- static int pressed_tab = -1;
- static int quit;
- static unsigned int count_lines(const struct logsrc *s)
- {
- unsigned int i, n = 0;
- if (!s->len)
- return 0;
- for (i = 0; i < s->len; i++)
- if (s->text[i] == '\n')
- n++;
- if (s->text[s->len - 1] != '\n')
- n++;
- return n;
- }
- static unsigned short line_color(const char *s, unsigned int n)
- {
- if (n >= 4 && s[0] == 'v' && s[1] == 'f' && s[2] == 's' &&
- s[3] == ':')
- return ui.theme.accent;
- if (n >= 4 && s[0] == 'b' && s[1] == 'l' && s[2] == 'k' &&
- s[3] == ':')
- return ui.theme.text_dim;
- if (n >= 6 && s[0] == '[' && s[1] == 'b' && s[2] == 'o' &&
- s[3] == 'o' && s[4] == 't' && s[5] == ']')
- return ui.theme.text_dim;
- return ui.theme.text;
- }
- /* Drop the oldest `drop` bytes, remembering how many text lines vanished so
- * an unfollowed view can keep its place. */
- static void trim_front(struct logsrc *s, unsigned int drop)
- {
- unsigned int i, lines = 0;
- if (drop > s->len)
- drop = s->len;
- for (i = 0; i < drop; i++)
- if (s->text[i] == '\n')
- lines++;
- for (i = 0; i + drop < s->len; i++)
- s->text[i] = s->text[i + drop];
- s->len -= drop;
- s->base += drop;
- s->trim_lines += (int)lines;
- }
- static void append_text(struct logsrc *s, const char *p, unsigned int n)
- {
- unsigned int i, need;
- if (n > s->cap) {
- unsigned int skip = n - s->cap;
- /* Not reachable with SYS_BOOTLOG_CHUNK-sized reads, but keep
- * the absolute mapping consistent if a buffer ever shrinks. */
- p += skip;
- s->base += skip;
- n = s->cap;
- }
- if (s->len + n > s->cap) {
- need = s->len + n - s->cap;
- /* Drop in quarter-buffer steps instead of copying the whole
- * buffer for every chunk once it is full. */
- if (need < s->cap / 4)
- need = s->cap / 4;
- trim_front(s, need);
- }
- for (i = 0; i < n; i++)
- s->text[s->len++] = p[i];
- }
- /* Fetch every byte that appeared since the last poll. Returns 1 when the
- * view changed. */
- static int pull(struct logsrc *s)
- {
- struct sys_bootlog info;
- int changed = 0;
- int passes = 0;
- if (u_bootlog_stat(s->kind, &info) != 0)
- return 0;
- if (!s->have_info || s->total != info.total ||
- s->rstart != info.start || s->rcap != info.capacity ||
- s->active != (int)info.active) {
- s->have_info = 1;
- s->total = info.total;
- s->rstart = info.start;
- s->rcap = info.capacity;
- s->active = (int)info.active;
- changed = 1;
- }
- if (!info.capacity)
- return changed;
- /* The source dropped bytes we still hold (the Serial window keeps the
- * newest bytes, the Scan ring overwrites). */
- if (info.start > s->base)
- trim_front(s, info.start - s->base);
- if (info.start > s->base) {
- s->base = info.start;
- s->len = 0;
- }
- s->seen = s->base + s->len;
- while (s->seen < info.total && passes++ < 64) {
- char chunk[SYS_BOOTLOG_CHUNK];
- int n = u_bootlog_read(s->kind, s->seen, chunk);
- if (n < 0) {
- /* The source wrapped between the two calls. */
- s->have_info = 0;
- break;
- }
- if (n == 0)
- break;
- append_text(s, chunk, (unsigned int)n);
- s->seen += (unsigned int)n;
- changed = 1;
- if ((unsigned int)n < SYS_BOOTLOG_CHUNK)
- break;
- }
- return changed;
- }
- static struct ngui_rect tab_rect(int i)
- {
- struct ngui_rect r;
- int x = PAD, j;
- r.y = 3;
- r.h = TAB_H - 6;
- for (j = 0; j < i; j++)
- x += (int)ngui_text_width_ui(&ui, src[j].label) + 32;
- r.x = x;
- r.w = (int)ngui_text_width_ui(&ui, src[i].label) + 30;
- return r;
- }
- static void draw_tabs(void)
- {
- struct ngui_rect hr = { 0, 0, ui.w, TAB_H };
- struct ngui_rect br = { 0, TAB_H - 1, ui.w, 1 };
- int i;
- ngui_fill_rect(&ui, hr, ui.theme.panel);
- ngui_fill_rect(&ui, br, ui.theme.border);
- for (i = 0; i < TAB_COUNT; i++) {
- struct ngui_rect r = tab_rect(i);
- int hot = ui.inside &&
- ngui_rect_contains(&r, ui.mouse_x, ui.mouse_y);
- int lh = ngui_text_line_height(&ui, LINE_PX, 0);
- int ty = r.y + (r.h - lh) / 2;
- unsigned short color;
- if (i == cur)
- ngui_fill_rect(&ui, r, ui.theme.panel_alt);
- else if (hot)
- ngui_fill_rect(&ui, r, ui.theme.button);
- color = (i == cur || hot) ? ui.theme.text : ui.theme.text_dim;
- if (ty < r.y)
- ty = r.y;
- ngui_text(&ui, r.x + 10, ty, src[i].label, color);
- if (i == cur)
- ngui_fill_rect(&ui, (struct ngui_rect){ r.x, TAB_H - 3,
- r.w, 2 },
- ui.theme.accent);
- if (src[i].fresh && i != cur)
- ngui_fill_round_rect(&ui, (struct ngui_rect){
- r.x + r.w - 12,
- r.y + r.h / 2 - 2, 4, 4 },
- 2, 2, 2, 2, ui.theme.accent);
- if (hot)
- ngui_set_cursor(&ui, CURSOR_DEFAULT);
- }
- }
- static int tab_hit(int x, int y)
- {
- int i;
- for (i = 0; i < TAB_COUNT; i++) {
- struct ngui_rect r = tab_rect(i);
- if (ngui_rect_contains(&r, x, y))
- return i;
- }
- return -1;
- }
- static void select_tab(int i)
- {
- if (i < 0 || i >= TAB_COUNT || i == cur)
- return;
- cur = i;
- src[i].fresh = 0;
- src[i].trim_lines = 0;
- sc.offset = 0;
- follow = 1;
- pending_tail = 1;
- ngui_invalidate(&ui);
- }
- static void tab_input(void)
- {
- int i;
- if (ui.pressed)
- pressed_tab = tab_hit(ui.press_x, ui.press_y);
- if (ui.released) {
- i = tab_hit(ui.release_x, ui.release_y);
- if (i >= 0 && i == pressed_tab)
- select_tab(i);
- pressed_tab = -1;
- }
- }
- static void draw_footer(struct ngui_rect r)
- {
- const char *status;
- const char *hint = "1/2/3 tabs | PgUp/PgDn | Esc";
- unsigned short color;
- int hint_w;
- struct logsrc *s = &src[cur];
- if (!s->have_info) {
- status = "waiting for the kernel log...";
- color = ui.theme.text_dim;
- } else if (!s->rcap) {
- status = s->kind == SYS_BOOTLOG_SCAN ?
- "boot scan log unavailable" : "log unavailable";
- color = ui.theme.warning;
- } else if (s->kind == SYS_BOOTLOG_BOOT) {
- status = s->active ? "boot console: live" :
- "boot console: full";
- color = s->active ? ui.theme.success : ui.theme.text_dim;
- } else if (s->kind == SYS_BOOTLOG_SERIAL) {
- status = s->rstart ? "serial log: live, oldest dropped" :
- "serial log: live";
- color = ui.theme.success;
- } else {
- status = s->active ? "live: boot scan running" :
- "boot scan finished";
- color = s->active ? ui.theme.success : ui.theme.text_dim;
- }
- ngui_fill_rect(&ui, r, ui.theme.panel);
- ngui_text(&ui, r.x + 2, r.y + 3, status, color);
- hint_w = (int)ngui_text_width_ui(&ui, hint);
- if (hint_w + 120 < r.w)
- ngui_text(&ui, r.x + r.w - hint_w - 2, r.y + 3, hint,
- ui.theme.text_dim);
- }
- static void draw(void)
- {
- struct ngui_rect body, foot;
- struct logsrc *s = &src[cur];
- int line_h = ngui_text_line_height(&ui, LINE_PX, 1);
- unsigned int lines = count_lines(s);
- unsigned int pos = 0, idx = 0;
- int max, y;
- if (line_h < 10)
- line_h = 10;
- ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
- ui.theme.bg);
- draw_tabs();
- body.x = PAD;
- body.y = TAB_H + 2;
- body.w = ui.w - 2 * PAD;
- body.h = ui.h - body.y - FOOT_H - 2;
- if (body.w < 20)
- body.w = 20;
- if (body.h < 20)
- body.h = 20;
- foot.x = 0;
- foot.y = ui.h - FOOT_H;
- foot.w = ui.w;
- foot.h = FOOT_H;
- sc.content_h = (int)lines * line_h;
- if (s->trim_lines) {
- if (!follow) {
- int off = sc.offset - s->trim_lines * line_h;
- sc.offset = off > 0 ? off : 0;
- }
- s->trim_lines = 0;
- }
- ngui_scroll_begin(&ui, body, &sc);
- ngui_scroll_wheel(&ui, body, &sc);
- max = sc.content_h - sc.view_h;
- if (max < 0)
- max = 0;
- if (pending_tail && follow)
- ngui_scroll_to(&sc, max);
- pending_tail = 0;
- follow = sc.offset >= max - 1;
- while (pos < s->len) {
- unsigned int start = pos, end;
- while (pos < s->len && s->text[pos] != '\n')
- pos++;
- end = pos;
- y = body.y + (int)idx * line_h - sc.offset;
- if (y + line_h > body.y && y < body.y + body.h) {
- ngui_text_px(&ui, body.x + 2, y + 1, s->text + start,
- end - start,
- line_color(s->text + start, end - start),
- LINE_PX, 0, 0, 1);
- }
- idx++;
- if (pos < s->len && s->text[pos] == '\n')
- pos++;
- }
- ngui_scroll_end(&ui, body, &sc);
- draw_footer(foot);
- }
- static void scroll_keys(void)
- {
- int k;
- int max = sc.content_h - sc.view_h;
- int page = sc.view_h - 22;
- if (page < 22)
- page = 22;
- if (max < 0)
- max = 0;
- while ((k = ngui_key(&ui)) != 0) {
- int old = sc.offset;
- if (k == NGUI_KEY_ESC)
- quit = 1;
- else if (k >= '1' && k <= '3')
- select_tab(k - '1');
- else if (k == NGUI_KEY_TAB)
- select_tab((cur + 1) % TAB_COUNT);
- else if (k == NGUI_KEY_PAGE_UP)
- ngui_scroll_to(&sc, sc.offset - page);
- else if (k == NGUI_KEY_PAGE_DOWN)
- ngui_scroll_to(&sc, sc.offset + page);
- else if (k == NGUI_KEY_UP)
- ngui_scroll_to(&sc, sc.offset - 20);
- else if (k == NGUI_KEY_DOWN)
- ngui_scroll_to(&sc, sc.offset + 20);
- else if (k == NGUI_KEY_HOME)
- ngui_scroll_to(&sc, 0);
- else if (k == NGUI_KEY_END)
- ngui_scroll_to(&sc, max);
- if (sc.offset != old)
- ngui_invalidate(&ui);
- }
- }
- void _start(const char *arg)
- {
- struct win_info wi;
- unsigned int sw = 0, sh = 0;
- unsigned int w = 440, h = 300;
- unsigned int last_poll;
- (void)arg;
- if (u_screen_size(&sw, &sh) == 0) {
- if (sw > 80 && w > sw - 40)
- w = sw - 40;
- if (sh > 200 && h > sh - 120)
- h = sh - 120;
- }
- if (w < 260)
- w = 260;
- if (h < 180)
- h = 180;
- if (u_win_create(w, h, "Boot scan", &wi) != 0) {
- u_puts("bootscan: no graphics window\n");
- u_exit(1);
- }
- ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
- ngui_font_attach(&ui);
- sc.hide_bar = 0;
- last_poll = u_ticks_ms() - POLL_MS;
- for (;;) {
- unsigned int now = u_ticks_ms();
- int changed = 0;
- int active_changed = 0;
- int i;
- if ((int)(now - (last_poll + POLL_MS)) >= 0) {
- last_poll = now;
- for (i = 0; i < TAB_COUNT; i++) {
- if (!pull(&src[i]))
- continue;
- changed = 1;
- if (i == cur)
- active_changed = 1;
- else
- src[i].fresh = 1;
- }
- if (changed) {
- if (active_changed)
- pending_tail = 1;
- ngui_invalidate(&ui);
- }
- }
- if (!ngui_begin(&ui)) {
- ngui_end_ex(&ui, 0);
- u_sleep_ms(POLL_MS);
- continue;
- }
- tab_input();
- scroll_keys();
- if (quit)
- break;
- draw();
- ngui_end(&ui);
- u_sleep_ms(POLL_MS);
- }
- u_win_close();
- u_exit(0);
- }
|