2

I would like to prefix my drivers (debug) output with its name, i.e. [myDriver] Actual message. Since it is tiresome to write printk(level NAMEMACRO "Actual message\n") every time I was thinking of overwriting printk/pr_* to actually include the [myDriver] part. However I can not think of a way to do this. In the best case the solution would not force me to change the printk/pr_* calls in the code (With changed calls this becomes trivial).

Is this possible? (Since I included other headers which in turn include the printk header it will always be defined this rules out not linking to the original as suggested in a different so answer)

Are there any reasons why current drivers do not at this to the text? (Is there another way to filter dmesg by driver?)

I am somewhat aware of dev_dbg but I have not found anything dev specific for warnings in general so I will use printk/pr_err for that.

ted
  • 4,791
  • 5
  • 38
  • 84
  • 1
    possible duplicate of [Linux-kernel debug printouts?](http://stackoverflow.com/questions/4991763/linux-kernel-debug-printouts) – CL. Dec 12 '14 at 12:36
  • An alternate to `pr_debug()` is `dev_dbg()` (in ); but some one who cann't see these are the same should not be writing kernel code. – artless noise Dec 12 '14 at 20:33
  • @artlessnoise despite `pr_debug` and `dev_debug` being quite similar, I think one should still differentiate, either just for the [null device check](http://lxr.free-electrons.com/source/drivers/base/core.c?v=3.18#L2088) or for the chance that the implementation might change. That is the same reason why the kernel has many macros that do nothing more than accessing a struct member; separating interface and implementation. While the direct access is the same now, it might change later on and be improved. – ted Dec 19 '14 at 11:02
  • @CL well spotted, and it has the answer I am looking for. – ted Dec 19 '14 at 11:03

2 Answers2

5

Its standard to use pr_{debug,warn,err}() with [drivername] prefixed.

ex:

 pr_debug("kvm: matched tsc offset for %llu\n", data);

Alternatively you can use dev_warn()

ex:

 dev_warn(&adap->dev, "Bus may be unreliable\n");

Is there another way to filter dmesg by driver?

Not unless you want to run dmesg -c to clear the logs, before getting the your driver loaded. Its always recommenced prefixing the driver name in your debug / print messages. As when you receives logs from customers, you don't want to waste time reading through each line manually.

askb
  • 6,501
  • 30
  • 43
  • While the input on the standard format and the reasoning was helpful, I assume there is no easier way, to turn `pr_debug("Message")` into `pr_debug("MyDriver: Message")`? Thats the main part of the question. – ted Dec 11 '14 at 16:46
  • Maybe you could simple do a text substitution. `%s/pr_debug("/pr_debug("MyDriver/gc"` using you favourite editor. – askb Dec 11 '14 at 16:53
2

The relevant answer (found in the duplicate) is to #define pr_fmt (code from the duplicate question linked above):

/* At the top of the file, before any includes */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/printk.h>

As an additional note, if I include variables, sometime pr_fmt is not automatically applied for me. Manual use as in printk(pr_fmt("message %p"), (void*)ptr) fixes those occasions, and adheres to the convention of defining pr_fmt

Since I did not find the duplicate question, I will not delete this question for other googlers like me.

Community
  • 1
  • 1
ted
  • 4,791
  • 5
  • 38
  • 84