static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, struct perf_probe_arg_field *field, struct probe_trace_arg_ref **ref_ptr, Dwarf_Die *die_mem) { struct probe_trace_arg_ref *ref = *ref_ptr; Dwarf_Die type; Dwarf_Word offs; int ret, tag; pr_debug("converting %s in %s\n", field->name, varname); if (die_get_real_type(vr_die, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type)); tag = dwarf_tag(&type); if (field->name[0] == '[' && (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) { if (field->next) memcpy(die_mem, &type, sizeof(*die_mem)); if (die_get_real_type(&type, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } pr_debug2("Array real type: (%x)\n", (unsigned)dwarf_dieoffset(&type)); if (tag == DW_TAG_pointer_type) { ref = zalloc(sizeof(struct probe_trace_arg_ref)); if (ref == NULL) return -ENOMEM; if (*ref_ptr) (*ref_ptr)->next = ref; else *ref_ptr = ref; } ref->offset += dwarf_bytesize(&type) * field->index; if (!field->next) memcpy(die_mem, vr_die, sizeof(*die_mem)); goto next; } else if (tag == DW_TAG_pointer_type) { if (!field->ref) { pr_err("Semantic error: %s must be referred by '->'\n", field->name); return -EINVAL; } if (die_get_real_type(&type, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } if (dwarf_tag(&type) != DW_TAG_structure_type) { pr_warning("%s is not a data structure.\n", varname); return -EINVAL; } ref = zalloc(sizeof(struct probe_trace_arg_ref)); if (ref == NULL) return -ENOMEM; if (*ref_ptr) (*ref_ptr)->next = ref; else *ref_ptr = ref; } else { if (tag != DW_TAG_structure_type) { pr_warning("%s is not a data structure.\n", varname); return -EINVAL; } if (field->name[0] == '[') { pr_err("Semantic error: %s is not a pointor" " nor array.\n", varname); return -EINVAL; } if (field->ref) { pr_err("Semantic error: %s must be referred by '.'\n", field->name); return -EINVAL; } if (!ref) { pr_warning("Structure on a register is not " "supported yet.\n"); return -ENOTSUP; } } if (die_find_member(&type, field->name, die_mem) == NULL) { pr_warning("%s(tyep:%s) has no member %s.\n", varname, dwarf_diename(&type), field->name); return -EINVAL; } ret = die_get_data_member_location(die_mem, &offs); if (ret < 0) { pr_warning("Failed to get the offset of %s.\n", field->name); return ret; } ref->offset += (long)offs; next: if (field->next) return convert_variable_fields(die_mem, field->name, field->next, &ref, die_mem); else return 0; }
/** * die_walk_instances - Walk on instances of given DIE * @or_die: an abstract original DIE * @callback: a callback function which is called with instance DIE * @data: user data * * Walk on the instances of give @in_die. @in_die must be an inlined function * declartion. This returns the return value of @callback if it returns * non-zero value, or -ENOENT if there is no instance. */ int die_walk_instances(Dwarf_Die *or_die, int (*callback)(Dwarf_Die *, void *), void *data) { Dwarf_Die cu_die; Dwarf_Die die_mem; struct __instance_walk_param iwp = { .addr = or_die->addr, .callback = callback, .data = data, .retval = -ENOENT, }; if (dwarf_diecu(or_die, &cu_die, NULL, NULL) == NULL) return -ENOENT; die_find_child(&cu_die, __die_walk_instances_cb, &iwp, &die_mem); return iwp.retval; } /* Line walker internal parameters */ struct __line_walk_param { bool recursive; line_walk_callback_t callback; void *data; int retval; }; static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data) { struct __line_walk_param *lw = data; Dwarf_Addr addr = 0; const char *fname; int lineno; if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) { fname = die_get_call_file(in_die); lineno = die_get_call_lineno(in_die); if (fname && lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) { lw->retval = lw->callback(fname, lineno, addr, lw->data); if (lw->retval != 0) return DIE_FIND_CB_END; } } if (!lw->recursive) /* Don't need to search recursively */ return DIE_FIND_CB_SIBLING; if (addr) { fname = dwarf_decl_file(in_die); if (fname && dwarf_decl_line(in_die, &lineno) == 0) { lw->retval = lw->callback(fname, lineno, addr, lw->data); if (lw->retval != 0) return DIE_FIND_CB_END; } } /* Continue to search nested inlined function call-sites */ return DIE_FIND_CB_CONTINUE; } /* Walk on lines of blocks included in given DIE */ static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive, line_walk_callback_t callback, void *data) { struct __line_walk_param lw = { .recursive = recursive, .callback = callback, .data = data, .retval = 0, }; Dwarf_Die die_mem; Dwarf_Addr addr; const char *fname; int lineno; /* Handle function declaration line */ fname = dwarf_decl_file(sp_die); if (fname && dwarf_decl_line(sp_die, &lineno) == 0 && dwarf_entrypc(sp_die, &addr) == 0) { lw.retval = callback(fname, lineno, addr, data); if (lw.retval != 0) goto done; } die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem); done: return lw.retval; } static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data) { struct __line_walk_param *lw = data; lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data); if (lw->retval != 0) return DWARF_CB_ABORT; return DWARF_CB_OK; } /** * die_walk_lines - Walk on lines inside given DIE * @rt_die: a root DIE (CU, subprogram or inlined_subroutine) * @callback: callback routine * @data: user data * * Walk on all lines inside given @rt_die and call @callback on each line. * If the @rt_die is a function, walk only on the lines inside the function, * otherwise @rt_die must be a CU DIE. * Note that this walks not only dwarf line list, but also function entries * and inline call-site. */ int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data) { Dwarf_Lines *lines; Dwarf_Line *line; Dwarf_Addr addr; const char *fname; int lineno, ret = 0; Dwarf_Die die_mem, *cu_die; size_t nlines, i; /* Get the CU die */ if (dwarf_tag(rt_die) != DW_TAG_compile_unit) cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL); else cu_die = rt_die; if (!cu_die) { pr_debug2("Failed to get CU from given DIE.\n"); return -EINVAL; } /* Get lines list in the CU */ if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) { pr_debug2("Failed to get source lines on this CU.\n"); return -ENOENT; } pr_debug2("Get %zd lines from this CU\n", nlines); /* Walk on the lines on lines list */ for (i = 0; i < nlines; i++) { line = dwarf_onesrcline(lines, i); if (line == NULL || dwarf_lineno(line, &lineno) != 0 || dwarf_lineaddr(line, &addr) != 0) { pr_debug2("Failed to get line info. " "Possible error in debuginfo.\n"); continue; } /* Filter lines based on address */ if (rt_die != cu_die) /* * Address filtering * The line is included in given function, and * no inline block includes it. */ if (!dwarf_haspc(rt_die, addr) || die_find_inlinefunc(rt_die, addr, &die_mem)) continue; /* Get source line */ fname = dwarf_linesrc(line, NULL, NULL); ret = callback(fname, lineno, addr, data); if (ret != 0) return ret; } /* * Dwarf lines doesn't include function declarations and inlined * subroutines. We have to check functions list or given function. */ if (rt_die != cu_die) /* * Don't need walk functions recursively, because nested * inlined functions don't have lines of the specified DIE. */ ret = __die_walk_funclines(rt_die, false, callback, data); else { struct __line_walk_param param = { .callback = callback, .data = data, .retval = 0, }; dwarf_getfuncs(cu_die, __die_walk_culines_cb, ¶m, 0); ret = param.retval; } return ret; } struct __find_variable_param { const char *name; Dwarf_Addr addr; }; static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data) { struct __find_variable_param *fvp = data; Dwarf_Attribute attr; int tag; tag = dwarf_tag(die_mem); if ((tag == DW_TAG_formal_parameter || tag == DW_TAG_variable) && die_compare_name(die_mem, fvp->name) && /* Does the DIE have location information or external instance? */ (dwarf_attr(die_mem, DW_AT_external, &attr) || dwarf_attr(die_mem, DW_AT_location, &attr))) return DIE_FIND_CB_END; if (dwarf_haspc(die_mem, fvp->addr)) return DIE_FIND_CB_CONTINUE; else return DIE_FIND_CB_SIBLING; } /** * die_find_variable_at - Find a given name variable at given address * @sp_die: a function DIE * @name: variable name * @addr: address * @die_mem: a buffer for result DIE * * Find a variable DIE called @name at @addr in @sp_die. */ Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name, Dwarf_Addr addr, Dwarf_Die *die_mem) { struct __find_variable_param fvp = { .name = name, .addr = addr}; return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp, die_mem); } static int __die_find_member_cb(Dwarf_Die *die_mem, void *data) { const char *name = data; if (dwarf_tag(die_mem) == DW_TAG_member) { if (die_compare_name(die_mem, name)) return DIE_FIND_CB_END; else if (!dwarf_diename(die_mem)) { /* Unnamed structure */ Dwarf_Die type_die, tmp_die; if (die_get_type(die_mem, &type_die) && die_find_member(&type_die, name, &tmp_die)) return DIE_FIND_CB_END; } } return DIE_FIND_CB_SIBLING; } /** * die_find_member - Find a given name member in a data structure * @st_die: a data structure type DIE * @name: member name * @die_mem: a buffer for result DIE * * Find a member DIE called @name in @st_die. */ Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name, Dwarf_Die *die_mem) { return die_find_child(st_die, __die_find_member_cb, (void *)name, die_mem); } /** * die_get_typename - Get the name of given variable DIE * @vr_die: a variable DIE * @buf: a buffer for result type name * @len: a max-length of @buf * * Get the name of @vr_die and stores it to @buf. Return the actual length * of type name if succeeded. Return -E2BIG if @len is not enough long, and * Return -ENOENT if failed to find type name. * Note that the result will stores typedef name if possible, and stores * "*(function_type)" if the type is a function pointer. */ int die_get_typename(Dwarf_Die *vr_die, char *buf, int len) { Dwarf_Die type; int tag, ret, ret2; const char *tmp = ""; if (__die_get_real_type(vr_die, &type) == NULL) return -ENOENT; tag = dwarf_tag(&type); if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type) tmp = "*"; else if (tag == DW_TAG_subroutine_type) { /* Function pointer */ ret = snprintf(buf, len, "(function_type)"); return (ret >= len) ? -E2BIG : ret; } else { if (!dwarf_diename(&type)) return -ENOENT; if (tag == DW_TAG_union_type) tmp = "union "; else if (tag == DW_TAG_structure_type) tmp = "struct "; else if (tag == DW_TAG_enumeration_type) tmp = "enum "; /* Write a base name */ ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type)); return (ret >= len) ? -E2BIG : ret; } ret = die_get_typename(&type, buf, len); if (ret > 0) { ret2 = snprintf(buf + ret, len - ret, "%s", tmp); ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret; } return ret; } /** * die_get_varname - Get the name and type of given variable DIE * @vr_die: a variable DIE * @buf: a buffer for type and variable name * @len: the max-length of @buf * * Get the name and type of @vr_die and stores it in @buf as "type\tname". */ int die_get_varname(Dwarf_Die *vr_die, char *buf, int len) { int ret, ret2; ret = die_get_typename(vr_die, buf, len); if (ret < 0) { pr_debug("Failed to get type, make it unknown.\n"); ret = snprintf(buf, len, "(unknown_type)"); } if (ret > 0) { ret2 = snprintf(buf + ret, len - ret, "\t%s", dwarf_diename(vr_die)); ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret; } return ret; }
static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, struct perf_probe_arg_field *field, struct probe_trace_arg_ref **ref_ptr, Dwarf_Die *die_mem) { struct probe_trace_arg_ref *ref = *ref_ptr; Dwarf_Die type; Dwarf_Word offs; int ret, tag; pr_debug("converting %s in %s\n", field->name, varname); if (die_get_real_type(vr_die, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type)); tag = dwarf_tag(&type); if (field->name[0] == '[' && (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) { if (field->next) /* Save original type for next field */ memcpy(die_mem, &type, sizeof(*die_mem)); /* Get the type of this array */ if (die_get_real_type(&type, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } pr_debug2("Array real type: (%x)\n", (unsigned)dwarf_dieoffset(&type)); if (tag == DW_TAG_pointer_type) { ref = zalloc(sizeof(struct probe_trace_arg_ref)); if (ref == NULL) return -ENOMEM; if (*ref_ptr) (*ref_ptr)->next = ref; else *ref_ptr = ref; } ref->offset += dwarf_bytesize(&type) * field->index; if (!field->next) /* Save vr_die for converting types */ memcpy(die_mem, vr_die, sizeof(*die_mem)); goto next; } else if (tag == DW_TAG_pointer_type) { /* Check the pointer and dereference */ if (!field->ref) { pr_err("Semantic error: %s must be referred by '->'\n", field->name); return -EINVAL; } /* Get the type pointed by this pointer */ if (die_get_real_type(&type, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } /* Verify it is a data structure */ tag = dwarf_tag(&type); if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) { pr_warning("%s is not a data structure nor an union.\n", varname); return -EINVAL; } ref = zalloc(sizeof(struct probe_trace_arg_ref)); if (ref == NULL) return -ENOMEM; if (*ref_ptr) (*ref_ptr)->next = ref; else *ref_ptr = ref; } else { /* Verify it is a data structure */ if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) { pr_warning("%s is not a data structure nor an union.\n", varname); return -EINVAL; } if (field->name[0] == '[') { pr_err("Semantic error: %s is not a pointer" " nor array.\n", varname); return -EINVAL; } /* While prcessing unnamed field, we don't care about this */ if (field->ref && dwarf_diename(vr_die)) { pr_err("Semantic error: %s must be referred by '.'\n", field->name); return -EINVAL; } if (!ref) { pr_warning("Structure on a register is not " "supported yet.\n"); return -ENOTSUP; } } if (die_find_member(&type, field->name, die_mem) == NULL) { pr_warning("%s(type:%s) has no member %s.\n", varname, dwarf_diename(&type), field->name); return -EINVAL; } /* Get the offset of the field */ if (tag == DW_TAG_union_type) { offs = 0; } else { ret = die_get_data_member_location(die_mem, &offs); if (ret < 0) { pr_warning("Failed to get the offset of %s.\n", field->name); return ret; } } ref->offset += (long)offs; /* If this member is unnamed, we need to reuse this field */ if (!dwarf_diename(die_mem)) return convert_variable_fields(die_mem, varname, field, &ref, die_mem); next: /* Converting next field */ if (field->next) return convert_variable_fields(die_mem, field->name, field->next, &ref, die_mem); else return 0; }
static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, struct perf_probe_arg_field *field, struct kprobe_trace_arg_ref **ref_ptr, Dwarf_Die *die_mem) { struct kprobe_trace_arg_ref *ref = *ref_ptr; Dwarf_Die type; Dwarf_Word offs; int ret; pr_debug("converting %s in %s\n", field->name, varname); if (die_get_real_type(vr_die, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } /* Check the pointer and dereference */ if (dwarf_tag(&type) == DW_TAG_pointer_type) { if (!field->ref) { pr_err("Semantic error: %s must be referred by '->'\n", field->name); return -EINVAL; } /* Get the type pointed by this pointer */ if (die_get_real_type(&type, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } /* Verify it is a data structure */ if (dwarf_tag(&type) != DW_TAG_structure_type) { pr_warning("%s is not a data structure.\n", varname); return -EINVAL; } ref = zalloc(sizeof(struct kprobe_trace_arg_ref)); if (ref == NULL) return -ENOMEM; if (*ref_ptr) (*ref_ptr)->next = ref; else *ref_ptr = ref; } else { /* Verify it is a data structure */ if (dwarf_tag(&type) != DW_TAG_structure_type) { pr_warning("%s is not a data structure.\n", varname); return -EINVAL; } if (field->ref) { pr_err("Semantic error: %s must be referred by '.'\n", field->name); return -EINVAL; } if (!ref) { pr_warning("Structure on a register is not " "supported yet.\n"); return -ENOTSUP; } } if (die_find_member(&type, field->name, die_mem) == NULL) { pr_warning("%s(tyep:%s) has no member %s.\n", varname, dwarf_diename(&type), field->name); return -EINVAL; } /* Get the offset of the field */ ret = die_get_data_member_location(die_mem, &offs); if (ret < 0) { pr_warning("Failed to get the offset of %s.\n", field->name); return ret; } ref->offset += (long)offs; /* Converting next field */ if (field->next) return convert_variable_fields(die_mem, field->name, field->next, &ref, die_mem); else return 0; }