* [PATCH] ramoops: scripts/ramoops.c for extracting oopses
@ 2011-10-05 18:25 Thomas Tuttle
2011-10-05 19:20 ` Valdis.Kletnieks
2011-10-05 20:32 ` Srivatsa S. Bhat
0 siblings, 2 replies; 4+ messages in thread
From: Thomas Tuttle @ 2011-10-05 18:25 UTC (permalink / raw)
To: linux-kernel; +Cc: Marco Stornelli
Add a C program to extract oopses stored by ramoops. Running with no
arguments will list all of the records; running with an argument number
will retrieve the oops from that record, if it is valid.
(In the long term, it'd be ideal to have some kernel interface to
ramoops records, but this script may be useful in the short term.)
BUG=chromium-os:21113
TEST=Adhoc, seems to work :)
Change-Id: I24edf4d09c96a5d89bb75859a83d5427502d4eb6
Signed-off-by: Thomas Tuttle <ttuttle@chromium.org>
---
scripts/ramoops.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 175 insertions(+), 0 deletions(-)
create mode 100644 scripts/ramoops.c
diff --git a/scripts/ramoops.c b/scripts/ramoops.c
new file mode 100644
index 0000000..25cc7c5
--- /dev/null
+++ b/scripts/ramoops.c
@@ -0,0 +1,175 @@
+/*
+ * ramoops: Extract ramoops images from /dev/mem.
+ *
+ * Copyright (C) 2011 Thomas Tuttle <ttuttle@google.com>
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License. See the file COPYING in the main directory of the Linux
+ * distribution for more details.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static char *progn;
+
+static void read_long(char *name, unsigned long *out)
+{
+ FILE *f = fopen(name, "r");
+ if (!f) {
+ fprintf(stderr, "%s: %s: %s\n",
+ progn, name, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ if (fscanf(f, "%lu", out) < 1) {
+ fprintf(stderr, "%s: %s: couldn't read a number\n",
+ progn, name);
+ exit(EXIT_FAILURE);
+ }
+
+ fclose(f);
+}
+
+static const char *MEM_NAME = "/dev/mem";
+
+static FILE *open_mem(void)
+{
+ FILE *mem = fopen(MEM_NAME, "r");
+ if (!mem) {
+ fprintf(stderr, "%s: open %s: %s\n",
+ progn, MEM_NAME, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ return mem;
+}
+
+static void seek_mem(FILE *mem, unsigned long off)
+{
+ if (fseek(mem, off, SEEK_SET) < 0) {
+ fprintf(stderr, "%s: seek %s: %s\n",
+ progn, MEM_NAME, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+}
+
+static void read_mem(FILE *mem, char *buf, unsigned long len)
+{
+ size_t res;
+
+ res = fread(buf, 1, len, mem);
+ if (res == 0 && ferror(mem)) {
+ fprintf(stderr, "%s: read %s: %s\n",
+ progn, MEM_NAME, strerror(errno));
+ exit(EXIT_FAILURE);
+ } else if (res < len) {
+ fprintf(stderr, "%s: read %s: short read\n",
+ progn, MEM_NAME);
+ exit(EXIT_FAILURE);
+ }
+}
+
+static void putsn(const char *s, size_t n)
+{
+ while (*s && n-- > 0)
+ putchar(*s++);
+}
+
+static int check_record(char *buf, char *time_out, size_t time_len)
+{
+ static const char *MAGIC = "====";
+ static const size_t MAGIC_LEN = 4;
+
+ char *s;
+
+ if (memcmp(buf, MAGIC, MAGIC_LEN))
+ return 0;
+
+ s = buf + MAGIC_LEN;
+ while (*s != '\n' && --time_len > 0)
+ *time_out++ = *s++;
+ *time_out = '\0';
+
+ return 1;
+}
+
+static const int REC_LIST = -1;
+
+#define RAMOOPS_PARAM "/sys/module/ramoops/parameters/"
+
+static int print_record(int record, int contents, FILE *mem,
+ unsigned long record_size, char *buf)
+{
+ static const size_t TIME_LEN = 64;
+
+ char time[TIME_LEN];
+
+ read_mem(mem, buf, record_size);
+ fprintf(stderr, "ramoops record %d: ", record);
+ if (check_record(buf, time, TIME_LEN)) {
+ fprintf(stderr, "valid, timestamp %s\n", time);
+ if (contents)
+ putsn(buf, record_size);
+ return 0;
+ } else {
+ fprintf(stderr, "invalid\n");
+ return 1;
+ }
+}
+
+static int print_records(int record)
+{
+ unsigned long mem_address, mem_size, record_size, num_records;
+ FILE *mem;
+ int i;
+ int ret = 0;
+
+ read_long(RAMOOPS_PARAM "mem_address", &mem_address);
+ read_long(RAMOOPS_PARAM "mem_size", &mem_size);
+ read_long(RAMOOPS_PARAM "record_size", &record_size);
+ num_records = mem_size / record_size;
+
+ if (record != REC_LIST &&
+ (record < 0 || (unsigned long)record >= num_records)) {
+ fprintf(stderr, "%s: record number %d out of range [0, %ld)\n",
+ progn, record, num_records);
+ return 1;
+ }
+
+ buf = malloc(record_size);
+ if (!buf) {
+ fprintf(stderr, "%s: failed to allocate record buffer\n",
+ progn);
+ return 1;
+ }
+
+ mem = open_mem();
+ if (record == REC_LIST) {
+ seek_mem(mem, mem_address);
+ for (i = 0; i < (int)num_records; i++)
+ print_record(i, 0, mem, record_size, buf);
+ } else {
+ seek_mem(mem, mem_address + record * record_size);
+ ret = print_record(record, 1, mem, record_size, buf);
+ }
+ fclose(mem);
+
+ return ret;
+}
+
+int main(int argc, char *argv[])
+{
+ progn = argv[0];
+
+ switch (argc) {
+ case 1:
+ return print_records(REC_LIST);
+ case 2:
+ return print_records(atoi(argv[1]));
+ default:
+ fprintf(stderr, "Usage: %s [record]\n", progn);
+ return 1;
+ }
+}
--
1.7.3.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] ramoops: scripts/ramoops.c for extracting oopses
2011-10-05 18:25 [PATCH] ramoops: scripts/ramoops.c for extracting oopses Thomas Tuttle
@ 2011-10-05 19:20 ` Valdis.Kletnieks
2011-10-06 11:22 ` Marco Stornelli
2011-10-05 20:32 ` Srivatsa S. Bhat
1 sibling, 1 reply; 4+ messages in thread
From: Valdis.Kletnieks @ 2011-10-05 19:20 UTC (permalink / raw)
To: Thomas Tuttle; +Cc: linux-kernel, Marco Stornelli
[-- Attachment #1: Type: text/plain, Size: 360 bytes --]
On Wed, 05 Oct 2011 14:25:03 EDT, Thomas Tuttle said:
> Add a C program to extract oopses stored by ramoops. Running with no
> arguments will list all of the records; running with an argument number
> will retrieve the oops from that record, if it is valid.
> + * ramoops: Extract ramoops images from /dev/mem.
How does this play with CONFIG_STRICT_DEVMEM?
[-- Attachment #2: Type: application/pgp-signature, Size: 227 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] ramoops: scripts/ramoops.c for extracting oopses
2011-10-05 19:20 ` Valdis.Kletnieks
@ 2011-10-06 11:22 ` Marco Stornelli
0 siblings, 0 replies; 4+ messages in thread
From: Marco Stornelli @ 2011-10-06 11:22 UTC (permalink / raw)
To: Valdis.Kletnieks; +Cc: Thomas Tuttle, linux-kernel
Hi,
2011/10/5 <Valdis.Kletnieks@vt.edu>:
> On Wed, 05 Oct 2011 14:25:03 EDT, Thomas Tuttle said:
>> Add a C program to extract oopses stored by ramoops. Running with no
>> arguments will list all of the records; running with an argument number
>> will retrieve the oops from that record, if it is valid.
>
>> + * ramoops: Extract ramoops images from /dev/mem.
>
> How does this play with CONFIG_STRICT_DEVMEM?
>
It's be a problem only if the driver uses ioremap exclusive, but it doesn't.
Marco
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] ramoops: scripts/ramoops.c for extracting oopses
2011-10-05 18:25 [PATCH] ramoops: scripts/ramoops.c for extracting oopses Thomas Tuttle
2011-10-05 19:20 ` Valdis.Kletnieks
@ 2011-10-05 20:32 ` Srivatsa S. Bhat
1 sibling, 0 replies; 4+ messages in thread
From: Srivatsa S. Bhat @ 2011-10-05 20:32 UTC (permalink / raw)
To: Thomas Tuttle; +Cc: linux-kernel, Marco Stornelli
Hi,
On 10/05/2011 11:55 PM, Thomas Tuttle wrote:
> +static int print_records(int record)
> +{
> + unsigned long mem_address, mem_size, record_size, num_records;
> + FILE *mem;
> + int i;
> + int ret = 0;
> +
> + read_long(RAMOOPS_PARAM "mem_address", &mem_address);
> + read_long(RAMOOPS_PARAM "mem_size", &mem_size);
> + read_long(RAMOOPS_PARAM "record_size", &record_size);
> + num_records = mem_size / record_size;
> +
> + if (record != REC_LIST &&
> + (record < 0 || (unsigned long)record >= num_records)) {
> + fprintf(stderr, "%s: record number %d out of range [0, %ld)\n",
> + progn, record, num_records);
> + return 1;
> + }
> +
> + buf = malloc(record_size);
Since buf is not declared, this fails program compilation.
> + if (!buf) {
> + fprintf(stderr, "%s: failed to allocate record buffer\n",
> + progn);
> + return 1;
> + }
> +
--
Regards,
Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com>
Linux Technology Center,
IBM India Systems and Technology Lab
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-10-06 11:24 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-05 18:25 [PATCH] ramoops: scripts/ramoops.c for extracting oopses Thomas Tuttle
2011-10-05 19:20 ` Valdis.Kletnieks
2011-10-06 11:22 ` Marco Stornelli
2011-10-05 20:32 ` Srivatsa S. Bhat
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox