• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

Linux: How to write in filesystem node from kernel for logging the kernel data

Hello experts,

Platform: Linux kernel: 2.6 +
Drivers dealing with: USB drivers, DMA driver

I have following requirement:

1. Create a node in filesystem (/proc or /dev) using proc filesystem or udev.
2. Write to this particular file from the driver mentioned above in all possible modes (Low power mode/ idle mode)
3. Avoid printf / kmsgs as it adds certain amount of delay while writing
4. Access file using cat command and read the data dumped by kernel components into mentioned file
5. During powerup of H/W, clear the file

I have gone through vast documentation available on internet but unable to find right solution to achieve above mentioned requirements. Please help me in identifying correct options of filesystem (udev / sysfs etc) which can be faster and easily accessible from both - kernel or userspace components.

Thanks in advance,

  • 2
1 Solution
First, procfs is virtual filesystem (memory).

How to Read and Write a /proc File
yogithiteAuthor Commented:
Hi Wesly_chen,

Thank you for information. I want to know whether using proc file is the "best" way for logging details from kernel driver? Will it add any delay while logging data in filesystem? Is there any other option for writing data from kernel to any file (by not using kmsg/printf functionality)?

procfs is a pseudo file system, it is used within the kernel  to export access to the FS internal structure to the "user land". it is not created with the regular functions, but you need to write a kernel module (driver).
One of the best books about linux devices is Alessandro Rubini's "Writing linux device drivers", available online here. Take a look. When you have learned all about drivers, you should be able to write your driver, as you request.
BUT since what you ask is something to debug the kernel, why not using the kernel debugging infrastructure? You have many solutions: debugfs, kdbg, etc.
> whether using proc file is the "best" way for logging details from kernel driver?
It depends.
If they are driver parameters/setting (all possible modes :Low power mode/ idle mode), then yes.
If they arre logs, then it'd better to put to /var for the record and tracing.
The link HappyCactus mentioned "Linux device drivers" is good but old.
Here is the newer version
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now