rasd: Add the requested tracepoint to evlist

Look for the requested tracepoint from the name and
add it to the list of events to enable.

Signed-off-by: Jean Pihet <jean.pihet@linaro.org>
Link: http://lkml.kernel.org/r/1412933690-25576-3-git-send-email-jean.pihet@linaro.org
Signed-off-by: Borislav Petkov <bp@suse.de>
diff --git a/src/rasd.c b/src/rasd.c
index a157f00..9147d65 100644
--- a/src/rasd.c
+++ b/src/rasd.c
@@ -4,12 +4,66 @@
 #include "ras.h"
 #include "debugfs.h"
 
+#define EVENT_STR_MAX_LENGTH	1024
+#define SYS_NAME_SEPARATOR	":"
+
 unsigned int page_size;
+struct perf_evlist evlist;
+struct perf_rasd rasd;
+
+/*
+ * Extract the event subsystem and event names from the command
+ * line argument. The syntax is 'sys:name'.
+ */
+static int extract_sys_name(char **str, char **sys, char **name)
+{
+	/* Look for the sys:name separator */
+	char *sys_ptr = strsep(str, SYS_NAME_SEPARATOR);
+
+	if (!*str || !sys_ptr)
+		return -1;
+
+	/* Is there something after the separator? */
+	if (!strlen(*str))
+		return -1;
+
+	/* Alloc and fill the sys part */
+	*sys = strndup(sys_ptr, EVENT_STR_MAX_LENGTH);
+
+	/*
+	 * Look for the newline in the rest of the string.
+	 * If found alloc and fill the name part.
+	 */
+	*name = strndup(strsep(str, "\n\r"), EVENT_STR_MAX_LENGTH);
+
+	return 0;
+}
+
+/* Add the requested tracepoint event to evlist */
+static int add_tp_event(char *event_str)
+{
+	struct perf_evsel *tp;
+	char **str = &event_str;
+	int ret;
+
+	if (extract_sys_name(str, &rasd.sys, &rasd.name))
+		err("invalid event specified, syntax is sys:name");
+
+	/* Initialize tracepoint evsel */
+	tp = perf_evsel__newtp_idx(rasd.sys, rasd.name, 0);
+	if (!tp)
+		err("init tracepoint evsel");
+
+	/* Add the event to the lists of events */
+	list_add_tail(&tp->node, &evlist.entries);
+
+	return ret;
+}
 
 int main()
 {
-	struct perf_evlist evlist;
-	struct perf_evsel counter, *c;
+	struct perf_evsel *c;
+	char *ev_name = strdup("mce:mce_record");
 
 	page_size = sysconf(_SC_PAGE_SIZE);
 
@@ -21,7 +75,8 @@
 
 	INIT_LIST_HEAD(&evlist.entries);
 
-	list_add_tail(&counter.node, &evlist.entries);
+	/* Add event from the name string */
+	add_tp_event(ev_name);
 
 	evlist__for_each(&evlist, c) {
 		/*
@@ -38,5 +93,8 @@
 
 	perf_evlist__delete(&evlist);
 
+	free(rasd.name);
+	free(rasd.sys);
+
 	return 0;
 }