libtraceevent: Add ways to read last boot info
cover libtraceevent: Add ways to read last boot info
From: "Steven Rostedt (Google)" <[email protected]>
When a persistent ring buffer is used to record a trace and the kernel crashes (or simply reboots), the function addresses in the buffer from the previous boot will not match the funciton addresses in the kallsyms of the current boot.
To solve this, the kernel creates a "last_boot_info" file in the instance directory. This holds the offsets of where "_text" is located for the core kernel code as well as where each module is loaded.
Add API to read the last_boot_info file as well as /proc/modules and use this to calculate function addresses based on the kallsyms of the current boot and the last_boot_info file that has the information of the previous boot.
Steven Rostedt (Google) (2):
libtraceevent: Add tep_parse_last_boot_info()
libtraceevent: Add tep_load_modules() API
Documentation/libtraceevent-parse-files.txt | 80 +++++- Documentation/libtraceevent.txt | 2 + include/traceevent/event-parse.h | 2 + src/event-parse-local.h | 9 + src/event-parse.c | 290 +++++++++++++++++++- src/trace-btf.c | 6 + 6 files changed, 377 insertions(+), 12 deletions(-)
Signature · 1 line
2.51.0
PATCH 1/2 libtraceevent: Add tep_parse_last_boot_info()
From: "Steven Rostedt (Google)" <[email protected]>
Add tep_parse_last_boot_info() API to load the tep handler with a persistent ring buffer's last_boot_info file. This is used to map the new boot's kallsyms with the boot from the persistent ring buffer kallsyms.
Note, currently only kernel addresses are supported. Module offsets will come later.
--- Documentation/libtraceevent-parse-files.txt | 17 ++- Documentation/libtraceevent.txt | 1 + include/traceevent/event-parse.h | 1 + src/event-parse-local.h | 6 + src/event-parse.c | 138 +++++++++++++++++++- src/trace-btf.c | 6 + 6 files changed, 165 insertions(+), 4 deletions(-)
Documentation/libtraceevent-parse-files.txt +15 -2
@@ -3,8 +3,8 @@ libtraceevent(3) NAME ---- -tep_parse_saved_cmdlines, tep_parse_printk_formats, tep_parse_kallsyms -- Parsing functions to load mappings +tep_parse_saved_cmdlines, tep_parse_printk_formats, tep_parse_kallsyms, +tep_parse_last_boot_info - Parsing functions to load mappings SYNOPSIS --------
@@ -15,6 +15,7 @@ SYNOPSIS int *tep_parse_saved_cmdlines*(struct tep_handle pass:[*]_tep_, const char pass:[*]_buf_); int *tep_parse_printk_formats*(struct tep_handle pass:[*]_tep_, const char pass:[*]_buf_); int *tep_parse_kallsyms*(struct tep_handle pass:[*]_tep_, const char pass:[*]_buf_); +int *tep_parse_last_boot_info*(struct tep_handle pass:[*]_tep_, const char pass:[*]_lbi_); -- DESCRIPTION
@@ -40,6 +41,15 @@ _tep_ handler such that function IP addresses can be mapped to their name when parsing events with %pS in the print format field. It parses the string _buf_ that holds the content of /proc/kallsyms and ends with a nul character ('\0'). +*tep_parse_last_boot_info()* loads information from the last_boot_info file of +a persistent ring buffer instance. If a tracefs instance is declared as a +persistent ring buffer and contains data from a previous boot, the last_boot_info +file will have the addresses of the kernel as well as modules. It will use +this information to base the function offsets in the binary buffer to match +the function offsets in the kallsyms (loaded by *tep_parse_kallsyms()*) +into the _tep_ handler. The _lbi_ is a nul terminated string that contains +the content of the last_boot_info file from the persistent ring buffer instance. + RETURN VALUE ------------ The *tep_parse_saved_cmdlines*() function returns 0 in case of success, or -1
@@ -51,6 +61,9 @@ in case of an error. The *tep_parse_kallsyms*() function returns 0 in case of success, or -1 in case of an error. +The *tep_parse_last_boot_info*() function retuns 0 in case of success, or -1 +in case of error. + EXAMPLE ------- [source,c]
Documentation/libtraceevent.txt +1 -0
@@ -55,6 +55,7 @@ Meta data parsing: int *tep_parse_saved_cmdlines*(struct tep_handle pass:[*]_tep_, const char pass:[*]_buf_); int *tep_parse_printk_formats*(struct tep_handle pass:[*]_tep_, const char pass:[*]_buf_); int *tep_parse_kallsyms*(struct tep_handle pass:[*]_tep_, const char pass:[*]_buf_); + int *tep_parse_last_boot_info*(struct tep_handle pass:[*]_tep_, const char pass:[*]_lbi_); Plugins management: struct tep_plugin_list pass:[*]*tep_load_plugins*(struct tep_handle pass:[*]_tep_);
include/traceevent/event-parse.h +1 -0
@@ -428,6 +428,7 @@ int tep_register_comm(struct tep_handle *tep, const char *comm, int pid);int tep_override_comm (struct tep_handle *tep,const char *comm,int pid);int tep_parse_saved_cmdlines (struct tep_handle *tep,const char *buf);int tep_parse_kallsyms (struct tep_handle *tep,const char *kallsyms); +int tep_parse_last_boot_info (struct tep_handle *tep,const char *lbi);int tep_register_function (struct tep_handle *tep,char *name,unsigned long long addr,char *mod);int tep_parse_printk_formats (struct tep_handle *tep,const char *buf);
src/event-parse-local.h +6 -0
@@ -15,6 +15,7 @@ struct event_handler;struct func_resolver ;struct tep_plugins_dir ;struct tep_btf ; +struct tep_mod_addr ;#define __hidden __attribute__((visibility ("hidden")))
@@ -46,6 +47,11 @@ struct tep_handle {struct func_resolver *func_resolver;struct func_list *funclist;unsigned int func_count; +unsigned long long func_offset; +unsigned long long _text_addr; +struct tep_mod_addr *mod_addrs; +int nr_mod_addrs; +struct printk_map *printk_map;struct printk_list *printklist;
src/event-parse.c +136 -2
192 lines not on this page · raw message
src/trace-btf.c +6 -0
@@ -551,6 +551,9 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,const char *param_name;int a, p, x, nr; +if (!func) +return -1 ; +if (init_btf_func (btf, s, args, nmem, size, func, &type) <0 )return -1 ;
@@ -597,6 +600,9 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,if (param_name)trace_seq_printf (s,"%s=" , param_name); +if (!param[p].type) +continue ; + t =btf_skip_modifiers (btf, param[p].type);switch (t ?BTF_INFO_KIND (t->info) :BTF_KIND_UNKN ) {
Signature · 1 line
2.51.0
PATCH 2/2 libtraceevent: Add tep_load_modules() API
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
When the last_boot_info is loaded by tep_parse_last_boot_info(), the offset between the last boot core functions can be calculated by the _text address of kallsyms of the current boot. But the module functions need to be calculated by where the modules are loaded. This can be found by the /proc/modules file.
Add tep_load_modules() to read the /proc/modules file and this will be used to calculate the offsets from a buffer from a previous boot and from the module functions in kallsyms of the current boot.
--- Documentation/libtraceevent-parse-files.txt | 65 ++++++-- Documentation/libtraceevent.txt | 1 + include/traceevent/event-parse.h | 1 + src/event-parse-local.h | 3 + src/event-parse.c | 158 +++++++++++++++++++- 5 files changed, 216 insertions(+), 12 deletions(-)
Documentation/libtraceevent-parse-files.txt +56 -9
@@ -4,7 +4,7 @@ libtraceevent(3) NAME ---- tep_parse_saved_cmdlines, tep_parse_printk_formats, tep_parse_kallsyms, -tep_parse_last_boot_info - Parsing functions to load mappings +tep_parse_last_boot_info, tep_load_modules - Parsing functions to load mappings SYNOPSIS --------
@@ -16,6 +16,7 @@ int *tep_parse_saved_cmdlines*(struct tep_handle pass:[*]_tep_, const char pass: int *tep_parse_printk_formats*(struct tep_handle pass:[*]_tep_, const char pass:[*]_buf_); int *tep_parse_kallsyms*(struct tep_handle pass:[*]_tep_, const char pass:[*]_buf_); int *tep_parse_last_boot_info*(struct tep_handle pass:[*]_tep_, const char pass:[*]_lbi_); +int *tep_load_modules*(struct tep_handle pass:[*]_tep_, char pass:[*]_modules_, size_t _size_); -- DESCRIPTION
@@ -50,6 +51,14 @@ the function offsets in the kallsyms (loaded by *tep_parse_kallsyms()*) into the _tep_ handler. The _lbi_ is a nul terminated string that contains the content of the last_boot_info file from the persistent ring buffer instance. +*tep_load_modules()* loads the contents of the file /proc/modules into the _tep_ +handler. This will be used to match the modules found in the last_boot_info (loaded +by *tep_parse_last_boot_info()*) to match the offsets of the binary data in +in a persistent ring buffer that matches the address of a module from the previous +boot to a module address found in _modules_. The _modules_ parameter does not need +to be nul terminated, but _size_ is used to denote the size of the _modules_ string. +This function is useless if *tep_parse_last_boot_info()* isnt used. + RETURN VALUE ------------ The *tep_parse_saved_cmdlines*() function returns 0 in case of success, or -1
@@ -64,6 +73,9 @@ in case of an error. The *tep_parse_last_boot_info*() function retuns 0 in case of success, or -1 in case of error. +The *tep_load_modules*() function returns 0 in case of success, or -1 +in case of error. + EXAMPLE ------- [source,c]
@@ -99,16 +111,16 @@ int load_print_strings(struct tep_handle *tep) return r; } -int load_kallsyms(struct tep_handle *tep) +static char *read_file(const char *file, size_t *size) { - char *line = NULL; - char *buf = NULL; - size_t sz = 0; FILE *fp; - int len = 0; - int r; + char *line = NULL; + char *buf = NULL; + size_t sz = 0; + size_t len = 0; + int r; - fp = fopen("/proc/kallsyms", "r"); + fp = fopen(file, "r"); while ((r = getline(&line, &sz, fp)) >= 0) { buf = realloc(buf, len + r + 1); memcpy(buf + len, line, r);
@@ -117,9 +129,44 @@ int load_kallsyms(struct tep_handle *tep) free(line); fclose(fp); if (!buf) - return -1; + return NULL; buf[len] = 0; + *size = len; + return buf; +} + +int load_kallsyms(struct tep_handle *tep) +{ + char *buf; + size_t sz; + int r; + + buf = read_file("/proc/kallsyms", &sz); + if (!buf) + return -1; + r = tep_parse_kallsyms(tep, buf); + free(buf); + return r; +} + +int load_last_boot(struct tep_handle *tep, struct tracefs_instance *instance) +{ + char *buf; + size_t sz; + int r; + + buf = tracefs_instance_file_read(instance, "last_boot_info", NULL); + if (!buf) + return 0; + + r = tep_parse_last_boot_info(tep, buf); + free(buf); + if (r < 0) + return r; + buf = read_file("/proc/modules", &sz); + if (!buf) + return -1; r = tep_parse_kallsyms(tep, buf); free(buf); return r;
Documentation/libtraceevent.txt +1 -0
@@ -56,6 +56,7 @@ Meta data parsing: int *tep_parse_printk_formats*(struct tep_handle pass:[*]_tep_, const char pass:[*]_buf_); int *tep_parse_kallsyms*(struct tep_handle pass:[*]_tep_, const char pass:[*]_buf_); int *tep_parse_last_boot_info*(struct tep_handle pass:[*]_tep_, const char pass:[*]_lbi_); + int *tep_load_modules*(struct tep_handle pass:[*]_tep_, char pass:[*]_modules_, size_t _size_); Plugins management: struct tep_plugin_list pass:[*]*tep_load_plugins*(struct tep_handle pass:[*]_tep_);
include/traceevent/event-parse.h +1 -0
@@ -429,6 +429,7 @@ int tep_override_comm(struct tep_handle *tep, const char *comm, int pid);int tep_parse_saved_cmdlines (struct tep_handle *tep,const char *buf);int tep_parse_kallsyms (struct tep_handle *tep,const char *kallsyms);int tep_parse_last_boot_info (struct tep_handle *tep,const char *lbi); +int tep_load_modules (struct tep_handle *tep,char *modules,size_t size);int tep_register_function (struct tep_handle *tep,char *name,unsigned long long addr,char *mod);int tep_parse_printk_formats (struct tep_handle *tep,const char *buf);
src/event-parse-local.h +3 -0
@@ -48,9 +48,12 @@ struct tep_handle {struct func_list *funclist;unsigned int func_count;unsigned long long func_offset; +unsigned long long mod_addr;unsigned long long _text_addr;struct tep_mod_addr *mod_addrs; +struct tep_mod_addr *proc_mods;int nr_mod_addrs; +int nr_proc_mods;struct printk_map *printk_map;
src/event-parse.c +155 -3
206 lines not on this page · raw message
Signature · 1 line
2.51.0