History

Merge branch 'tip/perf/core' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-2.6-trace into perf/core

Ingo Molnar committed 15 years ago

Commit 40bcea72 parents 492f73a 14a8fd726 files · +1663 −1003Browse files

Unified · Split

26 files
  1. Documentation/trace/kprobetrace.txt +5 −4
  2. arch/x86/include/asm/perf_event_p4.h +33 −0
  3. arch/x86/kernel/cpu/perf_event.c +0 −7
  4. arch/x86/kernel/cpu/perf_event_p4.c +106 −29
  5. include/linux/ftrace_event.h +1 −0
  6. kernel/kprobes.c +23 −10
  7. kernel/trace/ftrace.c +74 −21
  8. kernel/trace/trace.c +79 −13
  9. kernel/trace/trace.h +34 −16
  10. kernel/trace/trace_entries.h +2 −1
  11. kernel/trace/trace_functions_graph.c +1 −1
  12. kernel/trace/trace_kprobe.c +223 −95
  13. kernel/trace/trace_output.c +6 −5
  14. kernel/watchdog.c +0 −2
  15. tools/perf/Documentation/perf-probe.txt +4 −2
  16. tools/perf/Makefile +2 −0
  17. tools/perf/builtin-probe.c +2 −1
  18. tools/perf/util/dwarf-aux.c +663 −0
  19. tools/perf/util/dwarf-aux.h +100 −0
  20. tools/perf/util/probe-event.c +124 −41
  21. tools/perf/util/probe-event.h +1 −0
  22. tools/perf/util/probe-finder.c +102 −650
  23. tools/perf/util/probe-finder.h +29 −14
  24. tools/perf/util/string.c +19 −0
  25. tools/perf/util/trace-event-info.c +29 −91
  26. tools/perf/util/util.h +1 −0
Documentation/trace/kprobetrace.txt+5−4
@@ -22,14 +22,15 @@ current_tracer. Instead of that, add probe points via · 21 unmodified lines
2222
2323 Synopsis of kprobe_events
2424 -------------------------
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
2727 -:[GRP/]EVENT : Clear a probe
2828
2929 GRP : Group name. If omitted, use "kprobes" for it.
3030 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.
3334 MEMADDR : Address where the probe is inserted.
3435
3536 FETCHARGS : Arguments. Each probe can have up to 128 args.
arch/x86/include/asm/perf_event_p4.h+33−0
@@ -101,6 +101,14 @@ · 100 unmodified lines
101101 #define P4_CONFIG_HT_SHIFT 63
102102 #define P4_CONFIG_HT (1ULL << P4_CONFIG_HT_SHIFT)
103103
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+
104112 /*
105113 * The bits we allow to pass for RAW events
106114 */
@@ -123,6 +131,31 @@ · 16 unmodified lines
123131 (p4_config_pack_escr(P4_CONFIG_MASK_ESCR)) | \
124132 (p4_config_pack_cccr(P4_CONFIG_MASK_CCCR))
125133
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+
126159 static inline bool p4_is_event_cascaded(u64 config)
127160 {
128161 u32 cccr = p4_config_unpack_cccr(config);
arch/x86/kernel/cpu/perf_event.c+0−7
@@ -274,7 +274,6 @@ struct x86_pmu { · 273 unmodified lines
274274 void (*enable_all)(int added);
275275 void (*enable)(struct perf_event *);
276276 void (*disable)(struct perf_event *);
277− void (*hw_watchdog_set_attr)(struct perf_event_attr *attr);
278277 int (*hw_config)(struct perf_event *event);
279278 int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
280279 unsigned eventsel;
@@ -360,12 +359,6 @@ static u64 __read_mostly hw_cache_extra_regs · 79 unmodified lines
360359 [PERF_COUNT_HW_CACHE_OP_MAX]
361360 [PERF_COUNT_HW_CACHE_RESULT_MAX];
362361
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−
369362 /*
370363 * Propagate event elapsed time into the generic event.
371364 * Can only be executed on the CPU where the event is active.
arch/x86/kernel/cpu/perf_event_p4.c+106−29
@@ -570,11 +570,92 @@ static __initconst const u64 p4_hw_cache_event_ids · 569 unmodified lines
570570 },
571571 };
572572
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+
573653 static u64 p4_general_events[PERF_COUNT_HW_MAX] = {
574654 /* non-halted CPU clocks */
575655 [PERF_COUNT_HW_CPU_CYCLES] =
576656 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,
578659
579660 /*
580661 * retired instructions
@@ -719,31 +800,6 @@ static int p4_validate_raw_event(struct perf_event *event) · 138 unmodified lines
719800 return 0;
720801 }
721802
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−
747803 static int p4_hw_config(struct perf_event *event)
748804 {
749805 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
11591215 struct p4_event_bind *bind;
11601216 unsigned int i, thread, num;
11611217 int cntr_idx, escr_idx;
1218+ u64 config_alias;
1219+ int pass;
11621220
11631221 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
11641222 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
11671225
11681226 hwc = &cpuc->event_list[i]->hw;
11691227 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+
11701239 bind = p4_config_get_bind(hwc->config);
11711240 escr_idx = p4_get_escr_idx(bind->escr_msr[thread]);
11721241 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
11801249 }
11811250
11821251 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+ }
11851263
11861264 p4_pmu_swap_config_ts(hwc, cpu);
11871265 if (assign)
@@ -1218,7 +1296,6 @@ static __initconst const struct x86_pmu p4_pmu = { · 30 unmodified lines
12181296 .cntval_bits = ARCH_P4_CNTRVAL_BITS,
12191297 .cntval_mask = ARCH_P4_CNTRVAL_MASK,
12201298 .max_period = (1ULL << (ARCH_P4_CNTRVAL_BITS - 1)) - 1,
1221− .hw_watchdog_set_attr = p4_hw_watchdog_set_attr,
12221299 .hw_config = p4_hw_config,
12231300 .schedule_events = p4_pmu_schedule_events,
12241301 /*
include/linux/ftrace_event.h+1−0
@@ -76,6 +76,7 @@ struct trace_iterator { · 75 unmodified lines
7676 struct trace_entry *ent;
7777 unsigned long lost_events;
7878 int leftover;
79+ int ent_size;
7980 int cpu;
8081 u64 ts;
8182
kernel/kprobes.c+23−10
@@ -1255,19 +1255,29 @@ static int __kprobes in_kprobes_functions(unsigned long addr) · 1254 unmodified lines
12551255 /*
12561256 * If we have a symbol_name argument, look it up and add the offset field
12571257 * 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.
12581260 */
12591261 static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
12601262 {
12611263 kprobe_opcode_t *addr = p->addr;
1264+
1265+ if ((p->symbol_name && p->addr) ||
1266+ (!p->symbol_name && !p->addr))
1267+ goto invalid;
1268+
12621269 if (p->symbol_name) {
1263− if (addr)
1264− return NULL;
12651270 kprobe_lookup_name(p->symbol_name, addr);
1271+ if (!addr)
1272+ return ERR_PTR(-ENOENT);
12661273 }
12671274
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);
12711281 }
12721282
12731283 /* 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
13111321 kprobe_opcode_t *addr;
13121322
13131323 addr = kprobe_addr(p);
1314− if (!addr)
1315− return -EINVAL;
1324+ if (IS_ERR(addr))
1325+ return PTR_ERR(addr);
13161326 p->addr = addr;
13171327
13181328 ret = check_kprobe_rereg(p);
@@ -1335,6 +1345,8 @@ int __kprobes register_kprobe(struct kprobe *p) · 16 unmodified lines
13351345 */
13361346 probed_mod = __module_text_address((unsigned long) p->addr);
13371347 if (probed_mod) {
1348+ /* Return -ENOENT if fail. */
1349+ ret = -ENOENT;
13381350 /*
13391351 * We must hold a refcount of the probed module while updating
13401352 * its code to prohibit unexpected unloading.
@@ -1351,6 +1363,7 @@ int __kprobes register_kprobe(struct kprobe *p) · 10 unmodified lines
13511363 module_put(probed_mod);
13521364 goto fail_with_jump_label;
13531365 }
1366+ /* ret will be updated by following code */
13541367 }
13551368 preempt_enable();
13561369 jump_label_unlock();
@@ -1399,7 +1412,7 @@ out: · 42 unmodified lines
13991412 fail_with_jump_label:
14001413 preempt_enable();
14011414 jump_label_unlock();
1402− return -EINVAL;
1415+ return ret;
14031416 }
14041417 EXPORT_SYMBOL_GPL(register_kprobe);
14051418
@@ -1686,8 +1699,8 @@ int __kprobes register_kretprobe(struct kretprobe *rp) · 280 unmodified lines
16861699
16871700 if (kretprobe_blacklist_size) {
16881701 addr = kprobe_addr(&rp->kp);
1689− if (!addr)
1690− return -EINVAL;
1702+ if (IS_ERR(addr))
1703+ return PTR_ERR(addr);
16911704
16921705 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
16931706 if (kretprobe_blacklist[i].addr == addr)
kernel/trace/ftrace.c+74−21
@@ -88,6 +88,7 @@ static struct ftrace_ops ftrace_list_end __read_mostly = { · 87 unmodified lines
8888 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
8989 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
9090 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
91+static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
9192 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
9293 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
9394 static struct ftrace_ops global_ops;
@@ -146,9 +147,11 @@ void clear_ftrace_function(void) · 52 unmodified lines
146147 {
147148 ftrace_trace_function = ftrace_stub;
148149 __ftrace_trace_function = ftrace_stub;
150+ __ftrace_trace_function_delay = ftrace_stub;
149151 ftrace_pid_function = ftrace_stub;
150152 }
151153
154+#undef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
152155 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
153156 /*
154157 * 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
207210
208211 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
209212 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;
210217 #else
211218 __ftrace_trace_function = func;
219+#endif
212220 ftrace_trace_function = ftrace_test_stop_func;
213221 #endif
214222 }
@@ -1170,8 +1178,14 @@ alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) · 955 unmodified lines
11701178 return NULL;
11711179 }
11721180
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+
11731186 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)
11751189 {
11761190 struct ftrace_func_entry *entry;
11771191 struct hlist_node *tp, *tn;
@@ -1181,8 +1195,15 @@ ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src) · 3 unmodified lines
11811195 unsigned long key;
11821196 int size = src->count;
11831197 int bits = 0;
1198+ int ret;
11841199 int i;
11851200
1201+ /*
1202+ * Remove the current set, update the hash and add
1203+ * them back.
1204+ */
1205+ ftrace_hash_rec_disable(ops, enable);
1206+
11861207 /*
11871208 * If the new source is empty, just free dst and assign it
11881209 * the empty_hash.
@@ -1203,9 +1224,10 @@ ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src) · 14 unmodified lines
12031224 if (bits > FTRACE_HASH_MAX_BITS)
12041225 bits = FTRACE_HASH_MAX_BITS;
12051226
1227+ ret = -ENOMEM;
12061228 new_hash = alloc_ftrace_hash(bits);
12071229 if (!new_hash)
1208− return -ENOMEM;
1230+ goto out;
12091231
12101232 size = 1 << src->size_bits;
12111233 for (i = 0; i < size; i++) {
@@ -1224,7 +1246,16 @@ ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src) · 12 unmodified lines
12241246 rcu_assign_pointer(*dst, new_hash);
12251247 free_ftrace_hash_rcu(old_hash);
12261248
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;
12281259 }
12291260
12301261 /*
@@ -1584,6 +1615,12 @@ static int __ftrace_modify_code(void *data) · 353 unmodified lines
15841615 {
15851616 int *command = data;
15861617
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+
15871624 if (*command & FTRACE_ENABLE_CALLS)
15881625 ftrace_replace_code(1);
15891626 else if (*command & FTRACE_DISABLE_CALLS)
@@ -1597,6 +1634,18 @@ static int __ftrace_modify_code(void *data) · 7 unmodified lines
15971634 else if (*command & FTRACE_STOP_FUNC_RET)
15981635 ftrace_disable_ftrace_graph_caller();
15991636
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+
16001649 return 0;
16011650 }
16021651
@@ -2865,7 +2914,11 @@ ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, · 1262 unmodified lines
28652914 ftrace_match_records(hash, buf, len);
28662915
28672916 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+
28692922 mutex_unlock(&ftrace_lock);
28702923
28712924 mutex_unlock(&ftrace_regex_lock);
@@ -3048,18 +3101,12 @@ ftrace_regex_release(struct inode *inode, struct file *file) · 176 unmodified lines
30483101 orig_hash = &iter->ops->notrace_hash;
30493102
30503103 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+
30633110 mutex_unlock(&ftrace_lock);
30643111 }
30653112 free_ftrace_hash(iter->hash);
@@ -3338,7 +3385,7 @@ static int ftrace_process_locs(struct module *mod, · 272 unmodified lines
33383385 {
33393386 unsigned long *p;
33403387 unsigned long addr;
3341− unsigned long flags;
3388+ unsigned long flags = 0; /* Shut up gcc */
33423389
33433390 mutex_lock(&ftrace_lock);
33443391 p = start;
@@ -3356,12 +3403,18 @@ static int ftrace_process_locs(struct module *mod, · 11 unmodified lines
33563403 }
33573404
33583405 /*
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.
33613412 */
3362− local_irq_save(flags);
3413+ if (!mod)
3414+ local_irq_save(flags);
33633415 ftrace_update_code(mod);
3364− local_irq_restore(flags);
3416+ if (!mod)
3417+ local_irq_restore(flags);
33653418 mutex_unlock(&ftrace_lock);
33663419
33673420 return 0;
kernel/trace/trace.c+79−13
@@ -1248,6 +1248,15 @@ ftrace(struct trace_array *tr, struct trace_array_cpu *data, · 1247 unmodified lines
12481248 }
12491249
12501250 #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+
12511260 static void __ftrace_trace_stack(struct ring_buffer *buffer,
12521261 unsigned long flags,
12531262 int skip, int pc, struct pt_regs *regs)
@@ -1256,25 +1265,77 @@ static void __ftrace_trace_stack(struct ring_buffer *buffer, · 2 unmodified lines
12561265 struct ring_buffer_event *event;
12571266 struct stack_entry *entry;
12581267 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);
12591307
12601308 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1261− sizeof(*entry), flags, pc);
1309+ sizeof(*entry) + size, flags, pc);
12621310 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);
12661313
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;
12711329
1272− if (regs)
1273− save_stack_trace_regs(regs, &trace);
1274− else
1275− save_stack_trace(&trace);
12761330 if (!filter_check_discard(call, entry, buffer, event))
12771331 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+
12781339 }
12791340
12801341 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
15621623
15631624 ftrace_enable_cpu();
15641625
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;
15661632 }
15671633
15681634 static struct trace_entry *
kernel/trace/trace.h+34−16
@@ -278,6 +278,29 @@ struct tracer { · 277 unmodified lines
278278 };
279279
280280
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+
281304 #define TRACE_PIPE_ALL_CPU -1
282305
283306 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
516539 return 1;
517540
518541 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);
520552 return 1;
553+ }
521554 }
522555
523556 return 0;
@@ -795,19 +828,4 @@ extern const char *__stop___trace_bprintk_fmt[]; · 271 unmodified lines
795828 FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print))
796829 #include "trace_entries.h"
797830
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−
813831 #endif /* _LINUX_KERNEL_TRACE_H */
kernel/trace/trace_entries.h+2−1
@@ -161,7 +161,8 @@ FTRACE_ENTRY(kernel_stack, stack_entry, · 160 unmodified lines
161161 TRACE_STACK,
162162
163163 F_STRUCT(
164− __array( unsigned long, caller, FTRACE_STACK_ENTRIES )
164+ __field( int, size )
165+ __dynamic_array(unsigned long, caller )
165166 ),
166167
167168 F_printk("\t=> (%08lx)\n\t=> (%08lx)\n\t=> (%08lx)\n\t=> (%08lx)\n"
kernel/trace/trace_functions_graph.c+1−1
@@ -227,7 +227,7 @@ int __trace_graph_entry(struct trace_array *tr, · 226 unmodified lines
227227
228228 static inline int ftrace_graph_ignore_irqs(void)
229229 {
230− if (!ftrace_graph_skip_irqs)
230+ if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))
231231 return 0;
232232
233233 return in_irq();
kernel/trace/trace_kprobe.c+223−95
@@ -343,6 +343,14 @@ DEFINE_BASIC_FETCH_FUNCS(deref) · 342 unmodified lines
343343 DEFINE_FETCH_deref(string)
344344 DEFINE_FETCH_deref(string_size)
345345
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+
346354 static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
347355 {
348356 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
@@ -376,6 +384,19 @@ DEFINE_BASIC_FETCH_FUNCS(bitfield) · 27 unmodified lines
376384 #define fetch_bitfield_string NULL
377385 #define fetch_bitfield_string_size NULL
378386
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+
379400 static __kprobes void
380401 free_bitfield_fetch_param(struct bitfield_fetch_param *data)
381402 {
@@ -389,6 +410,7 @@ free_bitfield_fetch_param(struct bitfield_fetch_param *data) · 7 unmodified lines
389410 free_symbol_cache(data->orig.data);
390411 kfree(data);
391412 }
413+
392414 /* Default (unsigned long) fetch type */
393415 #define __DEFAULT_FETCH_TYPE(t) u##t
394416 #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
@@ -536,6 +558,7 @@ struct probe_arg { · 141 unmodified lines
536558 /* Flags for trace_probe */
537559 #define TP_FLAG_TRACE 1
538560 #define TP_FLAG_PROFILE 2
561+#define TP_FLAG_REGISTERED 4
539562
540563 struct trace_probe {
541564 struct list_head list;
@@ -555,16 +578,49 @@ struct trace_probe { · 13 unmodified lines
555578 (sizeof(struct probe_arg) * (n)))
556579
557580
558−static __kprobes int probe_is_return(struct trace_probe *tp)
581+static __kprobes int trace_probe_is_return(struct trace_probe *tp)
559582 {
560583 return tp->rp.handler != NULL;
561584 }
562585
563−static __kprobes const char *probe_symbol(struct trace_probe *tp)
586+static __kprobes const char *trace_probe_symbol(struct trace_probe *tp)
564587 {
565588 return tp->symbol ? tp->symbol : "unknown";
566589 }
567590
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+
568624 static int register_probe_event(struct trace_probe *tp);
569625 static void unregister_probe_event(struct trace_probe *tp);
570626
@@ -646,6 +702,16 @@ error: · 75 unmodified lines
646702 return ERR_PTR(ret);
647703 }
648704
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+
649715 static void free_probe_arg(struct probe_arg *arg)
650716 {
651717 if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
@@ -671,7 +737,7 @@ static void free_trace_probe(struct trace_probe *tp) · 19 unmodified lines
671737 kfree(tp);
672738 }
673739
674−static struct trace_probe *find_probe_event(const char *event,
740+static struct trace_probe *find_trace_probe(const char *event,
675741 const char *group)
676742 {
677743 struct trace_probe *tp;
@@ -683,13 +749,96 @@ static struct trace_probe *find_probe_event(const char *event, · 5 unmodified lines
683749 return NULL;
684750 }
685751
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+
686838 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
687839 static void unregister_trace_probe(struct trace_probe *tp)
688840 {
689− if (probe_is_return(tp))
690− unregister_kretprobe(&tp->rp);
691− else
692− unregister_kprobe(&tp->rp.kp);
841+ __unregister_trace_probe(tp);
693842 list_del(&tp->list);
694843 unregister_probe_event(tp);
695844 }
@@ -702,41 +851,65 @@ static int register_trace_probe(struct trace_probe *tp) · 6 unmodified lines
702851
703852 mutex_lock(&probe_lock);
704853
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);
707856 if (old_tp) {
708− /* delete old event */
709857 unregister_trace_probe(old_tp);
710858 free_trace_probe(old_tp);
711859 }
860+
861+ /* Register new event */
712862 ret = register_probe_event(tp);
713863 if (ret) {
714864 pr_warning("Failed to register probe event(%d)\n", ret);
715865 goto end;
716866 }
717867
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)
732871 unregister_probe_event(tp);
733− } else
872+ else
734873 list_add_tail(&tp->list, &probe_list);
874+
735875 end:
736876 mutex_unlock(&probe_lock);
737877 return ret;
738878 }
739879
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+
740913 /* Split symbol and offset. */
741914 static int split_symbol_offset(char *symbol, unsigned long *offset)
742915 {
@@ -962,8 +1135,8 @@ static int create_trace_probe(int argc, char **argv) · 219 unmodified lines
9621135 {
9631136 /*
9641137 * 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]
9671140 * Fetch args:
9681141 * $retval : fetch return value
9691142 * $stack : fetch stack address
@@ -1025,7 +1198,7 @@ static int create_trace_probe(int argc, char **argv) · 55 unmodified lines
10251198 return -EINVAL;
10261199 }
10271200 mutex_lock(&probe_lock);
1028− tp = find_probe_event(event, group);
1201+ tp = find_trace_probe(event, group);
10291202 if (!tp) {
10301203 mutex_unlock(&probe_lock);
10311204 pr_info("Event %s/%s doesn't exist.\n", group, event);
@@ -1144,7 +1317,7 @@ error: · 112 unmodified lines
11441317 return ret;
11451318 }
11461319
1147−static void cleanup_all_probes(void)
1320+static void release_all_trace_probes(void)
11481321 {
11491322 struct trace_probe *tp;
11501323
@@ -1158,7 +1331,6 @@ static void cleanup_all_probes(void) · 7 unmodified lines
11581331 mutex_unlock(&probe_lock);
11591332 }
11601333
1161−
11621334 /* Probes listing interfaces */
11631335 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
11641336 {
@@ -1181,15 +1353,16 @@ static int probes_seq_show(struct seq_file *m, void *v) · 16 unmodified lines
11811353 struct trace_probe *tp = v;
11821354 int i;
11831355
1184− seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
1356+ seq_printf(m, "%c", trace_probe_is_return(tp) ? 'r' : 'p');
11851357 seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
11861358
11871359 if (!tp->symbol)
11881360 seq_printf(m, " 0x%p", tp->rp.kp.addr);
11891361 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);
11911364 else
1192− seq_printf(m, " %s", probe_symbol(tp));
1365+ seq_printf(m, " %s", trace_probe_symbol(tp));
11931366
11941367 for (i = 0; i < tp->nr_args; i++)
11951368 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
12091382 {
12101383 if ((file->f_mode & FMODE_WRITE) &&
12111384 (file->f_flags & O_TRUNC))
1212− cleanup_all_probes();
1385+ release_all_trace_probes();
12131386
12141387 return seq_open(file, &probes_seq_op);
12151388 }
@@ -1513,30 +1686,6 @@ partial: · 297 unmodified lines
15131686 return TRACE_TYPE_PARTIAL_LINE;
15141687 }
15151688
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−
15401689 #undef DEFINE_FIELD
15411690 #define DEFINE_FIELD(type, item, name, is_signed) \
15421691 do { \
@@ -1598,7 +1747,7 @@ static int __set_print_fmt(struct trace_probe *tp, char *buf, int len) · 55 unmodified lines
15981747
15991748 const char *fmt, *arg;
16001749
1601− if (!probe_is_return(tp)) {
1750+ if (!trace_probe_is_return(tp)) {
16021751 fmt = "(%lx)";
16031752 arg = "REC->" FIELD_STRING_IP;
16041753 } else {
@@ -1715,49 +1864,25 @@ static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri, · 110 unmodified lines
17151864 head = this_cpu_ptr(call->perf_events);
17161865 perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
17171866 }
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−}
17441867 #endif /* CONFIG_PERF_EVENTS */
17451868
17461869 static __kprobes
17471870 int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
17481871 {
1872+ struct trace_probe *tp = (struct trace_probe *)event->data;
1873+
17491874 switch (type) {
17501875 case TRACE_REG_REGISTER:
1751− return probe_event_enable(event);
1876+ return enable_trace_probe(tp, TP_FLAG_TRACE);
17521877 case TRACE_REG_UNREGISTER:
1753− probe_event_disable(event);
1878+ disable_trace_probe(tp, TP_FLAG_TRACE);
17541879 return 0;
17551880
17561881 #ifdef CONFIG_PERF_EVENTS
17571882 case TRACE_REG_PERF_REGISTER:
1758− return probe_perf_enable(event);
1883+ return enable_trace_probe(tp, TP_FLAG_PROFILE);
17591884 case TRACE_REG_PERF_UNREGISTER:
1760− probe_perf_disable(event);
1885+ disable_trace_probe(tp, TP_FLAG_PROFILE);
17611886 return 0;
17621887 #endif
17631888 }
@@ -1807,7 +1932,7 @@ static int register_probe_event(struct trace_probe *tp) · 43 unmodified lines
18071932
18081933 /* Initialize ftrace_event_call */
18091934 INIT_LIST_HEAD(&call->class->fields);
1810− if (probe_is_return(tp)) {
1935+ if (trace_probe_is_return(tp)) {
18111936 call->event.funcs = &kretprobe_funcs;
18121937 call->class->define_fields = kretprobe_event_define_fields;
18131938 } else {
@@ -1846,6 +1971,9 @@ static __init int init_kprobe_trace(void) · 32 unmodified lines
18461971 struct dentry *d_tracer;
18471972 struct dentry *entry;
18481973
1974+ if (register_module_notifier(&trace_probe_module_nb))
1975+ return -EINVAL;
1976+
18491977 d_tracer = tracing_init_dentry();
18501978 if (!d_tracer)
18511979 return 0;
@@ -1899,12 +2027,12 @@ static __init int kprobe_trace_self_tests_init(void) · 47 unmodified lines
18992027 warn++;
19002028 } else {
19012029 /* Enable trace point */
1902− tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
2030+ tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
19032031 if (WARN_ON_ONCE(tp == NULL)) {
19042032 pr_warning("error on getting new probe.\n");
19052033 warn++;
19062034 } else
1907− probe_event_enable(&tp->call);
2035+ enable_trace_probe(tp, TP_FLAG_TRACE);
19082036 }
19092037
19102038 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
19142042 warn++;
19152043 } else {
19162044 /* Enable trace point */
1917− tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
2045+ tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
19182046 if (WARN_ON_ONCE(tp == NULL)) {
19192047 pr_warning("error on getting new probe.\n");
19202048 warn++;
19212049 } else
1922− probe_event_enable(&tp->call);
2050+ enable_trace_probe(tp, TP_FLAG_TRACE);
19232051 }
19242052
19252053 if (warn)
@@ -1940,7 +2068,7 @@ static __init int kprobe_trace_self_tests_init(void) · 14 unmodified lines
19402068 }
19412069
19422070 end:
1943− cleanup_all_probes();
2071+ release_all_trace_probes();
19442072 if (warn)
19452073 pr_cont("NG: Some tests are failed. Please check them.\n");
19462074 else
kernel/trace/trace_output.c+6−5
@@ -1107,19 +1107,20 @@ static enum print_line_t trace_stack_print(struct trace_iterator *iter, · 1106 unmodified lines
11071107 {
11081108 struct stack_entry *field;
11091109 struct trace_seq *s = &iter->seq;
1110− int i;
1110+ unsigned long *p;
1111+ unsigned long *end;
11111112
11121113 trace_assign_type(field, iter->ent);
1114+ end = (unsigned long *)((long)iter->ent + iter->ent_size);
11131115
11141116 if (!trace_seq_puts(s, "<stack trace>\n"))
11151117 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++) {
11191120 if (!trace_seq_puts(s, " => "))
11201121 goto partial;
11211122
1122− if (!seq_print_ip_sym(s, field->caller[i], flags))
1123+ if (!seq_print_ip_sym(s, *p, flags))
11231124 goto partial;
11241125 if (!trace_seq_puts(s, "\n"))
11251126 goto partial;
kernel/watchdog.c+0−2
@@ -200,7 +200,6 @@ static int is_softlockup(unsigned long touch_ts) · 199 unmodified lines
200200 }
201201
202202 #ifdef CONFIG_HARDLOCKUP_DETECTOR
203−void __weak hw_nmi_watchdog_set_attr(struct perf_event_attr *wd_attr) { }
204203
205204 static struct perf_event_attr wd_hw_attr = {
206205 .type = PERF_TYPE_HARDWARE,
@@ -372,7 +371,6 @@ static int watchdog_nmi_enable(int cpu) · 165 unmodified lines
372371
373372 wd_attr = &wd_hw_attr;
374373 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
375− hw_nmi_watchdog_set_attr(wd_attr);
376374
377375 /* Try to register using hardware perf events */
378376 event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
tools/perf/Documentation/perf-probe.txt+4−2
@@ -34,9 +34,11 @@ OPTIONS · 33 unmodified lines
3434 Specify vmlinux path which has debuginfo (Dwarf binary).
3535
3636 -m::
37−--module=MODNAME::
37+--module=MODNAME|PATH::
3838 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).
4042
4143 -s::
4244 --source=PATH::
tools/perf/Makefile+2−0
@@ -279,6 +279,7 @@ LIB_H += util/thread.h · 278 unmodified lines
279279 LIB_H += util/thread_map.h
280280 LIB_H += util/trace-event.h
281281 LIB_H += util/probe-finder.h
282+LIB_H += util/dwarf-aux.h
282283 LIB_H += util/probe-event.h
283284 LIB_H += util/pstack.h
284285 LIB_H += util/cpumap.h
@@ -435,6 +436,7 @@ else · 150 unmodified lines
435436 BASIC_CFLAGS += -DDWARF_SUPPORT
436437 EXTLIBS += -lelf -ldw
437438 LIB_OBJS += $(OUTPUT)util/probe-finder.o
439+ LIB_OBJS += $(OUTPUT)util/dwarf-aux.o
438440 endif # PERF_HAVE_DWARF_REGS
439441 endif # NO_DWARF
440442
tools/perf/builtin-probe.c+2−1
@@ -242,7 +242,8 @@ static const struct option options[] = { · 241 unmodified lines
242242 OPT_STRING('s', "source", &symbol_conf.source_prefix,
243243 "directory", "path to kernel source"),
244244 OPT_STRING('m', "module", &params.target_module,
245− "modname", "target module name"),
245+ "modname|path",
246+ "target module name (for online) or path (for offline)"),
246247 #endif
247248 OPT__DRY_RUN(&probe_event_dry_run),
248249 OPT_INTEGER('\0', "max-probes", &params.max_probe_points,
tools/perf/util/dwarf-aux.c+663−0
@@ -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, &param, 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+
tools/perf/util/dwarf-aux.h+100−0
@@ -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
tools/perf/util/probe-event.c+124−41
@@ -117,6 +117,10 @@ static struct map *kernel_get_module_map(const char *module) · 116 unmodified lines
117117 struct rb_node *nd;
118118 struct map_groups *grp = &machine.kmaps;
119119
120+ /* A file path -- this is an offline module */
121+ if (module && strchr(module, '/'))
122+ return machine__new_module(&machine, 0, module);
123+
120124 if (!module)
121125 module = "kernel";
122126
@@ -170,16 +174,24 @@ const char *kernel_get_module_path(const char *module) · 47 unmodified lines
170174 }
171175
172176 #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)
174179 {
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+ }
180193 }
181− pr_debug("Try to open %s\n", path);
182− return open(path, O_RDONLY);
194+ return debuginfo__new(path);
183195 }
184196
185197 /*
@@ -193,13 +205,24 @@ static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp, · 7 unmodified lines
193205 struct map *map;
194206 u64 addr;
195207 int ret = -ENOENT;
208+ struct debuginfo *dinfo;
196209
197210 sym = __find_kernel_function_by_name(tp->symbol, &map);
198211 if (sym) {
199212 addr = map->unmap_ip(map, sym->start + tp->offset);
200213 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
201214 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+ }
203226 }
204227 if (ret <= 0) {
205228 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
214237 return 0;
215238 }
216239
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+
217275 /* Try to find perf_probe_event with debuginfo */
218276 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)
221279 {
222280 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;
224283
225− fd = open_vmlinux(module);
226− if (fd < 0) {
284+ if (!dinfo) {
227285 if (need_dwarf) {
228286 pr_warning("Failed to open debuginfo file.\n");
229− return fd;
287+ return -ENOENT;
230288 }
231− pr_debug("Could not open vmlinux. Try to use symbols.\n");
289+ pr_debug("Could not open debuginfo. Try to use symbols.\n");
232290 return 0;
233291 }
234292
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);
237297
238298 if (ntevs > 0) { /* Succeeded to find trace events */
239299 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;
241304 }
242305
243306 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
371434 {
372435 int l = 1;
373436 struct line_node *ln;
437+ struct debuginfo *dinfo;
374438 FILE *fp;
375− int fd, ret;
439+ int ret;
376440 char *tmp;
377441
378442 /* Search a line range */
@@ -380,13 +444,14 @@ int show_line_range(struct line_range *lr, const char *module) · 1 unmodified line
380444 if (ret < 0)
381445 return ret;
382446
383− fd = open_vmlinux(module);
384− if (fd < 0) {
447+ dinfo = open_debuginfo(module);
448+ if (!dinfo) {
385449 pr_warning("Failed to open debuginfo file.\n");
386− return fd;
450+ return -ENOENT;
387451 }
388452
389− ret = find_line_range(fd, lr);
453+ ret = debuginfo__find_line_range(dinfo, lr);
454+ debuginfo__delete(dinfo);
390455 if (ret == 0) {
391456 pr_warning("Specified source line is not found.\n");
392457 return -ENOENT;
@@ -448,7 +513,8 @@ end: · 55 unmodified lines
448513 return ret;
449514 }
450515
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,
452518 int max_vls, struct strfilter *_filter,
453519 bool externs)
454520 {
@@ -463,7 +529,8 @@ static int show_available_vars_at(int fd, struct perf_probe_event *pev, · 8 unmodified lines
463529 return -EINVAL;
464530 pr_debug("Searching variables at %s\n", buf);
465531
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);
467534 if (ret <= 0) {
468535 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
469536 goto end;
@@ -504,24 +571,26 @@ int show_available_vars(struct perf_probe_event *pevs, int npevs, · 34 unmodified lines
504571 int max_vls, const char *module,
505572 struct strfilter *_filter, bool externs)
506573 {
507− int i, fd, ret = 0;
574+ int i, ret = 0;
575+ struct debuginfo *dinfo;
508576
509577 ret = init_vmlinux();
510578 if (ret < 0)
511579 return ret;
512580
581+ dinfo = open_debuginfo(module);
582+ if (!dinfo) {
583+ pr_warning("Failed to open debuginfo file.\n");
584+ return -ENOENT;
585+ }
586+
513587 setup_pager();
514588
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,
523591 externs);
524− }
592+
593+ debuginfo__delete(dinfo);
525594 return ret;
526595 }
527596
@@ -990,7 +1059,7 @@ bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) · 462 unmodified lines
9901059
9911060 /* Parse probe_events event into struct probe_point */
9921061 static int parse_probe_trace_command(const char *cmd,
993− struct probe_trace_event *tev)
1062+ struct probe_trace_event *tev)
9941063 {
9951064 struct probe_trace_point *tp = &tev->point;
9961065 char pr;
@@ -1023,8 +1092,14 @@ static int parse_probe_trace_command(const char *cmd, · 26 unmodified lines
10231092
10241093 tp->retprobe = (pr == 'r');
10251094
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,
10281103 &tp->offset);
10291104 if (ret == 1)
10301105 tp->offset = 0;
@@ -1269,9 +1344,10 @@ char *synthesize_probe_trace_command(struct probe_trace_event *tev) · 238 unmodified lines
12691344 if (buf == NULL)
12701345 return NULL;
12711346
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",
12731348 tp->retprobe ? 'r' : 'p',
12741349 tev->group, tev->event,
1350+ tp->module ?: "", tp->module ? ":" : "",
12751351 tp->symbol, tp->offset);
12761352 if (len <= 0)
12771353 goto error;
@@ -1378,6 +1454,8 @@ static void clear_probe_trace_event(struct probe_trace_event *tev) · 100 unmodified lines
13781454 free(tev->group);
13791455 if (tev->point.symbol)
13801456 free(tev->point.symbol);
1457+ if (tev->point.module)
1458+ free(tev->point.module);
13811459 for (i = 0; i < tev->nargs; i++) {
13821460 if (tev->args[i].name)
13831461 free(tev->args[i].name);
@@ -1729,7 +1807,7 @@ static int convert_to_probe_trace_events(struct perf_probe_event *pev, · 345 unmodified lines
17291807 /* Convert perf_probe_event with debuginfo */
17301808 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
17311809 if (ret != 0)
1732− return ret;
1810+ return ret; /* Found in debuginfo or got an error */
17331811
17341812 /* Allocate trace event buffer */
17351813 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
17421820 ret = -ENOMEM;
17431821 goto error;
17441822 }
1823+ tev->point.module = strdup(module);
1824+ if (tev->point.module == NULL) {
1825+ ret = -ENOMEM;
1826+ goto error;
1827+ }
17451828 tev->point.offset = pev->point.offset;
17461829 tev->point.retprobe = pev->point.retprobe;
17471830 tev->nargs = pev->nargs;
tools/perf/util/probe-event.h+1−0
@@ -10,6 +10,7 @@ extern bool probe_event_dry_run; · 9 unmodified lines
1010 /* kprobe-tracer tracing point */
1111 struct probe_trace_point {
1212 char *symbol; /* Base symbol */
13+ char *module; /* Module name */
1314 unsigned long offset; /* Offset from symbol */
1415 bool retprobe; /* Return probe flag */
1516 };
tools/perf/util/probe-finder.c+102−650
@@ -43,21 +43,6 @@ · 42 unmodified lines
4343 /* Kprobe tracer basic type is up to u64 */
4444 #define MAX_BASIC_TYPE_BITS 64
4545
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−
6146 /* Line number list operations */
6247
6348 /* Add a line to line number list */
@@ -131,29 +116,37 @@ static const Dwfl_Callbacks offline_callbacks = { · 67 unmodified lines
131116 };
132117
133118 /* 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)
135121 {
136122 Dwfl_Module *mod;
137− Dwarf *dbg = NULL;
123+ int fd;
138124
139− if (!dwflp)
140− return NULL;
125+ fd = open(path, O_RDONLY);
126+ if (fd < 0)
127+ return fd;
141128
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;
145132
146− mod = dwfl_report_offline(*dwflp, "", "", fd);
133+ mod = dwfl_report_offline(self->dwfl, "", "", fd);
147134 if (!mod)
148135 goto error;
149136
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;
152142 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;
157150 }
158151
159152 #if _ELFUTILS_PREREQ(0, 148)
@@ -189,597 +182,81 @@ static const Dwfl_Callbacks kernel_callbacks = { · 29 unmodified lines
189182 };
190183
191184 /* 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)
194187 {
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;
203191
204192 /* 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);
207195
208− dbg = dwfl_addrdwarf(*dwflp, addr, bias);
196+ self->dbg = dwfl_addrdwarf(self->dwfl, addr, &self->bias);
209197 /* Here, check whether we could get a real dwarf */
210− if (!dbg) {
198+ if (!self->dbg) {
211199 pr_debug("Failed to find kernel dwarf at %lx\n",
212200 (unsigned long)addr);
213− dwfl_end(*dwflp);
214− *dwflp = NULL;
201+ dwfl_end(self->dwfl);
202+ memset(self, 0, sizeof(*self));
203+ return -ENOENT;
215204 }
216− return dbg;
205+
206+ return 0;
217207 }
218208 #else
219209 /* 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)
222212 {
223− int fd;
224213 const char *path = kernel_get_module_path("kernel");
225214
226215 if (!path) {
227216 pr_err("Failed to find vmlinux path\n");
228− return NULL;
217+ return -ENOENT;
229218 }
230219
231220 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);
237222 }
238223 #endif
239224
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)
448226 {
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)
454229 return NULL;
455230
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;
485234 }
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;
511235
512− return DIE_FIND_CB_CONTINUE;
236+ return self;
513237 }
514238
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)
518240 {
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)
523243 return NULL;
524244
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, &param, 0);
674− ret = param.retval;
245+ if (debuginfo__init_online_kernel_dwarf(self, (Dwarf_Addr)addr) < 0) {
246+ free(self);
247+ self = NULL;
675248 }
676249
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;
765251 }
766252
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)
769254 {
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);
776259 }
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;
783260 }
784261
785262 /*
@@ -897,6 +374,7 @@ static int convert_variable_type(Dwarf_Die *vr_die, · 111 unmodified lines
897374 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
898375 Dwarf_Die type;
899376 char buf[16];
377+ int bsize, boffs, total;
900378 int ret;
901379
902380 /* TODO: check all types */
@@ -906,11 +384,15 @@ static int convert_variable_type(Dwarf_Die *vr_die, · 3 unmodified lines
906384 return (tvar->type == NULL) ? -ENOMEM : 0;
907385 }
908386
909− if (die_get_bit_size(vr_die) != 0) {
387+ bsize = dwarf_bitsize(vr_die);
388+ if (bsize > 0) {
910389 /* 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));
914396 goto formatted;
915397 }
916398
@@ -958,10 +440,11 @@ static int convert_variable_type(Dwarf_Die *vr_die, · 41 unmodified lines
958440 return (tvar->type == NULL) ? -ENOMEM : 0;
959441 }
960442
961− ret = BYTES_TO_BITS(die_get_byte_size(&type));
962− if (!ret)
443+ ret = dwarf_bytesize(&type);
444+ if (ret <= 0)
963445 /* No size ... try to use default type */
964446 return 0;
447+ ret = BYTES_TO_BITS(ret);
965448
966449 /* Check the bitwidth */
967450 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
1025508 else
1026509 *ref_ptr = ref;
1027510 }
1028− ref->offset += die_get_byte_size(&type) * field->index;
511+ ref->offset += dwarf_bytesize(&type) * field->index;
1029512 if (!field->next)
1030513 /* Save vr_die for converting types */
1031514 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
1245728
1246729 /* If no real subprogram, find a real one */
1247730 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);
1250732 if (!sp_die) {
1251733 pr_warning("Failed to find probe point in any "
1252734 "functions.\n");
@@ -1504,28 +986,18 @@ static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data) · 251 unmodified lines
1504986 }
1505987
1506988 /* 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)
1508991 {
1509992 struct perf_probe_point *pp = &pf->pev->point;
1510993 Dwarf_Off off, noff;
1511994 size_t cuhl;
1512995 Dwarf_Die *diep;
1513− Dwarf *dbg = NULL;
1514− Dwfl *dwfl;
1515− Dwarf_Addr bias; /* Currently ignored */
1516996 int ret = 0;
1517997
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−
1526998 #if _ELFUTILS_PREREQ(0, 142)
1527999 /* Get the call frame information from this dwarf */
1528− pf->cfi = dwarf_getcfi(dbg);
1000+ pf->cfi = dwarf_getcfi(self->dbg);
15291001 #endif
15301002
15311003 off = 0;
@@ -1544,7 +1016,8 @@ static int find_probes(int fd, struct probe_finder *pf) · 12 unmodified lines
15441016 .data = pf,
15451017 };
15461018
1547− dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
1019+ dwarf_getpubnames(self->dbg, pubname_search_cb,
1020+ &pubname_param, 0);
15481021 if (pubname_param.found) {
15491022 ret = probe_point_search_cb(&pf->sp_die, &probe_param);
15501023 if (ret)
@@ -1553,9 +1026,9 @@ static int find_probes(int fd, struct probe_finder *pf) · 2 unmodified lines
15531026 }
15541027
15551028 /* 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)) {
15571030 /* 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);
15591032 if (!diep)
15601033 continue;
15611034
@@ -1582,8 +1055,6 @@ static int find_probes(int fd, struct probe_finder *pf) · 20 unmodified lines
15821055
15831056 found:
15841057 line_list__free(&pf->lcache);
1585− if (dwfl)
1586− dwfl_end(dwfl);
15871058
15881059 return ret;
15891060 }
@@ -1629,8 +1100,9 @@ static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf) · 39 unmodified lines
16291100 }
16301101
16311102 /* 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)
16341106 {
16351107 struct trace_event_finder tf = {
16361108 .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
16451117 tf.tevs = *tevs;
16461118 tf.ntevs = 0;
16471119
1648− ret = find_probes(fd, &tf.pf);
1120+ ret = debuginfo__find_probes(self, &tf.pf);
16491121 if (ret < 0) {
16501122 free(*tevs);
16511123 *tevs = NULL;
@@ -1739,9 +1211,10 @@ out: · 87 unmodified lines
17391211 }
17401212
17411213 /* 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)
17451218 {
17461219 struct available_var_finder af = {
17471220 .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
17561229 af.vls = *vls;
17571230 af.nvls = 0;
17581231
1759− ret = find_probes(fd, &af.pf);
1232+ ret = debuginfo__find_probes(self, &af.pf);
17601233 if (ret < 0) {
17611234 /* Free vlist for error */
17621235 while (af.nvls--) {
@@ -1774,28 +1247,19 @@ int find_available_vars_at(int fd, struct perf_probe_event *pev, · 11 unmodified lines
17741247 }
17751248
17761249 /* 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)
17781252 {
17791253 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;
17831255 const char *fname = NULL, *func = NULL, *tmp;
17841256 int baseline = 0, lineno = 0, ret = 0;
17851257
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−
17951258 /* Adjust address with bias */
1796− addr += bias;
1259+ addr += self->bias;
1260+
17971261 /* 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)) {
17991263 pr_warning("Failed to find debug information for address %lx\n",
18001264 addr);
18011265 ret = -EINVAL;
@@ -1807,7 +1271,7 @@ int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt) · 5 unmodified lines
18071271 /* Don't care whether it failed or not */
18081272
18091273 /* 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)) {
18111275 /* Get function entry information */
18121276 tmp = dwarf_diename(&spdie);
18131277 if (!tmp ||
@@ -1871,8 +1335,6 @@ post: · 57 unmodified lines
18711335 }
18721336 }
18731337 end:
1874− if (dwfl)
1875− dwfl_end(dwfl);
18761338 if (ret == 0 && (fname || func))
18771339 ret = 1; /* Found a point */
18781340 return ret;
@@ -1982,26 +1444,15 @@ static int find_line_range_by_func(struct line_finder *lf) · 103 unmodified lines
19821444 return param.retval;
19831445 }
19841446
1985−int find_line_range(int fd, struct line_range *lr)
1447+int debuginfo__find_line_range(struct debuginfo *self, struct line_range *lr)
19861448 {
19871449 struct line_finder lf = {.lr = lr, .found = 0};
19881450 int ret = 0;
19891451 Dwarf_Off off = 0, noff;
19901452 size_t cuhl;
19911453 Dwarf_Die *diep;
1992− Dwarf *dbg = NULL;
1993− Dwfl *dwfl;
1994− Dwarf_Addr bias; /* Currently ignored */
19951454 const char *comp_dir;
19961455
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−
20051456 /* Fastpath: lookup by function name from .debug_pubnames section */
20061457 if (lr->function) {
20071458 struct pubname_callback_param pubname_param = {
@@ -2010,7 +1461,8 @@ int find_line_range(int fd, struct line_range *lr) · 2 unmodified lines
20101461 struct dwarf_callback_param line_range_param = {
20111462 .data = (void *)&lf, .retval = 0};
20121463
2013− dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
1464+ dwarf_getpubnames(self->dbg, pubname_search_cb,
1465+ &pubname_param, 0);
20141466 if (pubname_param.found) {
20151467 line_range_search_cb(&lf.sp_die, &line_range_param);
20161468 if (lf.found)
@@ -2020,11 +1472,12 @@ int find_line_range(int fd, struct line_range *lr) · 3 unmodified lines
20201472
20211473 /* Loop on CUs (Compilation Unit) */
20221474 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)
20241477 break;
20251478
20261479 /* 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);
20281481 if (!diep)
20291482 continue;
20301483
@@ -2058,7 +1511,6 @@ found: · 27 unmodified lines
20581511 }
20591512
20601513 pr_debug("path: %s\n", lr->path);
2061− dwfl_end(dwfl);
20621514 return (ret < 0) ? ret : lf.found;
20631515 }
20641516
tools/perf/util/probe-finder.h+29−14
@@ -16,27 +16,42 @@ static inline int is_c_varname(const char *name) · 15 unmodified lines
1616 }
1717
1818 #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+
1935 /* 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);
2340
2441 /* 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);
2745
2846 /* 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);
3049
3150 /* 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);
4055
4156 struct probe_finder {
4257 struct perf_probe_event *pev; /* Target probe event */
tools/perf/util/string.c+19−0
@@ -294,3 +294,22 @@ bool strlazymatch(const char *str, const char *pat) · 293 unmodified lines
294294 {
295295 return __match_glob(str, pat, true);
296296 }
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+
tools/perf/util/trace-event-info.c+29−91
@@ -183,106 +183,59 @@ int bigendian(void) · 182 unmodified lines
183183 return *ptr == 0x01020304;
184184 }
185185
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)
187188 {
188189 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;
207193
208194 fd = open(file, O_RDONLY);
209195 if (fd < 0)
210196 die("Can't read '%s'", file);
211− size = copy_file_fd(fd);
212− close(fd);
213197
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);
222200
223201 do {
224202 r = read(fd, buf, BUFSIZ);
225− if (r > 0)
203+ if (r > 0) {
226204 size += r;
205+ write_or_die(buf, r);
206+ }
227207 } 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);
243208 close(fd);
244209
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);
246217 }
247218
248219 static void read_header_files(void)
249220 {
250− unsigned long long size, check_size;
251221 char *path;
252− int fd;
222+ struct stat st;
253223
254224 path = get_tracing_file("events/header_page");
255− fd = open(path, O_RDONLY);
256− if (fd < 0)
225+ if (stat(path, &st) < 0)
257226 die("can't read '%s'", path);
258227
259− /* unfortunately, you can not stat debugfs files for size */
260− size = get_size_fd(fd);
261−
262228 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);
270230 put_tracing_file(path);
271231
272232 path = get_tracing_file("events/header_event");
273− fd = open(path, O_RDONLY);
274− if (fd < 0)
233+ if (stat(path, &st) < 0)
275234 die("can't read '%s'", path);
276235
277− size = get_size_fd(fd);
278−
279236 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);
284238 put_tracing_file(path);
285− close(fd);
286239 }
287240
288241 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
298251
299252 static void copy_event_system(const char *sys, struct tracepoint_path *tps)
300253 {
301− unsigned long long size, check_size;
302254 struct dirent *dent;
303255 struct stat st;
304256 char *format;
@@ -338,14 +290,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps) · 33 unmodified lines
338290 sprintf(format, "%s/%s/format", sys, dent->d_name);
339291 ret = stat(format, &st);
340292
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);
349295
350296 free(format);
351297 }
@@ -426,7 +372,7 @@ static void read_event_files(struct tracepoint_path *tps) · 74 unmodified lines
426372
427373 static void read_proc_kallsyms(void)
428374 {
429− unsigned int size, check_size;
375+ unsigned int size;
430376 const char *path = "/proc/kallsyms";
431377 struct stat st;
432378 int ret;
@@ -438,17 +384,12 @@ static void read_proc_kallsyms(void) · 5 unmodified lines
438384 write_or_die(&size, 4);
439385 return;
440386 }
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);
447388 }
448389
449390 static void read_ftrace_printk(void)
450391 {
451− unsigned int size, check_size;
392+ unsigned int size;
452393 char *path;
453394 struct stat st;
454395 int ret;
@@ -461,11 +402,8 @@ static void read_ftrace_printk(void) · 6 unmodified lines
461402 write_or_die(&size, 4);
462403 goto out;
463404 }
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+
469407 out:
470408 put_tracing_file(path);
471409 }
tools/perf/util/util.h+1−0
@@ -238,6 +238,7 @@ char **argv_split(const char *str, int *argcp); · 237 unmodified lines
238238 void argv_free(char **argv);
239239 bool strglobmatch(const char *str, const char *pat);
240240 bool strlazymatch(const char *str, const char *pat);
241+int strtailcmp(const char *s1, const char *s2);
241242 unsigned long convert_unit(unsigned long value, char *unit);
242243 int readn(int fd, void *buf, size_t size);
243244