kp_obj.c include some common ktap object operation,
like object dump, object length.
It also include generic memory allocate code.
Exposed functions:
1). kp_malloc/kp_zalloc/kp_free
2). kp_obj_dump/kp_obj_show
3). kp_obj_rawequal
4). kp_obj_len
5). kp_obj_new
allocate new object, all object is linked in G(ks)->allgc.
6). kp_obj_kstack2str
convert kernel stack to string.
7). kp_obj_freeall
free all object, called in kp_vm_exit.
Signed-off-by: Jovi Zhangwei
---
kernel/trace/ktap/kp_obj.c | 281 +
kernel/trace/ktap/kp_obj.h | 19 +++
2 files changed, 300 insertions(+)
create mode 100644 kernel/trace/ktap/kp_obj.c
create mode 100644 kernel/trace/ktap/kp_obj.h
diff --git a/kernel/trace/ktap/kp_obj.c b/kernel/trace/ktap/kp_obj.c
new file mode 100644
index 000..03e25fc
--- /dev/null
+++ b/kernel/trace/ktap/kp_obj.c
@@ -0,0 +1,281 @@
+/*
+ * kp_obj.c - ktap object generic operation
+ *
+ * This file is part of ktap by Jovi Zhangwei.
+ *
+ * Copyright (C) 2012-2014 Jovi Zhangwei .
+ *
+ * Adapted from luajit and lua interpreter.
+ * Copyright (C) 2005-2014 Mike Pall.
+ * Copyright (C) 1994-2008 Lua.org, PUC-Rio.
+ *
+ * ktap is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * ktap is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include
+#include
+#include
+#include
+#include
+#include "kp_obj.h"
+#include "kp_str.h"
+#include "kp_tab.h"
+#include "ktap.h"
+#include "kp_vm.h"
+#include "kp_transport.h"
+
+/* Error message strings. */
+const char *kp_err_allmsg =
+#define ERRDEF(name, msg) msg "\0"
+#include
+;
+
+/* memory allocation flag */
+#define KTAP_ALLOC_FLAGS ((GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN) \
+& ~__GFP_WAIT)
+
+void *kp_malloc(ktap_state_t *ks, int size)
+{
+ void *addr;
+
+ addr = kmalloc(size, KTAP_ALLOC_FLAGS);
+ if (unlikely(!addr)) {
+ kp_error(ks, "kmalloc failed\n");
+ }
+ return addr;
+}
+
+void *kp_zalloc(ktap_state_t *ks, int size)
+{
+ void *addr;
+
+ addr = kzalloc(size, KTAP_ALLOC_FLAGS);
+ if (unlikely(!addr))
+ kp_error(ks, "kzalloc failed\n");
+ return addr;
+}
+
+void kp_free(ktap_state_t *ks, void *addr)
+{
+ kfree(addr);
+}
+
+
+void kp_obj_dump(ktap_state_t *ks, const ktap_val_t *v)
+{
+ switch (itype(v)) {
+ case KTAP_TNIL:
+ kp_puts(ks, "NIL");
+ break;
+ case KTAP_TTRUE:
+ kp_printf(ks, "true");
+ break;
+ case KTAP_TFALSE:
+ kp_printf(ks, "false");
+ break;
+ case KTAP_TNUM:
+ kp_printf(ks, "NUM %ld", nvalue(v));
+ break;
+ case KTAP_TLIGHTUD:
+ kp_printf(ks, "LIGHTUD 0x%lx", (unsigned long)pvalue(v));
+ break;
+ case KTAP_TFUNC:
+ kp_printf(ks, "FUNCTION 0x%lx", (unsigned long)fvalue(v));
+ break;
+ case KTAP_TSTR:
+ kp_printf(ks, "STR #%s", svalue(v));
+ break;
+ case KTAP_TTAB:
+ kp_printf(ks, "TABLE 0x%lx", (unsigned long)hvalue(v));
+ break;
+default:
+ kp_printf(ks, "GCVALUE 0x%lx", (unsigned long)gcvalue(v));
+ break;
+ }
+}
+
+void kp_obj_show(ktap_state_t *ks, const ktap_val_t *v)
+{
+ switch (itype(v)) {
+ case KTAP_TNIL:
+ kp_puts(ks, "nil");
+ break;
+ case KTAP_TTRUE:
+ kp_puts(ks, "true");
+ break;
+ case KTAP_TFALSE:
+ kp_puts(ks, "false");
+ break;
+ case KTAP_TNUM:
+ kp_printf(ks, "%ld", nvalue(v));
+ break;
+ case KTAP_TLIGHTUD:
+ kp_printf(ks, "lightud 0x%lx", (unsigned long)pvalue(v));
+ break;
+ case KTAP_TCFUNC:
+ kp_printf(ks, "cfunction 0x%lx", (unsigned long)fvalue(v));
+ break;
+ case KTAP_TFUNC:
+ kp_printf(ks, "function 0x%lx", (unsigned long)gcvalue(v));
+ break;
+ case KTAP_TSTR:
+ kp_puts(ks, svalue(v));
+ break;
+ case KTAP_TTAB:
+ kp_printf(ks, "table 0x%lx", (unsigned long)hvalue(v));
+ break;
+ case KTAP_TEVENTSTR:
+ /* check event context */
+