Unified · Split
| @@ -22,14 +22,15 @@ current_tracer. Instead of that, add probe points via · 21 unmodified lines | ||
| 22 | 22 | |
| 23 | 23 | Synopsis of kprobe_events |
| 24 | 24 | ------------------------- |
| 25 | − p[:[GRP/]EVENT] SYMBOL[+offs]|MEMADDR [FETCHARGS] : Set a probe | |
| 26 | − r[:[GRP/]EVENT] SYMBOL[+0] [FETCHARGS] : Set a return probe | |
| 25 | + p[:[GRP/]EVENT] [MOD:]SYM[+offs]|MEMADDR [FETCHARGS] : Set a probe | |
| 26 | + r[:[GRP/]EVENT] [MOD:]SYM[+0] [FETCHARGS] : Set a return probe | |
| 27 | 27 | -:[GRP/]EVENT : Clear a probe |
| 28 | 28 | |
| 29 | 29 | GRP : Group name. If omitted, use "kprobes" for it. |
| 30 | 30 | EVENT : Event name. If omitted, the event name is generated |
| 31 | − based on SYMBOL+offs or MEMADDR. | |
| 32 | − SYMBOL[+offs] : Symbol+offset where the probe is inserted. | |
| 31 | + based on SYM+offs or MEMADDR. | |
| 32 | + MOD : Module name which has given SYM. | |
| 33 | + SYM[+offs] : Symbol+offset where the probe is inserted. | |
| 33 | 34 | MEMADDR : Address where the probe is inserted. |
| 34 | 35 | |
| 35 | 36 | FETCHARGS : Arguments. Each probe can have up to 128 args. |
| @@ -101,6 +101,14 @@ · 100 unmodified lines | ||
| 101 | 101 | #define P4_CONFIG_HT_SHIFT 63 |
| 102 | 102 | #define P4_CONFIG_HT (1ULL << P4_CONFIG_HT_SHIFT) |
| 103 | 103 | |
| 104 | +/* | |
| 105 | + * If an event has alias it should be marked | |
| 106 | + * with a special bit. (Don't forget to check | |
| 107 | + * P4_PEBS_CONFIG_MASK and related bits on | |
| 108 | + * modification.) | |
| 109 | + */ | |
| 110 | +#define P4_CONFIG_ALIASABLE (1 << 9) | |
| 111 | + | |
| 104 | 112 | /* |
| 105 | 113 | * The bits we allow to pass for RAW events |
| 106 | 114 | */ |
| @@ -123,6 +131,31 @@ · 16 unmodified lines | ||
| 123 | 131 | (p4_config_pack_escr(P4_CONFIG_MASK_ESCR)) | \ |
| 124 | 132 | (p4_config_pack_cccr(P4_CONFIG_MASK_CCCR)) |
| 125 | 133 | |
| 134 | +/* | |
| 135 | + * In case of event aliasing we need to preserve some | |
| 136 | + * caller bits otherwise the mapping won't be complete. | |
| 137 | + */ | |
| 138 | +#define P4_CONFIG_EVENT_ALIAS_MASK \ | |
| 139 | + (p4_config_pack_escr(P4_CONFIG_MASK_ESCR) | \ | |
| 140 | + p4_config_pack_cccr(P4_CCCR_EDGE | \ | |
| 141 | + P4_CCCR_THRESHOLD_MASK | \ | |
| 142 | + P4_CCCR_COMPLEMENT | \ | |
| 143 | + P4_CCCR_COMPARE)) | |
| 144 | + | |
| 145 | +#define P4_CONFIG_EVENT_ALIAS_IMMUTABLE_BITS \ | |
| 146 | + ((P4_CONFIG_HT) | \ | |
| 147 | + p4_config_pack_escr(P4_ESCR_T0_OS | \ | |
| 148 | + P4_ESCR_T0_USR | \ | |
| 149 | + P4_ESCR_T1_OS | \ | |
| 150 | + P4_ESCR_T1_USR) | \ | |
| 151 | + p4_config_pack_cccr(P4_CCCR_OVF | \ | |
| 152 | + P4_CCCR_CASCADE | \ | |
| 153 | + P4_CCCR_FORCE_OVF | \ | |
| 154 | + P4_CCCR_THREAD_ANY | \ | |
| 155 | + P4_CCCR_OVF_PMI_T0 | \ | |
| 156 | + P4_CCCR_OVF_PMI_T1 | \ | |
| 157 | + P4_CONFIG_ALIASABLE)) | |
| 158 | + | |
| 126 | 159 | static inline bool p4_is_event_cascaded(u64 config) |
| 127 | 160 | { |
| 128 | 161 | u32 cccr = p4_config_unpack_cccr(config); |
| @@ -274,7 +274,6 @@ struct x86_pmu { · 273 unmodified lines | ||
| 274 | 274 | void (*enable_all)(int added); |
| 275 | 275 | void (*enable)(struct perf_event *); |
| 276 | 276 | void (*disable)(struct perf_event *); |
| 277 | − void (*hw_watchdog_set_attr)(struct perf_event_attr *attr); | |
| 278 | 277 | int (*hw_config)(struct perf_event *event); |
| 279 | 278 | int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign); |
| 280 | 279 | unsigned eventsel; |
| @@ -360,12 +359,6 @@ static u64 __read_mostly hw_cache_extra_regs · 79 unmodified lines | ||
| 360 | 359 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 361 | 360 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
| 362 | 361 | |
| 363 | −void hw_nmi_watchdog_set_attr(struct perf_event_attr *wd_attr) | |
| 364 | −{ | |
| 365 | − if (x86_pmu.hw_watchdog_set_attr) | |
| 366 | − x86_pmu.hw_watchdog_set_attr(wd_attr); | |
| 367 | −} | |
| 368 | − | |
| 369 | 362 | /* |
| 370 | 363 | * Propagate event elapsed time into the generic event. |
| 371 | 364 | * Can only be executed on the CPU where the event is active. |
| @@ -570,11 +570,92 @@ static __initconst const u64 p4_hw_cache_event_ids · 569 unmodified lines | ||
| 570 | 570 | }, |
| 571 | 571 | }; |
| 572 | 572 | |
| 573 | +/* | |
| 574 | + * Because of Netburst being quite restricted in now | |
| 575 | + * many same events can run simultaneously, we use | |
| 576 | + * event aliases, ie different events which have the | |
| 577 | + * same functionallity but use non-intersected resources | |
| 578 | + * (ESCR/CCCR/couter registers). This allow us to run | |
| 579 | + * two or more semi-same events together. It is done | |
| 580 | + * transparently to a user space. | |
| 581 | + * | |
| 582 | + * Never set any cusom internal bits such as P4_CONFIG_HT, | |
| 583 | + * P4_CONFIG_ALIASABLE or bits for P4_PEBS_METRIC, they are | |
| 584 | + * either up-to-dated automatically either not appliable | |
| 585 | + * at all. | |
| 586 | + * | |
| 587 | + * And be really carefull choosing aliases! | |
| 588 | + */ | |
| 589 | +struct p4_event_alias { | |
| 590 | + u64 orig; | |
| 591 | + u64 alter; | |
| 592 | +} p4_event_aliases[] = { | |
| 593 | + { | |
| 594 | + /* | |
| 595 | + * Non-halted cycles can be substituted with | |
| 596 | + * non-sleeping cycles (see Intel SDM Vol3b for | |
| 597 | + * details). | |
| 598 | + */ | |
| 599 | + .orig = | |
| 600 | + p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_GLOBAL_POWER_EVENTS) | | |
| 601 | + P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING)), | |
| 602 | + .alter = | |
| 603 | + p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_EXECUTION_EVENT) | | |
| 604 | + P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS0)| | |
| 605 | + P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS1)| | |
| 606 | + P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS2)| | |
| 607 | + P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS3)| | |
| 608 | + P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS0) | | |
| 609 | + P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS1) | | |
| 610 | + P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS2) | | |
| 611 | + P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS3))| | |
| 612 | + p4_config_pack_cccr(P4_CCCR_THRESHOLD(15) | P4_CCCR_COMPLEMENT | | |
| 613 | + P4_CCCR_COMPARE), | |
| 614 | + }, | |
| 615 | +}; | |
| 616 | + | |
| 617 | +static u64 p4_get_alias_event(u64 config) | |
| 618 | +{ | |
| 619 | + u64 config_match; | |
| 620 | + int i; | |
| 621 | + | |
| 622 | + /* | |
| 623 | + * Probably we're lucky and don't have to do | |
| 624 | + * matching over all config bits. | |
| 625 | + */ | |
| 626 | + if (!(config & P4_CONFIG_ALIASABLE)) | |
| 627 | + return 0; | |
| 628 | + | |
| 629 | + config_match = config & P4_CONFIG_EVENT_ALIAS_MASK; | |
| 630 | + | |
| 631 | + /* | |
| 632 | + * If an event was previously swapped to the alter config | |
| 633 | + * we should swap it back otherwise contnention on registers | |
| 634 | + * will return back. | |
| 635 | + */ | |
| 636 | + for (i = 0; i < ARRAY_SIZE(p4_event_aliases); i++) { | |
| 637 | + if (config_match == p4_event_aliases[i].orig) { | |
| 638 | + config_match = p4_event_aliases[i].alter; | |
| 639 | + break; | |
| 640 | + } else if (config_match == p4_event_aliases[i].alter) { | |
| 641 | + config_match = p4_event_aliases[i].orig; | |
| 642 | + break; | |
| 643 | + } | |
| 644 | + } | |
| 645 | + | |
| 646 | + if (i >= ARRAY_SIZE(p4_event_aliases)) | |
| 647 | + return 0; | |
| 648 | + | |
| 649 | + return config_match | | |
| 650 | + (config & P4_CONFIG_EVENT_ALIAS_IMMUTABLE_BITS); | |
| 651 | +} | |
| 652 | + | |
| 573 | 653 | static u64 p4_general_events[PERF_COUNT_HW_MAX] = { |
| 574 | 654 | /* non-halted CPU clocks */ |
| 575 | 655 | [PERF_COUNT_HW_CPU_CYCLES] = |
| 576 | 656 | p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_GLOBAL_POWER_EVENTS) | |
| 577 | − P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING)), | |
| 657 | + P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING)) | | |
| 658 | + P4_CONFIG_ALIASABLE, | |
| 578 | 659 | |
| 579 | 660 | /* |
| 580 | 661 | * retired instructions |
| @@ -719,31 +800,6 @@ static int p4_validate_raw_event(struct perf_event *event) · 138 unmodified lines | ||
| 719 | 800 | return 0; |
| 720 | 801 | } |
| 721 | 802 | |
| 722 | −static void p4_hw_watchdog_set_attr(struct perf_event_attr *wd_attr) | |
| 723 | −{ | |
| 724 | − /* | |
| 725 | − * Watchdog ticks are special on Netburst, we use | |
| 726 | − * that named "non-sleeping" ticks as recommended | |
| 727 | − * by Intel SDM Vol3b. | |
| 728 | − */ | |
| 729 | − WARN_ON_ONCE(wd_attr->type != PERF_TYPE_HARDWARE || | |
| 730 | − wd_attr->config != PERF_COUNT_HW_CPU_CYCLES); | |
| 731 | − | |
| 732 | − wd_attr->type = PERF_TYPE_RAW; | |
| 733 | − wd_attr->config = | |
| 734 | − p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_EXECUTION_EVENT) | | |
| 735 | − P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS0) | | |
| 736 | − P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS1) | | |
| 737 | − P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS2) | | |
| 738 | − P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS3) | | |
| 739 | − P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS0) | | |
| 740 | − P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS1) | | |
| 741 | − P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS2) | | |
| 742 | − P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS3)) | | |
| 743 | − p4_config_pack_cccr(P4_CCCR_THRESHOLD(15) | P4_CCCR_COMPLEMENT | | |
| 744 | − P4_CCCR_COMPARE); | |
| 745 | −} | |
| 746 | − | |
| 747 | 803 | static int p4_hw_config(struct perf_event *event) |
| 748 | 804 | { |
| 749 | 805 | int cpu = get_cpu(); |
| @@ -1159,6 +1215,8 @@ static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign · 409 unmodified lines | ||
| 1159 | 1215 | struct p4_event_bind *bind; |
| 1160 | 1216 | unsigned int i, thread, num; |
| 1161 | 1217 | int cntr_idx, escr_idx; |
| 1218 | + u64 config_alias; | |
| 1219 | + int pass; | |
| 1162 | 1220 | |
| 1163 | 1221 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); |
| 1164 | 1222 | bitmap_zero(escr_mask, P4_ESCR_MSR_TABLE_SIZE); |
| @@ -1167,6 +1225,17 @@ static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign · 2 unmodified lines | ||
| 1167 | 1225 | |
| 1168 | 1226 | hwc = &cpuc->event_list[i]->hw; |
| 1169 | 1227 | thread = p4_ht_thread(cpu); |
| 1228 | + pass = 0; | |
| 1229 | + | |
| 1230 | +again: | |
| 1231 | + /* | |
| 1232 | + * Aliases are swappable so we may hit circular | |
| 1233 | + * lock if both original config and alias need | |
| 1234 | + * resources (MSR registers) which already busy. | |
| 1235 | + */ | |
| 1236 | + if (pass > 2) | |
| 1237 | + goto done; | |
| 1238 | + | |
| 1170 | 1239 | bind = p4_config_get_bind(hwc->config); |
| 1171 | 1240 | escr_idx = p4_get_escr_idx(bind->escr_msr[thread]); |
| 1172 | 1241 | if (unlikely(escr_idx == -1)) |
| @@ -1180,8 +1249,17 @@ static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign · 7 unmodified lines | ||
| 1180 | 1249 | } |
| 1181 | 1250 | |
| 1182 | 1251 | cntr_idx = p4_next_cntr(thread, used_mask, bind); |
| 1183 | − if (cntr_idx == -1 || test_bit(escr_idx, escr_mask)) | |
| 1184 | − goto done; | |
| 1252 | + if (cntr_idx == -1 || test_bit(escr_idx, escr_mask)) { | |
| 1253 | + /* | |
| 1254 | + * Probably an event alias is still available. | |
| 1255 | + */ | |
| 1256 | + config_alias = p4_get_alias_event(hwc->config); | |
| 1257 | + if (!config_alias) | |
| 1258 | + goto done; | |
| 1259 | + hwc->config = config_alias; | |
| 1260 | + pass++; | |
| 1261 | + goto again; | |
| 1262 | + } | |
| 1185 | 1263 | |
| 1186 | 1264 | p4_pmu_swap_config_ts(hwc, cpu); |
| 1187 | 1265 | if (assign) |
| @@ -1218,7 +1296,6 @@ static __initconst const struct x86_pmu p4_pmu = { · 30 unmodified lines | ||
| 1218 | 1296 | .cntval_bits = ARCH_P4_CNTRVAL_BITS, |
| 1219 | 1297 | .cntval_mask = ARCH_P4_CNTRVAL_MASK, |
| 1220 | 1298 | .max_period = (1ULL << (ARCH_P4_CNTRVAL_BITS - 1)) - 1, |
| 1221 | − .hw_watchdog_set_attr = p4_hw_watchdog_set_attr, | |
| 1222 | 1299 | .hw_config = p4_hw_config, |
| 1223 | 1300 | .schedule_events = p4_pmu_schedule_events, |
| 1224 | 1301 | /* |
| @@ -76,6 +76,7 @@ struct trace_iterator { · 75 unmodified lines | ||
| 76 | 76 | struct trace_entry *ent; |
| 77 | 77 | unsigned long lost_events; |
| 78 | 78 | int leftover; |
| 79 | + int ent_size; | |
| 79 | 80 | int cpu; |
| 80 | 81 | u64 ts; |
| 81 | 82 | |
| @@ -1255,19 +1255,29 @@ static int __kprobes in_kprobes_functions(unsigned long addr) · 1254 unmodified lines | ||
| 1255 | 1255 | /* |
| 1256 | 1256 | * If we have a symbol_name argument, look it up and add the offset field |
| 1257 | 1257 | * to it. This way, we can specify a relative address to a symbol. |
| 1258 | + * This returns encoded errors if it fails to look up symbol or invalid | |
| 1259 | + * combination of parameters. | |
| 1258 | 1260 | */ |
| 1259 | 1261 | static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p) |
| 1260 | 1262 | { |
| 1261 | 1263 | kprobe_opcode_t *addr = p->addr; |
| 1264 | + | |
| 1265 | + if ((p->symbol_name && p->addr) || | |
| 1266 | + (!p->symbol_name && !p->addr)) | |
| 1267 | + goto invalid; | |
| 1268 | + | |
| 1262 | 1269 | if (p->symbol_name) { |
| 1263 | − if (addr) | |
| 1264 | − return NULL; | |
| 1265 | 1270 | kprobe_lookup_name(p->symbol_name, addr); |
| 1271 | + if (!addr) | |
| 1272 | + return ERR_PTR(-ENOENT); | |
| 1266 | 1273 | } |
| 1267 | 1274 | |
| 1268 | − if (!addr) | |
| 1269 | − return NULL; | |
| 1270 | − return (kprobe_opcode_t *)(((char *)addr) + p->offset); | |
| 1275 | + addr = (kprobe_opcode_t *)(((char *)addr) + p->offset); | |
| 1276 | + if (addr) | |
| 1277 | + return addr; | |
| 1278 | + | |
| 1279 | +invalid: | |
| 1280 | + return ERR_PTR(-EINVAL); | |
| 1271 | 1281 | } |
| 1272 | 1282 | |
| 1273 | 1283 | /* Check passed kprobe is valid and return kprobe in kprobe_table. */ |
| @@ -1311,8 +1321,8 @@ int __kprobes register_kprobe(struct kprobe *p) · 37 unmodified lines | ||
| 1311 | 1321 | kprobe_opcode_t *addr; |
| 1312 | 1322 | |
| 1313 | 1323 | addr = kprobe_addr(p); |
| 1314 | − if (!addr) | |
| 1315 | − return -EINVAL; | |
| 1324 | + if (IS_ERR(addr)) | |
| 1325 | + return PTR_ERR(addr); | |
| 1316 | 1326 | p->addr = addr; |
| 1317 | 1327 | |
| 1318 | 1328 | ret = check_kprobe_rereg(p); |
| @@ -1335,6 +1345,8 @@ int __kprobes register_kprobe(struct kprobe *p) · 16 unmodified lines | ||
| 1335 | 1345 | */ |
| 1336 | 1346 | probed_mod = __module_text_address((unsigned long) p->addr); |
| 1337 | 1347 | if (probed_mod) { |
| 1348 | + /* Return -ENOENT if fail. */ | |
| 1349 | + ret = -ENOENT; | |
| 1338 | 1350 | /* |
| 1339 | 1351 | * We must hold a refcount of the probed module while updating |
| 1340 | 1352 | * its code to prohibit unexpected unloading. |
| @@ -1351,6 +1363,7 @@ int __kprobes register_kprobe(struct kprobe *p) · 10 unmodified lines | ||
| 1351 | 1363 | module_put(probed_mod); |
| 1352 | 1364 | goto fail_with_jump_label; |
| 1353 | 1365 | } |
| 1366 | + /* ret will be updated by following code */ | |
| 1354 | 1367 | } |
| 1355 | 1368 | preempt_enable(); |
| 1356 | 1369 | jump_label_unlock(); |
| @@ -1399,7 +1412,7 @@ out: · 42 unmodified lines | ||
| 1399 | 1412 | fail_with_jump_label: |
| 1400 | 1413 | preempt_enable(); |
| 1401 | 1414 | jump_label_unlock(); |
| 1402 | − return -EINVAL; | |
| 1415 | + return ret; | |
| 1403 | 1416 | } |
| 1404 | 1417 | EXPORT_SYMBOL_GPL(register_kprobe); |
| 1405 | 1418 | |
| @@ -1686,8 +1699,8 @@ int __kprobes register_kretprobe(struct kretprobe *rp) · 280 unmodified lines | ||
| 1686 | 1699 | |
| 1687 | 1700 | if (kretprobe_blacklist_size) { |
| 1688 | 1701 | addr = kprobe_addr(&rp->kp); |
| 1689 | − if (!addr) | |
| 1690 | − return -EINVAL; | |
| 1702 | + if (IS_ERR(addr)) | |
| 1703 | + return PTR_ERR(addr); | |
| 1691 | 1704 | |
| 1692 | 1705 | for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { |
| 1693 | 1706 | if (kretprobe_blacklist[i].addr == addr) |
| @@ -88,6 +88,7 @@ static struct ftrace_ops ftrace_list_end __read_mostly = { · 87 unmodified lines | ||
| 88 | 88 | static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end; |
| 89 | 89 | static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end; |
| 90 | 90 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; |
| 91 | +static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub; | |
| 91 | 92 | ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub; |
| 92 | 93 | ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub; |
| 93 | 94 | static struct ftrace_ops global_ops; |
| @@ -146,9 +147,11 @@ void clear_ftrace_function(void) · 52 unmodified lines | ||
| 146 | 147 | { |
| 147 | 148 | ftrace_trace_function = ftrace_stub; |
| 148 | 149 | __ftrace_trace_function = ftrace_stub; |
| 150 | + __ftrace_trace_function_delay = ftrace_stub; | |
| 149 | 151 | ftrace_pid_function = ftrace_stub; |
| 150 | 152 | } |
| 151 | 153 | |
| 154 | +#undef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST | |
| 152 | 155 | #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
| 153 | 156 | /* |
| 154 | 157 | * For those archs that do not test ftrace_trace_stop in their |
| @@ -207,8 +210,13 @@ static void update_ftrace_function(void) · 52 unmodified lines | ||
| 207 | 210 | |
| 208 | 211 | #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
| 209 | 212 | ftrace_trace_function = func; |
| 213 | +#else | |
| 214 | +#ifdef CONFIG_DYNAMIC_FTRACE | |
| 215 | + /* do not update till all functions have been modified */ | |
| 216 | + __ftrace_trace_function_delay = func; | |
| 210 | 217 | #else |
| 211 | 218 | __ftrace_trace_function = func; |
| 219 | +#endif | |
| 212 | 220 | ftrace_trace_function = ftrace_test_stop_func; |
| 213 | 221 | #endif |
| 214 | 222 | } |
| @@ -1170,8 +1178,14 @@ alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) · 955 unmodified lines | ||
| 1170 | 1178 | return NULL; |
| 1171 | 1179 | } |
| 1172 | 1180 | |
| 1181 | +static void | |
| 1182 | +ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash); | |
| 1183 | +static void | |
| 1184 | +ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash); | |
| 1185 | + | |
| 1173 | 1186 | static int |
| 1174 | −ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src) | |
| 1187 | +ftrace_hash_move(struct ftrace_ops *ops, int enable, | |
| 1188 | + struct ftrace_hash **dst, struct ftrace_hash *src) | |
| 1175 | 1189 | { |
| 1176 | 1190 | struct ftrace_func_entry *entry; |
| 1177 | 1191 | struct hlist_node *tp, *tn; |
| @@ -1181,8 +1195,15 @@ ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src) · 3 unmodified lines | ||
| 1181 | 1195 | unsigned long key; |
| 1182 | 1196 | int size = src->count; |
| 1183 | 1197 | int bits = 0; |
| 1198 | + int ret; | |
| 1184 | 1199 | int i; |
| 1185 | 1200 | |
| 1201 | + /* | |
| 1202 | + * Remove the current set, update the hash and add | |
| 1203 | + * them back. | |
| 1204 | + */ | |
| 1205 | + ftrace_hash_rec_disable(ops, enable); | |
| 1206 | + | |
| 1186 | 1207 | /* |
| 1187 | 1208 | * If the new source is empty, just free dst and assign it |
| 1188 | 1209 | * the empty_hash. |
| @@ -1203,9 +1224,10 @@ ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src) · 14 unmodified lines | ||
| 1203 | 1224 | if (bits > FTRACE_HASH_MAX_BITS) |
| 1204 | 1225 | bits = FTRACE_HASH_MAX_BITS; |
| 1205 | 1226 | |
| 1227 | + ret = -ENOMEM; | |
| 1206 | 1228 | new_hash = alloc_ftrace_hash(bits); |
| 1207 | 1229 | if (!new_hash) |
| 1208 | − return -ENOMEM; | |
| 1230 | + goto out; | |
| 1209 | 1231 | |
| 1210 | 1232 | size = 1 << src->size_bits; |
| 1211 | 1233 | for (i = 0; i < size; i++) { |
| @@ -1224,7 +1246,16 @@ ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src) · 12 unmodified lines | ||
| 1224 | 1246 | rcu_assign_pointer(*dst, new_hash); |
| 1225 | 1247 | free_ftrace_hash_rcu(old_hash); |
| 1226 | 1248 | |
| 1227 | − return 0; | |
| 1249 | + ret = 0; | |
| 1250 | + out: | |
| 1251 | + /* | |
| 1252 | + * Enable regardless of ret: | |
| 1253 | + * On success, we enable the new hash. | |
| 1254 | + * On failure, we re-enable the original hash. | |
| 1255 | + */ | |
| 1256 | + ftrace_hash_rec_enable(ops, enable); | |
| 1257 | + | |
| 1258 | + return ret; | |
| 1228 | 1259 | } |
| 1229 | 1260 | |
| 1230 | 1261 | /* |
| @@ -1584,6 +1615,12 @@ static int __ftrace_modify_code(void *data) · 353 unmodified lines | ||
| 1584 | 1615 | { |
| 1585 | 1616 | int *command = data; |
| 1586 | 1617 | |
| 1618 | + /* | |
| 1619 | + * Do not call function tracer while we update the code. | |
| 1620 | + * We are in stop machine, no worrying about races. | |
| 1621 | + */ | |
| 1622 | + function_trace_stop++; | |
| 1623 | + | |
| 1587 | 1624 | if (*command & FTRACE_ENABLE_CALLS) |
| 1588 | 1625 | ftrace_replace_code(1); |
| 1589 | 1626 | else if (*command & FTRACE_DISABLE_CALLS) |
| @@ -1597,6 +1634,18 @@ static int __ftrace_modify_code(void *data) · 7 unmodified lines | ||
| 1597 | 1634 | else if (*command & FTRACE_STOP_FUNC_RET) |
| 1598 | 1635 | ftrace_disable_ftrace_graph_caller(); |
| 1599 | 1636 | |
| 1637 | +#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST | |
| 1638 | + /* | |
| 1639 | + * For archs that call ftrace_test_stop_func(), we must | |
| 1640 | + * wait till after we update all the function callers | |
| 1641 | + * before we update the callback. This keeps different | |
| 1642 | + * ops that record different functions from corrupting | |
| 1643 | + * each other. | |
| 1644 | + */ | |
| 1645 | + __ftrace_trace_function = __ftrace_trace_function_delay; | |
| 1646 | +#endif | |
| 1647 | + function_trace_stop--; | |
| 1648 | + | |
| 1600 | 1649 | return 0; |
| 1601 | 1650 | } |
| 1602 | 1651 | |
| @@ -2865,7 +2914,11 @@ ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, · 1262 unmodified lines | ||
| 2865 | 2914 | ftrace_match_records(hash, buf, len); |
| 2866 | 2915 | |
| 2867 | 2916 | mutex_lock(&ftrace_lock); |
| 2868 | − ret = ftrace_hash_move(orig_hash, hash); | |
| 2917 | + ret = ftrace_hash_move(ops, enable, orig_hash, hash); | |
| 2918 | + if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED | |
| 2919 | + && ftrace_enabled) | |
| 2920 | + ftrace_run_update_code(FTRACE_ENABLE_CALLS); | |
| 2921 | + | |
| 2869 | 2922 | mutex_unlock(&ftrace_lock); |
| 2870 | 2923 | |
| 2871 | 2924 | mutex_unlock(&ftrace_regex_lock); |
| @@ -3048,18 +3101,12 @@ ftrace_regex_release(struct inode *inode, struct file *file) · 176 unmodified lines | ||
| 3048 | 3101 | orig_hash = &iter->ops->notrace_hash; |
| 3049 | 3102 | |
| 3050 | 3103 | mutex_lock(&ftrace_lock); |
| 3051 | − /* | |
| 3052 | − * Remove the current set, update the hash and add | |
| 3053 | − * them back. | |
| 3054 | − */ | |
| 3055 | − ftrace_hash_rec_disable(iter->ops, filter_hash); | |
| 3056 | − ret = ftrace_hash_move(orig_hash, iter->hash); | |
| 3057 | − if (!ret) { | |
| 3058 | − ftrace_hash_rec_enable(iter->ops, filter_hash); | |
| 3059 | − if (iter->ops->flags & FTRACE_OPS_FL_ENABLED | |
| 3060 | − && ftrace_enabled) | |
| 3061 | − ftrace_run_update_code(FTRACE_ENABLE_CALLS); | |
| 3062 | − } | |
| 3104 | + ret = ftrace_hash_move(iter->ops, filter_hash, | |
| 3105 | + orig_hash, iter->hash); | |
| 3106 | + if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED) | |
| 3107 | + && ftrace_enabled) | |
| 3108 | + ftrace_run_update_code(FTRACE_ENABLE_CALLS); | |
| 3109 | + | |
| 3063 | 3110 | mutex_unlock(&ftrace_lock); |
| 3064 | 3111 | } |
| 3065 | 3112 | free_ftrace_hash(iter->hash); |
| @@ -3338,7 +3385,7 @@ static int ftrace_process_locs(struct module *mod, · 272 unmodified lines | ||
| 3338 | 3385 | { |
| 3339 | 3386 | unsigned long *p; |
| 3340 | 3387 | unsigned long addr; |
| 3341 | − unsigned long flags; | |
| 3388 | + unsigned long flags = 0; /* Shut up gcc */ | |
| 3342 | 3389 | |
| 3343 | 3390 | mutex_lock(&ftrace_lock); |
| 3344 | 3391 | p = start; |
| @@ -3356,12 +3403,18 @@ static int ftrace_process_locs(struct module *mod, · 11 unmodified lines | ||
| 3356 | 3403 | } |
| 3357 | 3404 | |
| 3358 | 3405 | /* |
| 3359 | − * Disable interrupts to prevent interrupts from executing | |
| 3360 | − * code that is being modified. | |
| 3406 | + * We only need to disable interrupts on start up | |
| 3407 | + * because we are modifying code that an interrupt | |
| 3408 | + * may execute, and the modification is not atomic. | |
| 3409 | + * But for modules, nothing runs the code we modify | |
| 3410 | + * until we are finished with it, and there's no | |
| 3411 | + * reason to cause large interrupt latencies while we do it. | |
| 3361 | 3412 | */ |
| 3362 | − local_irq_save(flags); | |
| 3413 | + if (!mod) | |
| 3414 | + local_irq_save(flags); | |
| 3363 | 3415 | ftrace_update_code(mod); |
| 3364 | − local_irq_restore(flags); | |
| 3416 | + if (!mod) | |
| 3417 | + local_irq_restore(flags); | |
| 3365 | 3418 | mutex_unlock(&ftrace_lock); |
| 3366 | 3419 | |
| 3367 | 3420 | return 0; |
| @@ -1248,6 +1248,15 @@ ftrace(struct trace_array *tr, struct trace_array_cpu *data, · 1247 unmodified lines | ||
| 1248 | 1248 | } |
| 1249 | 1249 | |
| 1250 | 1250 | #ifdef CONFIG_STACKTRACE |
| 1251 | + | |
| 1252 | +#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long)) | |
| 1253 | +struct ftrace_stack { | |
| 1254 | + unsigned long calls[FTRACE_STACK_MAX_ENTRIES]; | |
| 1255 | +}; | |
| 1256 | + | |
| 1257 | +static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack); | |
| 1258 | +static DEFINE_PER_CPU(int, ftrace_stack_reserve); | |
| 1259 | + | |
| 1251 | 1260 | static void __ftrace_trace_stack(struct ring_buffer *buffer, |
| 1252 | 1261 | unsigned long flags, |
| 1253 | 1262 | int skip, int pc, struct pt_regs *regs) |
| @@ -1256,25 +1265,77 @@ static void __ftrace_trace_stack(struct ring_buffer *buffer, · 2 unmodified lines | ||
| 1256 | 1265 | struct ring_buffer_event *event; |
| 1257 | 1266 | struct stack_entry *entry; |
| 1258 | 1267 | struct stack_trace trace; |
| 1268 | + int use_stack; | |
| 1269 | + int size = FTRACE_STACK_ENTRIES; | |
| 1270 | + | |
| 1271 | + trace.nr_entries = 0; | |
| 1272 | + trace.skip = skip; | |
| 1273 | + | |
| 1274 | + /* | |
| 1275 | + * Since events can happen in NMIs there's no safe way to | |
| 1276 | + * use the per cpu ftrace_stacks. We reserve it and if an interrupt | |
| 1277 | + * or NMI comes in, it will just have to use the default | |
| 1278 | + * FTRACE_STACK_SIZE. | |
| 1279 | + */ | |
| 1280 | + preempt_disable_notrace(); | |
| 1281 | + | |
| 1282 | + use_stack = ++__get_cpu_var(ftrace_stack_reserve); | |
| 1283 | + /* | |
| 1284 | + * We don't need any atomic variables, just a barrier. | |
| 1285 | + * If an interrupt comes in, we don't care, because it would | |
| 1286 | + * have exited and put the counter back to what we want. | |
| 1287 | + * We just need a barrier to keep gcc from moving things | |
| 1288 | + * around. | |
| 1289 | + */ | |
| 1290 | + barrier(); | |
| 1291 | + if (use_stack == 1) { | |
| 1292 | + trace.entries = &__get_cpu_var(ftrace_stack).calls[0]; | |
| 1293 | + trace.max_entries = FTRACE_STACK_MAX_ENTRIES; | |
| 1294 | + | |
| 1295 | + if (regs) | |
| 1296 | + save_stack_trace_regs(regs, &trace); | |
| 1297 | + else | |
| 1298 | + save_stack_trace(&trace); | |
| 1299 | + | |
| 1300 | + if (trace.nr_entries > size) | |
| 1301 | + size = trace.nr_entries; | |
| 1302 | + } else | |
| 1303 | + /* From now on, use_stack is a boolean */ | |
| 1304 | + use_stack = 0; | |
| 1305 | + | |
| 1306 | + size *= sizeof(unsigned long); | |
| 1259 | 1307 | |
| 1260 | 1308 | event = trace_buffer_lock_reserve(buffer, TRACE_STACK, |
| 1261 | − sizeof(*entry), flags, pc); | |
| 1309 | + sizeof(*entry) + size, flags, pc); | |
| 1262 | 1310 | if (!event) |
| 1263 | − return; | |
| 1264 | − entry = ring_buffer_event_data(event); | |
| 1265 | − memset(&entry->caller, 0, sizeof(entry->caller)); | |
| 1311 | + goto out; | |
| 1312 | + entry = ring_buffer_event_data(event); | |
| 1266 | 1313 | |
| 1267 | − trace.nr_entries = 0; | |
| 1268 | − trace.max_entries = FTRACE_STACK_ENTRIES; | |
| 1269 | − trace.skip = skip; | |
| 1270 | − trace.entries = entry->caller; | |
| 1314 | + memset(&entry->caller, 0, size); | |
| 1315 | + | |
| 1316 | + if (use_stack) | |
| 1317 | + memcpy(&entry->caller, trace.entries, | |
| 1318 | + trace.nr_entries * sizeof(unsigned long)); | |
| 1319 | + else { | |
| 1320 | + trace.max_entries = FTRACE_STACK_ENTRIES; | |
| 1321 | + trace.entries = entry->caller; | |
| 1322 | + if (regs) | |
| 1323 | + save_stack_trace_regs(regs, &trace); | |
| 1324 | + else | |
| 1325 | + save_stack_trace(&trace); | |
| 1326 | + } | |
| 1327 | + | |
| 1328 | + entry->size = trace.nr_entries; | |
| 1271 | 1329 | |
| 1272 | − if (regs) | |
| 1273 | − save_stack_trace_regs(regs, &trace); | |
| 1274 | − else | |
| 1275 | − save_stack_trace(&trace); | |
| 1276 | 1330 | if (!filter_check_discard(call, entry, buffer, event)) |
| 1277 | 1331 | ring_buffer_unlock_commit(buffer, event); |
| 1332 | + | |
| 1333 | + out: | |
| 1334 | + /* Again, don't let gcc optimize things here */ | |
| 1335 | + barrier(); | |
| 1336 | + __get_cpu_var(ftrace_stack_reserve)--; | |
| 1337 | + preempt_enable_notrace(); | |
| 1338 | + | |
| 1278 | 1339 | } |
| 1279 | 1340 | |
| 1280 | 1341 | void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags, |
| @@ -1562,7 +1623,12 @@ peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts, · 281 unmodified lines | ||
| 1562 | 1623 | |
| 1563 | 1624 | ftrace_enable_cpu(); |
| 1564 | 1625 | |
| 1565 | − return event ? ring_buffer_event_data(event) : NULL; | |
| 1626 | + if (event) { | |
| 1627 | + iter->ent_size = ring_buffer_event_length(event); | |
| 1628 | + return ring_buffer_event_data(event); | |
| 1629 | + } | |
| 1630 | + iter->ent_size = 0; | |
| 1631 | + return NULL; | |
| 1566 | 1632 | } |
| 1567 | 1633 | |
| 1568 | 1634 | static struct trace_entry * |
| @@ -278,6 +278,29 @@ struct tracer { · 277 unmodified lines | ||
| 278 | 278 | }; |
| 279 | 279 | |
| 280 | 280 | |
| 281 | +/* Only current can touch trace_recursion */ | |
| 282 | +#define trace_recursion_inc() do { (current)->trace_recursion++; } while (0) | |
| 283 | +#define trace_recursion_dec() do { (current)->trace_recursion--; } while (0) | |
| 284 | + | |
| 285 | +/* Ring buffer has the 10 LSB bits to count */ | |
| 286 | +#define trace_recursion_buffer() ((current)->trace_recursion & 0x3ff) | |
| 287 | + | |
| 288 | +/* for function tracing recursion */ | |
| 289 | +#define TRACE_INTERNAL_BIT (1<<11) | |
| 290 | +#define TRACE_GLOBAL_BIT (1<<12) | |
| 291 | +/* | |
| 292 | + * Abuse of the trace_recursion. | |
| 293 | + * As we need a way to maintain state if we are tracing the function | |
| 294 | + * graph in irq because we want to trace a particular function that | |
| 295 | + * was called in irq context but we have irq tracing off. Since this | |
| 296 | + * can only be modified by current, we can reuse trace_recursion. | |
| 297 | + */ | |
| 298 | +#define TRACE_IRQ_BIT (1<<13) | |
| 299 | + | |
| 300 | +#define trace_recursion_set(bit) do { (current)->trace_recursion |= (bit); } while (0) | |
| 301 | +#define trace_recursion_clear(bit) do { (current)->trace_recursion &= ~(bit); } while (0) | |
| 302 | +#define trace_recursion_test(bit) ((current)->trace_recursion & (bit)) | |
| 303 | + | |
| 281 | 304 | #define TRACE_PIPE_ALL_CPU -1 |
| 282 | 305 | |
| 283 | 306 | int tracer_init(struct tracer *t, struct trace_array *tr); |
| @@ -516,8 +539,18 @@ static inline int ftrace_graph_addr(unsigned long addr) · 232 unmodified lines | ||
| 516 | 539 | return 1; |
| 517 | 540 | |
| 518 | 541 | for (i = 0; i < ftrace_graph_count; i++) { |
| 519 | − if (addr == ftrace_graph_funcs[i]) | |
| 542 | + if (addr == ftrace_graph_funcs[i]) { | |
| 543 | + /* | |
| 544 | + * If no irqs are to be traced, but a set_graph_function | |
| 545 | + * is set, and called by an interrupt handler, we still | |
| 546 | + * want to trace it. | |
| 547 | + */ | |
| 548 | + if (in_irq()) | |
| 549 | + trace_recursion_set(TRACE_IRQ_BIT); | |
| 550 | + else | |
| 551 | + trace_recursion_clear(TRACE_IRQ_BIT); | |
| 520 | 552 | return 1; |
| 553 | + } | |
| 521 | 554 | } |
| 522 | 555 | |
| 523 | 556 | return 0; |
| @@ -795,19 +828,4 @@ extern const char *__stop___trace_bprintk_fmt[]; · 271 unmodified lines | ||
| 795 | 828 | FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print)) |
| 796 | 829 | #include "trace_entries.h" |
| 797 | 830 | |
| 798 | −/* Only current can touch trace_recursion */ | |
| 799 | −#define trace_recursion_inc() do { (current)->trace_recursion++; } while (0) | |
| 800 | −#define trace_recursion_dec() do { (current)->trace_recursion--; } while (0) | |
| 801 | − | |
| 802 | −/* Ring buffer has the 10 LSB bits to count */ | |
| 803 | −#define trace_recursion_buffer() ((current)->trace_recursion & 0x3ff) | |
| 804 | − | |
| 805 | −/* for function tracing recursion */ | |
| 806 | −#define TRACE_INTERNAL_BIT (1<<11) | |
| 807 | −#define TRACE_GLOBAL_BIT (1<<12) | |
| 808 | − | |
| 809 | −#define trace_recursion_set(bit) do { (current)->trace_recursion |= (bit); } while (0) | |
| 810 | −#define trace_recursion_clear(bit) do { (current)->trace_recursion &= ~(bit); } while (0) | |
| 811 | −#define trace_recursion_test(bit) ((current)->trace_recursion & (bit)) | |
| 812 | − | |
| 813 | 831 | #endif /* _LINUX_KERNEL_TRACE_H */ |
| @@ -161,7 +161,8 @@ FTRACE_ENTRY(kernel_stack, stack_entry, · 160 unmodified lines | ||
| 161 | 161 | TRACE_STACK, |
| 162 | 162 | |
| 163 | 163 | F_STRUCT( |
| 164 | − __array( unsigned long, caller, FTRACE_STACK_ENTRIES ) | |
| 164 | + __field( int, size ) | |
| 165 | + __dynamic_array(unsigned long, caller ) | |
| 165 | 166 | ), |
| 166 | 167 | |
| 167 | 168 | F_printk("\t=> (%08lx)\n\t=> (%08lx)\n\t=> (%08lx)\n\t=> (%08lx)\n" |
| @@ -227,7 +227,7 @@ int __trace_graph_entry(struct trace_array *tr, · 226 unmodified lines | ||
| 227 | 227 | |
| 228 | 228 | static inline int ftrace_graph_ignore_irqs(void) |
| 229 | 229 | { |
| 230 | − if (!ftrace_graph_skip_irqs) | |
| 230 | + if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT)) | |
| 231 | 231 | return 0; |
| 232 | 232 | |
| 233 | 233 | return in_irq(); |
| @@ -343,6 +343,14 @@ DEFINE_BASIC_FETCH_FUNCS(deref) · 342 unmodified lines | ||
| 343 | 343 | DEFINE_FETCH_deref(string) |
| 344 | 344 | DEFINE_FETCH_deref(string_size) |
| 345 | 345 | |
| 346 | +static __kprobes void update_deref_fetch_param(struct deref_fetch_param *data) | |
| 347 | +{ | |
| 348 | + if (CHECK_FETCH_FUNCS(deref, data->orig.fn)) | |
| 349 | + update_deref_fetch_param(data->orig.data); | |
| 350 | + else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn)) | |
| 351 | + update_symbol_cache(data->orig.data); | |
| 352 | +} | |
| 353 | + | |
| 346 | 354 | static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data) |
| 347 | 355 | { |
| 348 | 356 | if (CHECK_FETCH_FUNCS(deref, data->orig.fn)) |
| @@ -376,6 +384,19 @@ DEFINE_BASIC_FETCH_FUNCS(bitfield) · 27 unmodified lines | ||
| 376 | 384 | #define fetch_bitfield_string NULL |
| 377 | 385 | #define fetch_bitfield_string_size NULL |
| 378 | 386 | |
| 387 | +static __kprobes void | |
| 388 | +update_bitfield_fetch_param(struct bitfield_fetch_param *data) | |
| 389 | +{ | |
| 390 | + /* | |
| 391 | + * Don't check the bitfield itself, because this must be the | |
| 392 | + * last fetch function. | |
| 393 | + */ | |
| 394 | + if (CHECK_FETCH_FUNCS(deref, data->orig.fn)) | |
| 395 | + update_deref_fetch_param(data->orig.data); | |
| 396 | + else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn)) | |
| 397 | + update_symbol_cache(data->orig.data); | |
| 398 | +} | |
| 399 | + | |
| 379 | 400 | static __kprobes void |
| 380 | 401 | free_bitfield_fetch_param(struct bitfield_fetch_param *data) |
| 381 | 402 | { |
| @@ -389,6 +410,7 @@ free_bitfield_fetch_param(struct bitfield_fetch_param *data) · 7 unmodified lines | ||
| 389 | 410 | free_symbol_cache(data->orig.data); |
| 390 | 411 | kfree(data); |
| 391 | 412 | } |
| 413 | + | |
| 392 | 414 | /* Default (unsigned long) fetch type */ |
| 393 | 415 | #define __DEFAULT_FETCH_TYPE(t) u##t |
| 394 | 416 | #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t) |
| @@ -536,6 +558,7 @@ struct probe_arg { · 141 unmodified lines | ||
| 536 | 558 | /* Flags for trace_probe */ |
| 537 | 559 | #define TP_FLAG_TRACE 1 |
| 538 | 560 | #define TP_FLAG_PROFILE 2 |
| 561 | +#define TP_FLAG_REGISTERED 4 | |
| 539 | 562 | |
| 540 | 563 | struct trace_probe { |
| 541 | 564 | struct list_head list; |
| @@ -555,16 +578,49 @@ struct trace_probe { · 13 unmodified lines | ||
| 555 | 578 | (sizeof(struct probe_arg) * (n))) |
| 556 | 579 | |
| 557 | 580 | |
| 558 | −static __kprobes int probe_is_return(struct trace_probe *tp) | |
| 581 | +static __kprobes int trace_probe_is_return(struct trace_probe *tp) | |
| 559 | 582 | { |
| 560 | 583 | return tp->rp.handler != NULL; |
| 561 | 584 | } |
| 562 | 585 | |
| 563 | −static __kprobes const char *probe_symbol(struct trace_probe *tp) | |
| 586 | +static __kprobes const char *trace_probe_symbol(struct trace_probe *tp) | |
| 564 | 587 | { |
| 565 | 588 | return tp->symbol ? tp->symbol : "unknown"; |
| 566 | 589 | } |
| 567 | 590 | |
| 591 | +static __kprobes unsigned long trace_probe_offset(struct trace_probe *tp) | |
| 592 | +{ | |
| 593 | + return tp->rp.kp.offset; | |
| 594 | +} | |
| 595 | + | |
| 596 | +static __kprobes bool trace_probe_is_enabled(struct trace_probe *tp) | |
| 597 | +{ | |
| 598 | + return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE)); | |
| 599 | +} | |
| 600 | + | |
| 601 | +static __kprobes bool trace_probe_is_registered(struct trace_probe *tp) | |
| 602 | +{ | |
| 603 | + return !!(tp->flags & TP_FLAG_REGISTERED); | |
| 604 | +} | |
| 605 | + | |
| 606 | +static __kprobes bool trace_probe_has_gone(struct trace_probe *tp) | |
| 607 | +{ | |
| 608 | + return !!(kprobe_gone(&tp->rp.kp)); | |
| 609 | +} | |
| 610 | + | |
| 611 | +static __kprobes bool trace_probe_within_module(struct trace_probe *tp, | |
| 612 | + struct module *mod) | |
| 613 | +{ | |
| 614 | + int len = strlen(mod->name); | |
| 615 | + const char *name = trace_probe_symbol(tp); | |
| 616 | + return strncmp(mod->name, name, len) == 0 && name[len] == ':'; | |
| 617 | +} | |
| 618 | + | |
| 619 | +static __kprobes bool trace_probe_is_on_module(struct trace_probe *tp) | |
| 620 | +{ | |
| 621 | + return !!strchr(trace_probe_symbol(tp), ':'); | |
| 622 | +} | |
| 623 | + | |
| 568 | 624 | static int register_probe_event(struct trace_probe *tp); |
| 569 | 625 | static void unregister_probe_event(struct trace_probe *tp); |
| 570 | 626 | |
| @@ -646,6 +702,16 @@ error: · 75 unmodified lines | ||
| 646 | 702 | return ERR_PTR(ret); |
| 647 | 703 | } |
| 648 | 704 | |
| 705 | +static void update_probe_arg(struct probe_arg *arg) | |
| 706 | +{ | |
| 707 | + if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn)) | |
| 708 | + update_bitfield_fetch_param(arg->fetch.data); | |
| 709 | + else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn)) | |
| 710 | + update_deref_fetch_param(arg->fetch.data); | |
| 711 | + else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn)) | |
| 712 | + update_symbol_cache(arg->fetch.data); | |
| 713 | +} | |
| 714 | + | |
| 649 | 715 | static void free_probe_arg(struct probe_arg *arg) |
| 650 | 716 | { |
| 651 | 717 | if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn)) |
| @@ -671,7 +737,7 @@ static void free_trace_probe(struct trace_probe *tp) · 19 unmodified lines | ||
| 671 | 737 | kfree(tp); |
| 672 | 738 | } |
| 673 | 739 | |
| 674 | −static struct trace_probe *find_probe_event(const char *event, | |
| 740 | +static struct trace_probe *find_trace_probe(const char *event, | |
| 675 | 741 | const char *group) |
| 676 | 742 | { |
| 677 | 743 | struct trace_probe *tp; |
| @@ -683,13 +749,96 @@ static struct trace_probe *find_probe_event(const char *event, · 5 unmodified lines | ||
| 683 | 749 | return NULL; |
| 684 | 750 | } |
| 685 | 751 | |
| 752 | +/* Enable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */ | |
| 753 | +static int enable_trace_probe(struct trace_probe *tp, int flag) | |
| 754 | +{ | |
| 755 | + int ret = 0; | |
| 756 | + | |
| 757 | + tp->flags |= flag; | |
| 758 | + if (trace_probe_is_enabled(tp) && trace_probe_is_registered(tp) && | |
| 759 | + !trace_probe_has_gone(tp)) { | |
| 760 | + if (trace_probe_is_return(tp)) | |
| 761 | + ret = enable_kretprobe(&tp->rp); | |
| 762 | + else | |
| 763 | + ret = enable_kprobe(&tp->rp.kp); | |
| 764 | + } | |
| 765 | + | |
| 766 | + return ret; | |
| 767 | +} | |
| 768 | + | |
| 769 | +/* Disable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */ | |
| 770 | +static void disable_trace_probe(struct trace_probe *tp, int flag) | |
| 771 | +{ | |
| 772 | + tp->flags &= ~flag; | |
| 773 | + if (!trace_probe_is_enabled(tp) && trace_probe_is_registered(tp)) { | |
| 774 | + if (trace_probe_is_return(tp)) | |
| 775 | + disable_kretprobe(&tp->rp); | |
| 776 | + else | |
| 777 | + disable_kprobe(&tp->rp.kp); | |
| 778 | + } | |
| 779 | +} | |
| 780 | + | |
| 781 | +/* Internal register function - just handle k*probes and flags */ | |
| 782 | +static int __register_trace_probe(struct trace_probe *tp) | |
| 783 | +{ | |
| 784 | + int i, ret; | |
| 785 | + | |
| 786 | + if (trace_probe_is_registered(tp)) | |
| 787 | + return -EINVAL; | |
| 788 | + | |
| 789 | + for (i = 0; i < tp->nr_args; i++) | |
| 790 | + update_probe_arg(&tp->args[i]); | |
| 791 | + | |
| 792 | + /* Set/clear disabled flag according to tp->flag */ | |
| 793 | + if (trace_probe_is_enabled(tp)) | |
| 794 | + tp->rp.kp.flags &= ~KPROBE_FLAG_DISABLED; | |
| 795 | + else | |
| 796 | + tp->rp.kp.flags |= KPROBE_FLAG_DISABLED; | |
| 797 | + | |
| 798 | + if (trace_probe_is_return(tp)) | |
| 799 | + ret = register_kretprobe(&tp->rp); | |
| 800 | + else | |
| 801 | + ret = register_kprobe(&tp->rp.kp); | |
| 802 | + | |
| 803 | + if (ret == 0) | |
| 804 | + tp->flags |= TP_FLAG_REGISTERED; | |
| 805 | + else { | |
| 806 | + pr_warning("Could not insert probe at %s+%lu: %d\n", | |
| 807 | + trace_probe_symbol(tp), trace_probe_offset(tp), ret); | |
| 808 | + if (ret == -ENOENT && trace_probe_is_on_module(tp)) { | |
| 809 | + pr_warning("This probe might be able to register after" | |
| 810 | + "target module is loaded. Continue.\n"); | |
| 811 | + ret = 0; | |
| 812 | + } else if (ret == -EILSEQ) { | |
| 813 | + pr_warning("Probing address(0x%p) is not an " | |
| 814 | + "instruction boundary.\n", | |
| 815 | + tp->rp.kp.addr); | |
| 816 | + ret = -EINVAL; | |
| 817 | + } | |
| 818 | + } | |
| 819 | + | |
| 820 | + return ret; | |
| 821 | +} | |
| 822 | + | |
| 823 | +/* Internal unregister function - just handle k*probes and flags */ | |
| 824 | +static void __unregister_trace_probe(struct trace_probe *tp) | |
| 825 | +{ | |
| 826 | + if (trace_probe_is_registered(tp)) { | |
| 827 | + if (trace_probe_is_return(tp)) | |
| 828 | + unregister_kretprobe(&tp->rp); | |
| 829 | + else | |
| 830 | + unregister_kprobe(&tp->rp.kp); | |
| 831 | + tp->flags &= ~TP_FLAG_REGISTERED; | |
| 832 | + /* Cleanup kprobe for reuse */ | |
| 833 | + if (tp->rp.kp.symbol_name) | |
| 834 | + tp->rp.kp.addr = NULL; | |
| 835 | + } | |
| 836 | +} | |
| 837 | + | |
| 686 | 838 | /* Unregister a trace_probe and probe_event: call with locking probe_lock */ |
| 687 | 839 | static void unregister_trace_probe(struct trace_probe *tp) |
| 688 | 840 | { |
| 689 | − if (probe_is_return(tp)) | |
| 690 | − unregister_kretprobe(&tp->rp); | |
| 691 | − else | |
| 692 | − unregister_kprobe(&tp->rp.kp); | |
| 841 | + __unregister_trace_probe(tp); | |
| 693 | 842 | list_del(&tp->list); |
| 694 | 843 | unregister_probe_event(tp); |
| 695 | 844 | } |
| @@ -702,41 +851,65 @@ static int register_trace_probe(struct trace_probe *tp) · 6 unmodified lines | ||
| 702 | 851 | |
| 703 | 852 | mutex_lock(&probe_lock); |
| 704 | 853 | |
| 705 | − /* register as an event */ | |
| 706 | − old_tp = find_probe_event(tp->call.name, tp->call.class->system); | |
| 854 | + /* Delete old (same name) event if exist */ | |
| 855 | + old_tp = find_trace_probe(tp->call.name, tp->call.class->system); | |
| 707 | 856 | if (old_tp) { |
| 708 | − /* delete old event */ | |
| 709 | 857 | unregister_trace_probe(old_tp); |
| 710 | 858 | free_trace_probe(old_tp); |
| 711 | 859 | } |
| 860 | + | |
| 861 | + /* Register new event */ | |
| 712 | 862 | ret = register_probe_event(tp); |
| 713 | 863 | if (ret) { |
| 714 | 864 | pr_warning("Failed to register probe event(%d)\n", ret); |
| 715 | 865 | goto end; |
| 716 | 866 | } |
| 717 | 867 | |
| 718 | − tp->rp.kp.flags |= KPROBE_FLAG_DISABLED; | |
| 719 | − if (probe_is_return(tp)) | |
| 720 | − ret = register_kretprobe(&tp->rp); | |
| 721 | − else | |
| 722 | − ret = register_kprobe(&tp->rp.kp); | |
| 723 | − | |
| 724 | − if (ret) { | |
| 725 | − pr_warning("Could not insert probe(%d)\n", ret); | |
| 726 | − if (ret == -EILSEQ) { | |
| 727 | − pr_warning("Probing address(0x%p) is not an " | |
| 728 | − "instruction boundary.\n", | |
| 729 | − tp->rp.kp.addr); | |
| 730 | − ret = -EINVAL; | |
| 731 | − } | |
| 868 | + /* Register k*probe */ | |
| 869 | + ret = __register_trace_probe(tp); | |
| 870 | + if (ret < 0) | |
| 732 | 871 | unregister_probe_event(tp); |
| 733 | − } else | |
| 872 | + else | |
| 734 | 873 | list_add_tail(&tp->list, &probe_list); |
| 874 | + | |
| 735 | 875 | end: |
| 736 | 876 | mutex_unlock(&probe_lock); |
| 737 | 877 | return ret; |
| 738 | 878 | } |
| 739 | 879 | |
| 880 | +/* Module notifier call back, checking event on the module */ | |
| 881 | +static int trace_probe_module_callback(struct notifier_block *nb, | |
| 882 | + unsigned long val, void *data) | |
| 883 | +{ | |
| 884 | + struct module *mod = data; | |
| 885 | + struct trace_probe *tp; | |
| 886 | + int ret; | |
| 887 | + | |
| 888 | + if (val != MODULE_STATE_COMING) | |
| 889 | + return NOTIFY_DONE; | |
| 890 | + | |
| 891 | + /* Update probes on coming module */ | |
| 892 | + mutex_lock(&probe_lock); | |
| 893 | + list_for_each_entry(tp, &probe_list, list) { | |
| 894 | + if (trace_probe_within_module(tp, mod)) { | |
| 895 | + __unregister_trace_probe(tp); | |
| 896 | + ret = __register_trace_probe(tp); | |
| 897 | + if (ret) | |
| 898 | + pr_warning("Failed to re-register probe %s on" | |
| 899 | + "%s: %d\n", | |
| 900 | + tp->call.name, mod->name, ret); | |
| 901 | + } | |
| 902 | + } | |
| 903 | + mutex_unlock(&probe_lock); | |
| 904 | + | |
| 905 | + return NOTIFY_DONE; | |
| 906 | +} | |
| 907 | + | |
| 908 | +static struct notifier_block trace_probe_module_nb = { | |
| 909 | + .notifier_call = trace_probe_module_callback, | |
| 910 | + .priority = 1 /* Invoked after kprobe module callback */ | |
| 911 | +}; | |
| 912 | + | |
| 740 | 913 | /* Split symbol and offset. */ |
| 741 | 914 | static int split_symbol_offset(char *symbol, unsigned long *offset) |
| 742 | 915 | { |
| @@ -962,8 +1135,8 @@ static int create_trace_probe(int argc, char **argv) · 219 unmodified lines | ||
| 962 | 1135 | { |
| 963 | 1136 | /* |
| 964 | 1137 | * Argument syntax: |
| 965 | − * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS] | |
| 966 | − * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS] | |
| 1138 | + * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS] | |
| 1139 | + * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS] | |
| 967 | 1140 | * Fetch args: |
| 968 | 1141 | * $retval : fetch return value |
| 969 | 1142 | * $stack : fetch stack address |
| @@ -1025,7 +1198,7 @@ static int create_trace_probe(int argc, char **argv) · 55 unmodified lines | ||
| 1025 | 1198 | return -EINVAL; |
| 1026 | 1199 | } |
| 1027 | 1200 | mutex_lock(&probe_lock); |
| 1028 | − tp = find_probe_event(event, group); | |
| 1201 | + tp = find_trace_probe(event, group); | |
| 1029 | 1202 | if (!tp) { |
| 1030 | 1203 | mutex_unlock(&probe_lock); |
| 1031 | 1204 | pr_info("Event %s/%s doesn't exist.\n", group, event); |
| @@ -1144,7 +1317,7 @@ error: · 112 unmodified lines | ||
| 1144 | 1317 | return ret; |
| 1145 | 1318 | } |
| 1146 | 1319 | |
| 1147 | −static void cleanup_all_probes(void) | |
| 1320 | +static void release_all_trace_probes(void) | |
| 1148 | 1321 | { |
| 1149 | 1322 | struct trace_probe *tp; |
| 1150 | 1323 | |
| @@ -1158,7 +1331,6 @@ static void cleanup_all_probes(void) · 7 unmodified lines | ||
| 1158 | 1331 | mutex_unlock(&probe_lock); |
| 1159 | 1332 | } |
| 1160 | 1333 | |
| 1161 | − | |
| 1162 | 1334 | /* Probes listing interfaces */ |
| 1163 | 1335 | static void *probes_seq_start(struct seq_file *m, loff_t *pos) |
| 1164 | 1336 | { |
| @@ -1181,15 +1353,16 @@ static int probes_seq_show(struct seq_file *m, void *v) · 16 unmodified lines | ||
| 1181 | 1353 | struct trace_probe *tp = v; |
| 1182 | 1354 | int i; |
| 1183 | 1355 | |
| 1184 | − seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p'); | |
| 1356 | + seq_printf(m, "%c", trace_probe_is_return(tp) ? 'r' : 'p'); | |
| 1185 | 1357 | seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name); |
| 1186 | 1358 | |
| 1187 | 1359 | if (!tp->symbol) |
| 1188 | 1360 | seq_printf(m, " 0x%p", tp->rp.kp.addr); |
| 1189 | 1361 | else if (tp->rp.kp.offset) |
| 1190 | − seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset); | |
| 1362 | + seq_printf(m, " %s+%u", trace_probe_symbol(tp), | |
| 1363 | + tp->rp.kp.offset); | |
| 1191 | 1364 | else |
| 1192 | − seq_printf(m, " %s", probe_symbol(tp)); | |
| 1365 | + seq_printf(m, " %s", trace_probe_symbol(tp)); | |
| 1193 | 1366 | |
| 1194 | 1367 | for (i = 0; i < tp->nr_args; i++) |
| 1195 | 1368 | seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm); |
| @@ -1209,7 +1382,7 @@ static int probes_open(struct inode *inode, struct file *file) · 13 unmodified lines | ||
| 1209 | 1382 | { |
| 1210 | 1383 | if ((file->f_mode & FMODE_WRITE) && |
| 1211 | 1384 | (file->f_flags & O_TRUNC)) |
| 1212 | − cleanup_all_probes(); | |
| 1385 | + release_all_trace_probes(); | |
| 1213 | 1386 | |
| 1214 | 1387 | return seq_open(file, &probes_seq_op); |
| 1215 | 1388 | } |
| @@ -1513,30 +1686,6 @@ partial: · 297 unmodified lines | ||
| 1513 | 1686 | return TRACE_TYPE_PARTIAL_LINE; |
| 1514 | 1687 | } |
| 1515 | 1688 | |
| 1516 | −static int probe_event_enable(struct ftrace_event_call *call) | |
| 1517 | −{ | |
| 1518 | − struct trace_probe *tp = (struct trace_probe *)call->data; | |
| 1519 | − | |
| 1520 | − tp->flags |= TP_FLAG_TRACE; | |
| 1521 | − if (probe_is_return(tp)) | |
| 1522 | − return enable_kretprobe(&tp->rp); | |
| 1523 | − else | |
| 1524 | − return enable_kprobe(&tp->rp.kp); | |
| 1525 | −} | |
| 1526 | − | |
| 1527 | −static void probe_event_disable(struct ftrace_event_call *call) | |
| 1528 | −{ | |
| 1529 | − struct trace_probe *tp = (struct trace_probe *)call->data; | |
| 1530 | − | |
| 1531 | − tp->flags &= ~TP_FLAG_TRACE; | |
| 1532 | − if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) { | |
| 1533 | − if (probe_is_return(tp)) | |
| 1534 | − disable_kretprobe(&tp->rp); | |
| 1535 | − else | |
| 1536 | − disable_kprobe(&tp->rp.kp); | |
| 1537 | − } | |
| 1538 | −} | |
| 1539 | − | |
| 1540 | 1689 | #undef DEFINE_FIELD |
| 1541 | 1690 | #define DEFINE_FIELD(type, item, name, is_signed) \ |
| 1542 | 1691 | do { \ |
| @@ -1598,7 +1747,7 @@ static int __set_print_fmt(struct trace_probe *tp, char *buf, int len) · 55 unmodified lines | ||
| 1598 | 1747 | |
| 1599 | 1748 | const char *fmt, *arg; |
| 1600 | 1749 | |
| 1601 | − if (!probe_is_return(tp)) { | |
| 1750 | + if (!trace_probe_is_return(tp)) { | |
| 1602 | 1751 | fmt = "(%lx)"; |
| 1603 | 1752 | arg = "REC->" FIELD_STRING_IP; |
| 1604 | 1753 | } else { |
| @@ -1715,49 +1864,25 @@ static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri, · 110 unmodified lines | ||
| 1715 | 1864 | head = this_cpu_ptr(call->perf_events); |
| 1716 | 1865 | perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head); |
| 1717 | 1866 | } |
| 1718 | − | |
| 1719 | −static int probe_perf_enable(struct ftrace_event_call *call) | |
| 1720 | −{ | |
| 1721 | − struct trace_probe *tp = (struct trace_probe *)call->data; | |
| 1722 | − | |
| 1723 | − tp->flags |= TP_FLAG_PROFILE; | |
| 1724 | − | |
| 1725 | − if (probe_is_return(tp)) | |
| 1726 | − return enable_kretprobe(&tp->rp); | |
| 1727 | − else | |
| 1728 | − return enable_kprobe(&tp->rp.kp); | |
| 1729 | −} | |
| 1730 | − | |
| 1731 | −static void probe_perf_disable(struct ftrace_event_call *call) | |
| 1732 | −{ | |
| 1733 | − struct trace_probe *tp = (struct trace_probe *)call->data; | |
| 1734 | − | |
| 1735 | − tp->flags &= ~TP_FLAG_PROFILE; | |
| 1736 | − | |
| 1737 | − if (!(tp->flags & TP_FLAG_TRACE)) { | |
| 1738 | − if (probe_is_return(tp)) | |
| 1739 | − disable_kretprobe(&tp->rp); | |
| 1740 | − else | |
| 1741 | − disable_kprobe(&tp->rp.kp); | |
| 1742 | − } | |
| 1743 | −} | |
| 1744 | 1867 | #endif /* CONFIG_PERF_EVENTS */ |
| 1745 | 1868 | |
| 1746 | 1869 | static __kprobes |
| 1747 | 1870 | int kprobe_register(struct ftrace_event_call *event, enum trace_reg type) |
| 1748 | 1871 | { |
| 1872 | + struct trace_probe *tp = (struct trace_probe *)event->data; | |
| 1873 | + | |
| 1749 | 1874 | switch (type) { |
| 1750 | 1875 | case TRACE_REG_REGISTER: |
| 1751 | − return probe_event_enable(event); | |
| 1876 | + return enable_trace_probe(tp, TP_FLAG_TRACE); | |
| 1752 | 1877 | case TRACE_REG_UNREGISTER: |
| 1753 | − probe_event_disable(event); | |
| 1878 | + disable_trace_probe(tp, TP_FLAG_TRACE); | |
| 1754 | 1879 | return 0; |
| 1755 | 1880 | |
| 1756 | 1881 | #ifdef CONFIG_PERF_EVENTS |
| 1757 | 1882 | case TRACE_REG_PERF_REGISTER: |
| 1758 | − return probe_perf_enable(event); | |
| 1883 | + return enable_trace_probe(tp, TP_FLAG_PROFILE); | |
| 1759 | 1884 | case TRACE_REG_PERF_UNREGISTER: |
| 1760 | − probe_perf_disable(event); | |
| 1885 | + disable_trace_probe(tp, TP_FLAG_PROFILE); | |
| 1761 | 1886 | return 0; |
| 1762 | 1887 | #endif |
| 1763 | 1888 | } |
| @@ -1807,7 +1932,7 @@ static int register_probe_event(struct trace_probe *tp) · 43 unmodified lines | ||
| 1807 | 1932 | |
| 1808 | 1933 | /* Initialize ftrace_event_call */ |
| 1809 | 1934 | INIT_LIST_HEAD(&call->class->fields); |
| 1810 | − if (probe_is_return(tp)) { | |
| 1935 | + if (trace_probe_is_return(tp)) { | |
| 1811 | 1936 | call->event.funcs = &kretprobe_funcs; |
| 1812 | 1937 | call->class->define_fields = kretprobe_event_define_fields; |
| 1813 | 1938 | } else { |
| @@ -1846,6 +1971,9 @@ static __init int init_kprobe_trace(void) · 32 unmodified lines | ||
| 1846 | 1971 | struct dentry *d_tracer; |
| 1847 | 1972 | struct dentry *entry; |
| 1848 | 1973 | |
| 1974 | + if (register_module_notifier(&trace_probe_module_nb)) | |
| 1975 | + return -EINVAL; | |
| 1976 | + | |
| 1849 | 1977 | d_tracer = tracing_init_dentry(); |
| 1850 | 1978 | if (!d_tracer) |
| 1851 | 1979 | return 0; |
| @@ -1899,12 +2027,12 @@ static __init int kprobe_trace_self_tests_init(void) · 47 unmodified lines | ||
| 1899 | 2027 | warn++; |
| 1900 | 2028 | } else { |
| 1901 | 2029 | /* Enable trace point */ |
| 1902 | − tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM); | |
| 2030 | + tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM); | |
| 1903 | 2031 | if (WARN_ON_ONCE(tp == NULL)) { |
| 1904 | 2032 | pr_warning("error on getting new probe.\n"); |
| 1905 | 2033 | warn++; |
| 1906 | 2034 | } else |
| 1907 | − probe_event_enable(&tp->call); | |
| 2035 | + enable_trace_probe(tp, TP_FLAG_TRACE); | |
| 1908 | 2036 | } |
| 1909 | 2037 | |
| 1910 | 2038 | ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target " |
| @@ -1914,12 +2042,12 @@ static __init int kprobe_trace_self_tests_init(void) · 3 unmodified lines | ||
| 1914 | 2042 | warn++; |
| 1915 | 2043 | } else { |
| 1916 | 2044 | /* Enable trace point */ |
| 1917 | − tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM); | |
| 2045 | + tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM); | |
| 1918 | 2046 | if (WARN_ON_ONCE(tp == NULL)) { |
| 1919 | 2047 | pr_warning("error on getting new probe.\n"); |
| 1920 | 2048 | warn++; |
| 1921 | 2049 | } else |
| 1922 | − probe_event_enable(&tp->call); | |
| 2050 | + enable_trace_probe(tp, TP_FLAG_TRACE); | |
| 1923 | 2051 | } |
| 1924 | 2052 | |
| 1925 | 2053 | if (warn) |
| @@ -1940,7 +2068,7 @@ static __init int kprobe_trace_self_tests_init(void) · 14 unmodified lines | ||
| 1940 | 2068 | } |
| 1941 | 2069 | |
| 1942 | 2070 | end: |
| 1943 | − cleanup_all_probes(); | |
| 2071 | + release_all_trace_probes(); | |
| 1944 | 2072 | if (warn) |
| 1945 | 2073 | pr_cont("NG: Some tests are failed. Please check them.\n"); |
| 1946 | 2074 | else |
| @@ -1107,19 +1107,20 @@ static enum print_line_t trace_stack_print(struct trace_iterator *iter, · 1106 unmodified lines | ||
| 1107 | 1107 | { |
| 1108 | 1108 | struct stack_entry *field; |
| 1109 | 1109 | struct trace_seq *s = &iter->seq; |
| 1110 | − int i; | |
| 1110 | + unsigned long *p; | |
| 1111 | + unsigned long *end; | |
| 1111 | 1112 | |
| 1112 | 1113 | trace_assign_type(field, iter->ent); |
| 1114 | + end = (unsigned long *)((long)iter->ent + iter->ent_size); | |
| 1113 | 1115 | |
| 1114 | 1116 | if (!trace_seq_puts(s, "<stack trace>\n")) |
| 1115 | 1117 | goto partial; |
| 1116 | − for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { | |
| 1117 | − if (!field->caller[i] || (field->caller[i] == ULONG_MAX)) | |
| 1118 | − break; | |
| 1118 | + | |
| 1119 | + for (p = field->caller; p && *p != ULONG_MAX && p < end; p++) { | |
| 1119 | 1120 | if (!trace_seq_puts(s, " => ")) |
| 1120 | 1121 | goto partial; |
| 1121 | 1122 | |
| 1122 | − if (!seq_print_ip_sym(s, field->caller[i], flags)) | |
| 1123 | + if (!seq_print_ip_sym(s, *p, flags)) | |
| 1123 | 1124 | goto partial; |
| 1124 | 1125 | if (!trace_seq_puts(s, "\n")) |
| 1125 | 1126 | goto partial; |
| @@ -200,7 +200,6 @@ static int is_softlockup(unsigned long touch_ts) · 199 unmodified lines | ||
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
| 203 | −void __weak hw_nmi_watchdog_set_attr(struct perf_event_attr *wd_attr) { } | |
| 204 | 203 | |
| 205 | 204 | static struct perf_event_attr wd_hw_attr = { |
| 206 | 205 | .type = PERF_TYPE_HARDWARE, |
| @@ -372,7 +371,6 @@ static int watchdog_nmi_enable(int cpu) · 165 unmodified lines | ||
| 372 | 371 | |
| 373 | 372 | wd_attr = &wd_hw_attr; |
| 374 | 373 | wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh); |
| 375 | − hw_nmi_watchdog_set_attr(wd_attr); | |
| 376 | 374 | |
| 377 | 375 | /* Try to register using hardware perf events */ |
| 378 | 376 | event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL); |
| @@ -34,9 +34,11 @@ OPTIONS · 33 unmodified lines | ||
| 34 | 34 | Specify vmlinux path which has debuginfo (Dwarf binary). |
| 35 | 35 | |
| 36 | 36 | -m:: |
| 37 | −--module=MODNAME:: | |
| 37 | +--module=MODNAME|PATH:: | |
| 38 | 38 | Specify module name in which perf-probe searches probe points |
| 39 | − or lines. | |
| 39 | + or lines. If a path of module file is passed, perf-probe | |
| 40 | + treat it as an offline module (this means you can add a probe on | |
| 41 | + a module which has not been loaded yet). | |
| 40 | 42 | |
| 41 | 43 | -s:: |
| 42 | 44 | --source=PATH:: |
| @@ -279,6 +279,7 @@ LIB_H += util/thread.h · 278 unmodified lines | ||
| 279 | 279 | LIB_H += util/thread_map.h |
| 280 | 280 | LIB_H += util/trace-event.h |
| 281 | 281 | LIB_H += util/probe-finder.h |
| 282 | +LIB_H += util/dwarf-aux.h | |
| 282 | 283 | LIB_H += util/probe-event.h |
| 283 | 284 | LIB_H += util/pstack.h |
| 284 | 285 | LIB_H += util/cpumap.h |
| @@ -435,6 +436,7 @@ else · 150 unmodified lines | ||
| 435 | 436 | BASIC_CFLAGS += -DDWARF_SUPPORT |
| 436 | 437 | EXTLIBS += -lelf -ldw |
| 437 | 438 | LIB_OBJS += $(OUTPUT)util/probe-finder.o |
| 439 | + LIB_OBJS += $(OUTPUT)util/dwarf-aux.o | |
| 438 | 440 | endif # PERF_HAVE_DWARF_REGS |
| 439 | 441 | endif # NO_DWARF |
| 440 | 442 | |
| @@ -242,7 +242,8 @@ static const struct option options[] = { · 241 unmodified lines | ||
| 242 | 242 | OPT_STRING('s', "source", &symbol_conf.source_prefix, |
| 243 | 243 | "directory", "path to kernel source"), |
| 244 | 244 | OPT_STRING('m', "module", ¶ms.target_module, |
| 245 | − "modname", "target module name"), | |
| 245 | + "modname|path", | |
| 246 | + "target module name (for online) or path (for offline)"), | |
| 246 | 247 | #endif |
| 247 | 248 | OPT__DRY_RUN(&probe_event_dry_run), |
| 248 | 249 | OPT_INTEGER('\0', "max-probes", ¶ms.max_probe_points, |
| @@ -0,0 +1,663 @@ | ||
| 1 | +/* | |
| 2 | + * dwarf-aux.c : libdw auxiliary interfaces | |
| 3 | + * | |
| 4 | + * This program is free software; you can redistribute it and/or modify | |
| 5 | + * it under the terms of the GNU General Public License as published by | |
| 6 | + * the Free Software Foundation; either version 2 of the License, or | |
| 7 | + * (at your option) any later version. | |
| 8 | + * | |
| 9 | + * This program is distributed in the hope that it will be useful, | |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + * GNU General Public License for more details. | |
| 13 | + * | |
| 14 | + * You should have received a copy of the GNU General Public License | |
| 15 | + * along with this program; if not, write to the Free Software | |
| 16 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 17 | + * | |
| 18 | + */ | |
| 19 | + | |
| 20 | +#include <stdbool.h> | |
| 21 | +#include "util.h" | |
| 22 | +#include "debug.h" | |
| 23 | +#include "dwarf-aux.h" | |
| 24 | + | |
| 25 | +/** | |
| 26 | + * cu_find_realpath - Find the realpath of the target file | |
| 27 | + * @cu_die: A DIE(dwarf information entry) of CU(compilation Unit) | |
| 28 | + * @fname: The tail filename of the target file | |
| 29 | + * | |
| 30 | + * Find the real(long) path of @fname in @cu_die. | |
| 31 | + */ | |
| 32 | +const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname) | |
| 33 | +{ | |
| 34 | + Dwarf_Files *files; | |
| 35 | + size_t nfiles, i; | |
| 36 | + const char *src = NULL; | |
| 37 | + int ret; | |
| 38 | + | |
| 39 | + if (!fname) | |
| 40 | + return NULL; | |
| 41 | + | |
| 42 | + ret = dwarf_getsrcfiles(cu_die, &files, &nfiles); | |
| 43 | + if (ret != 0) | |
| 44 | + return NULL; | |
| 45 | + | |
| 46 | + for (i = 0; i < nfiles; i++) { | |
| 47 | + src = dwarf_filesrc(files, i, NULL, NULL); | |
| 48 | + if (strtailcmp(src, fname) == 0) | |
| 49 | + break; | |
| 50 | + } | |
| 51 | + if (i == nfiles) | |
| 52 | + return NULL; | |
| 53 | + return src; | |
| 54 | +} | |
| 55 | + | |
| 56 | +/** | |
| 57 | + * cu_get_comp_dir - Get the path of compilation directory | |
| 58 | + * @cu_die: a CU DIE | |
| 59 | + * | |
| 60 | + * Get the path of compilation directory of given @cu_die. | |
| 61 | + * Since this depends on DW_AT_comp_dir, older gcc will not | |
| 62 | + * embedded it. In that case, this returns NULL. | |
| 63 | + */ | |
| 64 | +const char *cu_get_comp_dir(Dwarf_Die *cu_die) | |
| 65 | +{ | |
| 66 | + Dwarf_Attribute attr; | |
| 67 | + if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL) | |
| 68 | + return NULL; | |
| 69 | + return dwarf_formstring(&attr); | |
| 70 | +} | |
| 71 | + | |
| 72 | +/** | |
| 73 | + * cu_find_lineinfo - Get a line number and file name for given address | |
| 74 | + * @cu_die: a CU DIE | |
| 75 | + * @addr: An address | |
| 76 | + * @fname: a pointer which returns the file name string | |
| 77 | + * @lineno: a pointer which returns the line number | |
| 78 | + * | |
| 79 | + * Find a line number and file name for @addr in @cu_die. | |
| 80 | + */ | |
| 81 | +int cu_find_lineinfo(Dwarf_Die *cu_die, unsigned long addr, | |
| 82 | + const char **fname, int *lineno) | |
| 83 | +{ | |
| 84 | + Dwarf_Line *line; | |
| 85 | + Dwarf_Addr laddr; | |
| 86 | + | |
| 87 | + line = dwarf_getsrc_die(cu_die, (Dwarf_Addr)addr); | |
| 88 | + if (line && dwarf_lineaddr(line, &laddr) == 0 && | |
| 89 | + addr == (unsigned long)laddr && dwarf_lineno(line, lineno) == 0) { | |
| 90 | + *fname = dwarf_linesrc(line, NULL, NULL); | |
| 91 | + if (!*fname) | |
| 92 | + /* line number is useless without filename */ | |
| 93 | + *lineno = 0; | |
| 94 | + } | |
| 95 | + | |
| 96 | + return *lineno ?: -ENOENT; | |
| 97 | +} | |
| 98 | + | |
| 99 | +/** | |
| 100 | + * die_compare_name - Compare diename and tname | |
| 101 | + * @dw_die: a DIE | |
| 102 | + * @tname: a string of target name | |
| 103 | + * | |
| 104 | + * Compare the name of @dw_die and @tname. Return false if @dw_die has no name. | |
| 105 | + */ | |
| 106 | +bool die_compare_name(Dwarf_Die *dw_die, const char *tname) | |
| 107 | +{ | |
| 108 | + const char *name; | |
| 109 | + name = dwarf_diename(dw_die); | |
| 110 | + return name ? (strcmp(tname, name) == 0) : false; | |
| 111 | +} | |
| 112 | + | |
| 113 | +/** | |
| 114 | + * die_get_call_lineno - Get callsite line number of inline-function instance | |
| 115 | + * @in_die: a DIE of an inlined function instance | |
| 116 | + * | |
| 117 | + * Get call-site line number of @in_die. This means from where the inline | |
| 118 | + * function is called. | |
| 119 | + */ | |
| 120 | +int die_get_call_lineno(Dwarf_Die *in_die) | |
| 121 | +{ | |
| 122 | + Dwarf_Attribute attr; | |
| 123 | + Dwarf_Word ret; | |
| 124 | + | |
| 125 | + if (!dwarf_attr(in_die, DW_AT_call_line, &attr)) | |
| 126 | + return -ENOENT; | |
| 127 | + | |
| 128 | + dwarf_formudata(&attr, &ret); | |
| 129 | + return (int)ret; | |
| 130 | +} | |
| 131 | + | |
| 132 | +/** | |
| 133 | + * die_get_type - Get type DIE | |
| 134 | + * @vr_die: a DIE of a variable | |
| 135 | + * @die_mem: where to store a type DIE | |
| 136 | + * | |
| 137 | + * Get a DIE of the type of given variable (@vr_die), and store | |
| 138 | + * it to die_mem. Return NULL if fails to get a type DIE. | |
| 139 | + */ | |
| 140 | +Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) | |
| 141 | +{ | |
| 142 | + Dwarf_Attribute attr; | |
| 143 | + | |
| 144 | + if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) && | |
| 145 | + dwarf_formref_die(&attr, die_mem)) | |
| 146 | + return die_mem; | |
| 147 | + else | |
| 148 | + return NULL; | |
| 149 | +} | |
| 150 | + | |
| 151 | +/* Get a type die, but skip qualifiers */ | |
| 152 | +static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) | |
| 153 | +{ | |
| 154 | + int tag; | |
| 155 | + | |
| 156 | + do { | |
| 157 | + vr_die = die_get_type(vr_die, die_mem); | |
| 158 | + if (!vr_die) | |
| 159 | + break; | |
| 160 | + tag = dwarf_tag(vr_die); | |
| 161 | + } while (tag == DW_TAG_const_type || | |
| 162 | + tag == DW_TAG_restrict_type || | |
| 163 | + tag == DW_TAG_volatile_type || | |
| 164 | + tag == DW_TAG_shared_type); | |
| 165 | + | |
| 166 | + return vr_die; | |
| 167 | +} | |
| 168 | + | |
| 169 | +/** | |
| 170 | + * die_get_real_type - Get a type die, but skip qualifiers and typedef | |
| 171 | + * @vr_die: a DIE of a variable | |
| 172 | + * @die_mem: where to store a type DIE | |
| 173 | + * | |
| 174 | + * Get a DIE of the type of given variable (@vr_die), and store | |
| 175 | + * it to die_mem. Return NULL if fails to get a type DIE. | |
| 176 | + * If the type is qualifiers (e.g. const) or typedef, this skips it | |
| 177 | + * and tries to find real type (structure or basic types, e.g. int). | |
| 178 | + */ | |
| 179 | +Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) | |
| 180 | +{ | |
| 181 | + do { | |
| 182 | + vr_die = __die_get_real_type(vr_die, die_mem); | |
| 183 | + } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef); | |
| 184 | + | |
| 185 | + return vr_die; | |
| 186 | +} | |
| 187 | + | |
| 188 | +/* Get attribute and translate it as a udata */ | |
| 189 | +static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name, | |
| 190 | + Dwarf_Word *result) | |
| 191 | +{ | |
| 192 | + Dwarf_Attribute attr; | |
| 193 | + | |
| 194 | + if (dwarf_attr(tp_die, attr_name, &attr) == NULL || | |
| 195 | + dwarf_formudata(&attr, result) != 0) | |
| 196 | + return -ENOENT; | |
| 197 | + | |
| 198 | + return 0; | |
| 199 | +} | |
| 200 | + | |
| 201 | +/** | |
| 202 | + * die_is_signed_type - Check whether a type DIE is signed or not | |
| 203 | + * @tp_die: a DIE of a type | |
| 204 | + * | |
| 205 | + * Get the encoding of @tp_die and return true if the encoding | |
| 206 | + * is signed. | |
| 207 | + */ | |
| 208 | +bool die_is_signed_type(Dwarf_Die *tp_die) | |
| 209 | +{ | |
| 210 | + Dwarf_Word ret; | |
| 211 | + | |
| 212 | + if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret)) | |
| 213 | + return false; | |
| 214 | + | |
| 215 | + return (ret == DW_ATE_signed_char || ret == DW_ATE_signed || | |
| 216 | + ret == DW_ATE_signed_fixed); | |
| 217 | +} | |
| 218 | + | |
| 219 | +/** | |
| 220 | + * die_get_data_member_location - Get the data-member offset | |
| 221 | + * @mb_die: a DIE of a member of a data structure | |
| 222 | + * @offs: The offset of the member in the data structure | |
| 223 | + * | |
| 224 | + * Get the offset of @mb_die in the data structure including @mb_die, and | |
| 225 | + * stores result offset to @offs. If any error occurs this returns errno. | |
| 226 | + */ | |
| 227 | +int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs) | |
| 228 | +{ | |
| 229 | + Dwarf_Attribute attr; | |
| 230 | + Dwarf_Op *expr; | |
| 231 | + size_t nexpr; | |
| 232 | + int ret; | |
| 233 | + | |
| 234 | + if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL) | |
| 235 | + return -ENOENT; | |
| 236 | + | |
| 237 | + if (dwarf_formudata(&attr, offs) != 0) { | |
| 238 | + /* DW_AT_data_member_location should be DW_OP_plus_uconst */ | |
| 239 | + ret = dwarf_getlocation(&attr, &expr, &nexpr); | |
| 240 | + if (ret < 0 || nexpr == 0) | |
| 241 | + return -ENOENT; | |
| 242 | + | |
| 243 | + if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) { | |
| 244 | + pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n", | |
| 245 | + expr[0].atom, nexpr); | |
| 246 | + return -ENOTSUP; | |
| 247 | + } | |
| 248 | + *offs = (Dwarf_Word)expr[0].number; | |
| 249 | + } | |
| 250 | + return 0; | |
| 251 | +} | |
| 252 | + | |
| 253 | +/** | |
| 254 | + * die_find_child - Generic DIE search function in DIE tree | |
| 255 | + * @rt_die: a root DIE | |
| 256 | + * @callback: a callback function | |
| 257 | + * @data: a user data passed to the callback function | |
| 258 | + * @die_mem: a buffer for result DIE | |
| 259 | + * | |
| 260 | + * Trace DIE tree from @rt_die and call @callback for each child DIE. | |
| 261 | + * If @callback returns DIE_FIND_CB_END, this stores the DIE into | |
| 262 | + * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE, | |
| 263 | + * this continues to trace the tree. Optionally, @callback can return | |
| 264 | + * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only | |
| 265 | + * the children and trace only the siblings respectively. | |
| 266 | + * Returns NULL if @callback can't find any appropriate DIE. | |
| 267 | + */ | |
| 268 | +Dwarf_Die *die_find_child(Dwarf_Die *rt_die, | |
| 269 | + int (*callback)(Dwarf_Die *, void *), | |
| 270 | + void *data, Dwarf_Die *die_mem) | |
| 271 | +{ | |
| 272 | + Dwarf_Die child_die; | |
| 273 | + int ret; | |
| 274 | + | |
| 275 | + ret = dwarf_child(rt_die, die_mem); | |
| 276 | + if (ret != 0) | |
| 277 | + return NULL; | |
| 278 | + | |
| 279 | + do { | |
| 280 | + ret = callback(die_mem, data); | |
| 281 | + if (ret == DIE_FIND_CB_END) | |
| 282 | + return die_mem; | |
| 283 | + | |
| 284 | + if ((ret & DIE_FIND_CB_CHILD) && | |
| 285 | + die_find_child(die_mem, callback, data, &child_die)) { | |
| 286 | + memcpy(die_mem, &child_die, sizeof(Dwarf_Die)); | |
| 287 | + return die_mem; | |
| 288 | + } | |
| 289 | + } while ((ret & DIE_FIND_CB_SIBLING) && | |
| 290 | + dwarf_siblingof(die_mem, die_mem) == 0); | |
| 291 | + | |
| 292 | + return NULL; | |
| 293 | +} | |
| 294 | + | |
| 295 | +struct __addr_die_search_param { | |
| 296 | + Dwarf_Addr addr; | |
| 297 | + Dwarf_Die *die_mem; | |
| 298 | +}; | |
| 299 | + | |
| 300 | +/* die_find callback for non-inlined function search */ | |
| 301 | +static int __die_search_func_cb(Dwarf_Die *fn_die, void *data) | |
| 302 | +{ | |
| 303 | + struct __addr_die_search_param *ad = data; | |
| 304 | + | |
| 305 | + if (dwarf_tag(fn_die) == DW_TAG_subprogram && | |
| 306 | + dwarf_haspc(fn_die, ad->addr)) { | |
| 307 | + memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die)); | |
| 308 | + return DWARF_CB_ABORT; | |
| 309 | + } | |
| 310 | + return DWARF_CB_OK; | |
| 311 | +} | |
| 312 | + | |
| 313 | +/** | |
| 314 | + * die_find_realfunc - Search a non-inlined function at given address | |
| 315 | + * @cu_die: a CU DIE which including @addr | |
| 316 | + * @addr: target address | |
| 317 | + * @die_mem: a buffer for result DIE | |
| 318 | + * | |
| 319 | + * Search a non-inlined function DIE which includes @addr. Stores the | |
| 320 | + * DIE to @die_mem and returns it if found. Returns NULl if failed. | |
| 321 | + */ | |
| 322 | +Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr, | |
| 323 | + Dwarf_Die *die_mem) | |
| 324 | +{ | |
| 325 | + struct __addr_die_search_param ad; | |
| 326 | + ad.addr = addr; | |
| 327 | + ad.die_mem = die_mem; | |
| 328 | + /* dwarf_getscopes can't find subprogram. */ | |
| 329 | + if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0)) | |
| 330 | + return NULL; | |
| 331 | + else | |
| 332 | + return die_mem; | |
| 333 | +} | |
| 334 | + | |
| 335 | +/* die_find callback for inline function search */ | |
| 336 | +static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data) | |
| 337 | +{ | |
| 338 | + Dwarf_Addr *addr = data; | |
| 339 | + | |
| 340 | + if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine && | |
| 341 | + dwarf_haspc(die_mem, *addr)) | |
| 342 | + return DIE_FIND_CB_END; | |
| 343 | + | |
| 344 | + return DIE_FIND_CB_CONTINUE; | |
| 345 | +} | |
| 346 | + | |
| 347 | +/** | |
| 348 | + * die_find_inlinefunc - Search an inlined function at given address | |
| 349 | + * @cu_die: a CU DIE which including @addr | |
| 350 | + * @addr: target address | |
| 351 | + * @die_mem: a buffer for result DIE | |
| 352 | + * | |
| 353 | + * Search an inlined function DIE which includes @addr. Stores the | |
| 354 | + * DIE to @die_mem and returns it if found. Returns NULl if failed. | |
| 355 | + * If several inlined functions are expanded recursively, this trace | |
| 356 | + * it and returns deepest one. | |
| 357 | + */ | |
| 358 | +Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr, | |
| 359 | + Dwarf_Die *die_mem) | |
| 360 | +{ | |
| 361 | + Dwarf_Die tmp_die; | |
| 362 | + | |
| 363 | + sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die); | |
| 364 | + if (!sp_die) | |
| 365 | + return NULL; | |
| 366 | + | |
| 367 | + /* Inlined function could be recursive. Trace it until fail */ | |
| 368 | + while (sp_die) { | |
| 369 | + memcpy(die_mem, sp_die, sizeof(Dwarf_Die)); | |
| 370 | + sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, | |
| 371 | + &tmp_die); | |
| 372 | + } | |
| 373 | + | |
| 374 | + return die_mem; | |
| 375 | +} | |
| 376 | + | |
| 377 | +/* Line walker internal parameters */ | |
| 378 | +struct __line_walk_param { | |
| 379 | + const char *fname; | |
| 380 | + line_walk_callback_t callback; | |
| 381 | + void *data; | |
| 382 | + int retval; | |
| 383 | +}; | |
| 384 | + | |
| 385 | +static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data) | |
| 386 | +{ | |
| 387 | + struct __line_walk_param *lw = data; | |
| 388 | + Dwarf_Addr addr; | |
| 389 | + int lineno; | |
| 390 | + | |
| 391 | + if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) { | |
| 392 | + lineno = die_get_call_lineno(in_die); | |
| 393 | + if (lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) { | |
| 394 | + lw->retval = lw->callback(lw->fname, lineno, addr, | |
| 395 | + lw->data); | |
| 396 | + if (lw->retval != 0) | |
| 397 | + return DIE_FIND_CB_END; | |
| 398 | + } | |
| 399 | + } | |
| 400 | + return DIE_FIND_CB_SIBLING; | |
| 401 | +} | |
| 402 | + | |
| 403 | +/* Walk on lines of blocks included in given DIE */ | |
| 404 | +static int __die_walk_funclines(Dwarf_Die *sp_die, | |
| 405 | + line_walk_callback_t callback, void *data) | |
| 406 | +{ | |
| 407 | + struct __line_walk_param lw = { | |
| 408 | + .callback = callback, | |
| 409 | + .data = data, | |
| 410 | + .retval = 0, | |
| 411 | + }; | |
| 412 | + Dwarf_Die die_mem; | |
| 413 | + Dwarf_Addr addr; | |
| 414 | + int lineno; | |
| 415 | + | |
| 416 | + /* Handle function declaration line */ | |
| 417 | + lw.fname = dwarf_decl_file(sp_die); | |
| 418 | + if (lw.fname && dwarf_decl_line(sp_die, &lineno) == 0 && | |
| 419 | + dwarf_entrypc(sp_die, &addr) == 0) { | |
| 420 | + lw.retval = callback(lw.fname, lineno, addr, data); | |
| 421 | + if (lw.retval != 0) | |
| 422 | + goto done; | |
| 423 | + } | |
| 424 | + die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem); | |
| 425 | +done: | |
| 426 | + return lw.retval; | |
| 427 | +} | |
| 428 | + | |
| 429 | +static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data) | |
| 430 | +{ | |
| 431 | + struct __line_walk_param *lw = data; | |
| 432 | + | |
| 433 | + lw->retval = __die_walk_funclines(sp_die, lw->callback, lw->data); | |
| 434 | + if (lw->retval != 0) | |
| 435 | + return DWARF_CB_ABORT; | |
| 436 | + | |
| 437 | + return DWARF_CB_OK; | |
| 438 | +} | |
| 439 | + | |
| 440 | +/** | |
| 441 | + * die_walk_lines - Walk on lines inside given DIE | |
| 442 | + * @rt_die: a root DIE (CU or subprogram) | |
| 443 | + * @callback: callback routine | |
| 444 | + * @data: user data | |
| 445 | + * | |
| 446 | + * Walk on all lines inside given @rt_die and call @callback on each line. | |
| 447 | + * If the @rt_die is a function, walk only on the lines inside the function, | |
| 448 | + * otherwise @rt_die must be a CU DIE. | |
| 449 | + * Note that this walks not only dwarf line list, but also function entries | |
| 450 | + * and inline call-site. | |
| 451 | + */ | |
| 452 | +int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data) | |
| 453 | +{ | |
| 454 | + Dwarf_Lines *lines; | |
| 455 | + Dwarf_Line *line; | |
| 456 | + Dwarf_Addr addr; | |
| 457 | + const char *fname; | |
| 458 | + int lineno, ret = 0; | |
| 459 | + Dwarf_Die die_mem, *cu_die; | |
| 460 | + size_t nlines, i; | |
| 461 | + | |
| 462 | + /* Get the CU die */ | |
| 463 | + if (dwarf_tag(rt_die) == DW_TAG_subprogram) | |
| 464 | + cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL); | |
| 465 | + else | |
| 466 | + cu_die = rt_die; | |
| 467 | + if (!cu_die) { | |
| 468 | + pr_debug2("Failed to get CU from subprogram\n"); | |
| 469 | + return -EINVAL; | |
| 470 | + } | |
| 471 | + | |
| 472 | + /* Get lines list in the CU */ | |
| 473 | + if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) { | |
| 474 | + pr_debug2("Failed to get source lines on this CU.\n"); | |
| 475 | + return -ENOENT; | |
| 476 | + } | |
| 477 | + pr_debug2("Get %zd lines from this CU\n", nlines); | |
| 478 | + | |
| 479 | + /* Walk on the lines on lines list */ | |
| 480 | + for (i = 0; i < nlines; i++) { | |
| 481 | + line = dwarf_onesrcline(lines, i); | |
| 482 | + if (line == NULL || | |
| 483 | + dwarf_lineno(line, &lineno) != 0 || | |
| 484 | + dwarf_lineaddr(line, &addr) != 0) { | |
| 485 | + pr_debug2("Failed to get line info. " | |
| 486 | + "Possible error in debuginfo.\n"); | |
| 487 | + continue; | |
| 488 | + } | |
| 489 | + /* Filter lines based on address */ | |
| 490 | + if (rt_die != cu_die) | |
| 491 | + /* | |
| 492 | + * Address filtering | |
| 493 | + * The line is included in given function, and | |
| 494 | + * no inline block includes it. | |
| 495 | + */ | |
| 496 | + if (!dwarf_haspc(rt_die, addr) || | |
| 497 | + die_find_inlinefunc(rt_die, addr, &die_mem)) | |
| 498 | + continue; | |
| 499 | + /* Get source line */ | |
| 500 | + fname = dwarf_linesrc(line, NULL, NULL); | |
| 501 | + | |
| 502 | + ret = callback(fname, lineno, addr, data); | |
| 503 | + if (ret != 0) | |
| 504 | + return ret; | |
| 505 | + } | |
| 506 | + | |
| 507 | + /* | |
| 508 | + * Dwarf lines doesn't include function declarations and inlined | |
| 509 | + * subroutines. We have to check functions list or given function. | |
| 510 | + */ | |
| 511 | + if (rt_die != cu_die) | |
| 512 | + ret = __die_walk_funclines(rt_die, callback, data); | |
| 513 | + else { | |
| 514 | + struct __line_walk_param param = { | |
| 515 | + .callback = callback, | |
| 516 | + .data = data, | |
| 517 | + .retval = 0, | |
| 518 | + }; | |
| 519 | + dwarf_getfuncs(cu_die, __die_walk_culines_cb, ¶m, 0); | |
| 520 | + ret = param.retval; | |
| 521 | + } | |
| 522 | + | |
| 523 | + return ret; | |
| 524 | +} | |
| 525 | + | |
| 526 | +struct __find_variable_param { | |
| 527 | + const char *name; | |
| 528 | + Dwarf_Addr addr; | |
| 529 | +}; | |
| 530 | + | |
| 531 | +static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data) | |
| 532 | +{ | |
| 533 | + struct __find_variable_param *fvp = data; | |
| 534 | + int tag; | |
| 535 | + | |
| 536 | + tag = dwarf_tag(die_mem); | |
| 537 | + if ((tag == DW_TAG_formal_parameter || | |
| 538 | + tag == DW_TAG_variable) && | |
| 539 | + die_compare_name(die_mem, fvp->name)) | |
| 540 | + return DIE_FIND_CB_END; | |
| 541 | + | |
| 542 | + if (dwarf_haspc(die_mem, fvp->addr)) | |
| 543 | + return DIE_FIND_CB_CONTINUE; | |
| 544 | + else | |
| 545 | + return DIE_FIND_CB_SIBLING; | |
| 546 | +} | |
| 547 | + | |
| 548 | +/** | |
| 549 | + * die_find_variable_at - Find a given name variable at given address | |
| 550 | + * @sp_die: a function DIE | |
| 551 | + * @name: variable name | |
| 552 | + * @addr: address | |
| 553 | + * @die_mem: a buffer for result DIE | |
| 554 | + * | |
| 555 | + * Find a variable DIE called @name at @addr in @sp_die. | |
| 556 | + */ | |
| 557 | +Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name, | |
| 558 | + Dwarf_Addr addr, Dwarf_Die *die_mem) | |
| 559 | +{ | |
| 560 | + struct __find_variable_param fvp = { .name = name, .addr = addr}; | |
| 561 | + | |
| 562 | + return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp, | |
| 563 | + die_mem); | |
| 564 | +} | |
| 565 | + | |
| 566 | +static int __die_find_member_cb(Dwarf_Die *die_mem, void *data) | |
| 567 | +{ | |
| 568 | + const char *name = data; | |
| 569 | + | |
| 570 | + if ((dwarf_tag(die_mem) == DW_TAG_member) && | |
| 571 | + die_compare_name(die_mem, name)) | |
| 572 | + return DIE_FIND_CB_END; | |
| 573 | + | |
| 574 | + return DIE_FIND_CB_SIBLING; | |
| 575 | +} | |
| 576 | + | |
| 577 | +/** | |
| 578 | + * die_find_member - Find a given name member in a data structure | |
| 579 | + * @st_die: a data structure type DIE | |
| 580 | + * @name: member name | |
| 581 | + * @die_mem: a buffer for result DIE | |
| 582 | + * | |
| 583 | + * Find a member DIE called @name in @st_die. | |
| 584 | + */ | |
| 585 | +Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name, | |
| 586 | + Dwarf_Die *die_mem) | |
| 587 | +{ | |
| 588 | + return die_find_child(st_die, __die_find_member_cb, (void *)name, | |
| 589 | + die_mem); | |
| 590 | +} | |
| 591 | + | |
| 592 | +/** | |
| 593 | + * die_get_typename - Get the name of given variable DIE | |
| 594 | + * @vr_die: a variable DIE | |
| 595 | + * @buf: a buffer for result type name | |
| 596 | + * @len: a max-length of @buf | |
| 597 | + * | |
| 598 | + * Get the name of @vr_die and stores it to @buf. Return the actual length | |
| 599 | + * of type name if succeeded. Return -E2BIG if @len is not enough long, and | |
| 600 | + * Return -ENOENT if failed to find type name. | |
| 601 | + * Note that the result will stores typedef name if possible, and stores | |
| 602 | + * "*(function_type)" if the type is a function pointer. | |
| 603 | + */ | |
| 604 | +int die_get_typename(Dwarf_Die *vr_die, char *buf, int len) | |
| 605 | +{ | |
| 606 | + Dwarf_Die type; | |
| 607 | + int tag, ret, ret2; | |
| 608 | + const char *tmp = ""; | |
| 609 | + | |
| 610 | + if (__die_get_real_type(vr_die, &type) == NULL) | |
| 611 | + return -ENOENT; | |
| 612 | + | |
| 613 | + tag = dwarf_tag(&type); | |
| 614 | + if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type) | |
| 615 | + tmp = "*"; | |
| 616 | + else if (tag == DW_TAG_subroutine_type) { | |
| 617 | + /* Function pointer */ | |
| 618 | + ret = snprintf(buf, len, "(function_type)"); | |
| 619 | + return (ret >= len) ? -E2BIG : ret; | |
| 620 | + } else { | |
| 621 | + if (!dwarf_diename(&type)) | |
| 622 | + return -ENOENT; | |
| 623 | + if (tag == DW_TAG_union_type) | |
| 624 | + tmp = "union "; | |
| 625 | + else if (tag == DW_TAG_structure_type) | |
| 626 | + tmp = "struct "; | |
| 627 | + /* Write a base name */ | |
| 628 | + ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type)); | |
| 629 | + return (ret >= len) ? -E2BIG : ret; | |
| 630 | + } | |
| 631 | + ret = die_get_typename(&type, buf, len); | |
| 632 | + if (ret > 0) { | |
| 633 | + ret2 = snprintf(buf + ret, len - ret, "%s", tmp); | |
| 634 | + ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret; | |
| 635 | + } | |
| 636 | + return ret; | |
| 637 | +} | |
| 638 | + | |
| 639 | +/** | |
| 640 | + * die_get_varname - Get the name and type of given variable DIE | |
| 641 | + * @vr_die: a variable DIE | |
| 642 | + * @buf: a buffer for type and variable name | |
| 643 | + * @len: the max-length of @buf | |
| 644 | + * | |
| 645 | + * Get the name and type of @vr_die and stores it in @buf as "type\tname". | |
| 646 | + */ | |
| 647 | +int die_get_varname(Dwarf_Die *vr_die, char *buf, int len) | |
| 648 | +{ | |
| 649 | + int ret, ret2; | |
| 650 | + | |
| 651 | + ret = die_get_typename(vr_die, buf, len); | |
| 652 | + if (ret < 0) { | |
| 653 | + pr_debug("Failed to get type, make it unknown.\n"); | |
| 654 | + ret = snprintf(buf, len, "(unknown_type)"); | |
| 655 | + } | |
| 656 | + if (ret > 0) { | |
| 657 | + ret2 = snprintf(buf + ret, len - ret, "\t%s", | |
| 658 | + dwarf_diename(vr_die)); | |
| 659 | + ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret; | |
| 660 | + } | |
| 661 | + return ret; | |
| 662 | +} | |
| 663 | + | |
| @@ -0,0 +1,100 @@ | ||
| 1 | +#ifndef _DWARF_AUX_H | |
| 2 | +#define _DWARF_AUX_H | |
| 3 | +/* | |
| 4 | + * dwarf-aux.h : libdw auxiliary interfaces | |
| 5 | + * | |
| 6 | + * This program is free software; you can redistribute it and/or modify | |
| 7 | + * it under the terms of the GNU General Public License as published by | |
| 8 | + * the Free Software Foundation; either version 2 of the License, or | |
| 9 | + * (at your option) any later version. | |
| 10 | + * | |
| 11 | + * This program is distributed in the hope that it will be useful, | |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | + * GNU General Public License for more details. | |
| 15 | + * | |
| 16 | + * You should have received a copy of the GNU General Public License | |
| 17 | + * along with this program; if not, write to the Free Software | |
| 18 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 19 | + * | |
| 20 | + */ | |
| 21 | + | |
| 22 | +#include <dwarf.h> | |
| 23 | +#include <elfutils/libdw.h> | |
| 24 | +#include <elfutils/libdwfl.h> | |
| 25 | +#include <elfutils/version.h> | |
| 26 | + | |
| 27 | +/* Find the realpath of the target file */ | |
| 28 | +extern const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname); | |
| 29 | + | |
| 30 | +/* Get DW_AT_comp_dir (should be NULL with older gcc) */ | |
| 31 | +extern const char *cu_get_comp_dir(Dwarf_Die *cu_die); | |
| 32 | + | |
| 33 | +/* Get a line number and file name for given address */ | |
| 34 | +extern int cu_find_lineinfo(Dwarf_Die *cudie, unsigned long addr, | |
| 35 | + const char **fname, int *lineno); | |
| 36 | + | |
| 37 | +/* Compare diename and tname */ | |
| 38 | +extern bool die_compare_name(Dwarf_Die *dw_die, const char *tname); | |
| 39 | + | |
| 40 | +/* Get callsite line number of inline-function instance */ | |
| 41 | +extern int die_get_call_lineno(Dwarf_Die *in_die); | |
| 42 | + | |
| 43 | +/* Get type die */ | |
| 44 | +extern Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem); | |
| 45 | + | |
| 46 | +/* Get a type die, but skip qualifiers and typedef */ | |
| 47 | +extern Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem); | |
| 48 | + | |
| 49 | +/* Check whether the DIE is signed or not */ | |
| 50 | +extern bool die_is_signed_type(Dwarf_Die *tp_die); | |
| 51 | + | |
| 52 | +/* Get data_member_location offset */ | |
| 53 | +extern int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs); | |
| 54 | + | |
| 55 | +/* Return values for die_find_child() callbacks */ | |
| 56 | +enum { | |
| 57 | + DIE_FIND_CB_END = 0, /* End of Search */ | |
| 58 | + DIE_FIND_CB_CHILD = 1, /* Search only children */ | |
| 59 | + DIE_FIND_CB_SIBLING = 2, /* Search only siblings */ | |
| 60 | + DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */ | |
| 61 | +}; | |
| 62 | + | |
| 63 | +/* Search child DIEs */ | |
| 64 | +extern Dwarf_Die *die_find_child(Dwarf_Die *rt_die, | |
| 65 | + int (*callback)(Dwarf_Die *, void *), | |
| 66 | + void *data, Dwarf_Die *die_mem); | |
| 67 | + | |
| 68 | +/* Search a non-inlined function including given address */ | |
| 69 | +extern Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr, | |
| 70 | + Dwarf_Die *die_mem); | |
| 71 | + | |
| 72 | +/* Search an inlined function including given address */ | |
| 73 | +extern Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr, | |
| 74 | + Dwarf_Die *die_mem); | |
| 75 | + | |
| 76 | +/* Walker on lines (Note: line number will not be sorted) */ | |
| 77 | +typedef int (* line_walk_callback_t) (const char *fname, int lineno, | |
| 78 | + Dwarf_Addr addr, void *data); | |
| 79 | + | |
| 80 | +/* | |
| 81 | + * Walk on lines inside given DIE. If the DIE is a subprogram, walk only on | |
| 82 | + * the lines inside the subprogram, otherwise the DIE must be a CU DIE. | |
| 83 | + */ | |
| 84 | +extern int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, | |
| 85 | + void *data); | |
| 86 | + | |
| 87 | +/* Find a variable called 'name' at given address */ | |
| 88 | +extern Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name, | |
| 89 | + Dwarf_Addr addr, Dwarf_Die *die_mem); | |
| 90 | + | |
| 91 | +/* Find a member called 'name' */ | |
| 92 | +extern Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name, | |
| 93 | + Dwarf_Die *die_mem); | |
| 94 | + | |
| 95 | +/* Get the name of given variable DIE */ | |
| 96 | +extern int die_get_typename(Dwarf_Die *vr_die, char *buf, int len); | |
| 97 | + | |
| 98 | +/* Get the name and type of given variable DIE, stored as "type\tname" */ | |
| 99 | +extern int die_get_varname(Dwarf_Die *vr_die, char *buf, int len); | |
| 100 | +#endif | |
| @@ -117,6 +117,10 @@ static struct map *kernel_get_module_map(const char *module) · 116 unmodified lines | ||
| 117 | 117 | struct rb_node *nd; |
| 118 | 118 | struct map_groups *grp = &machine.kmaps; |
| 119 | 119 | |
| 120 | + /* A file path -- this is an offline module */ | |
| 121 | + if (module && strchr(module, '/')) | |
| 122 | + return machine__new_module(&machine, 0, module); | |
| 123 | + | |
| 120 | 124 | if (!module) |
| 121 | 125 | module = "kernel"; |
| 122 | 126 | |
| @@ -170,16 +174,24 @@ const char *kernel_get_module_path(const char *module) · 47 unmodified lines | ||
| 170 | 174 | } |
| 171 | 175 | |
| 172 | 176 | #ifdef DWARF_SUPPORT |
| 173 | −static int open_vmlinux(const char *module) | |
| 177 | +/* Open new debuginfo of given module */ | |
| 178 | +static struct debuginfo *open_debuginfo(const char *module) | |
| 174 | 179 | { |
| 175 | − const char *path = kernel_get_module_path(module); | |
| 176 | − if (!path) { | |
| 177 | − pr_err("Failed to find path of %s module.\n", | |
| 178 | − module ?: "kernel"); | |
| 179 | − return -ENOENT; | |
| 180 | + const char *path; | |
| 181 | + | |
| 182 | + /* A file path -- this is an offline module */ | |
| 183 | + if (module && strchr(module, '/')) | |
| 184 | + path = module; | |
| 185 | + else { | |
| 186 | + path = kernel_get_module_path(module); | |
| 187 | + | |
| 188 | + if (!path) { | |
| 189 | + pr_err("Failed to find path of %s module.\n", | |
| 190 | + module ?: "kernel"); | |
| 191 | + return NULL; | |
| 192 | + } | |
| 180 | 193 | } |
| 181 | − pr_debug("Try to open %s\n", path); | |
| 182 | − return open(path, O_RDONLY); | |
| 194 | + return debuginfo__new(path); | |
| 183 | 195 | } |
| 184 | 196 | |
| 185 | 197 | /* |
| @@ -193,13 +205,24 @@ static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp, · 7 unmodified lines | ||
| 193 | 205 | struct map *map; |
| 194 | 206 | u64 addr; |
| 195 | 207 | int ret = -ENOENT; |
| 208 | + struct debuginfo *dinfo; | |
| 196 | 209 | |
| 197 | 210 | sym = __find_kernel_function_by_name(tp->symbol, &map); |
| 198 | 211 | if (sym) { |
| 199 | 212 | addr = map->unmap_ip(map, sym->start + tp->offset); |
| 200 | 213 | pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol, |
| 201 | 214 | tp->offset, addr); |
| 202 | − ret = find_perf_probe_point((unsigned long)addr, pp); | |
| 215 | + | |
| 216 | + dinfo = debuginfo__new_online_kernel(addr); | |
| 217 | + if (dinfo) { | |
| 218 | + ret = debuginfo__find_probe_point(dinfo, | |
| 219 | + (unsigned long)addr, pp); | |
| 220 | + debuginfo__delete(dinfo); | |
| 221 | + } else { | |
| 222 | + pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n", | |
| 223 | + addr); | |
| 224 | + ret = -ENOENT; | |
| 225 | + } | |
| 203 | 226 | } |
| 204 | 227 | if (ret <= 0) { |
| 205 | 228 | pr_debug("Failed to find corresponding probes from " |
| @@ -214,30 +237,70 @@ static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp, · 8 unmodified lines | ||
| 214 | 237 | return 0; |
| 215 | 238 | } |
| 216 | 239 | |
| 240 | +static int add_module_to_probe_trace_events(struct probe_trace_event *tevs, | |
| 241 | + int ntevs, const char *module) | |
| 242 | +{ | |
| 243 | + int i, ret = 0; | |
| 244 | + char *tmp; | |
| 245 | + | |
| 246 | + if (!module) | |
| 247 | + return 0; | |
| 248 | + | |
| 249 | + tmp = strrchr(module, '/'); | |
| 250 | + if (tmp) { | |
| 251 | + /* This is a module path -- get the module name */ | |
| 252 | + module = strdup(tmp + 1); | |
| 253 | + if (!module) | |
| 254 | + return -ENOMEM; | |
| 255 | + tmp = strchr(module, '.'); | |
| 256 | + if (tmp) | |
| 257 | + *tmp = '\0'; | |
| 258 | + tmp = (char *)module; /* For free() */ | |
| 259 | + } | |
| 260 | + | |
| 261 | + for (i = 0; i < ntevs; i++) { | |
| 262 | + tevs[i].point.module = strdup(module); | |
| 263 | + if (!tevs[i].point.module) { | |
| 264 | + ret = -ENOMEM; | |
| 265 | + break; | |
| 266 | + } | |
| 267 | + } | |
| 268 | + | |
| 269 | + if (tmp) | |
| 270 | + free(tmp); | |
| 271 | + | |
| 272 | + return ret; | |
| 273 | +} | |
| 274 | + | |
| 217 | 275 | /* Try to find perf_probe_event with debuginfo */ |
| 218 | 276 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
| 219 | − struct probe_trace_event **tevs, | |
| 220 | − int max_tevs, const char *module) | |
| 277 | + struct probe_trace_event **tevs, | |
| 278 | + int max_tevs, const char *module) | |
| 221 | 279 | { |
| 222 | 280 | bool need_dwarf = perf_probe_event_need_dwarf(pev); |
| 223 | − int fd, ntevs; | |
| 281 | + struct debuginfo *dinfo = open_debuginfo(module); | |
| 282 | + int ntevs, ret = 0; | |
| 224 | 283 | |
| 225 | − fd = open_vmlinux(module); | |
| 226 | − if (fd < 0) { | |
| 284 | + if (!dinfo) { | |
| 227 | 285 | if (need_dwarf) { |
| 228 | 286 | pr_warning("Failed to open debuginfo file.\n"); |
| 229 | − return fd; | |
| 287 | + return -ENOENT; | |
| 230 | 288 | } |
| 231 | − pr_debug("Could not open vmlinux. Try to use symbols.\n"); | |
| 289 | + pr_debug("Could not open debuginfo. Try to use symbols.\n"); | |
| 232 | 290 | return 0; |
| 233 | 291 | } |
| 234 | 292 | |
| 235 | − /* Searching trace events corresponding to probe event */ | |
| 236 | − ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs); | |
| 293 | + /* Searching trace events corresponding to a probe event */ | |
| 294 | + ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs); | |
| 295 | + | |
| 296 | + debuginfo__delete(dinfo); | |
| 237 | 297 | |
| 238 | 298 | if (ntevs > 0) { /* Succeeded to find trace events */ |
| 239 | 299 | pr_debug("find %d probe_trace_events.\n", ntevs); |
| 240 | − return ntevs; | |
| 300 | + if (module) | |
| 301 | + ret = add_module_to_probe_trace_events(*tevs, ntevs, | |
| 302 | + module); | |
| 303 | + return ret < 0 ? ret : ntevs; | |
| 241 | 304 | } |
| 242 | 305 | |
| 243 | 306 | if (ntevs == 0) { /* No error but failed to find probe point. */ |
| @@ -371,8 +434,9 @@ int show_line_range(struct line_range *lr, const char *module) · 127 unmodified lines | ||
| 371 | 434 | { |
| 372 | 435 | int l = 1; |
| 373 | 436 | struct line_node *ln; |
| 437 | + struct debuginfo *dinfo; | |
| 374 | 438 | FILE *fp; |
| 375 | − int fd, ret; | |
| 439 | + int ret; | |
| 376 | 440 | char *tmp; |
| 377 | 441 | |
| 378 | 442 | /* Search a line range */ |
| @@ -380,13 +444,14 @@ int show_line_range(struct line_range *lr, const char *module) · 1 unmodified line | ||
| 380 | 444 | if (ret < 0) |
| 381 | 445 | return ret; |
| 382 | 446 | |
| 383 | − fd = open_vmlinux(module); | |
| 384 | − if (fd < 0) { | |
| 447 | + dinfo = open_debuginfo(module); | |
| 448 | + if (!dinfo) { | |
| 385 | 449 | pr_warning("Failed to open debuginfo file.\n"); |
| 386 | − return fd; | |
| 450 | + return -ENOENT; | |
| 387 | 451 | } |
| 388 | 452 | |
| 389 | − ret = find_line_range(fd, lr); | |
| 453 | + ret = debuginfo__find_line_range(dinfo, lr); | |
| 454 | + debuginfo__delete(dinfo); | |
| 390 | 455 | if (ret == 0) { |
| 391 | 456 | pr_warning("Specified source line is not found.\n"); |
| 392 | 457 | return -ENOENT; |
| @@ -448,7 +513,8 @@ end: · 55 unmodified lines | ||
| 448 | 513 | return ret; |
| 449 | 514 | } |
| 450 | 515 | |
| 451 | −static int show_available_vars_at(int fd, struct perf_probe_event *pev, | |
| 516 | +static int show_available_vars_at(struct debuginfo *dinfo, | |
| 517 | + struct perf_probe_event *pev, | |
| 452 | 518 | int max_vls, struct strfilter *_filter, |
| 453 | 519 | bool externs) |
| 454 | 520 | { |
| @@ -463,7 +529,8 @@ static int show_available_vars_at(int fd, struct perf_probe_event *pev, · 8 unmodified lines | ||
| 463 | 529 | return -EINVAL; |
| 464 | 530 | pr_debug("Searching variables at %s\n", buf); |
| 465 | 531 | |
| 466 | − ret = find_available_vars_at(fd, pev, &vls, max_vls, externs); | |
| 532 | + ret = debuginfo__find_available_vars_at(dinfo, pev, &vls, | |
| 533 | + max_vls, externs); | |
| 467 | 534 | if (ret <= 0) { |
| 468 | 535 | pr_err("Failed to find variables at %s (%d)\n", buf, ret); |
| 469 | 536 | goto end; |
| @@ -504,24 +571,26 @@ int show_available_vars(struct perf_probe_event *pevs, int npevs, · 34 unmodified lines | ||
| 504 | 571 | int max_vls, const char *module, |
| 505 | 572 | struct strfilter *_filter, bool externs) |
| 506 | 573 | { |
| 507 | − int i, fd, ret = 0; | |
| 574 | + int i, ret = 0; | |
| 575 | + struct debuginfo *dinfo; | |
| 508 | 576 | |
| 509 | 577 | ret = init_vmlinux(); |
| 510 | 578 | if (ret < 0) |
| 511 | 579 | return ret; |
| 512 | 580 | |
| 581 | + dinfo = open_debuginfo(module); | |
| 582 | + if (!dinfo) { | |
| 583 | + pr_warning("Failed to open debuginfo file.\n"); | |
| 584 | + return -ENOENT; | |
| 585 | + } | |
| 586 | + | |
| 513 | 587 | setup_pager(); |
| 514 | 588 | |
| 515 | − for (i = 0; i < npevs && ret >= 0; i++) { | |
| 516 | − fd = open_vmlinux(module); | |
| 517 | − if (fd < 0) { | |
| 518 | − pr_warning("Failed to open debug information file.\n"); | |
| 519 | − ret = fd; | |
| 520 | − break; | |
| 521 | − } | |
| 522 | − ret = show_available_vars_at(fd, &pevs[i], max_vls, _filter, | |
| 589 | + for (i = 0; i < npevs && ret >= 0; i++) | |
| 590 | + ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter, | |
| 523 | 591 | externs); |
| 524 | − } | |
| 592 | + | |
| 593 | + debuginfo__delete(dinfo); | |
| 525 | 594 | return ret; |
| 526 | 595 | } |
| 527 | 596 | |
| @@ -990,7 +1059,7 @@ bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) · 462 unmodified lines | ||
| 990 | 1059 | |
| 991 | 1060 | /* Parse probe_events event into struct probe_point */ |
| 992 | 1061 | static int parse_probe_trace_command(const char *cmd, |
| 993 | − struct probe_trace_event *tev) | |
| 1062 | + struct probe_trace_event *tev) | |
| 994 | 1063 | { |
| 995 | 1064 | struct probe_trace_point *tp = &tev->point; |
| 996 | 1065 | char pr; |
| @@ -1023,8 +1092,14 @@ static int parse_probe_trace_command(const char *cmd, · 26 unmodified lines | ||
| 1023 | 1092 | |
| 1024 | 1093 | tp->retprobe = (pr == 'r'); |
| 1025 | 1094 | |
| 1026 | − /* Scan function name and offset */ | |
| 1027 | − ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol, | |
| 1095 | + /* Scan module name(if there), function name and offset */ | |
| 1096 | + p = strchr(argv[1], ':'); | |
| 1097 | + if (p) { | |
| 1098 | + tp->module = strndup(argv[1], p - argv[1]); | |
| 1099 | + p++; | |
| 1100 | + } else | |
| 1101 | + p = argv[1]; | |
| 1102 | + ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol, | |
| 1028 | 1103 | &tp->offset); |
| 1029 | 1104 | if (ret == 1) |
| 1030 | 1105 | tp->offset = 0; |
| @@ -1269,9 +1344,10 @@ char *synthesize_probe_trace_command(struct probe_trace_event *tev) · 238 unmodified lines | ||
| 1269 | 1344 | if (buf == NULL) |
| 1270 | 1345 | return NULL; |
| 1271 | 1346 | |
| 1272 | − len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu", | |
| 1347 | + len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu", | |
| 1273 | 1348 | tp->retprobe ? 'r' : 'p', |
| 1274 | 1349 | tev->group, tev->event, |
| 1350 | + tp->module ?: "", tp->module ? ":" : "", | |
| 1275 | 1351 | tp->symbol, tp->offset); |
| 1276 | 1352 | if (len <= 0) |
| 1277 | 1353 | goto error; |
| @@ -1378,6 +1454,8 @@ static void clear_probe_trace_event(struct probe_trace_event *tev) · 100 unmodified lines | ||
| 1378 | 1454 | free(tev->group); |
| 1379 | 1455 | if (tev->point.symbol) |
| 1380 | 1456 | free(tev->point.symbol); |
| 1457 | + if (tev->point.module) | |
| 1458 | + free(tev->point.module); | |
| 1381 | 1459 | for (i = 0; i < tev->nargs; i++) { |
| 1382 | 1460 | if (tev->args[i].name) |
| 1383 | 1461 | free(tev->args[i].name); |
| @@ -1729,7 +1807,7 @@ static int convert_to_probe_trace_events(struct perf_probe_event *pev, · 345 unmodified lines | ||
| 1729 | 1807 | /* Convert perf_probe_event with debuginfo */ |
| 1730 | 1808 | ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module); |
| 1731 | 1809 | if (ret != 0) |
| 1732 | − return ret; | |
| 1810 | + return ret; /* Found in debuginfo or got an error */ | |
| 1733 | 1811 | |
| 1734 | 1812 | /* Allocate trace event buffer */ |
| 1735 | 1813 | tev = *tevs = zalloc(sizeof(struct probe_trace_event)); |
| @@ -1742,6 +1820,11 @@ static int convert_to_probe_trace_events(struct perf_probe_event *pev, · 6 unmodified lines | ||
| 1742 | 1820 | ret = -ENOMEM; |
| 1743 | 1821 | goto error; |
| 1744 | 1822 | } |
| 1823 | + tev->point.module = strdup(module); | |
| 1824 | + if (tev->point.module == NULL) { | |
| 1825 | + ret = -ENOMEM; | |
| 1826 | + goto error; | |
| 1827 | + } | |
| 1745 | 1828 | tev->point.offset = pev->point.offset; |
| 1746 | 1829 | tev->point.retprobe = pev->point.retprobe; |
| 1747 | 1830 | tev->nargs = pev->nargs; |
| @@ -10,6 +10,7 @@ extern bool probe_event_dry_run; · 9 unmodified lines | ||
| 10 | 10 | /* kprobe-tracer tracing point */ |
| 11 | 11 | struct probe_trace_point { |
| 12 | 12 | char *symbol; /* Base symbol */ |
| 13 | + char *module; /* Module name */ | |
| 13 | 14 | unsigned long offset; /* Offset from symbol */ |
| 14 | 15 | bool retprobe; /* Return probe flag */ |
| 15 | 16 | }; |
| @@ -43,21 +43,6 @@ · 42 unmodified lines | ||
| 43 | 43 | /* Kprobe tracer basic type is up to u64 */ |
| 44 | 44 | #define MAX_BASIC_TYPE_BITS 64 |
| 45 | 45 | |
| 46 | −/* | |
| 47 | − * Compare the tail of two strings. | |
| 48 | − * Return 0 if whole of either string is same as another's tail part. | |
| 49 | − */ | |
| 50 | −static int strtailcmp(const char *s1, const char *s2) | |
| 51 | −{ | |
| 52 | − int i1 = strlen(s1); | |
| 53 | − int i2 = strlen(s2); | |
| 54 | − while (--i1 >= 0 && --i2 >= 0) { | |
| 55 | − if (s1[i1] != s2[i2]) | |
| 56 | − return s1[i1] - s2[i2]; | |
| 57 | − } | |
| 58 | − return 0; | |
| 59 | −} | |
| 60 | − | |
| 61 | 46 | /* Line number list operations */ |
| 62 | 47 | |
| 63 | 48 | /* Add a line to line number list */ |
| @@ -131,29 +116,37 @@ static const Dwfl_Callbacks offline_callbacks = { · 67 unmodified lines | ||
| 131 | 116 | }; |
| 132 | 117 | |
| 133 | 118 | /* Get a Dwarf from offline image */ |
| 134 | −static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias) | |
| 119 | +static int debuginfo__init_offline_dwarf(struct debuginfo *self, | |
| 120 | + const char *path) | |
| 135 | 121 | { |
| 136 | 122 | Dwfl_Module *mod; |
| 137 | − Dwarf *dbg = NULL; | |
| 123 | + int fd; | |
| 138 | 124 | |
| 139 | − if (!dwflp) | |
| 140 | − return NULL; | |
| 125 | + fd = open(path, O_RDONLY); | |
| 126 | + if (fd < 0) | |
| 127 | + return fd; | |
| 141 | 128 | |
| 142 | − *dwflp = dwfl_begin(&offline_callbacks); | |
| 143 | − if (!*dwflp) | |
| 144 | − return NULL; | |
| 129 | + self->dwfl = dwfl_begin(&offline_callbacks); | |
| 130 | + if (!self->dwfl) | |
| 131 | + goto error; | |
| 145 | 132 | |
| 146 | − mod = dwfl_report_offline(*dwflp, "", "", fd); | |
| 133 | + mod = dwfl_report_offline(self->dwfl, "", "", fd); | |
| 147 | 134 | if (!mod) |
| 148 | 135 | goto error; |
| 149 | 136 | |
| 150 | − dbg = dwfl_module_getdwarf(mod, bias); | |
| 151 | − if (!dbg) { | |
| 137 | + self->dbg = dwfl_module_getdwarf(mod, &self->bias); | |
| 138 | + if (!self->dbg) | |
| 139 | + goto error; | |
| 140 | + | |
| 141 | + return 0; | |
| 152 | 142 | error: |
| 153 | − dwfl_end(*dwflp); | |
| 154 | − *dwflp = NULL; | |
| 155 | − } | |
| 156 | − return dbg; | |
| 143 | + if (self->dwfl) | |
| 144 | + dwfl_end(self->dwfl); | |
| 145 | + else | |
| 146 | + close(fd); | |
| 147 | + memset(self, 0, sizeof(*self)); | |
| 148 | + | |
| 149 | + return -ENOENT; | |
| 157 | 150 | } |
| 158 | 151 | |
| 159 | 152 | #if _ELFUTILS_PREREQ(0, 148) |
| @@ -189,597 +182,81 @@ static const Dwfl_Callbacks kernel_callbacks = { · 29 unmodified lines | ||
| 189 | 182 | }; |
| 190 | 183 | |
| 191 | 184 | /* Get a Dwarf from live kernel image */ |
| 192 | −static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp, | |
| 193 | − Dwarf_Addr *bias) | |
| 185 | +static int debuginfo__init_online_kernel_dwarf(struct debuginfo *self, | |
| 186 | + Dwarf_Addr addr) | |
| 194 | 187 | { |
| 195 | − Dwarf *dbg; | |
| 196 | − | |
| 197 | − if (!dwflp) | |
| 198 | − return NULL; | |
| 199 | − | |
| 200 | − *dwflp = dwfl_begin(&kernel_callbacks); | |
| 201 | − if (!*dwflp) | |
| 202 | − return NULL; | |
| 188 | + self->dwfl = dwfl_begin(&kernel_callbacks); | |
| 189 | + if (!self->dwfl) | |
| 190 | + return -EINVAL; | |
| 203 | 191 | |
| 204 | 192 | /* Load the kernel dwarves: Don't care the result here */ |
| 205 | − dwfl_linux_kernel_report_kernel(*dwflp); | |
| 206 | − dwfl_linux_kernel_report_modules(*dwflp); | |
| 193 | + dwfl_linux_kernel_report_kernel(self->dwfl); | |
| 194 | + dwfl_linux_kernel_report_modules(self->dwfl); | |
| 207 | 195 | |
| 208 | − dbg = dwfl_addrdwarf(*dwflp, addr, bias); | |
| 196 | + self->dbg = dwfl_addrdwarf(self->dwfl, addr, &self->bias); | |
| 209 | 197 | /* Here, check whether we could get a real dwarf */ |
| 210 | − if (!dbg) { | |
| 198 | + if (!self->dbg) { | |
| 211 | 199 | pr_debug("Failed to find kernel dwarf at %lx\n", |
| 212 | 200 | (unsigned long)addr); |
| 213 | − dwfl_end(*dwflp); | |
| 214 | − *dwflp = NULL; | |
| 201 | + dwfl_end(self->dwfl); | |
| 202 | + memset(self, 0, sizeof(*self)); | |
| 203 | + return -ENOENT; | |
| 215 | 204 | } |
| 216 | − return dbg; | |
| 205 | + | |
| 206 | + return 0; | |
| 217 | 207 | } |
| 218 | 208 | #else |
| 219 | 209 | /* With older elfutils, this just support kernel module... */ |
| 220 | −static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used, Dwfl **dwflp, | |
| 221 | − Dwarf_Addr *bias) | |
| 210 | +static int debuginfo__init_online_kernel_dwarf(struct debuginfo *self, | |
| 211 | + Dwarf_Addr addr __used) | |
| 222 | 212 | { |
| 223 | − int fd; | |
| 224 | 213 | const char *path = kernel_get_module_path("kernel"); |
| 225 | 214 | |
| 226 | 215 | if (!path) { |
| 227 | 216 | pr_err("Failed to find vmlinux path\n"); |
| 228 | − return NULL; | |
| 217 | + return -ENOENT; | |
| 229 | 218 | } |
| 230 | 219 | |
| 231 | 220 | pr_debug2("Use file %s for debuginfo\n", path); |
| 232 | − fd = open(path, O_RDONLY); | |
| 233 | − if (fd < 0) | |
| 234 | − return NULL; | |
| 235 | − | |
| 236 | − return dwfl_init_offline_dwarf(fd, dwflp, bias); | |
| 221 | + return debuginfo__init_offline_dwarf(self, path); | |
| 237 | 222 | } |
| 238 | 223 | #endif |
| 239 | 224 | |
| 240 | −/* Dwarf wrappers */ | |
| 241 | − | |
| 242 | −/* Find the realpath of the target file. */ | |
| 243 | −static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname) | |
| 244 | −{ | |
| 245 | − Dwarf_Files *files; | |
| 246 | − size_t nfiles, i; | |
| 247 | − const char *src = NULL; | |
| 248 | − int ret; | |
| 249 | − | |
| 250 | − if (!fname) | |
| 251 | − return NULL; | |
| 252 | − | |
| 253 | − ret = dwarf_getsrcfiles(cu_die, &files, &nfiles); | |
| 254 | − if (ret != 0) | |
| 255 | − return NULL; | |
| 256 | − | |
| 257 | − for (i = 0; i < nfiles; i++) { | |
| 258 | − src = dwarf_filesrc(files, i, NULL, NULL); | |
| 259 | − if (strtailcmp(src, fname) == 0) | |
| 260 | − break; | |
| 261 | − } | |
| 262 | − if (i == nfiles) | |
| 263 | − return NULL; | |
| 264 | − return src; | |
| 265 | −} | |
| 266 | − | |
| 267 | −/* Get DW_AT_comp_dir (should be NULL with older gcc) */ | |
| 268 | −static const char *cu_get_comp_dir(Dwarf_Die *cu_die) | |
| 269 | −{ | |
| 270 | − Dwarf_Attribute attr; | |
| 271 | − if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL) | |
| 272 | − return NULL; | |
| 273 | − return dwarf_formstring(&attr); | |
| 274 | −} | |
| 275 | − | |
| 276 | −/* Get a line number and file name for given address */ | |
| 277 | −static int cu_find_lineinfo(Dwarf_Die *cudie, unsigned long addr, | |
| 278 | − const char **fname, int *lineno) | |
| 279 | −{ | |
| 280 | − Dwarf_Line *line; | |
| 281 | − Dwarf_Addr laddr; | |
| 282 | − | |
| 283 | − line = dwarf_getsrc_die(cudie, (Dwarf_Addr)addr); | |
| 284 | − if (line && dwarf_lineaddr(line, &laddr) == 0 && | |
| 285 | − addr == (unsigned long)laddr && dwarf_lineno(line, lineno) == 0) { | |
| 286 | − *fname = dwarf_linesrc(line, NULL, NULL); | |
| 287 | − if (!*fname) | |
| 288 | − /* line number is useless without filename */ | |
| 289 | − *lineno = 0; | |
| 290 | − } | |
| 291 | − | |
| 292 | − return *lineno ?: -ENOENT; | |
| 293 | −} | |
| 294 | − | |
| 295 | −/* Compare diename and tname */ | |
| 296 | −static bool die_compare_name(Dwarf_Die *dw_die, const char *tname) | |
| 297 | −{ | |
| 298 | − const char *name; | |
| 299 | − name = dwarf_diename(dw_die); | |
| 300 | − return name ? (strcmp(tname, name) == 0) : false; | |
| 301 | −} | |
| 302 | − | |
| 303 | −/* Get callsite line number of inline-function instance */ | |
| 304 | −static int die_get_call_lineno(Dwarf_Die *in_die) | |
| 305 | −{ | |
| 306 | − Dwarf_Attribute attr; | |
| 307 | − Dwarf_Word ret; | |
| 308 | − | |
| 309 | − if (!dwarf_attr(in_die, DW_AT_call_line, &attr)) | |
| 310 | − return -ENOENT; | |
| 311 | − | |
| 312 | − dwarf_formudata(&attr, &ret); | |
| 313 | − return (int)ret; | |
| 314 | −} | |
| 315 | − | |
| 316 | −/* Get type die */ | |
| 317 | −static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) | |
| 318 | −{ | |
| 319 | − Dwarf_Attribute attr; | |
| 320 | − | |
| 321 | − if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) && | |
| 322 | − dwarf_formref_die(&attr, die_mem)) | |
| 323 | − return die_mem; | |
| 324 | − else | |
| 325 | − return NULL; | |
| 326 | −} | |
| 327 | − | |
| 328 | −/* Get a type die, but skip qualifiers */ | |
| 329 | −static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) | |
| 330 | −{ | |
| 331 | − int tag; | |
| 332 | − | |
| 333 | − do { | |
| 334 | − vr_die = die_get_type(vr_die, die_mem); | |
| 335 | − if (!vr_die) | |
| 336 | − break; | |
| 337 | − tag = dwarf_tag(vr_die); | |
| 338 | − } while (tag == DW_TAG_const_type || | |
| 339 | − tag == DW_TAG_restrict_type || | |
| 340 | − tag == DW_TAG_volatile_type || | |
| 341 | − tag == DW_TAG_shared_type); | |
| 342 | − | |
| 343 | − return vr_die; | |
| 344 | −} | |
| 345 | − | |
| 346 | −/* Get a type die, but skip qualifiers and typedef */ | |
| 347 | −static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) | |
| 348 | −{ | |
| 349 | − do { | |
| 350 | − vr_die = __die_get_real_type(vr_die, die_mem); | |
| 351 | − } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef); | |
| 352 | − | |
| 353 | − return vr_die; | |
| 354 | −} | |
| 355 | − | |
| 356 | −static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name, | |
| 357 | − Dwarf_Word *result) | |
| 358 | −{ | |
| 359 | − Dwarf_Attribute attr; | |
| 360 | − | |
| 361 | − if (dwarf_attr(tp_die, attr_name, &attr) == NULL || | |
| 362 | − dwarf_formudata(&attr, result) != 0) | |
| 363 | − return -ENOENT; | |
| 364 | − | |
| 365 | − return 0; | |
| 366 | −} | |
| 367 | − | |
| 368 | −static bool die_is_signed_type(Dwarf_Die *tp_die) | |
| 369 | −{ | |
| 370 | − Dwarf_Word ret; | |
| 371 | − | |
| 372 | − if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret)) | |
| 373 | − return false; | |
| 374 | − | |
| 375 | − return (ret == DW_ATE_signed_char || ret == DW_ATE_signed || | |
| 376 | − ret == DW_ATE_signed_fixed); | |
| 377 | −} | |
| 378 | − | |
| 379 | −static int die_get_byte_size(Dwarf_Die *tp_die) | |
| 380 | −{ | |
| 381 | − Dwarf_Word ret; | |
| 382 | − | |
| 383 | − if (die_get_attr_udata(tp_die, DW_AT_byte_size, &ret)) | |
| 384 | − return 0; | |
| 385 | − | |
| 386 | − return (int)ret; | |
| 387 | −} | |
| 388 | − | |
| 389 | −static int die_get_bit_size(Dwarf_Die *tp_die) | |
| 390 | −{ | |
| 391 | − Dwarf_Word ret; | |
| 392 | − | |
| 393 | − if (die_get_attr_udata(tp_die, DW_AT_bit_size, &ret)) | |
| 394 | − return 0; | |
| 395 | − | |
| 396 | − return (int)ret; | |
| 397 | −} | |
| 398 | − | |
| 399 | −static int die_get_bit_offset(Dwarf_Die *tp_die) | |
| 400 | −{ | |
| 401 | − Dwarf_Word ret; | |
| 402 | − | |
| 403 | − if (die_get_attr_udata(tp_die, DW_AT_bit_offset, &ret)) | |
| 404 | − return 0; | |
| 405 | − | |
| 406 | − return (int)ret; | |
| 407 | −} | |
| 408 | − | |
| 409 | −/* Get data_member_location offset */ | |
| 410 | −static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs) | |
| 411 | −{ | |
| 412 | − Dwarf_Attribute attr; | |
| 413 | − Dwarf_Op *expr; | |
| 414 | − size_t nexpr; | |
| 415 | − int ret; | |
| 416 | − | |
| 417 | − if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL) | |
| 418 | − return -ENOENT; | |
| 419 | − | |
| 420 | − if (dwarf_formudata(&attr, offs) != 0) { | |
| 421 | − /* DW_AT_data_member_location should be DW_OP_plus_uconst */ | |
| 422 | − ret = dwarf_getlocation(&attr, &expr, &nexpr); | |
| 423 | − if (ret < 0 || nexpr == 0) | |
| 424 | − return -ENOENT; | |
| 425 | − | |
| 426 | − if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) { | |
| 427 | − pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n", | |
| 428 | − expr[0].atom, nexpr); | |
| 429 | − return -ENOTSUP; | |
| 430 | − } | |
| 431 | − *offs = (Dwarf_Word)expr[0].number; | |
| 432 | − } | |
| 433 | − return 0; | |
| 434 | −} | |
| 435 | − | |
| 436 | −/* Return values for die_find callbacks */ | |
| 437 | −enum { | |
| 438 | − DIE_FIND_CB_FOUND = 0, /* End of Search */ | |
| 439 | − DIE_FIND_CB_CHILD = 1, /* Search only children */ | |
| 440 | − DIE_FIND_CB_SIBLING = 2, /* Search only siblings */ | |
| 441 | − DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */ | |
| 442 | −}; | |
| 443 | − | |
| 444 | −/* Search a child die */ | |
| 445 | −static Dwarf_Die *die_find_child(Dwarf_Die *rt_die, | |
| 446 | − int (*callback)(Dwarf_Die *, void *), | |
| 447 | − void *data, Dwarf_Die *die_mem) | |
| 225 | +struct debuginfo *debuginfo__new(const char *path) | |
| 448 | 226 | { |
| 449 | − Dwarf_Die child_die; | |
| 450 | − int ret; | |
| 451 | − | |
| 452 | − ret = dwarf_child(rt_die, die_mem); | |
| 453 | − if (ret != 0) | |
| 227 | + struct debuginfo *self = zalloc(sizeof(struct debuginfo)); | |
| 228 | + if (!self) | |
| 454 | 229 | return NULL; |
| 455 | 230 | |
| 456 | − do { | |
| 457 | − ret = callback(die_mem, data); | |
| 458 | − if (ret == DIE_FIND_CB_FOUND) | |
| 459 | − return die_mem; | |
| 460 | − | |
| 461 | − if ((ret & DIE_FIND_CB_CHILD) && | |
| 462 | − die_find_child(die_mem, callback, data, &child_die)) { | |
| 463 | − memcpy(die_mem, &child_die, sizeof(Dwarf_Die)); | |
| 464 | − return die_mem; | |
| 465 | − } | |
| 466 | − } while ((ret & DIE_FIND_CB_SIBLING) && | |
| 467 | − dwarf_siblingof(die_mem, die_mem) == 0); | |
| 468 | − | |
| 469 | − return NULL; | |
| 470 | −} | |
| 471 | − | |
| 472 | −struct __addr_die_search_param { | |
| 473 | − Dwarf_Addr addr; | |
| 474 | − Dwarf_Die *die_mem; | |
| 475 | −}; | |
| 476 | − | |
| 477 | −static int __die_search_func_cb(Dwarf_Die *fn_die, void *data) | |
| 478 | −{ | |
| 479 | − struct __addr_die_search_param *ad = data; | |
| 480 | − | |
| 481 | − if (dwarf_tag(fn_die) == DW_TAG_subprogram && | |
| 482 | − dwarf_haspc(fn_die, ad->addr)) { | |
| 483 | − memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die)); | |
| 484 | − return DWARF_CB_ABORT; | |
| 231 | + if (debuginfo__init_offline_dwarf(self, path) < 0) { | |
| 232 | + free(self); | |
| 233 | + self = NULL; | |
| 485 | 234 | } |
| 486 | − return DWARF_CB_OK; | |
| 487 | −} | |
| 488 | − | |
| 489 | −/* Search a real subprogram including this line, */ | |
| 490 | −static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr, | |
| 491 | − Dwarf_Die *die_mem) | |
| 492 | −{ | |
| 493 | − struct __addr_die_search_param ad; | |
| 494 | − ad.addr = addr; | |
| 495 | − ad.die_mem = die_mem; | |
| 496 | − /* dwarf_getscopes can't find subprogram. */ | |
| 497 | − if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0)) | |
| 498 | − return NULL; | |
| 499 | − else | |
| 500 | − return die_mem; | |
| 501 | −} | |
| 502 | − | |
| 503 | −/* die_find callback for inline function search */ | |
| 504 | −static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data) | |
| 505 | −{ | |
| 506 | − Dwarf_Addr *addr = data; | |
| 507 | − | |
| 508 | − if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine && | |
| 509 | − dwarf_haspc(die_mem, *addr)) | |
| 510 | − return DIE_FIND_CB_FOUND; | |
| 511 | 235 | |
| 512 | − return DIE_FIND_CB_CONTINUE; | |
| 236 | + return self; | |
| 513 | 237 | } |
| 514 | 238 | |
| 515 | −/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */ | |
| 516 | −static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr, | |
| 517 | − Dwarf_Die *die_mem) | |
| 239 | +struct debuginfo *debuginfo__new_online_kernel(unsigned long addr) | |
| 518 | 240 | { |
| 519 | − Dwarf_Die tmp_die; | |
| 520 | − | |
| 521 | − sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die); | |
| 522 | − if (!sp_die) | |
| 241 | + struct debuginfo *self = zalloc(sizeof(struct debuginfo)); | |
| 242 | + if (!self) | |
| 523 | 243 | return NULL; |
| 524 | 244 | |
| 525 | − /* Inlined function could be recursive. Trace it until fail */ | |
| 526 | − while (sp_die) { | |
| 527 | − memcpy(die_mem, sp_die, sizeof(Dwarf_Die)); | |
| 528 | − sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, | |
| 529 | − &tmp_die); | |
| 530 | − } | |
| 531 | − | |
| 532 | − return die_mem; | |
| 533 | −} | |
| 534 | − | |
| 535 | −/* Walker on lines (Note: line number will not be sorted) */ | |
| 536 | −typedef int (* line_walk_handler_t) (const char *fname, int lineno, | |
| 537 | − Dwarf_Addr addr, void *data); | |
| 538 | − | |
| 539 | −struct __line_walk_param { | |
| 540 | − const char *fname; | |
| 541 | − line_walk_handler_t handler; | |
| 542 | − void *data; | |
| 543 | − int retval; | |
| 544 | −}; | |
| 545 | − | |
| 546 | −static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data) | |
| 547 | −{ | |
| 548 | − struct __line_walk_param *lw = data; | |
| 549 | − Dwarf_Addr addr; | |
| 550 | − int lineno; | |
| 551 | − | |
| 552 | − if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) { | |
| 553 | − lineno = die_get_call_lineno(in_die); | |
| 554 | − if (lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) { | |
| 555 | − lw->retval = lw->handler(lw->fname, lineno, addr, | |
| 556 | − lw->data); | |
| 557 | − if (lw->retval != 0) | |
| 558 | − return DIE_FIND_CB_FOUND; | |
| 559 | − } | |
| 560 | − } | |
| 561 | − return DIE_FIND_CB_SIBLING; | |
| 562 | −} | |
| 563 | − | |
| 564 | −/* Walk on lines of blocks included in given DIE */ | |
| 565 | −static int __die_walk_funclines(Dwarf_Die *sp_die, | |
| 566 | − line_walk_handler_t handler, void *data) | |
| 567 | −{ | |
| 568 | − struct __line_walk_param lw = { | |
| 569 | − .handler = handler, | |
| 570 | − .data = data, | |
| 571 | − .retval = 0, | |
| 572 | − }; | |
| 573 | − Dwarf_Die die_mem; | |
| 574 | − Dwarf_Addr addr; | |
| 575 | − int lineno; | |
| 576 | − | |
| 577 | − /* Handle function declaration line */ | |
| 578 | − lw.fname = dwarf_decl_file(sp_die); | |
| 579 | − if (lw.fname && dwarf_decl_line(sp_die, &lineno) == 0 && | |
| 580 | − dwarf_entrypc(sp_die, &addr) == 0) { | |
| 581 | − lw.retval = handler(lw.fname, lineno, addr, data); | |
| 582 | − if (lw.retval != 0) | |
| 583 | − goto done; | |
| 584 | − } | |
| 585 | − die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem); | |
| 586 | −done: | |
| 587 | − return lw.retval; | |
| 588 | −} | |
| 589 | − | |
| 590 | −static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data) | |
| 591 | −{ | |
| 592 | − struct __line_walk_param *lw = data; | |
| 593 | − | |
| 594 | − lw->retval = __die_walk_funclines(sp_die, lw->handler, lw->data); | |
| 595 | − if (lw->retval != 0) | |
| 596 | − return DWARF_CB_ABORT; | |
| 597 | − | |
| 598 | − return DWARF_CB_OK; | |
| 599 | −} | |
| 600 | − | |
| 601 | −/* | |
| 602 | − * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on | |
| 603 | − * the lines inside the subprogram, otherwise PDIE must be a CU DIE. | |
| 604 | − */ | |
| 605 | −static int die_walk_lines(Dwarf_Die *pdie, line_walk_handler_t handler, | |
| 606 | − void *data) | |
| 607 | −{ | |
| 608 | − Dwarf_Lines *lines; | |
| 609 | − Dwarf_Line *line; | |
| 610 | − Dwarf_Addr addr; | |
| 611 | − const char *fname; | |
| 612 | − int lineno, ret = 0; | |
| 613 | − Dwarf_Die die_mem, *cu_die; | |
| 614 | − size_t nlines, i; | |
| 615 | − | |
| 616 | − /* Get the CU die */ | |
| 617 | − if (dwarf_tag(pdie) == DW_TAG_subprogram) | |
| 618 | − cu_die = dwarf_diecu(pdie, &die_mem, NULL, NULL); | |
| 619 | − else | |
| 620 | − cu_die = pdie; | |
| 621 | − if (!cu_die) { | |
| 622 | − pr_debug2("Failed to get CU from subprogram\n"); | |
| 623 | − return -EINVAL; | |
| 624 | − } | |
| 625 | − | |
| 626 | − /* Get lines list in the CU */ | |
| 627 | − if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) { | |
| 628 | − pr_debug2("Failed to get source lines on this CU.\n"); | |
| 629 | − return -ENOENT; | |
| 630 | − } | |
| 631 | − pr_debug2("Get %zd lines from this CU\n", nlines); | |
| 632 | − | |
| 633 | − /* Walk on the lines on lines list */ | |
| 634 | − for (i = 0; i < nlines; i++) { | |
| 635 | − line = dwarf_onesrcline(lines, i); | |
| 636 | − if (line == NULL || | |
| 637 | − dwarf_lineno(line, &lineno) != 0 || | |
| 638 | − dwarf_lineaddr(line, &addr) != 0) { | |
| 639 | − pr_debug2("Failed to get line info. " | |
| 640 | − "Possible error in debuginfo.\n"); | |
| 641 | − continue; | |
| 642 | − } | |
| 643 | − /* Filter lines based on address */ | |
| 644 | − if (pdie != cu_die) | |
| 645 | − /* | |
| 646 | − * Address filtering | |
| 647 | − * The line is included in given function, and | |
| 648 | − * no inline block includes it. | |
| 649 | − */ | |
| 650 | − if (!dwarf_haspc(pdie, addr) || | |
| 651 | − die_find_inlinefunc(pdie, addr, &die_mem)) | |
| 652 | − continue; | |
| 653 | − /* Get source line */ | |
| 654 | − fname = dwarf_linesrc(line, NULL, NULL); | |
| 655 | − | |
| 656 | − ret = handler(fname, lineno, addr, data); | |
| 657 | − if (ret != 0) | |
| 658 | − return ret; | |
| 659 | − } | |
| 660 | − | |
| 661 | − /* | |
| 662 | − * Dwarf lines doesn't include function declarations and inlined | |
| 663 | − * subroutines. We have to check functions list or given function. | |
| 664 | − */ | |
| 665 | − if (pdie != cu_die) | |
| 666 | − ret = __die_walk_funclines(pdie, handler, data); | |
| 667 | − else { | |
| 668 | − struct __line_walk_param param = { | |
| 669 | − .handler = handler, | |
| 670 | − .data = data, | |
| 671 | − .retval = 0, | |
| 672 | − }; | |
| 673 | − dwarf_getfuncs(cu_die, __die_walk_culines_cb, ¶m, 0); | |
| 674 | − ret = param.retval; | |
| 245 | + if (debuginfo__init_online_kernel_dwarf(self, (Dwarf_Addr)addr) < 0) { | |
| 246 | + free(self); | |
| 247 | + self = NULL; | |
| 675 | 248 | } |
| 676 | 249 | |
| 677 | − return ret; | |
| 678 | −} | |
| 679 | − | |
| 680 | −struct __find_variable_param { | |
| 681 | − const char *name; | |
| 682 | − Dwarf_Addr addr; | |
| 683 | −}; | |
| 684 | − | |
| 685 | −static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data) | |
| 686 | −{ | |
| 687 | − struct __find_variable_param *fvp = data; | |
| 688 | − int tag; | |
| 689 | − | |
| 690 | − tag = dwarf_tag(die_mem); | |
| 691 | − if ((tag == DW_TAG_formal_parameter || | |
| 692 | − tag == DW_TAG_variable) && | |
| 693 | − die_compare_name(die_mem, fvp->name)) | |
| 694 | − return DIE_FIND_CB_FOUND; | |
| 695 | − | |
| 696 | − if (dwarf_haspc(die_mem, fvp->addr)) | |
| 697 | − return DIE_FIND_CB_CONTINUE; | |
| 698 | − else | |
| 699 | − return DIE_FIND_CB_SIBLING; | |
| 700 | −} | |
| 701 | − | |
| 702 | −/* Find a variable called 'name' at given address */ | |
| 703 | −static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name, | |
| 704 | − Dwarf_Addr addr, Dwarf_Die *die_mem) | |
| 705 | −{ | |
| 706 | − struct __find_variable_param fvp = { .name = name, .addr = addr}; | |
| 707 | − | |
| 708 | − return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp, | |
| 709 | − die_mem); | |
| 710 | −} | |
| 711 | − | |
| 712 | −static int __die_find_member_cb(Dwarf_Die *die_mem, void *data) | |
| 713 | −{ | |
| 714 | − const char *name = data; | |
| 715 | − | |
| 716 | − if ((dwarf_tag(die_mem) == DW_TAG_member) && | |
| 717 | − die_compare_name(die_mem, name)) | |
| 718 | − return DIE_FIND_CB_FOUND; | |
| 719 | − | |
| 720 | − return DIE_FIND_CB_SIBLING; | |
| 721 | −} | |
| 722 | − | |
| 723 | −/* Find a member called 'name' */ | |
| 724 | −static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name, | |
| 725 | − Dwarf_Die *die_mem) | |
| 726 | −{ | |
| 727 | − return die_find_child(st_die, __die_find_member_cb, (void *)name, | |
| 728 | − die_mem); | |
| 729 | −} | |
| 730 | − | |
| 731 | −/* Get the name of given variable DIE */ | |
| 732 | −static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len) | |
| 733 | −{ | |
| 734 | − Dwarf_Die type; | |
| 735 | − int tag, ret, ret2; | |
| 736 | − const char *tmp = ""; | |
| 737 | − | |
| 738 | − if (__die_get_real_type(vr_die, &type) == NULL) | |
| 739 | − return -ENOENT; | |
| 740 | − | |
| 741 | − tag = dwarf_tag(&type); | |
| 742 | − if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type) | |
| 743 | − tmp = "*"; | |
| 744 | − else if (tag == DW_TAG_subroutine_type) { | |
| 745 | − /* Function pointer */ | |
| 746 | − ret = snprintf(buf, len, "(function_type)"); | |
| 747 | − return (ret >= len) ? -E2BIG : ret; | |
| 748 | − } else { | |
| 749 | − if (!dwarf_diename(&type)) | |
| 750 | − return -ENOENT; | |
| 751 | − if (tag == DW_TAG_union_type) | |
| 752 | − tmp = "union "; | |
| 753 | − else if (tag == DW_TAG_structure_type) | |
| 754 | − tmp = "struct "; | |
| 755 | − /* Write a base name */ | |
| 756 | − ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type)); | |
| 757 | − return (ret >= len) ? -E2BIG : ret; | |
| 758 | − } | |
| 759 | − ret = die_get_typename(&type, buf, len); | |
| 760 | − if (ret > 0) { | |
| 761 | − ret2 = snprintf(buf + ret, len - ret, "%s", tmp); | |
| 762 | − ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret; | |
| 763 | − } | |
| 764 | − return ret; | |
| 250 | + return self; | |
| 765 | 251 | } |
| 766 | 252 | |
| 767 | −/* Get the name and type of given variable DIE, stored as "type\tname" */ | |
| 768 | −static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len) | |
| 253 | +void debuginfo__delete(struct debuginfo *self) | |
| 769 | 254 | { |
| 770 | − int ret, ret2; | |
| 771 | − | |
| 772 | − ret = die_get_typename(vr_die, buf, len); | |
| 773 | − if (ret < 0) { | |
| 774 | − pr_debug("Failed to get type, make it unknown.\n"); | |
| 775 | − ret = snprintf(buf, len, "(unknown_type)"); | |
| 255 | + if (self) { | |
| 256 | + if (self->dwfl) | |
| 257 | + dwfl_end(self->dwfl); | |
| 258 | + free(self); | |
| 776 | 259 | } |
| 777 | − if (ret > 0) { | |
| 778 | − ret2 = snprintf(buf + ret, len - ret, "\t%s", | |
| 779 | − dwarf_diename(vr_die)); | |
| 780 | − ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret; | |
| 781 | − } | |
| 782 | − return ret; | |
| 783 | 260 | } |
| 784 | 261 | |
| 785 | 262 | /* |
| @@ -897,6 +374,7 @@ static int convert_variable_type(Dwarf_Die *vr_die, · 111 unmodified lines | ||
| 897 | 374 | struct probe_trace_arg_ref **ref_ptr = &tvar->ref; |
| 898 | 375 | Dwarf_Die type; |
| 899 | 376 | char buf[16]; |
| 377 | + int bsize, boffs, total; | |
| 900 | 378 | int ret; |
| 901 | 379 | |
| 902 | 380 | /* TODO: check all types */ |
| @@ -906,11 +384,15 @@ static int convert_variable_type(Dwarf_Die *vr_die, · 3 unmodified lines | ||
| 906 | 384 | return (tvar->type == NULL) ? -ENOMEM : 0; |
| 907 | 385 | } |
| 908 | 386 | |
| 909 | − if (die_get_bit_size(vr_die) != 0) { | |
| 387 | + bsize = dwarf_bitsize(vr_die); | |
| 388 | + if (bsize > 0) { | |
| 910 | 389 | /* This is a bitfield */ |
| 911 | − ret = snprintf(buf, 16, "b%d@%d/%zd", die_get_bit_size(vr_die), | |
| 912 | − die_get_bit_offset(vr_die), | |
| 913 | − BYTES_TO_BITS(die_get_byte_size(vr_die))); | |
| 390 | + boffs = dwarf_bitoffset(vr_die); | |
| 391 | + total = dwarf_bytesize(vr_die); | |
| 392 | + if (boffs < 0 || total < 0) | |
| 393 | + return -ENOENT; | |
| 394 | + ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs, | |
| 395 | + BYTES_TO_BITS(total)); | |
| 914 | 396 | goto formatted; |
| 915 | 397 | } |
| 916 | 398 | |
| @@ -958,10 +440,11 @@ static int convert_variable_type(Dwarf_Die *vr_die, · 41 unmodified lines | ||
| 958 | 440 | return (tvar->type == NULL) ? -ENOMEM : 0; |
| 959 | 441 | } |
| 960 | 442 | |
| 961 | − ret = BYTES_TO_BITS(die_get_byte_size(&type)); | |
| 962 | − if (!ret) | |
| 443 | + ret = dwarf_bytesize(&type); | |
| 444 | + if (ret <= 0) | |
| 963 | 445 | /* No size ... try to use default type */ |
| 964 | 446 | return 0; |
| 447 | + ret = BYTES_TO_BITS(ret); | |
| 965 | 448 | |
| 966 | 449 | /* Check the bitwidth */ |
| 967 | 450 | if (ret > MAX_BASIC_TYPE_BITS) { |
| @@ -1025,7 +508,7 @@ static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, · 57 unmodified lines | ||
| 1025 | 508 | else |
| 1026 | 509 | *ref_ptr = ref; |
| 1027 | 510 | } |
| 1028 | − ref->offset += die_get_byte_size(&type) * field->index; | |
| 511 | + ref->offset += dwarf_bytesize(&type) * field->index; | |
| 1029 | 512 | if (!field->next) |
| 1030 | 513 | /* Save vr_die for converting types */ |
| 1031 | 514 | memcpy(die_mem, vr_die, sizeof(*die_mem)); |
| @@ -1245,8 +728,7 @@ static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf) · 213 unmodified lines | ||
| 1245 | 728 | |
| 1246 | 729 | /* If no real subprogram, find a real one */ |
| 1247 | 730 | if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) { |
| 1248 | − sp_die = die_find_real_subprogram(&pf->cu_die, | |
| 1249 | − pf->addr, &die_mem); | |
| 731 | + sp_die = die_find_realfunc(&pf->cu_die, pf->addr, &die_mem); | |
| 1250 | 732 | if (!sp_die) { |
| 1251 | 733 | pr_warning("Failed to find probe point in any " |
| 1252 | 734 | "functions.\n"); |
| @@ -1504,28 +986,18 @@ static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data) · 251 unmodified lines | ||
| 1504 | 986 | } |
| 1505 | 987 | |
| 1506 | 988 | /* Find probe points from debuginfo */ |
| 1507 | −static int find_probes(int fd, struct probe_finder *pf) | |
| 989 | +static int debuginfo__find_probes(struct debuginfo *self, | |
| 990 | + struct probe_finder *pf) | |
| 1508 | 991 | { |
| 1509 | 992 | struct perf_probe_point *pp = &pf->pev->point; |
| 1510 | 993 | Dwarf_Off off, noff; |
| 1511 | 994 | size_t cuhl; |
| 1512 | 995 | Dwarf_Die *diep; |
| 1513 | − Dwarf *dbg = NULL; | |
| 1514 | − Dwfl *dwfl; | |
| 1515 | − Dwarf_Addr bias; /* Currently ignored */ | |
| 1516 | 996 | int ret = 0; |
| 1517 | 997 | |
| 1518 | − dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias); | |
| 1519 | − if (!dbg) { | |
| 1520 | − pr_warning("No debug information found in the vmlinux - " | |
| 1521 | − "please rebuild with CONFIG_DEBUG_INFO=y.\n"); | |
| 1522 | − close(fd); /* Without dwfl_end(), fd isn't closed. */ | |
| 1523 | − return -EBADF; | |
| 1524 | − } | |
| 1525 | − | |
| 1526 | 998 | #if _ELFUTILS_PREREQ(0, 142) |
| 1527 | 999 | /* Get the call frame information from this dwarf */ |
| 1528 | − pf->cfi = dwarf_getcfi(dbg); | |
| 1000 | + pf->cfi = dwarf_getcfi(self->dbg); | |
| 1529 | 1001 | #endif |
| 1530 | 1002 | |
| 1531 | 1003 | off = 0; |
| @@ -1544,7 +1016,8 @@ static int find_probes(int fd, struct probe_finder *pf) · 12 unmodified lines | ||
| 1544 | 1016 | .data = pf, |
| 1545 | 1017 | }; |
| 1546 | 1018 | |
| 1547 | − dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0); | |
| 1019 | + dwarf_getpubnames(self->dbg, pubname_search_cb, | |
| 1020 | + &pubname_param, 0); | |
| 1548 | 1021 | if (pubname_param.found) { |
| 1549 | 1022 | ret = probe_point_search_cb(&pf->sp_die, &probe_param); |
| 1550 | 1023 | if (ret) |
| @@ -1553,9 +1026,9 @@ static int find_probes(int fd, struct probe_finder *pf) · 2 unmodified lines | ||
| 1553 | 1026 | } |
| 1554 | 1027 | |
| 1555 | 1028 | /* Loop on CUs (Compilation Unit) */ |
| 1556 | − while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) { | |
| 1029 | + while (!dwarf_nextcu(self->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) { | |
| 1557 | 1030 | /* Get the DIE(Debugging Information Entry) of this CU */ |
| 1558 | − diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die); | |
| 1031 | + diep = dwarf_offdie(self->dbg, off + cuhl, &pf->cu_die); | |
| 1559 | 1032 | if (!diep) |
| 1560 | 1033 | continue; |
| 1561 | 1034 | |
| @@ -1582,8 +1055,6 @@ static int find_probes(int fd, struct probe_finder *pf) · 20 unmodified lines | ||
| 1582 | 1055 | |
| 1583 | 1056 | found: |
| 1584 | 1057 | line_list__free(&pf->lcache); |
| 1585 | − if (dwfl) | |
| 1586 | − dwfl_end(dwfl); | |
| 1587 | 1058 | |
| 1588 | 1059 | return ret; |
| 1589 | 1060 | } |
| @@ -1629,8 +1100,9 @@ static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf) · 39 unmodified lines | ||
| 1629 | 1100 | } |
| 1630 | 1101 | |
| 1631 | 1102 | /* Find probe_trace_events specified by perf_probe_event from debuginfo */ |
| 1632 | −int find_probe_trace_events(int fd, struct perf_probe_event *pev, | |
| 1633 | − struct probe_trace_event **tevs, int max_tevs) | |
| 1103 | +int debuginfo__find_trace_events(struct debuginfo *self, | |
| 1104 | + struct perf_probe_event *pev, | |
| 1105 | + struct probe_trace_event **tevs, int max_tevs) | |
| 1634 | 1106 | { |
| 1635 | 1107 | struct trace_event_finder tf = { |
| 1636 | 1108 | .pf = {.pev = pev, .callback = add_probe_trace_event}, |
| @@ -1645,7 +1117,7 @@ int find_probe_trace_events(int fd, struct perf_probe_event *pev, · 8 unmodified lines | ||
| 1645 | 1117 | tf.tevs = *tevs; |
| 1646 | 1118 | tf.ntevs = 0; |
| 1647 | 1119 | |
| 1648 | − ret = find_probes(fd, &tf.pf); | |
| 1120 | + ret = debuginfo__find_probes(self, &tf.pf); | |
| 1649 | 1121 | if (ret < 0) { |
| 1650 | 1122 | free(*tevs); |
| 1651 | 1123 | *tevs = NULL; |
| @@ -1739,9 +1211,10 @@ out: · 87 unmodified lines | ||
| 1739 | 1211 | } |
| 1740 | 1212 | |
| 1741 | 1213 | /* Find available variables at given probe point */ |
| 1742 | −int find_available_vars_at(int fd, struct perf_probe_event *pev, | |
| 1743 | − struct variable_list **vls, int max_vls, | |
| 1744 | − bool externs) | |
| 1214 | +int debuginfo__find_available_vars_at(struct debuginfo *self, | |
| 1215 | + struct perf_probe_event *pev, | |
| 1216 | + struct variable_list **vls, | |
| 1217 | + int max_vls, bool externs) | |
| 1745 | 1218 | { |
| 1746 | 1219 | struct available_var_finder af = { |
| 1747 | 1220 | .pf = {.pev = pev, .callback = add_available_vars}, |
| @@ -1756,7 +1229,7 @@ int find_available_vars_at(int fd, struct perf_probe_event *pev, · 8 unmodified lines | ||
| 1756 | 1229 | af.vls = *vls; |
| 1757 | 1230 | af.nvls = 0; |
| 1758 | 1231 | |
| 1759 | − ret = find_probes(fd, &af.pf); | |
| 1232 | + ret = debuginfo__find_probes(self, &af.pf); | |
| 1760 | 1233 | if (ret < 0) { |
| 1761 | 1234 | /* Free vlist for error */ |
| 1762 | 1235 | while (af.nvls--) { |
| @@ -1774,28 +1247,19 @@ int find_available_vars_at(int fd, struct perf_probe_event *pev, · 11 unmodified lines | ||
| 1774 | 1247 | } |
| 1775 | 1248 | |
| 1776 | 1249 | /* Reverse search */ |
| 1777 | −int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt) | |
| 1250 | +int debuginfo__find_probe_point(struct debuginfo *self, unsigned long addr, | |
| 1251 | + struct perf_probe_point *ppt) | |
| 1778 | 1252 | { |
| 1779 | 1253 | Dwarf_Die cudie, spdie, indie; |
| 1780 | − Dwarf *dbg = NULL; | |
| 1781 | − Dwfl *dwfl = NULL; | |
| 1782 | − Dwarf_Addr _addr, baseaddr, bias = 0; | |
| 1254 | + Dwarf_Addr _addr, baseaddr; | |
| 1783 | 1255 | const char *fname = NULL, *func = NULL, *tmp; |
| 1784 | 1256 | int baseline = 0, lineno = 0, ret = 0; |
| 1785 | 1257 | |
| 1786 | − /* Open the live linux kernel */ | |
| 1787 | − dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias); | |
| 1788 | − if (!dbg) { | |
| 1789 | − pr_warning("No debug information found in the vmlinux - " | |
| 1790 | − "please rebuild with CONFIG_DEBUG_INFO=y.\n"); | |
| 1791 | − ret = -EINVAL; | |
| 1792 | − goto end; | |
| 1793 | − } | |
| 1794 | − | |
| 1795 | 1258 | /* Adjust address with bias */ |
| 1796 | − addr += bias; | |
| 1259 | + addr += self->bias; | |
| 1260 | + | |
| 1797 | 1261 | /* Find cu die */ |
| 1798 | − if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) { | |
| 1262 | + if (!dwarf_addrdie(self->dbg, (Dwarf_Addr)addr - self->bias, &cudie)) { | |
| 1799 | 1263 | pr_warning("Failed to find debug information for address %lx\n", |
| 1800 | 1264 | addr); |
| 1801 | 1265 | ret = -EINVAL; |
| @@ -1807,7 +1271,7 @@ int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt) · 5 unmodified lines | ||
| 1807 | 1271 | /* Don't care whether it failed or not */ |
| 1808 | 1272 | |
| 1809 | 1273 | /* Find a corresponding function (name, baseline and baseaddr) */ |
| 1810 | − if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) { | |
| 1274 | + if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) { | |
| 1811 | 1275 | /* Get function entry information */ |
| 1812 | 1276 | tmp = dwarf_diename(&spdie); |
| 1813 | 1277 | if (!tmp || |
| @@ -1871,8 +1335,6 @@ post: · 57 unmodified lines | ||
| 1871 | 1335 | } |
| 1872 | 1336 | } |
| 1873 | 1337 | end: |
| 1874 | − if (dwfl) | |
| 1875 | − dwfl_end(dwfl); | |
| 1876 | 1338 | if (ret == 0 && (fname || func)) |
| 1877 | 1339 | ret = 1; /* Found a point */ |
| 1878 | 1340 | return ret; |
| @@ -1982,26 +1444,15 @@ static int find_line_range_by_func(struct line_finder *lf) · 103 unmodified lines | ||
| 1982 | 1444 | return param.retval; |
| 1983 | 1445 | } |
| 1984 | 1446 | |
| 1985 | −int find_line_range(int fd, struct line_range *lr) | |
| 1447 | +int debuginfo__find_line_range(struct debuginfo *self, struct line_range *lr) | |
| 1986 | 1448 | { |
| 1987 | 1449 | struct line_finder lf = {.lr = lr, .found = 0}; |
| 1988 | 1450 | int ret = 0; |
| 1989 | 1451 | Dwarf_Off off = 0, noff; |
| 1990 | 1452 | size_t cuhl; |
| 1991 | 1453 | Dwarf_Die *diep; |
| 1992 | − Dwarf *dbg = NULL; | |
| 1993 | − Dwfl *dwfl; | |
| 1994 | − Dwarf_Addr bias; /* Currently ignored */ | |
| 1995 | 1454 | const char *comp_dir; |
| 1996 | 1455 | |
| 1997 | − dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias); | |
| 1998 | − if (!dbg) { | |
| 1999 | − pr_warning("No debug information found in the vmlinux - " | |
| 2000 | − "please rebuild with CONFIG_DEBUG_INFO=y.\n"); | |
| 2001 | − close(fd); /* Without dwfl_end(), fd isn't closed. */ | |
| 2002 | − return -EBADF; | |
| 2003 | − } | |
| 2004 | − | |
| 2005 | 1456 | /* Fastpath: lookup by function name from .debug_pubnames section */ |
| 2006 | 1457 | if (lr->function) { |
| 2007 | 1458 | struct pubname_callback_param pubname_param = { |
| @@ -2010,7 +1461,8 @@ int find_line_range(int fd, struct line_range *lr) · 2 unmodified lines | ||
| 2010 | 1461 | struct dwarf_callback_param line_range_param = { |
| 2011 | 1462 | .data = (void *)&lf, .retval = 0}; |
| 2012 | 1463 | |
| 2013 | − dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0); | |
| 1464 | + dwarf_getpubnames(self->dbg, pubname_search_cb, | |
| 1465 | + &pubname_param, 0); | |
| 2014 | 1466 | if (pubname_param.found) { |
| 2015 | 1467 | line_range_search_cb(&lf.sp_die, &line_range_param); |
| 2016 | 1468 | if (lf.found) |
| @@ -2020,11 +1472,12 @@ int find_line_range(int fd, struct line_range *lr) · 3 unmodified lines | ||
| 2020 | 1472 | |
| 2021 | 1473 | /* Loop on CUs (Compilation Unit) */ |
| 2022 | 1474 | while (!lf.found && ret >= 0) { |
| 2023 | − if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0) | |
| 1475 | + if (dwarf_nextcu(self->dbg, off, &noff, &cuhl, | |
| 1476 | + NULL, NULL, NULL) != 0) | |
| 2024 | 1477 | break; |
| 2025 | 1478 | |
| 2026 | 1479 | /* Get the DIE(Debugging Information Entry) of this CU */ |
| 2027 | − diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die); | |
| 1480 | + diep = dwarf_offdie(self->dbg, off + cuhl, &lf.cu_die); | |
| 2028 | 1481 | if (!diep) |
| 2029 | 1482 | continue; |
| 2030 | 1483 | |
| @@ -2058,7 +1511,6 @@ found: · 27 unmodified lines | ||
| 2058 | 1511 | } |
| 2059 | 1512 | |
| 2060 | 1513 | pr_debug("path: %s\n", lr->path); |
| 2061 | − dwfl_end(dwfl); | |
| 2062 | 1514 | return (ret < 0) ? ret : lf.found; |
| 2063 | 1515 | } |
| 2064 | 1516 | |
| @@ -16,27 +16,42 @@ static inline int is_c_varname(const char *name) · 15 unmodified lines | ||
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | #ifdef DWARF_SUPPORT |
| 19 | + | |
| 20 | +#include "dwarf-aux.h" | |
| 21 | + | |
| 22 | +/* TODO: export debuginfo data structure even if no dwarf support */ | |
| 23 | + | |
| 24 | +/* debug information structure */ | |
| 25 | +struct debuginfo { | |
| 26 | + Dwarf *dbg; | |
| 27 | + Dwfl *dwfl; | |
| 28 | + Dwarf_Addr bias; | |
| 29 | +}; | |
| 30 | + | |
| 31 | +extern struct debuginfo *debuginfo__new(const char *path); | |
| 32 | +extern struct debuginfo *debuginfo__new_online_kernel(unsigned long addr); | |
| 33 | +extern void debuginfo__delete(struct debuginfo *self); | |
| 34 | + | |
| 19 | 35 | /* Find probe_trace_events specified by perf_probe_event from debuginfo */ |
| 20 | −extern int find_probe_trace_events(int fd, struct perf_probe_event *pev, | |
| 21 | − struct probe_trace_event **tevs, | |
| 22 | − int max_tevs); | |
| 36 | +extern int debuginfo__find_trace_events(struct debuginfo *self, | |
| 37 | + struct perf_probe_event *pev, | |
| 38 | + struct probe_trace_event **tevs, | |
| 39 | + int max_tevs); | |
| 23 | 40 | |
| 24 | 41 | /* Find a perf_probe_point from debuginfo */ |
| 25 | −extern int find_perf_probe_point(unsigned long addr, | |
| 26 | − struct perf_probe_point *ppt); | |
| 42 | +extern int debuginfo__find_probe_point(struct debuginfo *self, | |
| 43 | + unsigned long addr, | |
| 44 | + struct perf_probe_point *ppt); | |
| 27 | 45 | |
| 28 | 46 | /* Find a line range */ |
| 29 | −extern int find_line_range(int fd, struct line_range *lr); | |
| 47 | +extern int debuginfo__find_line_range(struct debuginfo *self, | |
| 48 | + struct line_range *lr); | |
| 30 | 49 | |
| 31 | 50 | /* Find available variables */ |
| 32 | −extern int find_available_vars_at(int fd, struct perf_probe_event *pev, | |
| 33 | − struct variable_list **vls, int max_points, | |
| 34 | − bool externs); | |
| 35 | − | |
| 36 | −#include <dwarf.h> | |
| 37 | −#include <elfutils/libdw.h> | |
| 38 | −#include <elfutils/libdwfl.h> | |
| 39 | −#include <elfutils/version.h> | |
| 51 | +extern int debuginfo__find_available_vars_at(struct debuginfo *self, | |
| 52 | + struct perf_probe_event *pev, | |
| 53 | + struct variable_list **vls, | |
| 54 | + int max_points, bool externs); | |
| 40 | 55 | |
| 41 | 56 | struct probe_finder { |
| 42 | 57 | struct perf_probe_event *pev; /* Target probe event */ |
| @@ -294,3 +294,22 @@ bool strlazymatch(const char *str, const char *pat) · 293 unmodified lines | ||
| 294 | 294 | { |
| 295 | 295 | return __match_glob(str, pat, true); |
| 296 | 296 | } |
| 297 | + | |
| 298 | +/** | |
| 299 | + * strtailcmp - Compare the tail of two strings | |
| 300 | + * @s1: 1st string to be compared | |
| 301 | + * @s2: 2nd string to be compared | |
| 302 | + * | |
| 303 | + * Return 0 if whole of either string is same as another's tail part. | |
| 304 | + */ | |
| 305 | +int strtailcmp(const char *s1, const char *s2) | |
| 306 | +{ | |
| 307 | + int i1 = strlen(s1); | |
| 308 | + int i2 = strlen(s2); | |
| 309 | + while (--i1 >= 0 && --i2 >= 0) { | |
| 310 | + if (s1[i1] != s2[i2]) | |
| 311 | + return s1[i1] - s2[i2]; | |
| 312 | + } | |
| 313 | + return 0; | |
| 314 | +} | |
| 315 | + | |
| @@ -183,106 +183,59 @@ int bigendian(void) · 182 unmodified lines | ||
| 183 | 183 | return *ptr == 0x01020304; |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | −static unsigned long long copy_file_fd(int fd) | |
| 186 | +/* unfortunately, you can not stat debugfs or proc files for size */ | |
| 187 | +static void record_file(const char *file, size_t hdr_sz) | |
| 187 | 188 | { |
| 188 | 189 | unsigned long long size = 0; |
| 189 | − char buf[BUFSIZ]; | |
| 190 | − int r; | |
| 191 | − | |
| 192 | − do { | |
| 193 | − r = read(fd, buf, BUFSIZ); | |
| 194 | − if (r > 0) { | |
| 195 | − size += r; | |
| 196 | − write_or_die(buf, r); | |
| 197 | − } | |
| 198 | − } while (r > 0); | |
| 199 | − | |
| 200 | − return size; | |
| 201 | −} | |
| 202 | − | |
| 203 | −static unsigned long long copy_file(const char *file) | |
| 204 | −{ | |
| 205 | − unsigned long long size = 0; | |
| 206 | − int fd; | |
| 190 | + char buf[BUFSIZ], *sizep; | |
| 191 | + off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR); | |
| 192 | + int r, fd; | |
| 207 | 193 | |
| 208 | 194 | fd = open(file, O_RDONLY); |
| 209 | 195 | if (fd < 0) |
| 210 | 196 | die("Can't read '%s'", file); |
| 211 | − size = copy_file_fd(fd); | |
| 212 | − close(fd); | |
| 213 | 197 | |
| 214 | − return size; | |
| 215 | −} | |
| 216 | − | |
| 217 | −static unsigned long get_size_fd(int fd) | |
| 218 | −{ | |
| 219 | − unsigned long long size = 0; | |
| 220 | − char buf[BUFSIZ]; | |
| 221 | − int r; | |
| 198 | + /* put in zeros for file size, then fill true size later */ | |
| 199 | + write_or_die(&size, hdr_sz); | |
| 222 | 200 | |
| 223 | 201 | do { |
| 224 | 202 | r = read(fd, buf, BUFSIZ); |
| 225 | − if (r > 0) | |
| 203 | + if (r > 0) { | |
| 226 | 204 | size += r; |
| 205 | + write_or_die(buf, r); | |
| 206 | + } | |
| 227 | 207 | } while (r > 0); |
| 228 | − | |
| 229 | − lseek(fd, 0, SEEK_SET); | |
| 230 | − | |
| 231 | − return size; | |
| 232 | −} | |
| 233 | − | |
| 234 | −static unsigned long get_size(const char *file) | |
| 235 | −{ | |
| 236 | − unsigned long long size = 0; | |
| 237 | − int fd; | |
| 238 | − | |
| 239 | − fd = open(file, O_RDONLY); | |
| 240 | − if (fd < 0) | |
| 241 | − die("Can't read '%s'", file); | |
| 242 | − size = get_size_fd(fd); | |
| 243 | 208 | close(fd); |
| 244 | 209 | |
| 245 | − return size; | |
| 210 | + /* ugh, handle big-endian hdr_size == 4 */ | |
| 211 | + sizep = (char*)&size; | |
| 212 | + if (bigendian()) | |
| 213 | + sizep += sizeof(u64) - hdr_sz; | |
| 214 | + | |
| 215 | + if (pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0) | |
| 216 | + die("writing to %s", output_file); | |
| 246 | 217 | } |
| 247 | 218 | |
| 248 | 219 | static void read_header_files(void) |
| 249 | 220 | { |
| 250 | − unsigned long long size, check_size; | |
| 251 | 221 | char *path; |
| 252 | − int fd; | |
| 222 | + struct stat st; | |
| 253 | 223 | |
| 254 | 224 | path = get_tracing_file("events/header_page"); |
| 255 | − fd = open(path, O_RDONLY); | |
| 256 | − if (fd < 0) | |
| 225 | + if (stat(path, &st) < 0) | |
| 257 | 226 | die("can't read '%s'", path); |
| 258 | 227 | |
| 259 | − /* unfortunately, you can not stat debugfs files for size */ | |
| 260 | − size = get_size_fd(fd); | |
| 261 | − | |
| 262 | 228 | write_or_die("header_page", 12); |
| 263 | − write_or_die(&size, 8); | |
| 264 | − check_size = copy_file_fd(fd); | |
| 265 | − close(fd); | |
| 266 | − | |
| 267 | − if (size != check_size) | |
| 268 | − die("wrong size for '%s' size=%lld read=%lld", | |
| 269 | − path, size, check_size); | |
| 229 | + record_file(path, 8); | |
| 270 | 230 | put_tracing_file(path); |
| 271 | 231 | |
| 272 | 232 | path = get_tracing_file("events/header_event"); |
| 273 | − fd = open(path, O_RDONLY); | |
| 274 | − if (fd < 0) | |
| 233 | + if (stat(path, &st) < 0) | |
| 275 | 234 | die("can't read '%s'", path); |
| 276 | 235 | |
| 277 | − size = get_size_fd(fd); | |
| 278 | − | |
| 279 | 236 | write_or_die("header_event", 13); |
| 280 | − write_or_die(&size, 8); | |
| 281 | − check_size = copy_file_fd(fd); | |
| 282 | − if (size != check_size) | |
| 283 | − die("wrong size for '%s'", path); | |
| 237 | + record_file(path, 8); | |
| 284 | 238 | put_tracing_file(path); |
| 285 | − close(fd); | |
| 286 | 239 | } |
| 287 | 240 | |
| 288 | 241 | static bool name_in_tp_list(char *sys, struct tracepoint_path *tps) |
| @@ -298,7 +251,6 @@ static bool name_in_tp_list(char *sys, struct tracepoint_path *tps) · 9 unmodified lines | ||
| 298 | 251 | |
| 299 | 252 | static void copy_event_system(const char *sys, struct tracepoint_path *tps) |
| 300 | 253 | { |
| 301 | − unsigned long long size, check_size; | |
| 302 | 254 | struct dirent *dent; |
| 303 | 255 | struct stat st; |
| 304 | 256 | char *format; |
| @@ -338,14 +290,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps) · 33 unmodified lines | ||
| 338 | 290 | sprintf(format, "%s/%s/format", sys, dent->d_name); |
| 339 | 291 | ret = stat(format, &st); |
| 340 | 292 | |
| 341 | − if (ret >= 0) { | |
| 342 | − /* unfortunately, you can not stat debugfs files for size */ | |
| 343 | − size = get_size(format); | |
| 344 | − write_or_die(&size, 8); | |
| 345 | − check_size = copy_file(format); | |
| 346 | − if (size != check_size) | |
| 347 | − die("error in size of file '%s'", format); | |
| 348 | − } | |
| 293 | + if (ret >= 0) | |
| 294 | + record_file(format, 8); | |
| 349 | 295 | |
| 350 | 296 | free(format); |
| 351 | 297 | } |
| @@ -426,7 +372,7 @@ static void read_event_files(struct tracepoint_path *tps) · 74 unmodified lines | ||
| 426 | 372 | |
| 427 | 373 | static void read_proc_kallsyms(void) |
| 428 | 374 | { |
| 429 | − unsigned int size, check_size; | |
| 375 | + unsigned int size; | |
| 430 | 376 | const char *path = "/proc/kallsyms"; |
| 431 | 377 | struct stat st; |
| 432 | 378 | int ret; |
| @@ -438,17 +384,12 @@ static void read_proc_kallsyms(void) · 5 unmodified lines | ||
| 438 | 384 | write_or_die(&size, 4); |
| 439 | 385 | return; |
| 440 | 386 | } |
| 441 | − size = get_size(path); | |
| 442 | − write_or_die(&size, 4); | |
| 443 | − check_size = copy_file(path); | |
| 444 | − if (size != check_size) | |
| 445 | − die("error in size of file '%s'", path); | |
| 446 | − | |
| 387 | + record_file(path, 4); | |
| 447 | 388 | } |
| 448 | 389 | |
| 449 | 390 | static void read_ftrace_printk(void) |
| 450 | 391 | { |
| 451 | − unsigned int size, check_size; | |
| 392 | + unsigned int size; | |
| 452 | 393 | char *path; |
| 453 | 394 | struct stat st; |
| 454 | 395 | int ret; |
| @@ -461,11 +402,8 @@ static void read_ftrace_printk(void) · 6 unmodified lines | ||
| 461 | 402 | write_or_die(&size, 4); |
| 462 | 403 | goto out; |
| 463 | 404 | } |
| 464 | − size = get_size(path); | |
| 465 | − write_or_die(&size, 4); | |
| 466 | − check_size = copy_file(path); | |
| 467 | − if (size != check_size) | |
| 468 | − die("error in size of file '%s'", path); | |
| 405 | + record_file(path, 4); | |
| 406 | + | |
| 469 | 407 | out: |
| 470 | 408 | put_tracing_file(path); |
| 471 | 409 | } |
| @@ -238,6 +238,7 @@ char **argv_split(const char *str, int *argcp); · 237 unmodified lines | ||
| 238 | 238 | void argv_free(char **argv); |
| 239 | 239 | bool strglobmatch(const char *str, const char *pat); |
| 240 | 240 | bool strlazymatch(const char *str, const char *pat); |
| 241 | +int strtailcmp(const char *s1, const char *s2); | |
| 241 | 242 | unsigned long convert_unit(unsigned long value, char *unit); |
| 242 | 243 | int readn(int fd, void *buf, size_t size); |
| 243 | 244 | |