1 // SPDX-License-Identifier: GPL-2.0
3 * in kernel monitor support: allows rv to control in-kernel monitors.
5 * Copyright (C) 2022 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
17 static int config_has_id
;
18 static int config_my_pid
;
19 static int config_trace
;
21 static char *config_initial_reactor
;
22 static char *config_reactor
;
25 * __ikm_read_enable - reads monitor's enable status
27 * __does not log errors.
29 * Returns the current status, or -1 if the monitor does not exist,
30 * __hence not logging errors.
32 static int __ikm_read_enable(char *monitor_name
)
38 snprintf(path
, MAX_PATH
, "rv/monitors/%s/enable", monitor_name
);
40 retval
= tracefs_instance_file_read_number(NULL
, path
, &enabled
);
48 * ikm_read_enable - reads monitor's enable status
50 * Returns the current status, or -1 on error.
52 static int ikm_read_enable(char *monitor_name
)
56 enabled
= __ikm_read_enable(monitor_name
);
58 err_msg("ikm: fail read enabled: %d\n", enabled
);
62 debug_msg("ikm: read enabled: %d\n", enabled
);
68 * ikm_write_enable - write to the monitor's enable file
70 * Return the number of bytes written, -1 on error.
72 static int ikm_write_enable(char *monitor_name
, char *enable_disable
)
77 debug_msg("ikm: writing enabled: %s\n", enable_disable
);
79 snprintf(path
, MAX_PATH
, "rv/monitors/%s/enable", monitor_name
);
80 retval
= tracefs_instance_file_write(NULL
, path
, enable_disable
);
81 if (retval
< strlen(enable_disable
)) {
82 err_msg("ikm: writing enabled: %s\n", enable_disable
);
90 * ikm_enable - enable a monitor
92 * Returns -1 on failure. Success otherwise.
94 static int ikm_enable(char *monitor_name
)
96 return ikm_write_enable(monitor_name
, "1");
100 * ikm_disable - disable a monitor
102 * Returns -1 on failure. Success otherwise.
104 static int ikm_disable(char *monitor_name
)
106 return ikm_write_enable(monitor_name
, "0");
110 * ikm_read_desc - read monitors' description
112 * Return a dynamically allocated string with the monitor's
113 * description, NULL otherwise.
115 static char *ikm_read_desc(char *monitor_name
)
120 snprintf(path
, MAX_PATH
, "rv/monitors/%s/desc", monitor_name
);
121 desc
= tracefs_instance_file_read(NULL
, path
, NULL
);
123 err_msg("ikm: error reading monitor %s desc\n", monitor_name
);
127 *strstr(desc
, "\n") = '\0';
133 * ikm_fill_monitor_definition - fill monitor's definition
135 * Returns -1 on error, 0 otherwise.
137 static int ikm_fill_monitor_definition(char *name
, struct monitor
*ikm
)
142 enabled
= ikm_read_enable(name
);
144 err_msg("ikm: monitor %s fail to read enable file, bug?\n", name
);
148 desc
= ikm_read_desc(name
);
150 err_msg("ikm: monitor %s does not have desc file, bug?\n", name
);
154 strncpy(ikm
->name
, name
, MAX_DA_NAME_LEN
);
155 ikm
->enabled
= enabled
;
156 strncpy(ikm
->desc
, desc
, MAX_DESCRIPTION
);
164 * ikm_write_reactor - switch the reactor to *reactor
166 * Return the number or characters written, -1 on error.
168 static int ikm_write_reactor(char *monitor_name
, char *reactor
)
173 snprintf(path
, MAX_PATH
, "rv/monitors/%s/reactors", monitor_name
);
174 retval
= tracefs_instance_file_write(NULL
, path
, reactor
);
175 debug_msg("ikm: write \"%s\" reactors: %d\n", reactor
, retval
);
181 * ikm_read_reactor - read the reactors file
183 * Returns a dynamically allocated string with monitor's
184 * available reactors, or NULL on error.
186 static char *ikm_read_reactor(char *monitor_name
)
191 snprintf(path
, MAX_PATH
, "rv/monitors/%s/reactors", monitor_name
);
192 reactors
= tracefs_instance_file_read(NULL
, path
, NULL
);
194 err_msg("ikm: fail reading monitor's %s reactors file\n", monitor_name
);
202 * ikm_get_current_reactor - get the current enabled reactor
204 * Reads the reactors file and find the currently enabled
207 * Returns a dynamically allocated memory with the current
208 * reactor. NULL otherwise.
210 static char *ikm_get_current_reactor(char *monitor_name
)
212 char *reactors
= ikm_read_reactor(monitor_name
);
213 char *curr_reactor
= NULL
;
220 start
= strstr(reactors
, "[");
226 end
= strstr(start
, "]");
232 curr_reactor
= calloc(strlen(start
) + 1, sizeof(char));
236 strncpy(curr_reactor
, start
, strlen(start
));
237 debug_msg("ikm: read current reactor %s\n", curr_reactor
);
245 static int ikm_has_id(char *monitor_name
)
251 snprintf(path
, MAX_PATH
, "events/rv/event_%s/format", monitor_name
);
252 format
= tracefs_instance_file_read(NULL
, path
, NULL
);
254 err_msg("ikm: fail reading monitor's %s format event file\n", monitor_name
);
258 /* print fmt: "%d: %s x %s -> %s %s", REC->id, ... */
259 has_id
= !!strstr(format
, "REC->id");
261 debug_msg("ikm: monitor %s has id: %s\n", monitor_name
, has_id
? "yes" : "no");
269 * ikm_list_monitors - list all available monitors
271 * Returns 0 on success, -1 otherwise.
273 int ikm_list_monitors(void)
275 char *available_monitors
;
280 available_monitors
= tracefs_instance_file_read(NULL
, "rv/available_monitors", NULL
);
282 if (!available_monitors
) {
283 err_msg("ikm: available monitors is not available, is CONFIG_RV enabled?\n");
287 curr
= available_monitors
;
289 next
= strstr(curr
, "\n");
292 retval
= ikm_fill_monitor_definition(curr
, &ikm
);
294 err_msg("ikm: error reading %d in kernel monitor, skipping\n", curr
);
296 printf("%-24s %s %s\n", ikm
.name
, ikm
.desc
, ikm
.enabled
? "[ON]" : "[OFF]");
299 } while (strlen(curr
));
301 free(available_monitors
);
306 static void ikm_print_header(struct trace_seq
*s
)
308 trace_seq_printf(s
, "%16s-%-8s %5s %5s ", "<TASK>", "PID", "[CPU]", "TYPE");
310 trace_seq_printf(s
, "%8s ", "ID");
312 trace_seq_printf(s
, "%24s x %-24s -> %-24s %s\n",
318 trace_seq_printf(s
, "%16s %-8s %5s %5s ", " | ", " | ", " | ", " | ");
321 trace_seq_printf(s
, "%8s ", " | ");
323 trace_seq_printf(s
, "%24s %-24s %-24s %s\n",
332 * ikm_event_handler - callback to handle event events
334 * Called any time a rv:"monitor"_event events is generated.
335 * It parses and prints event.
338 ikm_event_handler(struct trace_seq
*s
, struct tep_record
*record
,
339 struct tep_event
*trace_event
, void *context
)
341 /* if needed: struct trace_instance *inst = context; */
342 char *state
, *event
, *next_state
;
343 unsigned long long final_state
;
344 unsigned long long pid
;
345 unsigned long long id
;
346 int cpu
= record
->cpu
;
350 tep_get_field_val(s
, trace_event
, "id", record
, &id
, 1);
352 tep_get_common_field_val(s
, trace_event
, "common_pid", record
, &pid
, 1);
354 if (config_has_id
&& (config_my_pid
== id
))
356 else if (config_my_pid
&& (config_my_pid
== pid
))
359 tep_print_event(trace_event
->tep
, s
, record
, "%16s-%-8d ", TEP_PRINT_COMM
, TEP_PRINT_PID
);
361 trace_seq_printf(s
, "[%.3d] event ", cpu
);
364 trace_seq_printf(s
, "%8llu ", id
);
366 state
= tep_get_field_raw(s
, trace_event
, "state", record
, &val
, 0);
367 event
= tep_get_field_raw(s
, trace_event
, "event", record
, &val
, 0);
368 next_state
= tep_get_field_raw(s
, trace_event
, "next_state", record
, &val
, 0);
369 tep_get_field_val(s
, trace_event
, "final_state", record
, &final_state
, 1);
371 trace_seq_printf(s
, "%24s x %-24s -> %-24s %s\n",
375 final_state
? "Y" : "N");
377 trace_seq_do_printf(s
);
384 * ikm_error_handler - callback to handle error events
386 * Called any time a rv:"monitor"_errors events is generated.
387 * It parses and prints event.
390 ikm_error_handler(struct trace_seq
*s
, struct tep_record
*record
,
391 struct tep_event
*trace_event
, void *context
)
393 unsigned long long pid
, id
;
394 int cpu
= record
->cpu
;
399 tep_get_field_val(s
, trace_event
, "id", record
, &id
, 1);
401 tep_get_common_field_val(s
, trace_event
, "common_pid", record
, &pid
, 1);
403 if (config_has_id
&& config_my_pid
== id
)
405 else if (config_my_pid
== pid
)
408 trace_seq_printf(s
, "%8lld [%03d] error ", pid
, cpu
);
411 trace_seq_printf(s
, "%8llu ", id
);
413 state
= tep_get_field_raw(s
, trace_event
, "state", record
, &val
, 0);
414 event
= tep_get_field_raw(s
, trace_event
, "event", record
, &val
, 0);
416 trace_seq_printf(s
, "%24s x %s\n", state
, event
);
418 trace_seq_do_printf(s
);
425 * ikm_setup_trace_instance - set up a tracing instance to collect data
427 * Create a trace instance, enable rv: events and enable the trace.
429 * Returns the trace_instance * with all set, NULL otherwise.
431 static struct trace_instance
*ikm_setup_trace_instance(char *monitor_name
)
433 char event
[MAX_DA_NAME_LEN
+ 7]; /* max(error_,event_) + '0' = 7 */
434 struct trace_instance
*inst
;
440 config_has_id
= ikm_has_id(monitor_name
);
441 if (config_has_id
< 0) {
442 err_msg("ikm: failed to read monitor %s event format\n", monitor_name
);
447 inst
= calloc(1, sizeof(*inst
));
449 err_msg("ikm: failed to allocate trace instance");
453 retval
= trace_instance_init(inst
, monitor_name
);
458 snprintf(event
, sizeof(event
), "event_%s", monitor_name
);
459 retval
= tracefs_event_enable(inst
->inst
, "rv", event
);
463 tep_register_event_handler(inst
->tep
, -1, "rv", event
,
464 ikm_event_handler
, NULL
);
466 snprintf(event
, sizeof(event
), "error_%s", monitor_name
);
467 retval
= tracefs_event_enable(inst
->inst
, "rv", event
);
471 tep_register_event_handler(inst
->tep
, -1, "rv", event
,
472 ikm_error_handler
, NULL
);
474 /* ready to enable */
475 tracefs_trace_on(inst
->inst
);
480 trace_instance_destroy(inst
);
488 * ikm_destroy_trace_instance - destroy a previously created instance
490 static void ikm_destroy_trace_instance(struct trace_instance
*inst
)
495 trace_instance_destroy(inst
);
500 * ikm_usage_print_reactors - print all available reactors, one per line.
502 static void ikm_usage_print_reactors(void)
504 char *reactors
= tracefs_instance_file_read(NULL
, "rv/available_reactors", NULL
);
510 fprintf(stderr
, " available reactors:");
513 end
= strstr(start
, "\n");
518 fprintf(stderr
, " %s", start
);
521 end
= strstr(start
, "\n");
524 fprintf(stderr
, "\n");
527 * ikm_usage - print usage
529 static void ikm_usage(int exit_val
, char *monitor_name
, const char *fmt
, ...)
536 static const char *const usage
[] = {
538 " -h/--help: print this menu and the reactor list",
539 " -r/--reactor 'reactor': enables the 'reactor'",
540 " -s/--self: when tracing (-t), also trace rv command",
541 " -t/--trace: trace monitor's event",
542 " -v/--verbose: print debug messages",
548 vsnprintf(message
, sizeof(message
), fmt
, ap
);
551 fprintf(stderr
, " %s\n", message
);
553 fprintf(stderr
, "\n usage: rv mon %s [-h] [-q] [-r reactor] [-s] [-v]", monitor_name
);
555 for (i
= 0; usage
[i
]; i
++)
556 fprintf(stderr
, "%s\n", usage
[i
]);
558 ikm_usage_print_reactors();
563 * parse_arguments - parse arguments and set config
565 static int parse_arguments(char *monitor_name
, int argc
, char **argv
)
569 config_my_pid
= getpid();
572 static struct option long_options
[] = {
573 {"help", no_argument
, 0, 'h'},
574 {"reactor", required_argument
, 0, 'r'},
575 {"self", no_argument
, 0, 's'},
576 {"trace", no_argument
, 0, 't'},
577 {"verbose", no_argument
, 0, 'v'},
581 /* getopt_long stores the option index here. */
582 int option_index
= 0;
584 c
= getopt_long(argc
, argv
, "hr:stv", long_options
, &option_index
);
586 /* detect the end of the options. */
592 ikm_usage(0, monitor_name
, "help:");
595 config_reactor
= optarg
;
609 if (config_reactor
) {
610 config_initial_reactor
= ikm_get_current_reactor(monitor_name
);
611 if (!config_initial_reactor
)
612 ikm_usage(1, monitor_name
,
613 "ikm: failed to read current reactor, are reactors enabled?");
615 retval
= ikm_write_reactor(monitor_name
, config_reactor
);
617 ikm_usage(1, monitor_name
,
618 "ikm: failed to set %s reactor, is it available?",
622 debug_msg("ikm: my pid is %d\n", config_my_pid
);
628 * ikm_run_monitor - apply configs and run the monitor
630 * Returns 1 if a monitor was found an executed, 0 if no
631 * monitors were found, or -1 on error.
633 int ikm_run_monitor(char *monitor_name
, int argc
, char **argv
)
635 struct trace_instance
*inst
= NULL
;
639 * Check if monitor exists by seeing it is enabled.
641 retval
= __ikm_read_enable(monitor_name
);
646 err_msg("ikm: monitor %s (in-kernel) is already enabled\n", monitor_name
);
650 /* we should be good to go */
651 retval
= parse_arguments(monitor_name
, argc
, argv
);
653 ikm_usage(1, monitor_name
, "ikm: failed parsing arguments");
656 inst
= ikm_setup_trace_instance(monitor_name
);
661 retval
= ikm_enable(monitor_name
);
663 goto out_free_instance
;
666 ikm_print_header(inst
->seq
);
668 while (!should_stop()) {
670 retval
= tracefs_iterate_raw_events(inst
->tep
,
674 collect_registered_events
,
677 err_msg("ikm: error reading trace buffer\n");
685 ikm_disable(monitor_name
);
686 ikm_destroy_trace_instance(inst
);
688 if (config_reactor
&& config_initial_reactor
)
689 ikm_write_reactor(monitor_name
, config_initial_reactor
);
694 ikm_destroy_trace_instance(inst
);
695 if (config_reactor
&& config_initial_reactor
)
696 ikm_write_reactor(monitor_name
, config_initial_reactor
);