<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <HTML ><HEAD ><TITLE >Common Routines</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Unreliable Guide To Hacking The Linux Kernel" HREF="book1.html"><LINK REL="PREVIOUS" TITLE="Recipes for Deadlock" HREF="c127.html"><LINK REL="NEXT" TITLE=" copy_[to/from]_user() / get_user() / put_user() include/asm/uaccess.h " HREF="x159.html"></HEAD ><BODY CLASS="CHAPTER" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Unreliable Guide To Hacking The Linux Kernel</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="c127.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x159.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="CHAPTER" ><H1 ><A NAME="COMMON-ROUTINES" ></A >Common Routines</H1 ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="ROUTINES-PRINTK" ></A ><TT CLASS="FUNCTION" >printk()</TT > <TT CLASS="FILENAME" >include/linux/kernel.h</TT ></H1 ><P > <TT CLASS="FUNCTION" >printk()</TT > feeds kernel messages to the console, dmesg, and the syslog daemon. It is useful for debugging and reporting errors, and can be used inside interrupt context, but use with caution: a machine which has its console flooded with printk messages is unusable. It uses a format string mostly compatible with ANSI C printf, and C string concatenation to give it a first "priority" argument: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >printk(KERN_INFO "i = %u\n", i); </PRE ></TD ></TR ></TABLE ><P > See <TT CLASS="FILENAME" >include/linux/kernel.h</TT >; for other KERN_ values; these are interpreted by syslog as the level. Special case: for printing an IP address use </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >__u32 ipaddress; printk(KERN_INFO "my ip: %d.%d.%d.%d\n", NIPQUAD(ipaddress)); </PRE ></TD ></TR ></TABLE ><P > <TT CLASS="FUNCTION" >printk()</TT > internally uses a 1K buffer and does not catch overruns. Make sure that will be enough. </P ><DIV CLASS="NOTE" ><P ></P ><TABLE CLASS="NOTE" WIDTH="100%" BORDER="0" ><TR ><TD WIDTH="25" ALIGN="CENTER" VALIGN="TOP" ><IMG SRC="./stylesheet-images/note.gif" HSPACE="5" ALT="Note"></TD ><TD ALIGN="LEFT" VALIGN="TOP" ><P > You will know when you are a real kernel hacker when you start typoing printf as printk in your user programs :) </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="NOTE" ><P ></P ><TABLE CLASS="NOTE" WIDTH="100%" BORDER="0" ><TR ><TD WIDTH="25" ALIGN="CENTER" VALIGN="TOP" ><IMG SRC="./stylesheet-images/note.gif" HSPACE="5" ALT="Note"></TD ><TD ALIGN="LEFT" VALIGN="TOP" ><P > Another sidenote: the original Unix Version 6 sources had a comment on top of its printf function: "Printf should not be used for chit-chat". You should follow that advice. </P ></TD ></TR ></TABLE ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="c127.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="book1.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="x159.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Recipes for Deadlock</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" > </TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><TT CLASS="FUNCTION" >copy_[to/from]_user()</TT > / <TT CLASS="FUNCTION" >get_user()</TT > / <TT CLASS="FUNCTION" >put_user()</TT > <TT CLASS="FILENAME" >include/asm/uaccess.h</TT ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >