diff mbox series

[RFC,v2,4/5] kernel-shark: Add hello_kernel plugin

Message ID 20190703121023.16655-5-y.karadz@gmail.com (mailing list archive)
State Superseded
Headers show
Series Add support for text rendering | expand

Commit Message

Yordan Karadzhov July 3, 2019, 12:10 p.m. UTC
This plugin is meant to be used for demonstration purposes only,
or as part of a tutorial describing how to write a new plugin.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 kernel-shark/examples/CMakeLists.txt |  4 ++
 kernel-shark/examples/hello_kernel.c | 67 ++++++++++++++++++++++++++++
 kernel-shark/src/libkshark-plugin.h  |  3 ++
 3 files changed, 74 insertions(+)
 create mode 100644 kernel-shark/examples/hello_kernel.c
diff mbox series

Patch

diff --git a/kernel-shark/examples/CMakeLists.txt b/kernel-shark/examples/CMakeLists.txt
index e16216e..824b21c 100644
--- a/kernel-shark/examples/CMakeLists.txt
+++ b/kernel-shark/examples/CMakeLists.txt
@@ -23,3 +23,7 @@  target_link_libraries(dplot   kshark-plot)
 message(STATUS "widgetdemo")
 add_executable(widgetdemo          widgetdemo.cpp)
 target_link_libraries(widgetdemo   kshark-gui)
+
+add_library(hello             SHARED  hello_kernel.c)
+set_target_properties(hello   PROPERTIES PREFIX "plugin-")
+target_link_libraries(hello   kshark-plot)
diff --git a/kernel-shark/examples/hello_kernel.c b/kernel-shark/examples/hello_kernel.c
new file mode 100644
index 0000000..6c78411
--- /dev/null
+++ b/kernel-shark/examples/hello_kernel.c
@@ -0,0 +1,67 @@ 
+// SPDX-License-Identifier: LGPL-2.1
+
+/*
+ * Copyright (C) 2019 VMware Inc, Yordan Karadzhov <ykaradzhov@vmware.com>
+ */
+
+/**
+ *  @file    hello_kernel.c
+ *  @brief   Example plugin.
+ */
+
+// C
+#ifndef _GNU_SOURCE
+/** Use GNU C Library. */
+#define _GNU_SOURCE
+#endif
+#include <stdio.h>
+
+// KernelShark
+#include "libkshark.h"
+#include "libkshark-plot.h"
+#include "libkshark-plugin.h"
+
+char *font_file;
+struct ksplot_font mono_oblique_16;
+
+static void nop_action(struct kshark_context *kshark_ctx,
+		       struct tep_record *record,
+		       struct kshark_entry *entry)
+{}
+
+static void draw_hello(struct kshark_cpp_argv *argv_c,
+		       int val, int draw_action)
+{
+	if (!ksplot_font_is_loaded(&mono_oblique_16))
+		ksplot_init_font(&mono_oblique_16, 16, font_file);
+
+	ksplot_print_text(&mono_oblique_16, NULL, 100, 30, "hello kernel!");
+}
+
+/** Load this plugin. */
+int KSHARK_PLUGIN_INITIALIZER(struct kshark_context *kshark_ctx)
+{
+	font_file = ksplot_find_font_file("FreeMono", "FreeMonoOblique");
+	if (!font_file)
+		return 0;
+
+	kshark_register_event_handler(&kshark_ctx->event_handlers,
+				      KS_PLUGIN_NO_EVENT,
+				      nop_action,
+				      draw_hello);
+
+	return 1;
+}
+
+/** Unload this plugin. */
+int KSHARK_PLUGIN_DEINITIALIZER(struct kshark_context *kshark_ctx)
+{
+	kshark_unregister_event_handler(&kshark_ctx->event_handlers,
+					KS_PLUGIN_NO_EVENT,
+					nop_action,
+					draw_hello);
+
+	free(font_file);
+
+	return 1;
+}
diff --git a/kernel-shark/src/libkshark-plugin.h b/kernel-shark/src/libkshark-plugin.h
index b3cf1c6..b182d79 100644
--- a/kernel-shark/src/libkshark-plugin.h
+++ b/kernel-shark/src/libkshark-plugin.h
@@ -103,6 +103,9 @@  enum kshark_plugin_actions {
 	KSHARK_PLUGIN_CPU_DRAW,
 };
 
+/** No event identifier associated with the plugin. */
+#define KS_PLUGIN_NO_EVENT (-ENODEV)
+
 /**
  * Plugin Event handler structure, defining the properties of the required
  * kshark_entry.