public inbox for igt-dev@lists.freedesktop.org
 help / color / mirror / Atom feed
From: Petri Latvala <petri.latvala@intel.com>
To: Lucas De Marchi <lucas.demarchi@intel.com>
Cc: igt-dev@lists.freedesktop.org
Subject: Re: [igt-dev] [PATCH i-g-t] lib/kmod: reimplement kmsg_dump()
Date: Fri, 20 Sep 2019 13:25:00 +0300	[thread overview]
Message-ID: <20190920102500.GZ4019@platvala-desk.ger.corp.intel.com> (raw)
In-Reply-To: <20190920010723.27300-1-lucas.demarchi@intel.com>

On Thu, Sep 19, 2019 at 06:07:23PM -0700, Lucas De Marchi wrote:
> /dev/kmsg is record-oriented rather than line-oriented. A short read in
> which the record doesn't fit will return -EINVAL and getline() will stop
> working. This is in general not a problem since the (default) stdio
> buffer is bigger than the maximum record size (4096). However let's
> abide by the kernel ABI rather than relying on the libc buffer size.
> 
> While reimplementing, fix the following issues that made me look to the
> implementation in the first place:
> 
> 1) we were skipping 1 char in the message, producing messages like the
>    one below (see the missing i in intel_pch_type):
> 	(i915_selftest:3861) igt_kmod-WARNING: ntel_pch_type [i915]] Found Tiger Lake LP PCH
> 
> 2) we were printing the key=val pair of kmsg as if it was part of the
> messsages, and even mangling the result looking for a ':':
> 	(i915_selftest:3861) igt_kmod-WARNING: 000:00:02.0
> 
> The ABI for /dev/kmsg stands:
> 	Accessing the buffer:
> 		Every read() from the opened device node receives one record
> 		of the kernel's printk buffer.
> 
> 		[ ... ]
> 
> 		Future extensions might add more comma separated values before
> 		the terminating ';'. Unknown fields and values should be
> 		gracefully ignored.
> 
> 		The human readable text string starts directly after the ';'
> 		and is terminated by a '\n'. Untrusted values derived from
> 		hardware or other facilities are printed, therefore
> 		all non-printable characters and '\' itself in the log message
> 		are escaped by "\x00" C-style hex encoding.
> 
> 		A line starting with ' ', is a continuation line, adding
> 		key/value pairs to the log message, which provide the machine
> 		readable context of the message, for reliable processing in
> 		userspace.
> 
> Now the line in (1) print as:
> 	(i915_selftest:5070) igt_kmod-WARNING: [drm:intel_pch_type [i915]] Found Tiger Lake LP PCH
> 
> This also fixes a double close on fclose() already closes the file
> descriptor.
> 
> Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
> ---
>  lib/igt_kmod.c | 43 ++++++++++++++++++++++++++-----------------
>  1 file changed, 26 insertions(+), 17 deletions(-)
> 
> diff --git a/lib/igt_kmod.c b/lib/igt_kmod.c
> index 91662eb3..01faca39 100644
> --- a/lib/igt_kmod.c
> +++ b/lib/igt_kmod.c
> @@ -374,25 +374,34 @@ igt_i915_driver_unload(void)
>  
>  static void kmsg_dump(int fd)
>  {
> -	FILE *file;
> -
> -	file = NULL;
> -	if (fd != -1)
> -		file = fdopen(fd, "r");
> -	if (file) {
> -		size_t len = 0;
> -		char *line = NULL;
> -
> -		while (getline(&line, &len, file) != -1) {
> -			char *start = strchr(line, ':');
> -			if (start)
> -				igt_warn("%s", start + 2);
> -		}
> +	char record[4096 + 1];
>  
> -		free(line);
> -		fclose(file);
> -	} else {
> +	if (fd == -1) {
>  		igt_warn("Unable to retrieve kernel log (from /dev/kmsg)\n");
> +		return;
> +	}
> +
> +	record[sizeof(record) - 1] = '\0';
> +
> +	for (;;) {
> +		const char *start, *end;
> +		ssize_t r;
> +
> +		r = read(fd, record, sizeof(record) - 1);
> +		if (r < 0) {
> +			if (errno == EINTR)
> +				continue;
> +			if (errno != EAGAIN)
> +				igt_warn("kmsg truncated due to unknown error: %m\n");
> +			break;
> +		}
> +
> +		start = strchr(record, ';');
> +		if (start) {
> +			start++;
> +			end = strchrnul(start, '\n');
> +			igt_warn("%.*s\n", (int)(end - start), start);
> +		}
>  	}
>  }


Reviewed-by: Petri Latvala <petri.latvala@intel.com>


Can I interest you in refactoring this and parse_dmesg_line() in
runner/resultgen.c together? :P
_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev

  parent reply	other threads:[~2019-09-20 10:25 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-20  1:07 [igt-dev] [PATCH i-g-t] lib/kmod: reimplement kmsg_dump() Lucas De Marchi
2019-09-20  1:37 ` [igt-dev] ✓ Fi.CI.BAT: success for " Patchwork
2019-09-20 10:25 ` Petri Latvala [this message]
2019-09-20 15:35 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork
2019-09-27 20:09 ` [igt-dev] [PATCH i-g-t] " Chris Wilson
2019-09-27 20:24   ` Lucas De Marchi
2019-09-27 20:32     ` Lucas De Marchi
2019-09-27 20:54       ` Chris Wilson
2019-09-27 22:20         ` Lucas De Marchi
2019-09-27 22:23           ` Chris Wilson
2019-09-27 23:34 ` [igt-dev] ✓ Fi.CI.BAT: success for lib/kmod: reimplement kmsg_dump() (rev2) Patchwork
2019-09-28 16:48 ` [igt-dev] ✗ Fi.CI.IGT: failure " Patchwork

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=20190920102500.GZ4019@platvala-desk.ger.corp.intel.com \
    --to=petri.latvala@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=lucas.demarchi@intel.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