<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968" /><title>Chapter 6. Common Routines</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><link rel="home" href="index.html" title="Unreliable Guide To Hacking The Linux Kernel" /><link rel="up" href="index.html" title="Unreliable Guide To Hacking The Linux Kernel" /><link rel="prev" href="ch05.html" title="Chapter 5. Recipes for Deadlock" /><link rel="next" href="ch06s02.html" title="copy_[to/from]_user() / get_user() / put_user() include/asm/uaccess.h" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 6. Common Routines</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch05.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch06s02.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter 6. Common Routines"><div class="titlepage"><div><div><h2 class="title"><a id="common-routines"></a>Chapter 6. Common Routines</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch06.html#routines-printk"> <code class="function">printk()</code> <code class="filename">include/linux/kernel.h</code> </a></span></dt><dt><span class="sect1"><a href="ch06s02.html"> <code class="function">copy_[to/from]_user()</code> / <code class="function">get_user()</code> / <code class="function">put_user()</code> <code class="filename">include/asm/uaccess.h</code> </a></span></dt><dt><span class="sect1"><a href="ch06s03.html"><code class="function">kmalloc()</code>/<code class="function">kfree()</code> <code class="filename">include/linux/slab.h</code></a></span></dt><dt><span class="sect1"><a href="ch06s04.html"><code class="function">current</code> <code class="filename">include/asm/current.h</code></a></span></dt><dt><span class="sect1"><a href="ch06s05.html"><code class="function">mdelay()</code>/<code class="function">udelay()</code> <code class="filename">include/asm/delay.h</code> <code class="filename">include/linux/delay.h</code> </a></span></dt><dt><span class="sect1"><a href="ch06s06.html"><code class="function">cpu_to_be32()</code>/<code class="function">be32_to_cpu()</code>/<code class="function">cpu_to_le32()</code>/<code class="function">le32_to_cpu()</code> <code class="filename">include/asm/byteorder.h</code> </a></span></dt><dt><span class="sect1"><a href="ch06s07.html"><code class="function">local_irq_save()</code>/<code class="function">local_irq_restore()</code> <code class="filename">include/asm/system.h</code> </a></span></dt><dt><span class="sect1"><a href="ch06s08.html"><code class="function">local_bh_disable()</code>/<code class="function">local_bh_enable()</code> <code class="filename">include/linux/interrupt.h</code></a></span></dt><dt><span class="sect1"><a href="ch06s09.html"><code class="function">smp_processor_id</code>() <code class="filename">include/asm/smp.h</code></a></span></dt><dt><span class="sect1"><a href="ch06s10.html"><span class="type">__init</span>/<span class="type">__exit</span>/<span class="type">__initdata</span> <code class="filename">include/linux/init.h</code></a></span></dt><dt><span class="sect1"><a href="ch06s11.html"><code class="function">__initcall()</code>/<code class="function">module_init()</code> <code class="filename">include/linux/init.h</code></a></span></dt><dt><span class="sect1"><a href="ch06s12.html"> <code class="function">module_exit()</code> <code class="filename">include/linux/init.h</code> </a></span></dt><dt><span class="sect1"><a href="ch06s13.html"> <code class="function">try_module_get()</code>/<code class="function">module_put()</code> <code class="filename">include/linux/module.h</code></a></span></dt></dl></div><div class="sect1" title="printk() include/linux/kernel.h"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="routines-printk"></a> <code class="function">printk()</code> <code class="filename">include/linux/kernel.h</code> </h2></div></div></div><p> <code class="function">printk()</code> 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><pre class="programlisting"> printk(KERN_INFO "i = %u\n", i); </pre><p> See <code class="filename">include/linux/kernel.h</code>; for other KERN_ values; these are interpreted by syslog as the level. Special case: for printing an IP address use </p><pre class="programlisting"> __be32 ipaddress; printk(KERN_INFO "my ip: %pI4\n", &ipaddress); </pre><p> <code class="function">printk()</code> internally uses a 1K buffer and does not catch overruns. Make sure that will be enough. </p><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p> You will know when you are a real kernel hacker when you start typoing printf as printk in your user programs :) </p></div><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><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></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch05.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch06s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 5. Recipes for Deadlock </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">  <code class="function">copy_[to/from]_user()</code> / <code class="function">get_user()</code> / <code class="function">put_user()</code> <code class="filename">include/asm/uaccess.h</code> </td></tr></table></div></body></html>