| 1 |
#include "staprun.h" |
| 2 |
#include <termios.h> |
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#ifdef HAVE_MONITOR_LIBS |
| 6 |
|
| 7 |
#include <json-c/json.h> |
| 8 |
#include <panel.h> |
| 9 |
#include <curses.h> |
| 10 |
#include <time.h> |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
#define COMP_FNS 7 |
| 14 |
#define MAX_INDEX_LEN 5 |
| 15 |
#define MAX_COLS 256 |
| 16 |
#define MAX_DATA 262144 /* XXX: pass procfs.read().maxsize(NNN) from stap */ |
| 17 |
#define MAX_HISTORY 8192 |
| 18 |
|
| 19 |
#define MIN(X,Y) (((X) < (Y)) ? (X) : (Y)) |
| 20 |
#define MAX(X,Y) (((X) > (Y)) ? (X) : (Y)) |
| 21 |
|
| 22 |
#define HIGHLIGHT(NAME,IDX,CUR) (((IDX == CUR)) ? (NAME "*") : (NAME)) |
| 23 |
|
| 24 |
typedef struct History_Queue |
| 25 |
{ |
| 26 |
char *lines[MAX_HISTORY]; /* each malloc/strdup'd(). */ |
| 27 |
int oldest; |
| 28 |
int newest; |
| 29 |
int count; |
| 30 |
} History_Queue; |
| 31 |
|
| 32 |
enum probe_attributes |
| 33 |
{ |
| 34 |
p_index, |
| 35 |
p_state, |
| 36 |
p_hits, |
| 37 |
p_min, |
| 38 |
p_avg, |
| 39 |
p_max, |
| 40 |
p_name, |
| 41 |
num_attributes |
| 42 |
}; |
| 43 |
|
| 44 |
static enum state |
| 45 |
{ |
| 46 |
normal, |
| 47 |
exited, |
| 48 |
insert, |
| 49 |
} monitor_state; |
| 50 |
|
| 51 |
static History_Queue h_queue; |
| 52 |
static pthread_mutex_t mutex; |
| 53 |
static char probe[MAX_INDEX_LEN]; |
| 54 |
static WINDOW *status = NULL; |
| 55 |
static WINDOW *output = NULL; |
| 56 |
static WINDOW *help = NULL; |
| 57 |
static WINDOW *status_border = NULL; |
| 58 |
static WINDOW *help_border = NULL; |
| 59 |
static PANEL *status_panel = NULL; |
| 60 |
static PANEL *output_panel = NULL; |
| 61 |
static PANEL *help_panel = NULL; |
| 62 |
static PANEL *status_border_panel = NULL; |
| 63 |
static PANEL *help_border_panel = NULL; |
| 64 |
static int comp_fn_index = 0; |
| 65 |
static int num_probes = 0; |
| 66 |
static int probe_scroll = 0; |
| 67 |
static int output_scroll = 0; |
| 68 |
static time_t elapsed_time = 0; |
| 69 |
static time_t start_time = 0; |
| 70 |
static int resized = 0; |
| 71 |
static int input = 0; /* fresh input received in monitor_input() */ |
| 72 |
static int rendered = 0; |
| 73 |
static int status_hidden = 0; |
| 74 |
static int active_window = 1; /* 0 for status window, 1 for output window */ |
| 75 |
static int disable_curses = 0; /* 0 if curses should be enabled, 1 otherwise */ |
| 76 |
/* Forward declarations */ |
| 77 |
static int comp_index(const void *p1, const void *p2); |
| 78 |
static int comp_state(const void *p1, const void *p2); |
| 79 |
static int comp_hits(const void *p1, const void *p2); |
| 80 |
static int comp_min(const void *p1, const void *p2); |
| 81 |
static int comp_avg(const void *p1, const void *p2); |
| 82 |
static int comp_max(const void *p1, const void *p2); |
| 83 |
static int comp_name(const void *p1, const void *p2); |
| 84 |
|
| 85 |
static int (*comp_fn[COMP_FNS])(const void *p1, const void *p2) = |
| 86 |
{ |
| 87 |
comp_index, |
| 88 |
comp_state, |
| 89 |
comp_hits, |
| 90 |
comp_min, |
| 91 |
comp_avg, |
| 92 |
comp_max, |
| 93 |
comp_name |
| 94 |
}; |
| 95 |
|
| 96 |
static int comp_index(const void *p1, const void *p2) |
| 97 |
{ |
| 98 |
json_object *j1 = *(json_object**)p1; |
| 99 |
json_object *j2 = *(json_object**)p2; |
| 100 |
json_object *index1, *index2; |
| 101 |
json_object_object_get_ex(j1, "index", &index1); |
| 102 |
json_object_object_get_ex(j2, "index", &index2); |
| 103 |
return json_object_get_int(index1) - json_object_get_int(index2); |
| 104 |
} |
| 105 |
|
| 106 |
static int comp_state(const void *p1, const void *p2) |
| 107 |
{ |
| 108 |
json_object *j1 = *(json_object**)p1; |
| 109 |
json_object *j2 = *(json_object**)p2; |
| 110 |
json_object *state1, *state2; |
| 111 |
json_object_object_get_ex(j1, "state", &state1); |
| 112 |
json_object_object_get_ex(j2, "state", &state2); |
| 113 |
return strcmp(json_object_get_string(state1), json_object_get_string(state2)); |
| 114 |
} |
| 115 |
|
| 116 |
static int comp_hits(const void *p1, const void *p2) |
| 117 |
{ |
| 118 |
json_object *j1 = *(json_object**)p1; |
| 119 |
json_object *j2 = *(json_object**)p2; |
| 120 |
json_object *hits1, *hits2; |
| 121 |
json_object_object_get_ex(j1, "hits", &hits1); |
| 122 |
json_object_object_get_ex(j2, "hits", &hits2); |
| 123 |
return json_object_get_int(hits2) - json_object_get_int(hits1); |
| 124 |
} |
| 125 |
|
| 126 |
static int comp_min(const void *p1, const void *p2) |
| 127 |
{ |
| 128 |
json_object *j1 = *(json_object**)p1; |
| 129 |
json_object *j2 = *(json_object**)p2; |
| 130 |
json_object *min1, *min2; |
| 131 |
json_object_object_get_ex(j1, "min", &min1); |
| 132 |
json_object_object_get_ex(j2, "min", &min2); |
| 133 |
return json_object_get_int(min2) - json_object_get_int(min1); |
| 134 |
} |
| 135 |
|
| 136 |
static int comp_avg(const void *p1, const void *p2) |
| 137 |
{ |
| 138 |
json_object *j1 = *(json_object**)p1; |
| 139 |
json_object *j2 = *(json_object**)p2; |
| 140 |
json_object *avg1, *avg2; |
| 141 |
json_object_object_get_ex(j1, "avg", &avg1); |
| 142 |
json_object_object_get_ex(j2, "avg", &avg2); |
| 143 |
return json_object_get_int(avg2) - json_object_get_int(avg1); |
| 144 |
} |
| 145 |
|
| 146 |
static int comp_max(const void *p1, const void *p2) |
| 147 |
{ |
| 148 |
json_object *j1 = *(json_object**)p1; |
| 149 |
json_object *j2 = *(json_object**)p2; |
| 150 |
json_object *max1, *max2; |
| 151 |
json_object_object_get_ex(j1, "max", &max1); |
| 152 |
json_object_object_get_ex(j2, "max", &max2); |
| 153 |
return json_object_get_int(max2) - json_object_get_int(max1); |
| 154 |
} |
| 155 |
|
| 156 |
static int comp_name(const void *p1, const void *p2) |
| 157 |
{ |
| 158 |
json_object *j1 = *(json_object**)p1; |
| 159 |
json_object *j2 = *(json_object**)p2; |
| 160 |
json_object *name1, *name2; |
| 161 |
json_object_object_get_ex(j1, "name", &name1); |
| 162 |
json_object_object_get_ex(j2, "name", &name2); |
| 163 |
return strcmp(json_object_get_string(name1), json_object_get_string(name2)); |
| 164 |
} |
| 165 |
|
| 166 |
static void write_command(const char *msg) |
| 167 |
{ |
| 168 |
char path[PATH_MAX]; |
| 169 |
FILE* fp; |
| 170 |
size_t len = strlen(msg); |
| 171 |
|
| 172 |
if (sprintf_chk(path, "/proc/systemtap/%s/monitor_control", modname)) |
| 173 |
return; |
| 174 |
if (!(fp = fopen(path, "w"))) |
| 175 |
return; |
| 176 |
if (fwrite(msg, 1, len, fp) != len) |
| 177 |
{ |
| 178 |
fclose(fp); |
| 179 |
return; |
| 180 |
} |
| 181 |
fclose(fp); |
| 182 |
} |
| 183 |
|
| 184 |
static void clear_screen() |
| 185 |
{ |
| 186 |
if (status) |
| 187 |
{ |
| 188 |
del_panel(status_panel); |
| 189 |
del_panel(status_border_panel); |
| 190 |
delwin(status); |
| 191 |
delwin(status_border); |
| 192 |
status = NULL; |
| 193 |
} |
| 194 |
if (output) |
| 195 |
{ |
| 196 |
del_panel(output_panel); |
| 197 |
delwin(output); |
| 198 |
output = NULL; |
| 199 |
} |
| 200 |
if (help) |
| 201 |
{ |
| 202 |
del_panel(help_border_panel); |
| 203 |
del_panel(help_panel); |
| 204 |
delwin(help_border); |
| 205 |
delwin(help); |
| 206 |
help = NULL; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
static void setup_status_window() |
| 211 |
{ |
| 212 |
/* Status window not needed when hidden */ |
| 213 |
if (!status_hidden) |
| 214 |
{ |
| 215 |
status_border = newwin(LINES/2, COLS, 0, 0); |
| 216 |
status_border_panel = new_panel(status_border); |
| 217 |
status = newwin(LINES/2-2, COLS-2, 1, 1); |
| 218 |
status_panel = new_panel(status); |
| 219 |
box(status_border, 0 ,0); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
static void setup_output_window() |
| 224 |
{ |
| 225 |
/* Use the entire screen if not displaying status */ |
| 226 |
if (status_hidden) |
| 227 |
output = newwin(LINES,COLS,0,0); |
| 228 |
else |
| 229 |
output = newwin(LINES/2,COLS,LINES/2,0); |
| 230 |
output_panel = new_panel(output); |
| 231 |
scrollok(output, TRUE); |
| 232 |
} |
| 233 |
|
| 234 |
static void setup_help_window() |
| 235 |
{ |
| 236 |
help_border = newwin((3.0/4)*LINES+2, (3.0/4)*COLS+2, LINES/8-1, COLS/8-1); |
| 237 |
help = newwin((3.0/4)*LINES, (3.0/4)*COLS, LINES/8, COLS/8); |
| 238 |
help_border_panel = new_panel(help_border); |
| 239 |
help_panel = new_panel(help); |
| 240 |
hide_panel(help_border_panel); |
| 241 |
hide_panel(help_panel); |
| 242 |
box(help_border, 0 ,0); |
| 243 |
wattron(help, A_BOLD); |
| 244 |
wprintw(help, "MONITOR MODE COMMANDS\n"); |
| 245 |
wattroff(help, A_BOLD); |
| 246 |
wprintw(help, "h - Show/hide help page\n"); |
| 247 |
wprintw(help, "c - Reset all global variables to their initial states\n"); |
| 248 |
wprintw(help, "s - Rotate sort column for probes\n"); |
| 249 |
wprintw(help, "t - Open a prompt to enter the index of a probe to toggle\n"); |
| 250 |
wprintw(help, "p/r - Pause/resume script by toggling off/on all probes\n"); |
| 251 |
wprintw(help, "x - Hide/show the status window\n"); |
| 252 |
wprintw(help, "q - Quit script\n"); |
| 253 |
wprintw(help, "j,k/DownArrow,UpArrow - Scroll down/up by one entry\n"); |
| 254 |
wprintw(help, "PgDown/PgUp - Scroll down/up by one page\n"); |
| 255 |
wprintw(help, "Home/End - Scroll to beginning/end\n"); |
| 256 |
wprintw(help, "Tab - Toggle scroll window\n"); |
| 257 |
} |
| 258 |
|
| 259 |
static void handle_resize() |
| 260 |
{ |
| 261 |
usleep (500*1000); /* prevent too many allocations */ |
| 262 |
endwin(); |
| 263 |
refresh(); |
| 264 |
clear_screen(); |
| 265 |
setup_status_window(); |
| 266 |
setup_output_window(); |
| 267 |
setup_help_window(); |
| 268 |
resized = 0; |
| 269 |
} |
| 270 |
|
| 271 |
void monitor_winch(__attribute__((unused)) int signum) |
| 272 |
{ |
| 273 |
resized = 1; |
| 274 |
} |
| 275 |
|
| 276 |
void monitor_setup(void) |
| 277 |
{ |
| 278 |
pthread_mutex_init(&mutex, NULL); |
| 279 |
probe[0] = '\0'; |
| 280 |
start_time = time(NULL); |
| 281 |
// If we have a non tty output, no need to render with curses |
| 282 |
disable_curses = !isatty(STDOUT_FILENO); |
| 283 |
if(disable_curses) return; |
| 284 |
initscr(); |
| 285 |
curs_set(0); |
| 286 |
cbreak(); |
| 287 |
noecho(); |
| 288 |
keypad(stdscr, TRUE); |
| 289 |
nodelay(stdscr, TRUE); |
| 290 |
setup_status_window(); |
| 291 |
setup_output_window(); |
| 292 |
setup_help_window(); |
| 293 |
} |
| 294 |
|
| 295 |
void monitor_cleanup(void) |
| 296 |
{ |
| 297 |
pthread_mutex_destroy(&mutex); |
| 298 |
monitor_end = 1; |
| 299 |
if(disable_curses) return; |
| 300 |
endwin(); |
| 301 |
} |
| 302 |
|
| 303 |
void monitor_render(void) |
| 304 |
{ |
| 305 |
if(disable_curses) return; |
| 306 |
FILE *monitor_fp; |
| 307 |
char path[PATH_MAX]; |
| 308 |
time_t current_time = time(NULL); |
| 309 |
int monitor_x, monitor_y, max_cols, max_rows, cur_y, cur_x; |
| 310 |
int i; |
| 311 |
int discard; |
| 312 |
|
| 313 |
if (resized) |
| 314 |
handle_resize(); |
| 315 |
|
| 316 |
/* Bound scrolling by window height and entry count */ |
| 317 |
getmaxyx(output, max_rows, max_cols); |
| 318 |
output_scroll = MIN(MAX(0, h_queue.count-max_rows+1), output_scroll); |
| 319 |
|
| 320 |
/* Render previously recorded output */ |
| 321 |
wclear(output); |
| 322 |
pthread_mutex_lock(&mutex); |
| 323 |
for (i = 0; i < h_queue.count-output_scroll; i++) |
| 324 |
wprintw(output, "%s", h_queue.lines[(h_queue.oldest+i) % MAX_HISTORY]); |
| 325 |
pthread_mutex_unlock(&mutex); |
| 326 |
update_panels(); |
| 327 |
doupdate(); |
| 328 |
|
| 329 |
if (status_hidden) |
| 330 |
return; |
| 331 |
|
| 332 |
if (!input && rendered && (elapsed_time = current_time - start_time) < monitor_interval) |
| 333 |
return; |
| 334 |
|
| 335 |
start_time = current_time; |
| 336 |
input = 0; |
| 337 |
|
| 338 |
getmaxyx(status, monitor_y, monitor_x); |
| 339 |
max_rows = monitor_y; |
| 340 |
max_cols = MIN(MAX_COLS, monitor_x); |
| 341 |
|
| 342 |
static json_object *jso = NULL; /* survives across refresh calls */ |
| 343 |
char json[MAX_DATA]; |
| 344 |
size_t bytes = 0; |
| 345 |
|
| 346 |
/* Render monitor mode statistics */ |
| 347 |
if (sprintf_chk(path, "/proc/systemtap/%s/monitor_status", modname)) |
| 348 |
return; |
| 349 |
monitor_fp = fopen(path, "r"); |
| 350 |
if (monitor_fp) |
| 351 |
{ |
| 352 |
bytes = fread(json, sizeof(char), sizeof(json), monitor_fp); |
| 353 |
fclose(monitor_fp); |
| 354 |
} |
| 355 |
if (bytes >= 1) |
| 356 |
{ |
| 357 |
/* Free allocated memory */ |
| 358 |
if (jso) |
| 359 |
json_object_put(jso); |
| 360 |
jso = json_tokener_parse(json); |
| 361 |
} |
| 362 |
|
| 363 |
wclear(status); |
| 364 |
|
| 365 |
if (jso) |
| 366 |
{ |
| 367 |
char monitor_str[MAX_COLS]; |
| 368 |
char monitor_out[MAX_COLS]; |
| 369 |
int printed; |
| 370 |
int col; |
| 371 |
int i; |
| 372 |
int width[num_attributes] = {0}; |
| 373 |
json_object *jso_uptime, *jso_uid, *jso_mem, |
| 374 |
*jso_name, *jso_globals, *jso_probe_list; |
| 375 |
struct json_object_iterator it, it_end; |
| 376 |
|
| 377 |
rendered = 1; |
| 378 |
|
| 379 |
json_object_object_get_ex(jso, "uptime", &jso_uptime); |
| 380 |
json_object_object_get_ex(jso, "uid", &jso_uid); |
| 381 |
json_object_object_get_ex(jso, "memory", &jso_mem); |
| 382 |
json_object_object_get_ex(jso, "module_name", &jso_name); |
| 383 |
json_object_object_get_ex(jso, "globals", &jso_globals); |
| 384 |
json_object_object_get_ex(jso, "probe_list", &jso_probe_list); |
| 385 |
num_probes = json_object_array_length(jso_probe_list); |
| 386 |
|
| 387 |
wmove(status, 0, 0); |
| 388 |
|
| 389 |
wprintw(status, "uptime: %s uid: %s memory: %s\n", |
| 390 |
json_object_get_string(jso_uptime), |
| 391 |
json_object_get_string(jso_uid), |
| 392 |
json_object_get_string(jso_mem)); |
| 393 |
|
| 394 |
wprintw(status, "module_name: %s probes: %d \n", |
| 395 |
json_object_get_string(jso_name), |
| 396 |
num_probes); |
| 397 |
|
| 398 |
col = 0; |
| 399 |
col += snprintf(monitor_out, max_cols, "globals: "); |
| 400 |
it = json_object_iter_begin(jso_globals); |
| 401 |
it_end = json_object_iter_end(jso_globals); |
| 402 |
while (!json_object_iter_equal(&it, &it_end)) |
| 403 |
{ |
| 404 |
printed = snprintf(monitor_str, max_cols, "%s: %s ", |
| 405 |
json_object_iter_peek_name(&it), |
| 406 |
json_object_get_string(json_object_iter_peek_value(&it))) + 1; |
| 407 |
|
| 408 |
/* Prevent line folding for globals */ |
| 409 |
if ((max_cols - (col + printed)) >= 0) |
| 410 |
{ |
| 411 |
col += snprintf(monitor_out+col, printed, "%s", monitor_str); |
| 412 |
json_object_iter_next(&it); |
| 413 |
} |
| 414 |
else if (printed > max_cols) |
| 415 |
{ |
| 416 |
/* Skip globals that do not fit on one line */ |
| 417 |
json_object_iter_next(&it); |
| 418 |
} |
| 419 |
else |
| 420 |
{ |
| 421 |
wprintw(status, "%s\n", monitor_out); |
| 422 |
col = 0; |
| 423 |
} |
| 424 |
} |
| 425 |
if (col != 0) |
| 426 |
wprintw(status, "%s\n", monitor_out); |
| 427 |
|
| 428 |
|
| 429 |
/* Find max width of each field for alignment uses */ |
| 430 |
for (i = 0; i < num_probes; i++) |
| 431 |
{ |
| 432 |
json_object *probe, *field; |
| 433 |
probe = json_object_array_get_idx(jso_probe_list, i); |
| 434 |
|
| 435 |
json_object_object_get_ex(probe, "index", &field); |
| 436 |
width[p_index] = MAX(width[p_index], (int) strlen(json_object_get_string(field))); |
| 437 |
json_object_object_get_ex(probe, "state", &field); |
| 438 |
width[p_state] = MAX(width[p_state], (int) strlen(json_object_get_string(field))); |
| 439 |
json_object_object_get_ex(probe, "hits", &field); |
| 440 |
width[p_hits] = MAX(width[p_hits], (int) strlen(json_object_get_string(field))); |
| 441 |
json_object_object_get_ex(probe, "min", &field); |
| 442 |
width[p_min] = MAX(width[p_min], (int) strlen(json_object_get_string(field))); |
| 443 |
json_object_object_get_ex(probe, "avg", &field); |
| 444 |
width[p_avg] = MAX(width[p_avg], (int) strlen(json_object_get_string(field))); |
| 445 |
json_object_object_get_ex(probe, "max", &field); |
| 446 |
width[p_max] = MAX(width[p_max], (int) strlen(json_object_get_string(field))); |
| 447 |
json_object_object_get_ex(probe, "name", &field); |
| 448 |
width[p_name] = MAX(width[p_name], (int) strlen(json_object_get_string(field))); |
| 449 |
} |
| 450 |
|
| 451 |
json_object_array_sort(jso_probe_list, comp_fn[comp_fn_index]); |
| 452 |
|
| 453 |
if (active_window == 0) |
| 454 |
wattron(status, A_BOLD); |
| 455 |
wprintw(status, "\n%*s\t%*s\t%*s\t%*s\t%*s\t%*s\t%s\n", |
| 456 |
width[p_index], HIGHLIGHT("index", p_index, comp_fn_index), |
| 457 |
width[p_state], HIGHLIGHT("state", p_state, comp_fn_index), |
| 458 |
width[p_hits], HIGHLIGHT("hits", p_hits, comp_fn_index), |
| 459 |
width[p_min], HIGHLIGHT("min", p_min, comp_fn_index), |
| 460 |
width[p_avg], HIGHLIGHT("avg", p_avg, comp_fn_index), |
| 461 |
width[p_max], HIGHLIGHT("max", p_max, comp_fn_index), |
| 462 |
HIGHLIGHT("name", p_name, comp_fn_index)); |
| 463 |
if (active_window == 0) |
| 464 |
wattroff(status, A_BOLD); |
| 465 |
|
| 466 |
getyx(status, cur_y, discard); |
| 467 |
if (probe_scroll >= num_probes) |
| 468 |
probe_scroll = num_probes-1; |
| 469 |
for (i = probe_scroll; i < MIN(num_probes, probe_scroll+max_rows-cur_y); i++) |
| 470 |
{ |
| 471 |
json_object *probe, *field; |
| 472 |
probe = json_object_array_get_idx(jso_probe_list, i); |
| 473 |
json_object_object_get_ex(probe, "index", &field); |
| 474 |
wprintw(status, "%*s\t", width[p_index], json_object_get_string(field)); |
| 475 |
json_object_object_get_ex(probe, "state", &field); |
| 476 |
wprintw(status, "%*s\t", width[p_state], json_object_get_string(field)); |
| 477 |
json_object_object_get_ex(probe, "hits", &field); |
| 478 |
wprintw(status, "%*s\t", width[p_hits], json_object_get_string(field)); |
| 479 |
json_object_object_get_ex(probe, "min", &field); |
| 480 |
wprintw(status, "%*s\t", width[p_min], json_object_get_string(field)); |
| 481 |
json_object_object_get_ex(probe, "avg", &field); |
| 482 |
wprintw(status, "%*s\t", width[p_avg], json_object_get_string(field)); |
| 483 |
json_object_object_get_ex(probe, "max", &field); |
| 484 |
wprintw(status, "%*s\t", width[p_max], json_object_get_string(field)); |
| 485 |
getyx(status, discard, cur_x); |
| 486 |
json_object_object_get_ex(probe, "name", &field); |
| 487 |
wprintw(status, "%.*s", max_cols-cur_x-1, json_object_get_string(field)); |
| 488 |
wprintw(status, "\n"); |
| 489 |
} |
| 490 |
mvwprintw(status, max_rows-1, 0, |
| 491 |
"press h for help\n"); |
| 492 |
} |
| 493 |
else /* ! jso */ |
| 494 |
{ |
| 495 |
wmove(status, 0, 0); |
| 496 |
wprintw(status, "(data not available)"); |
| 497 |
} |
| 498 |
|
| 499 |
if (monitor_state == insert) |
| 500 |
mvwprintw(status, max_rows-1, 0, "enter probe index: %s\n", probe); |
| 501 |
else if (monitor_state == exited) |
| 502 |
mvwprintw(status, max_rows-1, 0, |
| 503 |
"EXITED: press q again to quit\n"); |
| 504 |
|
| 505 |
update_panels(); |
| 506 |
doupdate(); |
| 507 |
(void) discard; /* Unused */ |
| 508 |
} |
| 509 |
|
| 510 |
/* Called in staprun/relay.c */ |
| 511 |
void monitor_remember_output_line(const char* buf, const size_t bytes) |
| 512 |
{ |
| 513 |
if(disable_curses && monitor_state != exited){ |
| 514 |
// Just output normally |
| 515 |
printf("%.*s", (int)bytes, buf); |
| 516 |
return; |
| 517 |
} |
| 518 |
pthread_mutex_lock(&mutex); |
| 519 |
free (h_queue.lines[h_queue.newest]); |
| 520 |
h_queue.lines[h_queue.newest] = strndup(buf, bytes); |
| 521 |
h_queue.newest = (h_queue.newest+1) % MAX_HISTORY; |
| 522 |
|
| 523 |
if (h_queue.count < MAX_HISTORY) |
| 524 |
h_queue.count++; /* and h_queue.oldest stays at 0 */ |
| 525 |
else |
| 526 |
h_queue.oldest = (h_queue.oldest+1) % MAX_HISTORY; |
| 527 |
pthread_mutex_unlock(&mutex); |
| 528 |
} |
| 529 |
|
| 530 |
void monitor_exited(void) |
| 531 |
{ |
| 532 |
// We don't have interactive curses running, so just cleanup fully. |
| 533 |
// No need for the 2 stage shutdown |
| 534 |
if(disable_curses){ |
| 535 |
cleanup_and_exit(0, 0 /* error_detected unavailable here */ ); |
| 536 |
} |
| 537 |
|
| 538 |
monitor_state = exited; |
| 539 |
input = 1; |
| 540 |
} |
| 541 |
|
| 542 |
void monitor_input(void) |
| 543 |
{ |
| 544 |
if(disable_curses) return; |
| 545 |
static int i = 0; |
| 546 |
int ch; |
| 547 |
int max_rows; |
| 548 |
int cur_y; |
| 549 |
int discard; |
| 550 |
|
| 551 |
getmaxyx(output, max_rows, discard); |
| 552 |
getyx(status, cur_y, discard); |
| 553 |
|
| 554 |
switch (monitor_state) |
| 555 |
{ |
| 556 |
case normal: |
| 557 |
case exited: |
| 558 |
ch = getch(); |
| 559 |
switch (ch) |
| 560 |
{ |
| 561 |
case '\t': |
| 562 |
active_window ^= 1; |
| 563 |
break; |
| 564 |
case 'j': |
| 565 |
case KEY_DOWN: |
| 566 |
if (active_window == 0) |
| 567 |
probe_scroll++; |
| 568 |
else |
| 569 |
output_scroll--; |
| 570 |
break; |
| 571 |
case 'k': |
| 572 |
case KEY_UP: |
| 573 |
if (active_window == 0) |
| 574 |
probe_scroll--; |
| 575 |
else |
| 576 |
output_scroll++; |
| 577 |
break; |
| 578 |
case KEY_NPAGE: |
| 579 |
if (active_window == 0) |
| 580 |
probe_scroll += max_rows-cur_y; |
| 581 |
else |
| 582 |
output_scroll -= max_rows-1; |
| 583 |
break; |
| 584 |
case KEY_PPAGE: |
| 585 |
if (active_window == 0) |
| 586 |
probe_scroll -= max_rows-cur_y; |
| 587 |
else |
| 588 |
output_scroll += max_rows-1; |
| 589 |
break; |
| 590 |
case KEY_HOME: |
| 591 |
if (active_window == 0) |
| 592 |
probe_scroll = 0; |
| 593 |
else |
| 594 |
output_scroll = h_queue.count-max_rows+1; |
| 595 |
break; |
| 596 |
case KEY_END: |
| 597 |
if (active_window == 0) |
| 598 |
probe_scroll = num_probes-1; |
| 599 |
else |
| 600 |
output_scroll = 0; |
| 601 |
break; |
| 602 |
case 's': |
| 603 |
comp_fn_index++; |
| 604 |
if (comp_fn_index == COMP_FNS) |
| 605 |
comp_fn_index = 0; |
| 606 |
break; |
| 607 |
case 'c': |
| 608 |
write_command("clear"); |
| 609 |
break; |
| 610 |
case 'r': |
| 611 |
write_command("resume"); |
| 612 |
break; |
| 613 |
case 'p': |
| 614 |
write_command("pause"); |
| 615 |
break; |
| 616 |
case 'q': |
| 617 |
if (monitor_state == exited) |
| 618 |
{ |
| 619 |
cleanup_and_exit(0, 0 /* error_detected unavailable here */ ); |
| 620 |
/* NOTREACHED */ |
| 621 |
} |
| 622 |
else |
| 623 |
write_command("quit"); |
| 624 |
break; |
| 625 |
case 't': |
| 626 |
monitor_state = insert; |
| 627 |
break; |
| 628 |
case 'h': |
| 629 |
if (panel_hidden(help_panel)) |
| 630 |
{ |
| 631 |
show_panel(help_border_panel); |
| 632 |
show_panel(help_panel); |
| 633 |
} |
| 634 |
else |
| 635 |
{ |
| 636 |
hide_panel(help_border_panel); |
| 637 |
hide_panel(help_panel); |
| 638 |
} |
| 639 |
break; |
| 640 |
case 'x': |
| 641 |
status_hidden ^= 1; |
| 642 |
handle_resize(); |
| 643 |
break; |
| 644 |
} |
| 645 |
probe_scroll = MAX(0, probe_scroll); |
| 646 |
output_scroll = MAX(0, output_scroll); |
| 647 |
if (ch != ERR) |
| 648 |
input = 1; |
| 649 |
break; |
| 650 |
case insert: |
| 651 |
ch = getch(); |
| 652 |
if (ch == '\n' || i == MAX_INDEX_LEN) |
| 653 |
{ |
| 654 |
write_command(probe); |
| 655 |
monitor_state = normal; |
| 656 |
i = 0; |
| 657 |
probe[0] = '\0'; |
| 658 |
rendered = 0; |
| 659 |
} |
| 660 |
else if (ch == KEY_BACKSPACE && i > 0) |
| 661 |
{ |
| 662 |
probe[--i] = '\0'; |
| 663 |
input = 1; |
| 664 |
} |
| 665 |
else if (ch != ERR && ch != KEY_BACKSPACE) |
| 666 |
{ |
| 667 |
probe[i++] = ch; |
| 668 |
probe[i] = '\0'; |
| 669 |
input = 1; |
| 670 |
} |
| 671 |
break; |
| 672 |
} |
| 673 |
(void) discard; /* Unused */ |
| 674 |
} |
| 675 |
|
| 676 |
#else /* ! HAVE_MONITOR_LIBS */ |
| 677 |
|
| 678 |
void monitor_winch(int signum) { (void) signum; } |
| 679 |
void monitor_setup(void) {} |
| 680 |
void monitor_cleanup(void) {} |
| 681 |
void monitor_render(void) {} |
| 682 |
void monitor_input(void) {} |
| 683 |
void monitor_exited(void) {} |
| 684 |
void monitor_remember_output_line(const char* buf, const size_t bytes) { (void)buf; (void) bytes; } |
| 685 |
|
| 686 |
#endif |
| 687 |
|
| 688 |
void *redirect_stdin(void *arg) |
| 689 |
{ |
| 690 |
char path[PATH_MAX]; |
| 691 |
int c; |
| 692 |
int fd; |
| 693 |
|
| 694 |
snprintf(path, PATH_MAX - 25, "/proc/systemtap/%s/__stdin", modname); |
| 695 |
/* destination file may not exist yet */ |
| 696 |
while ((fd = open(path, O_WRONLY)) == -1) { |
| 697 |
if (errno != ENOENT) { |
| 698 |
_perr("Unexpected failure during file open.\n"); |
| 699 |
exit(1); |
| 700 |
} |
| 701 |
usleep(2000); |
| 702 |
} |
| 703 |
while ((c = getchar()) != EOF) { |
| 704 |
char ch = (char)c; |
| 705 |
if (! write(fd, &ch, sizeof(ch)) && errno != ENOENT) { |
| 706 |
_perr("Unexpected failure during write.\n"); |
| 707 |
exit(1); |
| 708 |
} |
| 709 |
} |
| 710 |
read_stdin_cleanup(); |
| 711 |
close(fd); /* if it pleases m'lud coverity */ |
| 712 |
return arg; |
| 713 |
} |
| 714 |
|
| 715 |
void read_stdin_setup(void) |
| 716 |
{ |
| 717 |
pthread_t t; |
| 718 |
|
| 719 |
if (isatty(STDIN_FILENO)) { |
| 720 |
/* enter non-canonical mode */ |
| 721 |
struct termios oldterm, newterm; |
| 722 |
if (tcgetattr(STDIN_FILENO, &oldterm) != 0) { |
| 723 |
_perr("Failed to reconfigure terminal.\n"); |
| 724 |
exit(1); |
| 725 |
} |
| 726 |
newterm = oldterm; |
| 727 |
newterm.c_lflag &= ~ICANON & ~ECHO; |
| 728 |
if (tcsetattr(STDIN_FILENO, TCSANOW, &newterm) != 0) { |
| 729 |
_perr("Failed to reconfigure terminal.\n"); |
| 730 |
exit(1); |
| 731 |
} |
| 732 |
} |
| 733 |
if (setvbuf(stdin, NULL, _IONBF, 0) != 0) { |
| 734 |
_perr("Failed to reconfigure input stream.\n"); |
| 735 |
exit(1); |
| 736 |
} |
| 737 |
if (pthread_create(&t, NULL, redirect_stdin, NULL) != 0) { |
| 738 |
_perr("Failed to create thread.\n"); |
| 739 |
exit(1); |
| 740 |
} |
| 741 |
if (pthread_detach(t) != 0) { |
| 742 |
_perr("Failed to detach thread.\n"); |
| 743 |
exit(1); |
| 744 |
} |
| 745 |
return; |
| 746 |
} |
| 747 |
|
| 748 |
void read_stdin_cleanup(void) |
| 749 |
{ |
| 750 |
if (isatty(STDIN_FILENO)) { |
| 751 |
struct termios newterm; |
| 752 |
|
| 753 |
if (tcgetattr(STDIN_FILENO, &newterm) != 0) { |
| 754 |
_perr("Failed to reconfigure terminal.\n"); |
| 755 |
return; |
| 756 |
} |
| 757 |
newterm.c_lflag |= ICANON | ECHO; |
| 758 |
if (tcsetattr(STDIN_FILENO, TCSANOW, &newterm) != 0) { |
| 759 |
_perr("Failed to reconfigure terminal.\n"); |
| 760 |
} |
| 761 |
} |
| 762 |
return; |
| 763 |
} |
| 764 |
|
| 765 |
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |