linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Shishkin <alexander.shishkin@linux.intel.com>
To: Peter Zijlstra <a.p.zijlstra@chello.nl>, Ingo Molnar <mingo@redhat.com>
Cc: linux-kernel@vger.kernel.org, vince@deater.net,
	eranian@google.com, johannes@sipsolutions.net,
	Arnaldo Carvalho de Melo <acme@infradead.org>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>
Subject: [PATCH RFC v3 4/6] perf tools: Add a simple JSON parser
Date: Fri, 11 Sep 2015 19:00:03 +0300	[thread overview]
Message-ID: <1441987205-4021-5-git-send-email-alexander.shishkin@linux.intel.com> (raw)
In-Reply-To: <1441987205-4021-1-git-send-email-alexander.shishkin@linux.intel.com>

In order to process kernel's extended syscall error reports, we need a
JSON parser. This one I wrote myself, it should be very simple and
straightforward and extensible when/if somebody needs more features from
it.

Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 tools/include/tools/json.h |  40 ++++++++
 tools/lib/util/json.c      | 250 +++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/Build      |   5 +
 3 files changed, 295 insertions(+)
 create mode 100644 tools/include/tools/json.h
 create mode 100644 tools/lib/util/json.c

diff --git a/tools/include/tools/json.h b/tools/include/tools/json.h
new file mode 100644
index 0000000000..a1684574c2
--- /dev/null
+++ b/tools/include/tools/json.h
@@ -0,0 +1,40 @@
+/*
+ * A simple JSON parser
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program 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.
+ *
+ * This program 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.
+ */
+
+#ifndef _TOOLS_JSON_H
+#define _TOOLS_JSON_H
+
+#define JSON_DEPTH 8
+
+struct json_member {
+	const char	*key;
+	char		*value;
+	unsigned int	size;
+};
+
+struct json_parser {
+	const char		*buffer;
+	const char		*end;
+	const char		*cursor;
+	unsigned int		state;
+	unsigned int		level;
+	unsigned char		stack[JSON_DEPTH];
+	struct json_member	*schema;
+	unsigned int		schema_strict;
+	int			key;
+};
+
+int parse_json(struct json_parser *p);
+
+#endif /* _TOOLS_JSON_H */
diff --git a/tools/lib/util/json.c b/tools/lib/util/json.c
new file mode 100644
index 0000000000..029c244d0e
--- /dev/null
+++ b/tools/lib/util/json.c
@@ -0,0 +1,250 @@
+/*
+ * A simple JSON parser
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program 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.
+ *
+ * This program 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.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <ctype.h>
+
+#include "tools/json.h"
+
+enum {
+	JS_VALUE = 0,
+	JS_SEPARATOR,
+	JS_KEY,
+	JS_END,
+};
+
+enum {
+	OBJECT = 0,
+	ARRAY,
+	LITERAL,
+};
+
+static int got_key(struct json_parser *p, unsigned int back)
+{
+	const char *key = p->cursor - back;
+	int i;
+
+	for (i = 0; p->schema[i].key; i++) {
+		struct json_member *m = &p->schema[i];
+
+		if (!strncmp(key, m->key, strlen(m->key))) {
+			p->key = i;
+			return 0;
+		}
+	}
+
+	p->key = -1;
+	return p->schema_strict ? -EINVAL : 0;
+}
+
+static int got_value(struct json_parser *p, unsigned int back)
+{
+	unsigned int len = back;
+
+	if (p->key < 0)
+		return p->schema_strict ? -EINVAL : 0;
+
+	if (len > p->schema[p->key].size)
+		len = p->schema[p->key].size;
+
+	memcpy(p->schema[p->key].value, p->cursor - back, len);
+	p->schema[p->key].value[len] = 0;
+	p->key = -1;
+	return 0;
+}
+
+static int consume_string(struct json_parser *p)
+{
+	int ret = 0;
+
+	for (p->cursor++; p->cursor < p->end;
+	     p->cursor++, ret++) {
+		switch(*p->cursor) {
+		case '"':
+			goto done;
+		case '\\':
+			p->cursor++;
+			ret++;
+		default:
+			continue;
+		}
+	}
+
+	return -EINVAL;
+
+done:
+	if (p->state == JS_KEY)
+		ret = got_key(p, ret);
+	else if (p->state == JS_VALUE)
+		ret = got_value(p, ret);
+
+	return ret;
+}
+
+static int consume_number(struct json_parser *p)
+{
+	unsigned int hex = 0;
+	int ret = 0;
+
+	for (; p->cursor < p->end;
+	     p->cursor++, ret++) {
+		switch (*p->cursor) {
+		case '.': /* float */
+			if (!ret)
+				return -EINVAL;
+			continue;
+		case 'x': /* hex */
+			if (ret != 1 || *(p->cursor - 1) != '0')
+				return -EINVAL;
+			hex = 1;
+			continue;
+		case '-': /* negative */
+			if (ret)
+				return -EINVAL;
+			continue;
+		default:
+			if (!hex && !isdigit(*p->cursor))
+				goto done;
+			if (hex && !isxdigit(*p->cursor))
+				goto done;
+
+			continue;
+		}
+	}
+
+	return -EINVAL;
+
+done:
+	if (p->state == JS_KEY)
+		return -EINVAL;
+	else if (p->state == JS_VALUE)
+		ret = got_value(p, ret);
+
+	p->cursor--;
+
+	return ret;
+}
+
+int parse_json(struct json_parser *p)
+{
+	int ret;
+
+	p->key = -1;
+	p->level = 0;
+	p->stack[0] = ARRAY;
+
+	for (p->cursor = p->buffer, p->state = JS_VALUE;
+	     p->cursor < p->end; p->cursor++) {
+		switch (*p->cursor) {
+		case ' ':
+		case '\t':
+		case '\n':
+			/* doesn't change state */
+			continue;
+		case '{':
+			if (p->state != JS_VALUE)
+				return -EINVAL;
+
+			p->stack[++p->level] = OBJECT;
+			p->state = JS_KEY;
+			break;
+		case '}':
+			if ((p->state != JS_END && p->state != JS_KEY) ||
+			    !p->level)
+				return -EINVAL;
+
+			p->level--;
+			p->state = JS_END;
+			break;
+		case ':':
+			if (p->state != JS_SEPARATOR)
+				return -EINVAL;
+
+			p->state = JS_VALUE;
+			break;
+		case '[':
+			if (p->state != JS_VALUE)
+				return -EINVAL;
+
+			p->stack[++p->level] = OBJECT;
+			p->state = JS_VALUE;
+			break;
+		case ']':
+			if ((p->state != JS_END && p->state != JS_VALUE) ||
+			    !p->level)
+				return -EINVAL;
+
+			p->level--;
+			/* still JS_END */
+			break;
+		case '"':
+			/* consume everything up to the matching quote */
+			ret = consume_string(p);
+			if (ret < 0)
+				return ret;
+
+			switch (p->state) {
+			case JS_KEY:
+				p->state = JS_SEPARATOR;
+				break;
+			case JS_VALUE:
+				p->state = JS_END;
+				break;
+			default:
+				return -EINVAL;
+			}
+
+			continue;
+		case ',':
+			if (p->state != JS_END)
+				return -EINVAL;
+
+			if (p->stack[p->level] == ARRAY)
+				p->state = JS_VALUE;
+			else /* OBJECT */
+				p->state = JS_KEY;
+			break;
+		default:
+			if (p->state != JS_VALUE)
+				return -EINVAL;
+
+			if (isdigit(*p->cursor) || *p->cursor == '-') {
+				ret = consume_number(p);
+				if (ret < 0)
+					return ret;
+			} else if (!strncasecmp(p->cursor, "null", 4)) {
+				p->cursor += 3;
+				got_value(p, 4);
+			} else if (!strncasecmp(p->cursor, "true", 4)) {
+				p->cursor += 3;
+				got_value(p, 4);
+			} else if (!strncasecmp(p->cursor, "false", 5)) {
+				p->cursor += 4;
+				got_value(p, 5);
+			} else {
+				return -EINVAL;
+			}
+
+			p->state = JS_END;
+			break;
+		}
+	}
+
+	if (p->level || p->state != JS_END)
+		return -EINVAL;
+
+	return 0;
+}
+
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 349bc96ca1..af5acb9a02 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -12,6 +12,7 @@ libperf-y += evsel.o
 libperf-y += exec_cmd.o
 libperf-y += find_next_bit.o
 libperf-y += help.o
+libperf-y += json.o
 libperf-y += kallsyms.o
 libperf-y += levenshtein.o
 libperf-y += llvm-utils.o
@@ -147,6 +148,10 @@ $(OUTPUT)util/find_next_bit.o: ../lib/util/find_next_bit.c FORCE
 	$(call rule_mkdir)
 	$(call if_changed_dep,cc_o_c)
 
+$(OUTPUT)util/json.o: ../lib/util/json.c FORCE
+	$(call rule_mkdir)
+	$(call if_changed_dep,cc_o_c)
+
 $(OUTPUT)util/rbtree.o: ../lib/rbtree.c FORCE
 	$(call rule_mkdir)
 	$(call if_changed_dep,cc_o_c)
-- 
2.5.1


  parent reply	other threads:[~2015-09-11 16:00 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-11 15:59 [PATCH RFC v3 0/6] Introduce extended syscall error reporting Alexander Shishkin
2015-09-11 16:00 ` [PATCH RFC v3 1/6] exterr: " Alexander Shishkin
2015-09-14 20:19   ` Jonathan Corbet
2015-09-15 14:15     ` Alexander Shishkin
2015-09-15 14:31       ` Johannes Berg
2015-09-15 15:24         ` Alexander Shishkin
2015-09-15 16:35           ` Jonathan Corbet
2015-09-15 17:02             ` Johannes Berg
2015-09-14 21:59   ` Jonathan Corbet
2016-01-11 10:34   ` Alexander Shishkin
2015-09-11 16:00 ` [PATCH RFC v3 2/6] perf: Use " Alexander Shishkin
2015-09-11 16:00 ` [PATCH RFC v3 3/6] perf/x86: Annotate a BTS error with extended " Alexander Shishkin
2015-09-11 16:00 ` Alexander Shishkin [this message]
2015-09-11 16:00 ` [PATCH RFC v3 5/6] perf tools: Add userspace counterpart for " Alexander Shishkin
2015-09-11 16:00 ` [PATCH RFC v3 6/6] perf tools: Use extended syscall " Alexander Shishkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1441987205-4021-5-git-send-email-alexander.shishkin@linux.intel.com \
    --to=alexander.shishkin@linux.intel.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@infradead.org \
    --cc=eranian@google.com \
    --cc=johannes@sipsolutions.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=vince@deater.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).