Google Answers Logo
View Question
 
Q: Linux kernel code to write to a file ( Answered,   0 Comments )
Question  
Subject: Linux kernel code to write to a file
Category: Computers > Programming
Asked by: ga1000-ga
List Price: $10.00
Posted: 10 Feb 2003 15:43 PST
Expires: 12 Mar 2003 15:43 PST
Question ID: 159694
Would like to know a short program of Linux kernel code (not user
space) to show me how to write a character or string output to a disk
file.

I currently use printk for debug purpose and have to look at the
output at the message file, which is a very tedious process.

Request for Question Clarification by maniac-ga on 10 Feb 2003 16:06 PST
Hello Ga1000,

Are you sure you *really* want to write to a file from kernel mode? A
quick search of the linux-kernel mailing list
  http://www.uwsg.iu.edu/hypermail/linux/kernel/0005.3/0061.html
suggests what you have asked is difficult and prone to bugs. I can
make such an answer, but it may be easier to suggest a few
alternatives including:

- continue to use printk, but start each message with a unique string
(e.g., ga1000:) and then use
  grep ga1000: /var/log/messages
to get just the messages you want or
  grep ga1000: /var/log/messages > ga1000.txt
to extract them into a file named ga1000.txt.
 - revise the syslog configuration file to capture "kernel debug"
messages in a separate file; mark all your printk messages as debug
messages.
 - review other techniques described in Linux Device Drivers, chapter
4
  http://www.xml.com/ldd/chapter/book/

Would you be interested in such a solution?

  --Maniac

Clarification of Question by ga1000-ga on 10 Feb 2003 16:52 PST
The system message file is too long and may have messages generated
earlier.

I will settle for a file that has the messages from the latest run
only, with some special strings, like 'flag1', 'flag2'.

By the way, the hardware used in an earlier question is an in-house
product, not ready to use, because of the 'missing prompt sign' bug.

My current approach is to implement XON, XOFF, in case there are
missing characters. That may or may not solve the problem.

From my test, a control Z will bring back the prompt sign. It is
possible that the BASH interpreter waits for and does not the control
Z, which happens to be the EOF.

Control C also can bring the prompt sign back. But that is probably a
forced action, not the character the Shell is looking for.

I have asked another question regarding ASCII character meanings.
Answer  
Subject: Re: Linux kernel code to write to a file
Answered By: maniac-ga on 10 Feb 2003 17:31 PST
 
Hello Ga1000,

You are asking for a way to gather debugging messages from the "latest
run".

Using the existing syslog facility, add
  kern.debug    /var/log/kern.debug
to capture the debugging messages to the file /var/log/kern.debug.

Restart the syslogd server to make the changes take effect
  killall -HUP syslogd
(or similar method)

In your driver use
  printk ( KERN_DEBUG "-- your message here--" );
to generate the debug messages. Run your test, review the output in
/var/log/kern.debug. Before starting the next run, you can "rotate"
the log file so you can check the progress of separate runs.

To "rotate" the log file, a command such as logrotate can be used
  logrotate -f -s /var/lib/logrotate.kd.status /etc/logrotate.kd
where /etc/logrotate.kd has contents
  /var/log/kern.debug {
    rotate 10
    daily
    postrotate
      /sbin/killall -HUP syslogd
    endscript
  }
which will keep logfiles for the last 10 tests in separate files named
/var/log/kern.debug.1, .2, etc.

If you need to separate data more finely, unique strings would still
work - use grep on the log file to pull out those specific items.

References:
Syslog.conf man page for Linux:
  http://www.die.net/doc/linux/man/man5/syslog.conf.5.html
Example code using printk ( KERN_DEBUG ...)
  http://www.linuxdevices.com/eljonline/misc/6226l1.html
Man page for logrotate
    http://nodevice.com/sections/ManIndex/man0735.html

  --Maniac
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy