+static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
+ struct kprobe_trace_event **tevs)
+{
+ struct symbol *sym;
+ bool need_dwarf;
+#ifndef NO_DWARF_SUPPORT
+ int fd;
+#endif
+ int ntevs = 0, i;
+ struct kprobe_trace_event *tev;
+
+ need_dwarf = perf_probe_event_need_dwarf(pev);
+
+ if (need_dwarf)
+#ifdef NO_DWARF_SUPPORT
+ die("Debuginfo-analysis is not supported");
+#else /* !NO_DWARF_SUPPORT */
+ pr_debug("Some probes require debuginfo.\n");
+
+ fd = open_vmlinux();
+ if (fd < 0) {
+ if (need_dwarf)
+ die("Could not open debuginfo file.");
+
+ pr_debug("Could not open vmlinux/module file."
+ " Try to use symbols.\n");
+ goto end_dwarf;
+ }
+
+ /* Searching probe points */
+ ntevs = find_kprobe_trace_events(fd, pev, tevs);
+
+ if (ntevs > 0) /* Found */
+ goto found;
+
+ if (ntevs == 0) /* No error but failed to find probe point. */
+ die("Probe point '%s' not found. - probe not added.",
+ synthesize_perf_probe_point(&pev->point));
+
+ /* Error path */
+ if (need_dwarf) {
+ if (ntevs == -ENOENT)
+ pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
+ die("Could not analyze debuginfo.");
+ }
+ pr_debug("An error occurred in debuginfo analysis."
+ " Try to use symbols.\n");
+
+end_dwarf:
+#endif /* !NO_DWARF_SUPPORT */
+
+ /* Allocate trace event buffer */
+ ntevs = 1;
+ tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
+
+ /* Copy parameters */
+ tev->point.symbol = xstrdup(pev->point.function);
+ tev->point.offset = pev->point.offset;
+ tev->nargs = pev->nargs;
+ if (tev->nargs) {
+ tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
+ * tev->nargs);
+ for (i = 0; i < tev->nargs; i++)
+ tev->args[i].value = xstrdup(pev->args[i].name);
+ }
+
+ /* Currently just checking function name from symbol map */
+ sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
+ tev->point.symbol, NULL);
+ if (!sym)
+ die("Kernel symbol \'%s\' not found - probe not added.",
+ tev->point.symbol);
+#ifndef NO_DWARF_SUPPORT
+found:
+ close(fd);
+#endif
+ return ntevs;
+}
+
+struct __event_package {
+ struct perf_probe_event *pev;
+ struct kprobe_trace_event *tevs;
+ int ntevs;
+};
+
+void add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
+ bool force_add)
+{
+ int i;
+ struct __event_package *pkgs;
+
+ pkgs = xzalloc(sizeof(struct __event_package) * npevs);
+
+ /* Init vmlinux path */
+ init_vmlinux();
+
+ /* Loop 1: convert all events */
+ for (i = 0; i < npevs; i++) {
+ pkgs[i].pev = &pevs[i];
+ /* Convert with or without debuginfo */
+ pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev,
+ &pkgs[i].tevs);
+ }
+
+ /* Loop 2: add all events */
+ for (i = 0; i < npevs; i++)
+ __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
+ pkgs[i].ntevs, force_add);
+ /* TODO: cleanup all trace events? */
+}
+