From: "Mike Frysinger" <vapier.adi@gmail.com>
To: "Robin Getz" <rgetz@blackfin.uclinux.org>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
linux-kernel@vger.kernel.org, "Greg Ungerer" <gerg@snapgear.com>,
"Russell King" <rmk@arm.linux.org.uk>,
"Paul Mundt" <lethal@linux-sh.org>,
"Tim Bird" <tim.bird@am.sony.com>,
bryan.wu@analog.com
Subject: Re: early_printk accessing __log_buf
Date: Sun, 22 Jul 2007 19:50:47 -0400 [thread overview]
Message-ID: <8bd0f97a0707221650r3bab822fq2d457d19eba1c8eb@mail.gmail.com> (raw)
In-Reply-To: <200707182358.24984.rgetz@blackfin.uclinux.org>
[-- Attachment #1: Type: text/plain, Size: 1678 bytes --]
On 7/18/07, Robin Getz <rgetz@blackfin.uclinux.org> wrote:
> On Wed 18 Jul 2007 20:26, Andrew Morton pondered:
> > Robin Getz wrote:
> > > [need to access _log_buf from external for early debugging code]
> > >
> > > Something simple like - early_copy_log_buff(void *dest, size_t n)
> > >
> > > copies n bytes from log_buf to memory area dest. Returns number of
> > > bytes that could not be copied. Can find out how many bytes are in
> > > the log_buff by calling with zero size.
> >
> > When I was at $EARLIER_EMPLOYER, we had code in there to copy the last
> > kilobyte-odd of the log buffer into flash when the box oopsed.
> >
>
> Hmm - I think that if you call with NULL dest, and have it advance the
> pointer, you could do the same thing with something like:
>
> /* see how many bytes are in the buff */
> length = early_copy_log_buff(NULL, NULL);
> /* advance the pointer, so we only copy the last 1k */
> if (length >= 1024 )
> left = early_copy_log_buff(NULL, length - 1024);
> /* copy to temp buffer, to save to flash */
> early_copy_log_buff(buff, 1024);
> save_buff_to_flash(buff);
>
> That way - you can put this in the standard places for failure, and still have
> only one function polluting printk.c (Although if you want to use it for
> failure trapping - it's up for "normal" run time use, and doesn't go into
> __init.
>
> > Probably there are others, but they'll mainly be in the consumer/embedded
> > area, and those sorts of engineers don't read this mailing list much.
>
> Adding a few more 'embedded' folks - who might have some thoughts/opinions.
i think the attached two functions account for what Robin and Andrew
were thinking ...
-mike
[-- Attachment #2: linux-log_buf_read.patch --]
[-- Type: application/octet-stream, Size: 2990 bytes --]
Add two new functions for reading the kernel log buffer. Their intention
is to be used by recovery/dump/debug code so the kernel log can be easily
retrieved/parsed in a crash scenario, but they are generic enough for other
people to dream up other fun uses.
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
---
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 4300bb4..3fcc09e 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -156,6 +156,8 @@ asmlinkage int vprintk(const char *fmt, va_list args)
__attribute__ ((format (printf, 1, 0)));
asmlinkage int printk(const char * fmt, ...)
__attribute__ ((format (printf, 1, 2))) __cold;
+extern int log_buf_read(int len, char *buf, int grab_lock);
+extern int log_buf_read_byte(int idx, char *byte, int grab_lock);
#else
static inline int vprintk(const char *s, va_list args)
__attribute__ ((format (printf, 1, 0)));
@@ -163,6 +165,8 @@ static inline int vprintk(const char *s, va_list args) { return 0; }
static inline int printk(const char *s, ...)
__attribute__ ((format (printf, 1, 2)));
static inline int __cold printk(const char *s, ...) { return 0; }
+static inline int log_buf_read(int len, char *buf, int grab_lock) { return 0; }
+static inline int log_buf_read_byte(int len, char *buf, int grab_lock) { return 0; }
#endif
unsigned long int_sqrt(unsigned long);
diff --git a/kernel/printk.c b/kernel/printk.c
index 051d27e..f08dbf5 100644
--- a/kernel/printk.c
+++ b/kernel/printk.c
@@ -163,6 +163,71 @@ out:
__setup("log_buf_len=", log_buf_len_setup);
/*
+ * Read the log buffer into the supplied buffer. The len option
+ * tells whether to copy from the beginning (> 0), the end (< 0), or
+ * just query the number of existing chars. The number of bytes
+ * actually copied is returned.
+ */
+int log_buf_read(int len, char *buf, int grab_lock)
+{
+ unsigned long start, end;
+ int num_to_copy;
+
+ if (len == 0)
+ return log_end - log_start;
+
+ if (grab_lock)
+ spin_lock_irq(&logbuf_lock);
+
+ num_to_copy = min(abs(len), abs(log_end - log_start));
+
+ if (len < 0) {
+ start = log_end - num_to_copy;
+ end = log_end;
+ } else {
+ start = log_start;
+ end = log_start + num_to_copy;
+ }
+
+ while (start != end)
+ *buf++ = LOG_BUF(start++);
+
+ if (grab_lock)
+ spin_unlock_irq(&logbuf_lock);
+
+ return num_to_copy;
+}
+
+/*
+ * Grab a single byte out of the log buffer. The idx option
+ * tells whether to index from the beginning (>= 0) or the
+ * end (< 0) of the buffer.
+ */
+int log_buf_read_byte(int idx, char *byte, int grab_lock)
+{
+ int ret = 1;
+
+ if (grab_lock)
+ spin_lock_irq(&logbuf_lock);
+
+ if (abs(idx) >= log_buf_len || abs(idx) >= logged_chars) {
+ ret = -1;
+ goto done;
+ }
+
+ if (idx < 0)
+ *byte = LOG_BUF(log_end + idx);
+ else
+ *byte = LOG_BUF(log_start + idx);
+
+ done:
+ if (grab_lock)
+ spin_unlock_irq(&logbuf_lock);
+
+ return ret;
+}
+
+/*
* Commands to do_syslog:
*
* 0 -- Close the log. Currently a NOP.
next prev parent reply other threads:[~2007-07-22 23:51 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-07-18 21:56 Robin Getz
2007-07-18 22:16 ` Andrew Morton
2007-07-18 23:39 ` Robin Getz
2007-07-18 23:53 ` Mike Frysinger
2007-07-19 3:37 ` Robin Getz
2007-07-19 3:47 ` Andrew Morton
2007-07-19 0:26 ` Andrew Morton
2007-07-19 2:26 ` Stephane Couture
2007-07-19 3:58 ` Robin Getz
2007-07-22 23:50 ` Mike Frysinger [this message]
2007-07-23 0:14 ` Paul Mundt
2007-07-23 18:19 ` Robin Getz
2007-07-23 20:15 ` Andrew Morton
2007-07-23 20:54 ` Mike Frysinger
2007-07-23 21:05 ` Andrew Morton
2007-07-23 22:10 ` Mike Frysinger
2007-07-23 22:15 ` Robin Getz
2007-07-23 22:34 ` Andrew Morton
2007-07-24 17:50 ` Robin Getz
2007-07-24 18:12 ` Andrew Morton
2007-07-24 20:12 ` Robin Getz
2007-07-24 21:30 ` Andrew Morton
2007-07-31 7:47 ` Mike Frysinger
2007-07-31 7:59 ` Bryan Wu
2007-07-31 8:11 ` Mike Frysinger
2007-07-31 18:36 ` Andrew Morton
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=8bd0f97a0707221650r3bab822fq2d457d19eba1c8eb@mail.gmail.com \
--to=vapier.adi@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=bryan.wu@analog.com \
--cc=gerg@snapgear.com \
--cc=lethal@linux-sh.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rgetz@blackfin.uclinux.org \
--cc=rmk@arm.linux.org.uk \
--cc=tim.bird@am.sony.com \
/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
all inboxes | Powered by JetHome®