+ /*
+ * Query the raw code for later use. Do it now to simplify error
+ * management.
+ */
+ ret = pfm_get_event_info(pencoder.idx, PFM_OS_NONE, &info);
+ if (ret != PFM_SUCCESS) {
+ fprintf(stderr, "libpfm: error pfm_get_event_info: %s\n", pfm_strerror(ret));
+ ret = 1;
+ goto end;
+ }
+
+ /*
+ * Now that the event is found, try to use it to validate that
+ * the current user has access to it and that it can be used on that
+ * host.
+ */
+
+ /* Set the event to disabled to prevent unnecessary side effects. */
+ pencoder.attr->disabled = 1;
+
+ /* perf_event_open is provided by perfmon/perf_event.h. */
+ fd = perf_event_open(pencoder.attr, 0, -1, -1, 0);
+ if (fd == -1) {
+ fprintf(stderr, "perf: error perf_event_open: %d: %s\n", errno,
+ strerror(errno));
+ ret = 2;
+ goto end;
+ }
+
+ /* We close the fd immediately since the event is actionable. */
+ close(fd);
+
+ /* Output the raw code for the event */
+ fprintf(stdout, "r%" PRIx64 "\n", info.code);
+ ret = 0;