1 //: A debugging helper that lets you zoom in/out on a trace.
  2 //:
  3 //: To try it out, first create an example trace:
  4 //:   mu --trace nqueens.mu
  5 //: Then to browse the trace, which was stored in a file called 'last_run':
  6 //:   mu browse-trace last_run
  7 //:
  8 //: You should now find yourself in a UI showing a subsequence of lines from
  9 //: the trace, each line starting with a numeric depth, and ending with a
 10 //: parenthetical count of trace lines hidden after it with greater depths.
 11 //:
 12 //: For example, this line:
 13 //:   2 app: line1 (30)
 14 //: indicates that it was logged with depth 2, and that 30 following lines
 15 //: have been hidden at a depth greater than 2.
 16 //:
 17 //: As an experiment, hidden counts of 1000 or more are in red to highlight
 18 //: where you might be particularly interested in expanding.
 19 //:
 20 //: The UI provides the following hotkeys:
 21 //:
 22 //:   `q` or `ctrl-c`: Quit.
 23 //:
 24 //:   `Enter`: 'Zoom into' this line. Expand some or all of the hidden lines
 25 //:   at the next higher level, updating parenthetical counts of hidden lines.
 26 //:
 27 //:   `Backspace`: 'Zoom out' on a line after zooming in, collapsing expanded
 28 //:   lines below by some series of <Enter> commands.
 29 //:
 30 //:   `j` or `down-arrow`: Move/scroll cursor down one line.
 31 //:   `k` or `up-arrow`: Move/scroll cursor up one line.
 32 //:   `J` or `ctrl-f` or `page-down`: Scroll cursor down one page.
 33 //:   `K` or `ctrl-b` or `page-up`: Scroll cursor up one page.
 34 //:   `h` or `left-arrow`: Scroll cursor left one character.
 35 //:   `l` or `right-arrow`: Scroll cursor right one character.
 36 //:   `H`: Scroll cursor left one screen-width.
 37 //:   `L`: Scroll cursor right one screen-width.
 38 //:
 39 //:   `g` or `home`: Move cursor to start of trace.
 40 //:   `G` or `end`: Move cursor to end of trace.
 41 //:
 42 //:   `t`: Move cursor to top line on screen.
 43 //:   `c`: Move cursor to center line on screen.
 44 //:   `b`: Move cursor to bottom line on screen.
 45 //:   `T`: Scroll line at cursor to top of screen.
 46 
 47 :(before "End Primitive Recipe Declarations")
 48 _BROWSE_TRACE,
 49 :(before "End Primitive Recipe Numbers")
 50 put(Recipe_ordinal, "$browse-trace", _BROWSE_TRACE);
 51 :(before "End Primitive Recipe Checks")
 52 case _BROWSE_TRACE: {
 53   break;
 54 }
 55 :(before "End Primitive Recipe Implementations")
 56 case _BROWSE_TRACE: {
 57   start_trace_browser();
 58   break;
 59 }
 60 
 61 //: browse a trace loaded from a file
 62 :(after "Commandline Parsing")
 63 if (argc == 3 && is_equal(argv[1], "browse-trace")) {
 64   load_trace(argv[2]);
 65   start_trace_browser();
 66   return 0;
 67 }
 68 
 69 :(before "End Globals")
 70 set<int> Visible;
 71 int Top_of_screen = 0;
 72 int Left_of_screen = 0;
 73 int Last_printed_row = 0;
 74 map<int, int> Trace_index;  // screen row -> trace index
 75 
 76 :(code)
 77 void start_trace_browser() {
 78   if (!Trace_stream) return;
 79   cerr << "computing min depth to display\n";
 80   int min_depth = 9999;
 81   for (int i = 0; i < SIZE(Trace_stream->past_lines); ++i) {
 82   ¦ trace_line& curr_line = Trace_stream->past_lines.at(i);
 83   ¦ if (curr_line.depth < min_depth) min_depth = curr_line.depth;
 84   }
 85   cerr << "min depth is " << min_depth << '\n';
 86   cerr << "computing lines to display\n";
 87   for (int i = 0; i < SIZE(Trace_stream->past_lines); ++i) {
 88   ¦ if (Trace_stream->past_lines.at(i).depth == min_depth)
 89   ¦ ¦ Visible.insert(i);
 90   }
 91   tb_init();
 92   Display_row = Display_column = 0;
 93   tb_event event;
 94   Top_of_screen = 0;
 95   refresh_screen_rows();
 96   while (true) {
 97   ¦ render();
 98   ¦ do {
 99   ¦ ¦ tb_poll_event(&event);
100   ¦ } while (event.type != TB_EVENT_KEY);
101   ¦ int key = event.key ? event.key : event.ch;
102   ¦ if (key == 'q' || key == 'Q' || key == TB_KEY_CTRL_C) break;
103   ¦ if (key == 'j' || key == TB_KEY_ARROW_DOWN) {
104   ¦ ¦ // move cursor one line down
105   ¦ ¦ if (Display_row < Last_printed_row) ++Display_row;
106   ¦ }
107   ¦ if (key == 'k' || key == TB_KEY_ARROW_UP) {
108   ¦ ¦ // move cursor one line up
109   ¦ ¦ if (Display_row > 0) --Display_row;
110   ¦ }
111   ¦ if (key == 't') {
112   ¦ ¦ // move cursor to top of screen
113   ¦ ¦ Display_row = 0;
114   ¦ }
115   ¦ if (key == 'c') {
116   ¦ ¦ // move cursor to center of screen
117   ¦ ¦ Display_row = tb_height()/2;
118   ¦ }
119   ¦ if (key == 'b') {
120   ¦ ¦ // move cursor to bottom of screen
121   ¦ ¦ Display_row = tb_height()-1;
122   ¦ }
123   ¦ if (key == 'T') {
124   ¦ ¦ // move cursor _row_ to top of screen
125   ¦ ¦ Top_of_screen = get(Trace_index, Display_row);
126   ¦ ¦ Display_row = 0;
127   ¦ ¦ refresh_screen_rows();
128   ¦ }
129   ¦ if (key == 'h' || key == TB_KEY_ARROW_LEFT) {
130   ¦ ¦ // pan screen one character right
131   ¦ ¦ --Left_of_screen;
132   ¦ }
133   ¦ if (key == 'l' || key == TB_KEY_ARROW_RIGHT) {
134   ¦ ¦ // pan screen one character left
135   ¦ ¦ ++Left_of_screen;
136   ¦ }
137   ¦ if (key == 'H') {
138   ¦ ¦ // pan screen one screen-width left
139   ¦ ¦ Left_of_screen -= (tb_width() - 5);
140   ¦ ¦ if (Left_of_screen < 0) Left_of_screen = 0;
141   ¦ }
142   ¦ if (key == 'L') {
143   ¦ ¦ // pan screen one screen-width right
144   ¦ ¦ Left_of_screen += (tb_width() - 5);
145   ¦ }
146   ¦ if (key == 'J' || key == TB_KEY_PGDN || key == TB_KEY_CTRL_F) {
147   ¦ ¦ // page-down
148   ¦ ¦ if (Trace_index.find(tb_height()-1) != Trace_index.end()) {
149   ¦ ¦ ¦ Top_of_screen = get(Trace_index, tb_height()-1) + 1;
150   ¦ ¦ ¦ refresh_screen_rows();
151   ¦ ¦ }
152   ¦ }
153   ¦ if (key == 'K' || key == TB_KEY_PGUP || key == TB_KEY_CTRL_B) {
154   ¦ ¦ // page-up is more convoluted
155   ¦ ¦ for (int screen_row = tb_height(); screen_row > 0 && Top_of_screen > 0; --screen_row) {
156   ¦ ¦ ¦ --Top_of_screen;
157   ¦ ¦ ¦ if (Top_of_screen <= 0) break;
158   ¦ ¦ ¦ while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen))
159   ¦ ¦ ¦ ¦ --Top_of_screen;
160   ¦ ¦ }
161   ¦ ¦ if (Top_of_screen >= 0)
162   ¦ ¦ ¦ refresh_screen_rows();
163   ¦ }
164   ¦ if (key == 'g' || key == TB_KEY_HOME) {
165   ¦ ¦ ¦ Top_of_screen = 0;
166   ¦ ¦ ¦ Last_printed_row = 0;
167   ¦ ¦ ¦ Display_row = 0;
168   ¦ ¦ ¦ refresh_screen_rows();
169   ¦ }
170   ¦ if (key == 'G' || key == TB_KEY_END) {
171   ¦ ¦ // go to bottom of screen; largely like page-up, interestingly
172   ¦ ¦ Top_of_screen = SIZE(Trace_stream->past_lines)-1;
173   ¦ ¦ for (int screen_row = tb_height(); screen_row > 0 && Top_of_screen > 0; --screen_row) {
174   ¦ ¦ ¦ --Top_of_screen;
175   ¦ ¦ ¦ if (Top_of_screen <= 0) break;
176   ¦ ¦ ¦ while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen))
177   ¦ ¦ ¦ ¦ --Top_of_screen;
178   ¦ ¦ }
179   ¦ ¦ refresh_screen_rows();
180   ¦ ¦ // move cursor to bottom
181   ¦ ¦ Display_row = Last_printed_row;
182   ¦ ¦ refresh_screen_rows();
183   ¦ }
184   ¦ if (key == TB_KEY_CARRIAGE_RETURN) {
185   ¦ ¦ // expand lines under current by one level
186   ¦ ¦ assert(contains_key(Trace_index, Display_row));
187   ¦ ¦ int start_index = get(Trace_index, Display_row);
188   ¦ ¦ int index = 0;
189   ¦ ¦ // simultaneously compute end_index and min_depth
190   ¦ ¦ int min_depth = 9999;
191   ¦ ¦ for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) {
192   ¦ ¦ ¦ if (contains_key(Visible, index)) break;
193   ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
194   ¦ ¦ ¦ assert(curr_line.depth > Trace_stream->past_lines.at(start_index).depth);
195   ¦ ¦ ¦ if (curr_line.depth < min_depth) min_depth = curr_line.depth;
196   ¦ ¦ }
197   ¦ ¦ int end_index = index;
198   ¦ ¦ // mark as visible all intervening indices at min_depth
199   ¦ ¦ for (index = start_index; index < end_index; ++index) {
200   ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
201   ¦ ¦ ¦ if (curr_line.depth == min_depth) {
202   ¦ ¦ ¦ ¦ Visible.insert(index);
203   ¦ ¦ ¦ }
204   ¦ ¦ }
205   ¦ ¦ refresh_screen_rows();
206   ¦ }
207   ¦ if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) {
208   ¦ ¦ // collapse all lines under current
209   ¦ ¦ assert(contains_key(Trace_index, Display_row));
210   ¦ ¦ int start_index = get(Trace_index, Display_row);
211   ¦ ¦ int index = 0;
212   ¦ ¦ // end_index is the next line at a depth same as or lower than start_index
213   ¦ ¦ int initial_depth = Trace_stream->past_lines.at(start_index).depth;
214   ¦ ¦ for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) {
215   ¦ ¦ ¦ if (!contains_key(Visible, index)) continue;
216   ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
217   ¦ ¦ ¦ if (curr_line.depth <= initial_depth) break;
218   ¦ ¦ }
219   ¦ ¦ int end_index = index;
220   ¦ ¦ // mark as visible all intervening indices at min_depth
221   ¦ ¦ for (index = start_index+1; index < end_index; ++index) {
222   ¦ ¦ ¦ Visible.erase(index);
223   ¦ ¦ }
224   ¦ ¦ refresh_screen_rows();
225   ¦ }
226   }
227   tb_shutdown();
228 }
229 
230 // update Trace_indices for each screen_row on the basis of Top_of_screen and Visible
231 void refresh_screen_rows() {
232   int screen_row = 0, index = 0;
233   Trace_index.clear();
234   for (screen_row = 0, index = Top_of_screen; screen_row < tb_height() && index < SIZE(Trace_stream->past_lines); ++screen_row, ++index) {
235   ¦ // skip lines without depth for now
236   ¦ while (!contains_key(Visible, index)) {
237   ¦ ¦ ++index;
238   ¦ ¦ if (index >= SIZE(Trace_stream->past_lines)) goto done;
239   ¦ }
240   ¦ assert(index < SIZE(Trace_stream->past_lines));
241   ¦ put(Trace_index, screen_row, index);
242   }
243 done:;
244 }
245 
246 void render() {
247   int screen_row = 0;
248   for (screen_row = 0; screen_row < tb_height(); ++screen_row) {
249   ¦ if (!contains_key(Trace_index, screen_row)) break;
250   ¦ trace_line& curr_line = Trace_stream->past_lines.at(get(Trace_index, screen_row));
251   ¦ ostringstream out;
252   ¦ out << std::setw(4) << curr_line.depth << ' ' << curr_line.label << ": " << curr_line.contents;
253   ¦ if (screen_row < tb_height()-1) {
254   ¦ ¦ int delta = lines_hidden(screen_row);
255   ¦ ¦ // home-brew escape sequence for red
256   ¦ ¦ if (delta > 999) out << static_cast<char>(1);
257   ¦ ¦ out << " (" << delta << ")";
258   ¦ ¦ if (delta > 999) out << static_cast<char>(2);
259   ¦ }
260   ¦ render_line(screen_row, out.str(), screen_row == Display_row);
261   }
262   // clear rest of screen
263   Last_printed_row = screen_row-1;
264   for (; screen_row < tb_height(); ++screen_row) {
265   ¦ render_line(screen_row, "~", /*highlight?*/false);
266   }
267   // move cursor back to display row at the end
268   tb_set_cursor(0, Display_row);
269   tb_present();
270 }
271 
272 int lines_hidden(int screen_row) {
273   assert(contains_key(Trace_index, screen_row));
274   if (!contains_key(Trace_index, screen_row+1))
275   ¦ return SIZE(Trace_stream->past_lines) - get(Trace_index, screen_row);
276   else
277   ¦ return get(Trace_index, screen_row+1) - get(Trace_index, screen_row);
278 }
279 
280 void render_line(int screen_row, const string& s, bool highlight) {
281   int col = 0;
282   int color = TB_WHITE;
283   for (col = 0; col < tb_width() && col+Left_of_screen < SIZE(s); ++col) {
284   ¦ char c = s.at(col+Left_of_screen);  // todo: unicode
285   ¦ if (c == '\n') c = ';';  // replace newlines with semi-colons
286   ¦ // escapes. hack: can't start a line with them.
287   ¦ if (c == '\1') { color = /*red*/1; c = ' '; }
288   ¦ if (c == '\2') { color = TB_WHITE; c = ' '; }
289   ¦ tb_change_cell(col, screen_row, c, color, highlight ? /*subtle grey*/240 : TB_BLACK);
290   }
291   for (; col < tb_width(); ++col)
292   ¦ tb_change_cell(col, screen_row, ' ', TB_WHITE, highlight ? /*subtle grey*/240 : TB_BLACK);
293 }
294 
295 void load_trace(const char* filename) {
296   ifstream tin(filename);
297   if (!tin) {
298   ¦ cerr << "no such file: " << filename << '\n';
299   ¦ exit(1);
300   }
301   Trace_stream = new trace_stream;
302   while (has_data(tin)) {
303   ¦ tin >> std::noskipws;
304   ¦ ¦ skip_whitespace_but_not_newline(tin);
305   ¦ ¦ if (!isdigit(tin.peek())) {
306   ¦ ¦ ¦ string dummy;
307   ¦ ¦ ¦ getline(tin, dummy);
308   ¦ ¦ ¦ continue;
309   ¦ ¦ }
310   ¦ tin >> std::skipws;
311   ¦ int depth;
312   ¦ tin >> depth;
313   ¦ string label;
314   ¦ tin >> label;
315   ¦ if (*--label.end() == ':') label.erase(--label.end());
316   ¦ string line;
317   ¦ getline(tin, line);
318   ¦ Trace_stream->past_lines.push_back(trace_line(depth, label, line));
319   }
320   cerr << "lines read: " << Trace_stream->past_lines.size() << '\n';
321 }