<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Real-Time Embedded</title>
	
	<link>http://www.rt-embedded.com/blog</link>
	<description>Embedded Linux and Software Engineering Blog</description>
	<lastBuildDate>Sun, 29 Jul 2012 03:11:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/rt-embedded" /><feedburner:info uri="rt-embedded" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license><image><link>http://creativecommons.org/licenses/by-nc-sa/3.0/</link><url>http://creativecommons.org/images/public/somerights20.gif</url><title>Some Rights Reserved</title></image><feedburner:emailServiceId>rt-embedded</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Using Linux Netfilter framework</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/oatUVtEMek4/</link>
		<comments>http://www.rt-embedded.com/blog/archives/using-linux-netfilter-framewor/#comments</comments>
		<pubDate>Sat, 14 Jul 2012 00:28:20 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[netfilter hook]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=1191</guid>
		<description><![CDATA[<p style="text-align: justify;">The Linux Netfilter framework is a flexible mechanism that allows you to examine packets during several processing points (for example, when they are received, after processing, before transmission, etc.). The framework is flexible, because you can register (and unregister) your hooks during run-time, and even to select the exact processing point you are [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright" title="filter" src="http://www.rt-embedded.com/blog/wp-content/uploads/2012/07/filter.jpg" alt="" width="200" height="211" />The Linux Netfilter framework is a flexible mechanism that allows you to examine packets during several processing points (for example, when they are received, after processing, before transmission, etc.). The framework is flexible, because you can register (and unregister) your hooks during run-time, and even to select the exact processing point you are interested in. Once you have finished the packet examination, you are able to modify the packet contents, and also either tell the system to continue processing, drop the packet, queue the packet for user space or to handle it yourself (steal it) and abort further processing. The Netfilter framework is very useful for firewall rules, but it is also useful for other advanced uses. In this post, I will show how to enable it in the kernel, discuss about the existing Netfilter hooks in the kernel, and provide examples how to register and implement a Netfilter hook.</p>
<p style="text-align: justify;"><span id="more-1191"></span></p>
<h3 style="text-align: justify;">Netfilter hooks</h3>
<p style="text-align: justify;">The Linux networking sub-system is instrumented with Netfilter hooks in various locations. As per <a title="NF_HOOK netfilter hook" href="http://lxr.linux.no/#linux+v2.6.31/include/linux/netfilter.h#L219" target="_blank">LXR</a>, the hooks are located in the IPv4, IPv6, ARP, DECNET and Bridging code. There are 5 processing points which you can register your hooks:</p>
<ul>
<li>NF_INET_PRE_ROUTING &#8211; Before routing decision</li>
<li>NF_INET_LOCAL_IN &#8211; When locally receiving a packet</li>
<li>NF_INET_FORWARD &#8211; When forwarding a packet</li>
<li>NF_INET_LOCAL_OUT &#8211; When locally transmitting a packet</li>
<li>NF_INET_POST_ROUTING &#8211; After routing decision</li>
</ul>
<p>The hook itself, is a macro, which is defined as follows:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="text-align: left;">
<pre class="code">#define <strong>NF_HOOK</strong>(pf, hook, skb, indev, outdev, okfn)</pre>
</td>
</tr>
</tbody>
</table>
<p>Where:</p>
<ul>
<li>pf &#8211; is the protocol. It defines who calls the hook, so that the correct listeners would be notified. It could be PF_BRIDGE, PF_IPV6, PF_IPV4, etc.</li>
<li>hook &#8211; is one of the processing points listed above.</li>
<li>sbk &#8211; is a pointer to the current packet.</li>
<li>indev &#8211; is the network device that the packet was received in.</li>
<li>outdev &#8211; is the network device that is going to transmit the packet. Note that it could be NULL in some cases, before this decision was made, or if the packet&#8217;s destination is the local machine.</li>
<li>okfn &#8211; is a function which is called when the packet inspection of all listeners is complete, and all of them returned NF_ACCEPT, i.e. this packet is OK, continue processing. The OK Function is the final processing function of the specific networking sub-system. In most cases, the name of this function is <em>xxx_action_finish</em>.</li>
</ul>
<p>In case the Netfilter sub-system is disabled, the <em>NF_HOOK</em> macro is expanded to call the <em>okfn</em> only.</p>
<h3>Enabling Netfilter</h3>
<p style="text-align: justify;">Run &#8220;make menuconfig&#8221;, enter the &#8220;Networking support  &#8212;&gt; &#8221; menu, then enter the &#8220;Networking options  &#8212;&gt;&#8221; menu, and enable the &#8220;Network packet filtering framework (Netfilter)  &#8212;&gt;&#8221; option. Additional configuration is available in this sub-menu, but nothing is needed to be changed for the purpose of enabling basic Netfilter functionality and registering your hooks. You will have to recompile your kernel after this change.</p>
<h3>Registering Netfilter hooks</h3>
<p>The linux/netfilter.h file typedefs the <em>nf_hookfn</em> callback type as follows:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">typedef unsigned int <strong>nf_hookfn</strong>(unsigned int hooknum,
                               struct sk_buff *skb,
                               const struct net_device *in,
                               const struct net_device *out,
                               int (*okfn)(struct sk_buff *));</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">This function type must be implemented by all the listeners of the Netfilter hooks, and it has a similar set of fields to the<em> NF_HOOK</em> macro. I will show a simple implementation later.</p>
<p style="text-align: justify;">The next thing we need to know about is the <em>nf_hook_ops</em> structure, also defined in the linux/netfilter.h file. This structure allows us to register our hook with the framework.</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">struct <strong>nf_hook_ops</strong>
{
        struct list_head list;

        /* User fills in from here down. */
        nf_hookfn *hook;
        struct module *owner;
        u_int8_t pf;
        unsigned int hooknum;
        /* Hooks are ordered in ascending priority. */
        int priority;
};</pre>
</td>
</tr>
</tbody>
</table>
<p>When we want to register a hook, we need to instantiate a variable of this type and use it to register. Here is the information we need to provide:</p>
<ul>
<li>A pointer to our hook implementation (of type <em>nf_hookfn</em>)</li>
<li>A pointer to the owner &#8211; Use &#8220;THIS_MODULE&#8221; as an argument.</li>
<li>The protocol that we are interested in (one of the items I listed above).</li>
<li>The processing point (hook number) we are interested in.</li>
<li>Hook&#8217;s priority in ascending order &#8211; can be used to coordinate hooks ordering.</li>
</ul>
<p style="text-align: justify;">Let&#8217;s register two hooks with the framework. One hook will be called for each incoming packet (local in), and the other will be called for each outgoing packet (local out). Both hooks will be attached to the bridge; this will allow us to examine all packets which are received and transmitted by all the interfaces under the bridge. First, let&#8217;s declare the hooks prototypes:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">static unsigned int <strong>rte_netfilter_local_in_hook</strong>( unsigned int hooknum, 
        struct sk_buff *skb,
        const struct net_device *in, const struct net_device *out,
        int (*okfn)(struct sk_buff *) );

static unsigned int <strong>rte_netfilter_local_out_hook</strong>( unsigned int hooknum, 
        struct sk_buff *skb,
        const struct net_device *in, const struct net_device *out,
        int (*okfn)(struct sk_buff *) );</pre>
</td>
</tr>
</tbody>
</table>
<p>Now, let&#8217;s instantiate the <em>nf_hook_ops</em> structure with our two hooks functions:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">static struct nf_hook_ops rte_hook_ops[] __read_mostly =
{
    {
            .pf = NFPROTO_BRIDGE,
            .priority = 1,
            .hooknum = NF_INET_LOCAL_IN,
            .hook = rte_netfilter_local_in_hook,
            .owner = THIS_MODULE,
    },
    {
            .pf = NFPROTO_BRIDGE,
            .priority = 1,
            .hooknum = NF_INET_LOCAL_OUT,
            .hook = rte_netfilter_local_out_hook,
            .owner = THIS_MODULE,
    }
};</pre>
</td>
</tr>
</tbody>
</table>
<p>Note the <em>__read_mostly </em>declaration that may optimize access to this data. The last step is to call the Netfilter register function. The following example show both the register and unregister calls:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">int <strong>rte_netfilter_init</strong>( void )
{
    int ret;

    <em>/* Register netfilter hooks */</em>
    ret = <strong>nf_register_hook</strong>(&amp;rte_hook_ops[0]);
    ret |= <strong>nf_register_hook</strong>(&amp;rte_hook_ops[1]);

    return ret;
}

void <strong>rte_netfilter_fini</strong>( void )
{
    <em>/* Unregister hooks */</em>
    <strong>nf_unregister_hook</strong>(&amp;rte_hook_ops[0]);
    <strong>nf_unregister_hook</strong>(&amp;rte_hook_ops[1]);
}</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">As soon as <em>nf_register_hook</em> returns successfully, our hooks will be called upon each packet reaching the specified processing point. Don&#8217;t forget to call the <em>rte_netfilter_init</em> function.</p>
<h3>Implementing a Netfilter hook</h3>
<p>In this section, I will provide a basic example which demonstrate a few operations that can be done on a packet. It is really up to you to decide what you want to do in each hook&#8230;</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">#include &lt;linux/kernel.h&gt;
#include &lt;linux/netdevice.h&gt;
#include &lt;linux/etherdevice.h&gt;
#include &lt;net/ip.h&gt;
#include &lt;linux/tcp.h&gt;
#include &lt;linux/udp.h&gt;

unsigned int <strong>rte_netfilter_local_out_hook</strong>( unsigned int hooknum, struct sk_buff *skb,
        const struct net_device *in, const struct net_device *out,
        int(*okfn)( struct sk_buff * ) )
{
    return <strong>NF_ACCEPT</strong>;
}

unsigned int <strong>rte_netfilter_local_in_hook</strong>( unsigned int hooknum, struct sk_buff *skb,
        const struct net_device *in, const struct net_device *out,
        int(*okfn)( struct sk_buff * ) )
{
    struct ethhdr *eth = eth_hdr(skb); <em>/* Get a pointer to the Ethernet header */</em>
    u_int16_t etype;

    printk( "%s: Received a packet %p, device in = %s\n", __func__,
                skb, in ? in-&gt;name : "&lt;NONE&gt;" );

    <em>/* Is this a multicast packet ? */</em>
    if( is_multicast_ether_addr(eth-&gt;h_dest) )
    {
        <em>/* Do something */</em>
        printk( "Packet is multicast\n" );
    }

    if( is_broadcast_ether_addr(eth-&gt;h_dest) )
    {
        <em>/* Do something else */</em>
        printk( "Packet is broadcast\n");
    }

    <em>/* Get EtherType field */</em>
    etype = ntohs( eth-&gt;h_proto );

    if( etype == <strong>ETH_P_IP</strong> )
    {
        struct iphdr *ip = NULL;
        struct udphdr *udp = NULL;
        struct tcphdr *tcp = NULL;

        <em>/* This is an IP packet */</em>
        ip = ip_hdr(skb);
        if (ip == NULL)
        {
            return <strong>NF_ACCEPT</strong>;
        }

        if (ip-&gt;protocol == <strong>IPPROTO_UDP</strong>)
        {
            <em>/* UDP packet */</em>
            udp = (struct udphdr *)(skb_network_header(skb) + ip_hdrlen(skb));

            <em>/* Do something here */</em>
            printk( "UDP packet\n");
        }
        else if (ip-&gt;protocol == <strong>IPPROTO_TCP</strong>)
        {
            <em>/* TCP packet */</em>
            tcp = (struct tcphdr *)(skb_network_header(skb) + ip_hdrlen(skb));

            <em>/* Do something here */</em>
            printk( "TCP packet\n");
        }
    }

   /* We might decide to drop this packet, so we need to:
      * return NF_DROP;
      *
      * If we want to handle this packet ourselves, we need to:
      * return NF_STOLEN;
      */

    return <strong>NF_ACCEPT</strong>;
}</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;"> As we can see, the local out function does not do anything, and accepts all packets (i.e. allowing all packets to be transmitted). The local in function also accepts all packets, however, I added some example code that allows us to examine the Ethernet, IP, TCP and UDP headers (if they exist). It is really up to you to do whatever you want to do with this hook. I also specified 3 types of return values.</p>
<h3>Running our Netfilter hook</h3>
<p>Here&#8217;s the example code running in a loadable kernel module context. We can see plenty of incoming packets on device <em>eth</em>0. One of the packets is a broadcast UDP packet.</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">rte_netfilter_local_in_hook: Received a packet 872ae2c0, device in = eth0
rte_netfilter_local_in_hook: Received a packet 87b75380, device in = eth0
rte_netfilter_local_in_hook: Received a packet 87b74520, device in = eth0
rte_netfilter_local_in_hook: Received a packet 872ade20, device in = eth0
rte_netfilter_local_in_hook: Received a packet 872ade20, device in = eth0
Packet is multicast
Packet is broadcast
UDP packet
rte_netfilter_local_in_hook: Received a packet 87b65ca0, device in = eth0
rte_netfilter_local_in_hook: Received a packet 872aea40, device in = eth0
rte_netfilter_local_in_hook: Received a packet 87b68540, device in = eth0
rte_netfilter_local_in_hook: Received a packet 8787c800, device in = eth0
rte_netfilter_local_in_hook: Received a packet 87b67260, device in = eth0</pre>
</td>
</tr>
</tbody>
</table>
<h3>Additional resources</h3>
<p><a title="Netfilter modules" href="http://inai.de/documents/Netfilter_Modules.pdf" target="_blank">Writing netfilter modules</a><br />
Netfilter.org: <a title="Netfilter howto" href="http://www.netfilter.org/documentation/HOWTO/netfilter-hacking-HOWTO-3.html" target="_blank">Netfilter howto</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/oatUVtEMek4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/using-linux-netfilter-framewor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/using-linux-netfilter-framewor/</feedburner:origLink></item>
		<item>
		<title>Macros in the C programming language</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/lei9XRMN8Gg/</link>
		<comments>http://www.rt-embedded.com/blog/archives/macros-in-the-c-programming-language/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 22:04:46 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[macro]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=1141</guid>
		<description><![CDATA[<p>Macros are very common and often used in C programming to declare constants, such as strings, addresses, or any other values such as maximum number of elements in an array, and so on. For those who are not familiar with them, Macros are declared by the #define keyword. Macros are also used to define some [...]]]></description>
				<content:encoded><![CDATA[<p>Macros are very common and often used in C programming to declare constants, such as strings, addresses, or any other values such as maximum number of elements in an array, and so on. For those who are not familiar with them, Macros are declared by the #define keyword. Macros are also used to define some basic functionality given a set of 1 or more typeless parameters, similarly to an inline function.</p>
<p><span style="text-decoration: underline;">There are some benefits by using Macros:</span></p>
<ol>
<li>Code maintainability: Using a macro to define a constant provides an easy and correct method to ensure that if this value needs to be updated; all instances of this value will be automatically updated. Otherwise, the program may behave incorrectly, and even crash.</li>
<li>Code readability: Specifying a macro name is much more readable and understandable to someone who needs to read the code, than a plain number or address.</li>
</ol>
<p><span style="text-decoration: underline;">There are also some disadvantages:</span></p>
<ol>
<li>Hard to extend: If the macros become complex and a change is required, any errors you introduce may yield vague compilation errors by the compiler, and always, the error line number points to the start of the macro.</li>
<li>Hard to debug: Debuggers often do not provide clear access and step ability to a code inside macros.</li>
</ol>
<div>
<p>In this post, I’ll review some intermediate stuff about macros, and also show how we can use macros to automate code generation (often referred as X-Macro magic).</p>
<p><span id="more-1141"></span></p>
<div>
<h3>Some facts about macros:</h3>
</div>
<div>
<ol>
<li>Macros are expanded from the top of the file, to the bottom.</li>
<li>Macros can be defined, undefined and then redefined.</li>
<li>Macros, unlike other code, are not being compiled. You can write junk stuff as a macro and the file will be compiled. Due to the same reason, macros can contain any text, even if it is not declared or defined yet.</li>
<li>Macros are expanded only when used in the code. If you’ll use a junk macro in the code, it will be converted to junk which will fails your compilation.</li>
<li>Macros can span over only a single line. The only way to extend a macro to another line is by adding a new line marker &#8216;\&#8217; in the end of the line. No other character is allowed after the new line marker.</li>
<li>You can always inspect the expanded macros by using the same compilation command, with the addition of the -E parameter. The output in this case, will be the preprocessed source code, where all the macros are expanded.</li>
</ol>
<h2>Basic Macro usage</h2>
<h3>Macro definition</h3>
<p>In the C programming language, Macros are defined using the <em>#define</em> keyword. A Macro could be just defined, defined as a value, or defined as a piece of code. When it is defined as a value, of instances of this Macro&#8217;s name in the code will be replaced with this value, and only then, the code will be compiled. A Macro can also accept typeless parameter(s) or place holders. These Macro parameters will be replaced by actual variables or statements during macro expansion and then will be compiled. Here are some examples or a simple macro, a macro that holds a value, and a macro with a parameter that implements some logic. The last line shows how to cancel a macro (un-define). Please note that it is very important to have parentheses around every value.</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">#define ENABLE_MY_FEAUTRE
#define MAX_ITERATIONS   (4)
#define IS_POSITIVE( _x ) ( _x &gt; 0 )

#undef ENABLE_MY_FEATURE</pre>
</td>
</tr>
</tbody>
</table>
<h3> Conditional checks</h3>
<p>Macros allow you to apply logic and conditional checks very easily using the <em>#if</em> and <em>#ifdef</em> keywords. You can ask if a macro is defined and implement some specific code for it, apply logic like OR, AND and NOT, and also comparisons like &lt; &gt; = and !=. It is required to end the conditional by an <em>#endif</em> keyword. You can also use #else, and #elif. Here&#8217;s an example:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">#ifdef ENABLE_MY_FEATURE
/* Implement my feature */
...
#endif

#if (MAX_ITERATIONS &gt; 5) &amp;&amp; defined(ENABLE_MY_FEATURE)
/* Implement the better implementation */
#else
/* Do something else */
#endif</pre>
</td>
</tr>
</tbody>
</table>
<h2>Intermediate Macro usage</h2>
<h3>The do-while(0) mystery</h3>
<p>I bet you’ve encountered these weird definitions of macros, especially if you saw a header file from the Linux include directory. This wrapper opens a new name space and performs the loop only once, because the control enters the loop without any condition, and terminates it in the end because the condition is always false. This wrapper is used when the macro performs more than one command, and it ensures that <strong>all of them</strong> are performed, by preventing accidental conditional mistakes.</p>
<p>Let’s look on this example, where we check for a condition, and if true, we will use a macro that we&#8217;ll define shortly. The else is not that important now.</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">if( condition )
    DO_SOMETHING_HERE(x);
else
    ...</pre>
</td>
</tr>
</tbody>
</table>
<p>Now let&#8217;s define the DO_SOMETHING_HERE macro, and expand it ourselves:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>#define DO_SOMETHING_HERE(_x) foo(_x); bar(_x);

if( condition )
    foo(_x); bar(_x);;
else
    ...</pre>
</td>
</tr>
</tbody>
</table>
<p>This is a compilation error because if condition is true, we will call foo, but bar will be always called and the if statement is terminated. But then, the else is not attached to an if. A problem.</p>
<p>Now, if will add curly parentheses around the macro:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">#define DO_SOMETHING_HERE(_x) { foo(_x); bar(_x); } 

if( condition )
    { foo(_x); bar(_x); };
else
    ...</pre>
</td>
</tr>
</tbody>
</table>
<p>We will still have a compilation error because there is an extra semi-colon in the if statement, where there shouldn&#8217;t be one. You shall have to remove it if you want this version to compile.</p>
<p>Now let&#8217;s try the do-while(0) version:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">#define DO_SOMETHING_HERE(_x) do{ foo(_x); bar(_x); }while(0) 

if( condition )
    do{ foo(_x); bar(_x); } while(0);
else
    ...</pre>
</td>
</tr>
</tbody>
</table>
<p>Now, the program compiles and works correctly.</p>
<h3>Where am I?</h3>
<p>When using gcc C compiler (and probably other compilers), there are some built in variables that can tell you which code is currently executing. Although this is not completely macro stuff, I added this here because they are very useful with macros. The following variables are available:</p>
<p>__FUNCTION__: Contains a string of the current function.</p>
<p>__LINE__: Contains the current line number.</p>
<p>__FILE__: Contains the name of the current source file.</p>
<p>If you’ll use these along with printf, you’ll be able to understand the exact origin of any message. Macros for example, can help you find who calls a specific function, and print a message each time it is called. All you have to do is add a ‘__’ prefix to your original function, and create a macro with the original function name. Inside this macro, use the 3 variables I just mentioned in a printf, and then call the original function (now starts with a ‘__’).</p>
<h3>Stringification</h3>
<p>One cool feature that macros can do, is to convert any code text to strings. This is a nice and an automatic way to convert any code text to readable and printable text, and it’s very informative. Basically, all that is required to convert something to a string is to add a ‘#’ sign before. However, it is common to use the following form when converting code to strings:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">#define STR(_x)   #_x
#define XSTR(_x)  STR(_x)</pre>
</td>
</tr>
</tbody>
</table>
<p>There are two stages to this macro expansion. The reason is that if the parameter to XSTR needs to be expanded first, it will be done in this stage and the final output will be the contents of the parameter and not its name.</p>
<p>For example:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">#define RT_EMBEDDED cool

STR(RT_EMBEDDED) /* Results in "RT_EMBEDDED" */
XSTR(RT_EMBEDDED) /* Results in "cool" */</pre>
</td>
</tr>
</tbody>
</table>
<h3>Concatenation</h3>
<p>Another cool feature is the ability to concatenate various code pieces to create new code (i.e. code generation). In order to concatenate, you need to specify the ‘##’, and it is also common usage to wrap this inside a macro. Here&#8217;s an example:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">#define MACRO_CONCAT( _x, _y )   _x##_y
#define FUNC_PROTO( _handler ) int handler_func_##_handler( int );

MACRO_CONCAT( hello, dolly ) /* Results in hellodolly */
FUNC_PROTO( integer ) /* Results in: "int handler_func_integer( int );" It's a function prototype declaration */</pre>
</td>
</tr>
</tbody>
</table>
<p>The MACRO_CONCAT macro will create a new code which is the concatenation of what you write in _x and what you write in _y. Note that the macro can add more text, so the result could be more complex than a simple concatenation, but rather composition of some text along with _x and _y. The FUNC_PROTO macro generates a function prototype from the given keyword.</p>
<h3>Multiple arguments</h3>
<p>Macros also support list of parameters of a dynamic size, similarly to the printf functions group, where the list of arguments to it can be of any size.  It is done by specifying the ‘…’ sign. The following example, taken from the pcd source code,  shows how we can wrap a call to printf to add some more information:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">extern bool_t verboseOutput;

#define PCD_PRINT_PREFIX                            "pcd: "
#define PCD_PRINTF_STDOUT( _format, _args... )        \
    do { if( verboseOutput ) fprintf( stdout, "%s"_format "%s", PCD_PRINT_PREFIX, ##_args, ".\n" ); } while( 0 )</pre>
</td>
</tr>
</tbody>
</table>
<p>When the macro is used, the expanded version check if verboseOutput is true, and only if true, it formats a string to output on the console with the &#8220;pcd :&#8221; prefix. It also adds a point and a new line in the end of the string. In this way, all printouts from this program will have the same look and feel.</p>
<div>
<h3>Return value</h3>
<p>Macros can also be used to do some computation and to “return” a value. This is not actually a classic return value like in a function call, but rather a result of a flow. The following example shows a macro that checks if a number is even or odd. In the following example, the macro&#8217;s return value is a string. We use the string to print our conclusion.</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define IS_EVEN_STR( _x ) \
    ( _x &amp; 1 ? "odd" : "even" )

int main( int argc, char *argv[] )
{   
    int val;

    if(argc&lt;2)
        return;

    /* Convert to integer */
    val = atoi(argv[1]);

    /* Print our conclusion */
    printf( "The number %d is %s\n", val, IS_EVEN_STR(val));

    return 0;
}</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>The output from this program is:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">$ ./even 45
The number 45 is odd
$ ./even 64
The number 64 is even</pre>
</td>
</tr>
</tbody>
</table>
<h2>Advanced Macro usage</h2>
<h3>Assertion</h3>
<p>The following set of macros are very useful for debugging user space applications. You should actually use only the MY_ASSERT macro. The rest are helper macros.  I designed it to operate as follows; in case the assertion fails (the condition is false), the macro will print a very detailed error message, including the file name, function name and line number, and then, it will terminate the program. You can add calls to this macro in every location of your program where a sanity check is required. The macro takes a condition to check, and format with arguments to display a message in case the condition fails.</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>/* Crash the process */
#define __CRASH()    (*(char *)NULL)

/* Generate a textual message about the assertion */
#define __BUG_REPORT( _cond, _format, _args ... ) \
    fprintf( stderr, "%s:%d: Assertion error in function '%s' for condition '%s': " _format "\n", \
    __FILE__, __LINE__, __FUNCTION__, # _cond, ##_args ) &amp;&amp; fflush( NULL ) != (EOF-1)

/* Check a condition, and report and crash in case the condition is false */
#define MY_ASSERT( _cond, _format, _args ... ) \
do { if(!(_cond)) { __CRASH() = __BUG_REPORT( _cond, _format, ##_args ); } } while( 0 )</pre>
</td>
</tr>
</tbody>
</table>
<p>Now let&#8217;s take a look on this short program. It asserts that the number of parameters must be at least 3, but not more than 4. The call to my assertion macro will guarantee this condition, or else, it will display an error message, and terminate the program.</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define MIN_PARAMS 3
#define MAX_PARAMS 4

int main( int argc, char *argv[] )
{
    int params = argc - 1;
    MY_ASSERT( params &gt;= MIN_PARAMS &amp;&amp; params &lt;= MAX_PARAMS,
        "Invalid parameters! must specify at least %d parameters, where %d specified", MIN_PARAMS, params );
    return 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<p>Let&#8217;s see the output for various parameters:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">$ ./macro 1 2
macro.c:21: Assertion error in function 'main' for condition 'params &gt;= 3 &amp;&amp; params &lt;= 5': Invalid parameters! must specify at least 3 parameters, where 2 specified
Segmentation fault
$ ./macro 1 2 3
$ ./macro 1 2 3 4
$ ./macro 1 2 3 4 5
macro.c:21: Assertion error in function 'main' for condition 'params &gt;= 3 &amp;&amp; params &lt;= 4': Invalid parameters! must specify at least 3 parameters, where 5 specified
Segmentation fault</pre>
</td>
</tr>
</tbody>
</table>
<h3>Code Generation</h3>
<p class="MsoNormal">I bet you thought the assertion macro is cool. There&#8217;s even cooler usage of macros, called Code Generation. Code generation is real magic. It may not be readable and maintainable at all, but on the other hand, it may automate many things, prevent human mistakes, and it is very useful when things are added in <strong>patterns</strong>. If you detect that some things you do in the code are repeating, or if you have to do the same or similar action per each object, you may be able to simplify things by using macros.</p>
<p class="MsoNormal">Here&#8217;s an example. Suppose you have a list of keywords. You would like to create an enumeration for these keywords, and for each keyword you need to define a callback function, and a flag.<span>  </span>You would also like to have every keyword as a string for other purposes. Without macros, you will have to do the same actions for each and every new keyword you’ll add. It is also error prone, because there could be a mismatch between the enumeration and other things in case you forgot to add it there.</p>
<p class="MsoNormal">Here are my keywords (taken from the pcd project as an example): RULE, START_COND, COMMAND, END_COND, END_COND_TIMEOUT, FAILURE_ACTION, ACTIVE, SCHED, DAEMON, USER, VERSION and INCLUDE. The last 5 keywords are not mandatory for the implementation &#8211; This is a property flag which is attached to each keyword.</p>
<p class="MsoNormal">The first step we need to do is to create a logical list that will contain all the keywords. Inside the list, we will put each keyword in an internal macro, along with any other information we would like to include with this keyword. In our example it’s the mandatory flag setting. The internal macro is our tool to selectively extract the information provided in each line. Remember, the internal macro is not defined yet. We will define it according to what we would like to generate. This is not a compilation error because the list macro is not expanded until it is used. Note that we are not obligated to use the entire information in each line.</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">/* Keyword,        Mandatory */
#define PCD_PARSER_KEYWORDS \
    PCD_PARSER_KEYWORD( RULE,               1 )\
    PCD_PARSER_KEYWORD( START_COND,         1 )\
    PCD_PARSER_KEYWORD( COMMAND,            1 )\
    PCD_PARSER_KEYWORD( END_COND,           1 )\
    PCD_PARSER_KEYWORD( END_COND_TIMEOUT,   1 )\
    PCD_PARSER_KEYWORD( FAILURE_ACTION,     1 )\
    PCD_PARSER_KEYWORD( ACTIVE,             1 )\
    PCD_PARSER_KEYWORD( SCHED,              0 )\
    PCD_PARSER_KEYWORD( DAEMON,             0 )\
    PCD_PARSER_KEYWORD( USER,               0 )\
    PCD_PARSER_KEYWORD( VERSION,            0 )\
    PCD_PARSER_KEYWORD( INCLUDE,            0 )</pre>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal">Now, we can start generating code out of this list. Let’s start by generating an enumeration in the header file. The following code automatically generates an enum from the list. In this example, we define PCD_PARSER_KEYWORD as a macro that takes two parameters (because that&#8217;s how we declared it on the list), but it actually uses only one, the keyword. Using concatenation with the SET_KEYWORD_ENUM macro, we create a list of names, all of them have the PCD_PARSER_KEYWORD_ prefix.</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">/***********************************************
 * Keyword enumeration
 ***********************************************/
#define SET_KEYWORD_ENUM(x) \
 PCD_PARSER_KEYWORD_##x

#define PCD_PARSER_KEYWORD( keyword, mandatory ) \
 SET_KEYWORD_ENUM( keyword ),

typedef enum parserKeywords_e
{
    PCD_PARSER_KEYWORDS</pre>
<pre class="code">    PCD_PARSER_KEYWORD_LAST

} parserKeywords_e;

#undef PCD_PARSER_KEYWORD</pre>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal">That’s the preprocessed code output (actually, the real output does not have a new line after each keyword, which I added here for cosmetic purposes only. Compilers ignore white spaces anyway):</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">typedef enum parserKeywords_e
{
    PCD_PARSER_KEYWORD_RULE,
    PCD_PARSER_KEYWORD_START_COND,
    PCD_PARSER_KEYWORD_COMMAND,
    PCD_PARSER_KEYWORD_END_COND,
    PCD_PARSER_KEYWORD_END_COND_TIMEOUT,
    PCD_PARSER_KEYWORD_FAILURE_ACTION,
    PCD_PARSER_KEYWORD_ACTIVE, P
    CD_PARSER_KEYWORD_SCHED,
    PCD_PARSER_KEYWORD_DAEMON,
    PCD_PARSER_KEYWORD_USER,
    PCD_PARSER_KEYWORD_VERSION,
    PCD_PARSER_KEYWORD_INCLUDE,

    PCD_PARSER_KEYWORD_LAST

} parserKeywords_e;</pre>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal">Now, we can generate the callback functions’ prototypes. In order to take a keyword and generate a full function declaration, we will have to add for each keyword a prefix (like the return value, and a standard name prefix) and a suffix (function parameters and a semicolon). In this example, all function are static, and all return &#8220;int32_t&#8221;. The common prefix name is PCD_parser_handle_. All functions take a char* line as a single parameter. Here&#8217;s what we do:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">/**************************************************
 * Declarations for the keyword handlers.
 **************************************************/
#define SET_HANDLER_FUNC(x)   PCD_parser_handle_##x
#define PCD_PARSER_KEYWORD( keyword, mandatory )\
    static int32_t SET_HANDLER_FUNC( keyword ) ( char *line );

PCD_PARSER_KEYWORDS

#undef PCD_PARSER_KEYWORD</pre>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal">That’s the preprocessed output (this time I did not add too much new lines):</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">static int32_t PCD_parser_handle_RULE ( char *line ); static int32_t PCD_parser_handle_START_COND ( char *line );
static int32_t PCD_parser_handle_COMMAND ( char *line ); static int32_t PCD_parser_handle_END_COND ( char *line );
static int32_t PCD_parser_handle_END_COND_TIMEOUT ( char *line ); static int32_t PCD_parser_handle_FAILURE_ACTION ( char *line );
static int32_t PCD_parser_handle_ACTIVE ( char *line ); static int32_t PCD_parser_handle_SCHED ( char *line );
static int32_t PCD_parser_handle_DAEMON ( char *line ); static int32_t PCD_parser_handle_USER ( char *line );
static int32_t PCD_parser_handle_VERSION ( char *line ); static int32_t PCD_parser_handle_INCLUDE ( char *line );</pre>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal">Note that you shall have to implement the functions; there is a limit to what a macro can generate. However, if the functions are also made out of patterns, you can generate them as well! Here&#8217;s a hint: for each PCD_PARSER_KEYWORD that we declared in the table, you can use the information in each line to construct an entire function. You can decide what to do in run time for each keyword, based on the information provided in the line, or you can decide in compile time in the following way; If you have for example 2 types of handler functions, you can declare keywords that need the first type of handler using PCD_PARSER_KEYWORD_HANDLER1 macro, and the rest with PCD_PARSER_KEYWORD_HANDLER2 macros. Then, use these macros to generate different functions for each keyword group.</p>
<p class="MsoNormal">Now, in the source file, suppose we have a structure that holds the keyword string name, it’s callback function pointer, and the flag value. It can also hold other information that we do not initialize now. We can construct this structure automatically using the following code. Our internal macro now fills each line of the structure:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">typedef struct configKeywordHandler_t
{
    char      *name;
    int32_t     (*handler)(char *line);</pre>
<pre class="code">    /* set at run time. */
    u_int32_t    parse_flag;</pre>
<pre class="code">    /* indicate if this is a mandatory field. */
    u_int32_t    mandatory_flag;

} configKeywordHandler_t;

/**************************************************************************
 * Initialize keyword array
 **************************************************************************/
#define PCD_PARSER_KEYWORD( keyword, mandatory ) \
 { XSTR( keyword ), SET_HANDLER_FUNC( keyword ), 0, mandatory },

configKeywordHandler_t keywordHandlersList[] =
{
    PCD_PARSER_KEYWORDS
    { NULL,       NULL,          0, 0},
};

#undef PCD_PARSER_KEYWORD</pre>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal">Note that the XSTR call converts the keyword to a string, and the SET_HANDLER_FUNC generates a function name.</p>
<p class="MsoNormal">That’s the code preprocessed output (again, I added new lines after each line. You can see that NULL is expanded to ((void *)0) ):</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">configKeywordHandler_t keywordHandlersList[] =
{
    { "RULE", PCD_parser_handle_RULE, 0, 1 },
    { "START_COND", PCD_parser_handle_START_COND, 0, 1 },
    { "COMMAND", PCD_parser_handle_COMMAND, 0, 1 },
    { "END_COND", PCD_parser_handle_END_COND, 0, 1 },
    { "END_COND_TIMEOUT", PCD_parser_handle_END_COND_TIMEOUT, 0, 1 },
    { "FAILURE_ACTION", PCD_parser_handle_FAILURE_ACTION, 0, 1 },
    { "ACTIVE", PCD_parser_handle_ACTIVE, 0, 1 },
    { "SCHED", PCD_parser_handle_SCHED, 0, 0 },
    { "DAEMON", PCD_parser_handle_DAEMON, 0, 0 },
    { "USER", PCD_parser_handle_USER, 0, 0 },
    { "VERSION", PCD_parser_handle_VERSION, 0, 0 },
    { "INCLUDE", PCD_parser_handle_INCLUDE, 0, 0 },
    { ((void *)0), ((void *)0), 0, 0},
};</pre>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal">What did we gain here?</p>
<ol>
<li>Big amount of generated code that we didn&#8217;t have to write ourselves.</li>
<li>No mistakes, no compilation errors.</li>
<li>Adding a new keyword requires only a single line addition in the main table!</li>
</ol>
<h3 class="MsoNormal">Conclusions</h3>
<p class="MsoNormal">Once you grasp the idea of macros, understand how to see the patterns, and the fact that the macros are not expanded until they are used, you could create wonderful things with these macros, such as this example.</p>
<p class="MsoNormal">You can download the pcd project source code <a title="Process control daemon" href="http://sourceforge.net/projects/pcd/" target="_blank">here</a>, and continue to review the usage of code generation.</p>
<p class="MsoNormal">
</div>
</div>
<p>&nbsp;</p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/lei9XRMN8Gg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/macros-in-the-c-programming-language/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/macros-in-the-c-programming-language/</feedburner:origLink></item>
		<item>
		<title>Resolving/Debugging memory corruption</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/2Mhz7hd4Mrc/</link>
		<comments>http://www.rt-embedded.com/blog/archives/resolving-memory-corruption/#comments</comments>
		<pubDate>Mon, 16 May 2011 15:00:42 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=1110</guid>
		<description><![CDATA[<p style="text-align: justify;">Memory corruption is a scenario where a given buffer (or memory area) is unintentionally modified by an unknown piece of code. This data change causes the rightful users of the buffer to receive bad information which could either modify their behavior or even crash the program. It is usually very hard to find [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-1120" title="bitwise-corruption" src="http://www.rt-embedded.com/blog/wp-content/uploads/2011/05/bitwise-corruption.jpg" alt="" width="168" height="142" />Memory corruption is a scenario where a given buffer (or memory area) is unintentionally modified by an unknown piece of code. This data change causes the rightful users of the buffer to receive bad information which could either modify their behavior or even crash the program. It is usually very hard to find the root cause of memory corruptions because the corruption itself may take place asynchronously to the actual use of the buffer, thus when it actually happens, the crash may occur seconds or minutes later.</p>
<p style="text-align: justify;">In this post I will present one of the ways that may help you catch such corruptions using memory protections.</p>
<p style="text-align: justify;"><span id="more-1110"></span>In most complicated memory corruption scenarios, a buffer is passed along multiple functions, some of them may reside in external libraries (it can get even more complicated if you don&#8217;t have all sources). Due to the asynchronous nature of the effects of memory corruption it won&#8217;t help if you&#8217;ll place a break point when the rightful users access the buffer, because it&#8217;s already too late.</p>
<p style="text-align: justify;">In order to help us catch the code that writes to our buffer, we utilize memory protections. The idea is simple. First, we need to allocate memory for the buffer, then, we need to somehow make it &#8220;read-only&#8221;, and lastly, let the program run using our read-only version of the buffer. When some code tries to write something to it, we will be notified.</p>
<p style="text-align: justify;">Linux allows us to assign different protection properties to different pages in memory. Therefore, we can create our own page, allocate the required amount of memory for our buffer, initialize it with the correct data and then lock it for reading only. Once it is locked, we can pass on this buffer and see what happens. In case some code tries to write to this region, the program will get a <em>SIGSEGV</em> exception and terminate. This will prevent the corruption on one hand, but will not let us know yet who did it. In order to figure it out, we must use a signal handler which will handle this exception and provide some more information. In case your project does not have a process controller, a recommended option is to use <a href="http://www.rt-embedded.com/blog/pcd-process-control-daemon/" target="_blank">pcd</a>; otherwise, you will have to write your own signal handler. With the information provided by the signal handler, we will get the address of the instruction that tried to write to our buffer, and using <em>addr2line</em> application, we could retrieve the source code file name and line. In case you don&#8217;t have the source code, this is a good proof to your library provider, that there is a bug in his library.</p>
<p style="text-align: justify;">As an example, I wrote two helper functions that will help us facilitate this ability. The first one; <em>rte_malloc</em>, has a similar interface to standard <em>malloc</em>. However, instead of allocating the size of your buffer, it allocates a complete page and places the buffer inside of it. The reason is because memory protection can be applied only on pages and the buffer address must be page aligned. The second function; <em>rte_protect</em>, activates the required protection on the buffer, either read, write or both. You can call this function anytime and dynamically change the protection on your buffer during your investigation. Here&#8217;s the source code of these functions:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">/* Tracking buffer abuse using read/write protection.
 * Written by Hai Shalom.
 *
 * http://www.rt-embedded.com/
 */

#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;sys/mman.h&gt;
#include &lt;errno.h&gt;
#include &lt;limits.h&gt;    /* for PAGESIZE */
#ifndef PAGESIZE
#define PAGESIZE 4096
#endif

void *rte_malloc( size_t size )
{
  char *buf;

  /* Allocate memory, reserve space for a full page */
  buf = malloc( size + PAGESIZE - 1 );

  if( !buf )
    return NULL; /* Failed to allocate memory */

  /* Align pointer to a multiple of PAGESIZE */
  buf = (char *)(((int) buf + PAGESIZE - 1 ) &amp; ~( PAGESIZE - 1 ));

  return buf;
}

int rte_protect( void *buf, int protect_read, int protect_write )
{
  int prot = PROT_READ | PROT_WRITE; /* Default buffer protection */

  /* Configure protection */
  if( protect_read )
    prot &amp;= ~PROT_READ;

  if( protect_write )
    prot &amp;= ~PROT_WRITE;

  /* Now, buffer is aligned, apply desired protection */
  return mprotect( buf, PAGESIZE, prot );
}</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Now let&#8217;s write a short program to demonstrate how to use it. It is assumed that the above functions are available to it. I also instrumented it with the pcd&#8217;s exception handlers which you can either use if you have pcd running in your target, or replace with your own implementation:</p>
<p style="text-align: justify;">
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">/* Optional, required only if PCD is used */
#include "pcdapi.h"

#define BUF_SIZE 100

int main( int argc, char *argv[] )
{
    char *buf;
    char c;

    /* Register to the PCD exception handler.
     * Note that this is optional, and added only for
     * getting a detailed signal information.
     */
    PCD_API_REGISTER_EXCEPTION_HANDLERS();

    /* Allocate aligned memory */
    buf = rte_malloc( BUF_SIZE );

    if( !buf )
    {
        perror("Failed to allocate memory");
        return errno;
    }
    else
    {
        printf( "Buffer address = %p\n", buf );
    }

    /* Write OK */
    buf[ 0 ] = 1;

    /* Read OK */
    c = buf[0];

    /* Now apply write protection */
    if( rte_protect( buf, 0, 1 ) )
    {
        perror( "Failed to apply protection" );
        return errno;
    }

    /* Read OK */
    c = buf[ 0 ];

    /* Write protected, expect program to terminate */
    buf[ 0 ] = 1;

    return 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">As we can see from this example, we allocated a buffer in the size of 100, wrote a byte and read a byte. Then, we applied the write protection and tried to repeat these action. The program will receive a SIGSEGV signal when it will try to write the byte in the second time. Now let&#8217;s review the crash dump from pcd (taken in MIPS platform):</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">/tmp # ./protect
Buffer address = 0x412000
/tmp #
**************************************************************************
**************************** Exception Caught ****************************
**************************************************************************

Signal information:

Time: Thu Jan  1 10:55:12 1970
Process name: ./protect
PID: 2283
Fault Address: 0x412000
Signal: Segmentation fault
Signal Code: Invalid permissions for mapped object
Last error: Success (0)
Last error (by signal): 0

MIPS registers:

regmask=0x2ab8d510
status=0x0000003b
pc=0x00400940
zero=0x00000000
at=0x00000001
v0=0x00000000
v1=0x2ab1d000
a0=0x00412000
a1=0x00001000
a2=0x00000001
a3=0x00000000
t0=0x00000000
t1=0x81730470
t2=0x00000000
t3=0x00000000
t4=0x00000000
t5=0x81f145f0
t6=0x00100071
t7=0x81f145f0
s0=0x00412000
s1=0x00000001
s2=0x7fb930b8
s3=0x7fb93174
s4=0x00000001
s5=0x00400614
s6=0x004008c8
s7=0x00000000
t8=0x00000000
t9=0x2ab2a040
k0=0x00000000
k1=0x00000000
gp=0x00418b60
sp=0x7fb93078
fp=0x0045a3ac
ra=0x00400934

Maps file:

00400000-00401000 r-xp 00000000 00:09 170737     /tmp/protect
00410000-00411000 rw-p 00000000 00:09 170737     /tmp/protect
00411000-00412000 rwxp 00000000 00:00 0
00412000-00413000 r--p 00000000 00:00 0
2aaa8000-2aaad000 r-xp 00000000 1f:01 59         /lib/ld-uClibc-0.9.30.so
2aaad000-2aaae000 rw-p 00000000 00:00 0
2aabc000-2aabd000 r--p 00004000 1f:01 59         /lib/ld-uClibc-0.9.30.so
2aabd000-2aabe000 rw-p 00005000 1f:01 59         /lib/ld-uClibc-0.9.30.so
2aabe000-2aac0000 r-xp 00000000 1f:01 77         /lib/libpcd.so
2aac0000-2aacf000 ---p 00000000 00:00 0
2aacf000-2aad0000 rw-p 00001000 1f:01 77         /lib/libpcd.so
2aad0000-2aad2000 r-xp 00000000 1f:01 81         /lib/libipc.so
2aad2000-2aae1000 ---p 00000000 00:00 0
2aae1000-2aae2000 rw-p 00001000 1f:01 81         /lib/libipc.so
2aae2000-2ab0c000 r-xp 00000000 1f:01 83         /lib/libgcc_s.so.1
2ab0c000-2ab1c000 ---p 00000000 00:00 0
2ab1c000-2ab1d000 rw-p 0002a000 1f:01 83         /lib/libgcc_s.so.1
2ab1d000-2ab75000 r-xp 00000000 1f:01 68         /lib/libuClibc-0.9.30.so
2ab75000-2ab84000 ---p 00000000 00:00 0
2ab84000-2ab85000 r--p 00057000 1f:01 68         /lib/libuClibc-0.9.30.so
2ab85000-2ab86000 rw-p 00058000 1f:01 68         /lib/libuClibc-0.9.30.so
2ab86000-2ab8b000 rw-p 00000000 00:00 0
7fb7f000-7fb94000 rwxp 00000000 00:00 0          [stack]

**************************************************************************</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">As we can see from the program printout, the buffer address is <em>0&#215;412000</em>. Now let&#8217;s examine the map file (which is also accessible while the program is running by running cat <em>/proc/&lt;pid&gt;/maps</em>). We can see that there is a region in the range of <em>0&#215;412000 &#8211; 0&#215;413000</em> which has the &#8220;r&#8211;p&#8221; protection bits. This information corresponds to our buffer address and applied protection, and in fact, this line describes the page our buffer was allocated from. Next, we can see that the reported fault address is also <em>0&#215;412000</em>. It means that the program crashed while accessing this address. This also corresponds with our program, because we indeed tried to access <em>buf[ 0 ]</em>. The last piece of information is the program counter register (pc) which holds the address of the instruction that caused the memory write. The pc shows that the instruction address is <em>0&#215;00400940</em>, which suggests that it&#8217;s inside our application and not in any of the linked libraries. We can now use <em>addr2line</em> application to match the address to a file name and line number:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">mips-linux-uclibc-addr2line -e /home/hai/protect 0x00400940
/home/hai/protect.c:93</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Line 93 indeed matches to the write memory access in the program, after the protection was enabled.</p>
<p style="text-align: justify;">For conclusion, although this example was very simple, you can apply the same technique for large programs, by dynamically enabling and disabling the memory protection. For example, disable the protection for pieces of the code that you examined and found out that are OK, and enable for the rest of the program.</p>
<p style="text-align: justify;">This memory protection technique can be also used to resolve other kinds of problems. For example, use after free. Use after free is a scenario where a piece of code tries to access portion of memory that was already freed and might read garbage (although sometimes that right data may still be there, that makes it even worse). In order to do that, replace the call for <em>free</em> by enabling read protection. This will catch the code that tries to access this memory area.</p>
<p style="text-align: justify;">Another example is memory overrun detection. In this case, a piece of code write more bytes than the buffer was designed to hold, and might overwrite some other data. In this case, you can allocate two pages of memory. Enable write protection to the second page, and allocate the buffer from the end of the first page. In this case, when a memory overrun will occur, it will be written in the first byte of the second page, thus triggering the <em>SIGSEGV </em>signal.</p>
<p style="text-align: justify;">You can download the source code for this example <a href="http://www.rt-embedded.com/blog/wp-content/uploads/2011/05/protect.c">here</a>.</p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/2Mhz7hd4Mrc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/resolving-memory-corruption/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/resolving-memory-corruption/</feedburner:origLink></item>
		<item>
		<title>How to write u-boot scripts</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/uvjYtxeNqFg/</link>
		<comments>http://www.rt-embedded.com/blog/archives/u-boot-scripts/#comments</comments>
		<pubDate>Tue, 10 May 2011 00:32:02 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[u-boot]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=1006</guid>
		<description><![CDATA[<p style="text-align: justify;">U-boot is one of the most popular boot-loaders that are used in the industry. It supports plenty of platforms and it&#8217;s open source. There are many other advantages of using u-boot, however, in this post I will present the concept of u-boot scripts (or macros) which could improve your productivity (and your life&#8230;) [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;">U-boot is one of the most popular boot-loaders that are used in the industry. It supports plenty of platforms and it&#8217;s open source. There are many other advantages of using u-boot, however, in this post I will present the concept of u-boot scripts (or macros) which could improve your productivity (and your life&#8230;) if you are a programmer or a user of u-boot and do many repeating operations.</p>
<p style="text-align: justify;">We all know that when we work on a project, we need to perform various repeating operations, such as erasing a partition in the flash, loading an image, loading parameters block, or any other repeating operation. These operations usually are composed of several commands which also require us to type addresses in the memory. This operation is inconvenient and error prone. There are some cases where the memory map changes, and the commands which were used are invalid and need to be updated.</p>
<p style="text-align: justify;"><span id="more-1006"></span>The u-boot supports scripts (or macros), which are similar to batch/shell scripts. Using these scripts, you can define new commands and actually concatenate many other commands to form one multi-step command. These scripts are saved as part of the environment variables and are visible when running the &#8220;<em>printenv</em>&#8221; command. Using the same method, we can also define memory addresses or any other values which will be computed by the code and set in the environment.</p>
<p style="text-align: justify;">Let&#8217;s take an example of how this method could improve your user experience: Loading an image (either kernel image, filesystem or a unified image).  Assuming our RAM address starts at <em>0&#215;80000000 </em>and our image address in the flash starts at <em>0x9f500000</em>, we would have to run the following commands in order to load an image:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre># tftpboot 0x81000000 image.img</pre>
<pre>TFTP from server 192.168.1.10; our IP address is 192.168.1.1
Filename 'image.img'.
Load address: 0x81000000
Loading: #################################################################
 #################################################################
 #################
done
Bytes transferred = 750731 (b748b hex)</pre>
<pre># erase 0x9f500000 +${filesize}</pre>
<pre>Erasing flash... ............
Erased 12 sectors</pre>
<pre># cp.b ${fileaddr} 0x9f500000 ${filesize}</pre>
<pre>Copy to Flash... ...........done</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Note that u-boot initializes the <em>filesize </em>and <em>fileaddr</em> right after the TFTP session terminates. As mentioned, it requires 3 steps, and requires us to know the destination addresses. It might get complicated if we have multiple files to load each time, thus we are required to repeat these commands with different file names and addresses.</p>
<p style="text-align: justify;">As first improvement, we can create a script (in the form of a new environment variable) which will be called <em>update. </em>This script can do this work in a single command:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="text-align: justify;">
<pre># set update "tftpboot 0x81000000 image.img &amp;&amp; erase 0x9f500000 +"\${filesize}" 
       &amp;&amp; cp.b "\${fileaddr}" 0x9f500000 "\${filesize}""</pre>
<pre># saveenv</pre>
</td>
</tr>
</tbody>
</table>
<p>Note that this command is made of concatenating 3 commands using the <em>&amp;&amp; </em>operator, which can also be used in the u-boot prompt. We also saved this command in the environment using the &#8220;saveenv&#8221; command. This may not be applicable for platforms without environment sectors. In this case, you&#8217;ll have to use the other method which I will present later. Now, we can get the image updated in a single command:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<div>
<pre># run update</pre>
<pre>TFTP from server 192.168.1.10; our IP address is 192.168.1.1
Filename 'image.img'.
Load address: 0x81000000
Loading: #################################################################
 #################################################################
 #################
done
Bytes transferred = 750731 (b748b hex)</pre>
<pre>Erasing flash... ............
Erased 12 sectors</pre>
<pre>Copy to Flash... ...........done</pre>
</div>
</td>
</tr>
</tbody>
</table>
<p>This is already better. However, this command has hard coded values for file name and address. The next step is to replace these hard coded values with variables, and using recoursive calls. The addresses and file names can be stored in other environment variables, and we can create another script that initializes these values as parameters to our <em>update</em> script. Here&#8217;s the updated version of <em>update</em> that uses only vairables (assuming the 0&#215;81000000 address never changes). The <em>updatek</em> script updates the kernel image and the <em>updatefs</em> script updates the file system image. Both initialize the appropriate file name and address, and call <em>update</em>.</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre># set update "tftpboot 0x81000000 "\${filename}" &amp;&amp; erase "\${loadaddr}" 
      +"\${filesize}" &amp;&amp; cp.b "\${fileaddr}" "\${loadaddr}" "\${filesize}""</pre>
<pre># set updatek "filename=zImage.uImage loadaddr=0x9f500000 &amp;&amp; run update"</pre>
<pre># set updatefs "filename=rootfs.img loadaddr=0x9f800000 &amp;&amp; run update"</pre>
<pre># saveenv</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Now our <em>update </em>script is generic and can be further enhanced for other images or any other actions. The next step is to plant these scripts inside the u-boot binary image. Otherwise, we will have to manually initialize them in each new target we receive and this is not what we want. Furthermore, platforms without environment variables in the flash may not be able to save them at all.</p>
<p style="text-align: justify;">In u-boot, each platform has a configuration header file which is stored in the <em>include/configs</em> directory. Locate your platform&#8217;s header file by reading the <em>include/config.h </em>file. This file configures your platform, and also configures the default environment variables settings. The u-boot has default variables that will be initialized during the first boot on a new board and we can enhance this list very easily using the <em>CONFIG_EXTRA_ENV_SETTINGS</em> macro. The u-boot code in environment.c file will use all the values in this macro and enhance the default variable settings. For the purpose of our example, we may want to add at least 3 lines to this macro that define <em>update, updatek </em>and <em>updatefs</em>. We can add further variables to define file names and addresses if they are dynamically auto-detected or changed by some logic (for example, if the code detects various flash devices with different sizes). For the example, we will add the following:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>#define ENV_UPDATE \
 "update=tftpboot " MK_STR(CFG_LOAD_ADDR) " ${filename} &amp;&amp; " \
 "erase ${loadaddr} +${filesize} &amp;&amp; "    \
 "cp.b ${fileaddr} ${loadaddr} ${filesize}\0"</pre>
<pre>#define ENV_UPDATE_KERNEL \
 "updatek=loadaddr=0x9f500000 &amp;&amp; filename=zImage.uImage &amp;&amp; " \
 "run update\0"</pre>
<pre>#define ENV_UPDATE_FS \
 "updatefs=loadaddr=0x9f800000 &amp;&amp; filename=rootfs.img &amp;&amp; " \
 "run update\0"</pre>
<pre>#define CONFIG_EXTRA_ENV_SETTINGS \
 ENV_UPDATE \
 ENV_UPDATE_KERNEL \
 ENV_UPDATE_FS \
 ""</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Note that each command must end with a NULL (\0) and the last entry in the <em>CONFIG_EXTRA_ENV_SETTINGS</em> macro must be empty. As I mentioned, the addresses in the macros can be environment variables as well.</p>
<p style="text-align: justify;">Now, after we recompile u-boot, these new 3 macros will be added to the environment variables on new boards, or on all boards without a saved environments. For all other boards, you will have to erase the environment sector and reset to take effect.</p>
<h3 style="text-align: justify;">References</h3>
<p><a href="http://www.denx.de/wiki/U-Boot">http://www.denx.de/wiki/U-Boot</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/uvjYtxeNqFg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/u-boot-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/u-boot-scripts/</feedburner:origLink></item>
		<item>
		<title>How to setup an NFS client on the target</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/GlPLJFQEE2g/</link>
		<comments>http://www.rt-embedded.com/blog/archives/how-to-setup-an-nfs-client-on-the-target/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 23:03:24 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=1014</guid>
		<description><![CDATA[<p style="text-align: justify;">The NFS (Network Filesystem) is a very common Linux feature. It enables any Linux machine to mount directories which are not physically available in the machine&#8217;s hardware, but located somewhere else, and reachable only via a network interface. Mounting an NFS allows us to extend the storage capabilities of the mounting machine beyond [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-1023" title="NFS" src="http://www.rt-embedded.com/blog/wp-content/uploads/2011/02/NFS.jpg" alt="" width="220" height="163" />The NFS (Network Filesystem) is a very common Linux feature. It enables any Linux machine to mount directories which are not physically available in the machine&#8217;s hardware, but located somewhere else, and reachable only via a network interface. Mounting an NFS allows us to extend the storage capabilities of the mounting machine beyond its physical storage limitations. The NFS mount can be used to store additional software or data. For embedded devices, NFS is usually used for debugging purposes, mostly because these products must operate without network availability and must contain all required software in their storage device. With limited storage capabilities, NFS is a great way to extend the machine&#8217;s storage capabilities without changing any hardware. Make sure that you grant access to the IP address of your target to the requested directory.</p>
<p style="text-align: justify;"><span id="more-1014"></span></p>
<h3 style="text-align: justify;">Step 1: Setup an NFS Server</h3>
<p style="text-align: justify;">The first step is to setup an NFS server. There are many good articles of how to do that in any standard Linux distribution, and there are also nice solutions for Windows hosts (although not very recommended). Checkout the reference section, and setup your NFS server. Note that if you don&#8217;t have a Linux machine or are not allowed to change the settings in your existing machine, you can always create a virtual machine and use it as your NFS server. I personally recommend <a href="http://www.virtualbox.org/wiki/Downloads" target="_blank">VirtualBox</a>, which is an open source software from Oracle.</p>
<h3 style="text-align: justify;">Step 2: Enable NFS support in your Kernel</h3>
<p>In order to enable NFS, the Kernel you are using on your target must support it. Usually, this option is disabled by default in order to save space of an unneeded feature. In order to enable it, run &#8220;<em>make menuconfig</em>&#8221; and enter the &#8220;File systems  &#8212;&gt;&#8221; sub menu first.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-1015" title="NFS-Menuconfig" src="http://www.rt-embedded.com/blog/wp-content/uploads/2011/02/NFS-Menuconfig.jpg" alt="" width="764" height="572" /></p>
<p style="text-align: justify;">Select the &#8220;<em>Enable POSIX file locking API</em>&#8220;, which is required for NFS. Make sure there is a * inside the brackets. Now, select the &#8220;<em>Network File Systems  &#8212;&gt;</em>&#8221; option and get inside this sub menu. Enable both &#8220;<em>NFS client support</em>&#8221; and &#8220;<em>NFS client support for NFS version 3</em>&#8220;. Whilst the former enables NFS client support, the latter implements the version 3 of the protocol which might be mandatory for some recent servers. You can also enable the version 4 support if your server supports it, although the code maturity in older kernels is experimental. There is no need to enable the NFS server options here for client support. The NFS server configuration here is only if you want your target machine to be an NFS server, which is not what you want in most cases. Now exit and save your configuration and recompile your kernel.</p>
<h3 style="text-align: justify;">Step 3: Compile port mapper utility</h3>
<p style="text-align: justify;">The port mapper utility is another essential piece of software you must have in order to enable NFS support. It implements RPC (Remote Procedure Call) layer that NFS uses. Currently, there is no support for it in BusyBox, so you must download, compile and install it manually in your target. You can download the latest version <a href="http://neil.brown.name/portmap/" target="_blank">here</a>. In order to compile it for my embedded target, I had to comment the line that includes <em>tcpd.h</em> in <em>pmap_check.c</em> file, and compile it while defining CC to be my cross compiler and NO_TCP_WRAPPER=y. The Makefile generates three executable files: <em>portmap</em>, <em>pmap_dump</em>, and <em>pmap_set</em>. Copy all three to your target&#8217;s file system image in /bin directory.</p>
<h3>Step 4: Enable NFS mount in BusyBox</h3>
<p style="text-align: justify;">By default, the NFS mount is disabled in BusyBox in order to save space. However, we must enable it so it will include the required code to mount an NFS directory. In the BusyBox directory, run &#8220;<em>make menuconfig</em>&#8220;. Enter the &#8220;<em>Linux System Utilities  &#8212;&gt;</em>&#8221; sub menu and enable the &#8220;<em>Support mounting NFS file systems</em>&#8221; option under the &#8220;<em>mount</em>&#8221; option.</p>
<p style="text-align: justify;"><img class="aligncenter size-full wp-image-1018" title="BusyBox-mount-menuconfig" src="http://www.rt-embedded.com/blog/wp-content/uploads/2011/02/BusyBox-mount-menuconfig.jpg" alt="" width="764" height="573" /></p>
<p style="text-align: justify;">Now exit and save your BusyBox configuration and recompile BusyBox.</p>
<h3>Step 5: Runtime configuration</h3>
<p style="text-align: justify;">Once your updated kernel and filesystem are loaded in your embedded device, and your networking interface is up, it is time to mount your NFS directory. Assuming you&#8217;ve done the configuration correctly in your server, all you have to do is the following:</p>
<ul>
<li style="text-align: justify;">Run the port mapper utility: Just type &#8220;<em>portmap</em>&#8221; in your shell.</li>
<li style="text-align: justify;">Create a mounting point; a directory which will be the gate to your NFS directory. You can either create a mounting point in /var/ or /tmp/ directories using the <em>mkdir</em> command. For example: &#8220;<em>mkdir /var/mnt</em>&#8220;.</li>
<li style="text-align: justify;">Mount an NFS directory using the <em>mount</em> command. This command accepts the filesystem type (NFS), the IP address of your NFS server, the source directory and the mount point (destination) directory. For example, assuming my NFS server is on 192.168.1.100, and I exported the /export/hais-stuff, my mount command would be: &#8220;<em>mount -t nfs 192.168.1.100:/export/hais-stuff /var/mnt</em>&#8220;.</li>
</ul>
<p style="text-align: justify;">Now I can change directory to /var/mnt and use it as if it was a standard storage on my embedded device. Note that any file changes you&#8217;ll perform in the server will be seen in the target as well.</p>
<h3 style="text-align: justify;">References</h3>
<p>VirtualBox -<a href="http://www.virtualbox.org/" target="_blank"> http://www.virtualbox.org/</a><br />
Setup an NFS server in Ubuntu &#8211; <a href="https://help.ubuntu.com/community/SettingUpNFSHowTo" target="_blank">https://help.ubuntu.com/community/SettingUpNFSHowTo<br />
</a>Setup an NFS server in Fedora &#8211; <a href="http://www.howtoforge.com/setting-up-an-nfs-server-and-client-on-fedora-13" target="_blank">http://www.howtoforge.com/setting-up-an-nfs-server-and-client-on-fedora-13</a><br />
Port mapper utility &#8211; <a href="http://neil.brown.name/portmap/" target="_blank">http://neil.brown.name/portmap/</a><br />
NFS FAQs &#8211; <a href="http://nfs.sourceforge.net/" target="_blank">http://nfs.sourceforge.net/</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/GlPLJFQEE2g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/how-to-setup-an-nfs-client-on-the-target/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/how-to-setup-an-nfs-client-on-the-target/</feedburner:origLink></item>
		<item>
		<title>A typical embedded system description</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/zGbLBY-zPyE/</link>
		<comments>http://www.rt-embedded.com/blog/archives/typical-embedded-system-description/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 15:32:06 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Reviews]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=960</guid>
		<description><![CDATA[<p style="text-align: justify;">Today&#8217;s consumer electronics, networking and communication devices are small and highly sophisticated computers (or Systems-on-Chips; SoCs). Each chip contains numerous peripherals and special features that require the right software and drivers to drive them correctly and efficiently. In this article, I am going to describe in high level the system&#8217;s hardware components and [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-973" title="board-2" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/09/board-2.jpg" alt="" width="224" height="168" />Today&#8217;s consumer electronics, networking and communication devices are small and highly sophisticated computers (or Systems-on-Chips; <em>SoCs</em>). Each chip contains numerous peripherals and special features that require the right software and drivers to drive them correctly and efficiently. In this article, I am going to describe in high level the system&#8217;s hardware components and the system&#8217;s software components.  I will also describe what they do in high level. This article is meant for people who want to get a high level introduction to the Real-Time Embedded systems.</p>
<p style="text-align: justify;"><span id="more-960"></span></p>
<h4 style="text-align: justify;">The System&#8217;s hardware in high-level</h4>
<p style="text-align: justify;">A final product is based on an optimal board design, which is usually small and contains only the minimum hardware that is required for it. However, development boards (EVMs) are usually larger and contain many debug ports and additional hardware to support the development. An example for it, is the JTAG port which allows to connect a hardware emulator (debugger) that connects a special software in the PC to the target board and allows the user to read and modify memory and registers, and debug the system (steps, breakpoints, etc.). Another useful debug port is the serial console port (RS232) which allows the running programs to display information and logs on the console. There are cases where vendor remove these ports in the final product in order to reduce manufacturing costs and prevent access to hackers.</p>
<p style="text-align: justify;"><img class="aligncenter size-full wp-image-965" title="typical-board" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/09/typical-board.jpg" alt="" width="642" height="452" /></p>
<p style="text-align: justify;">A typical system, as seen in the picture is usually composed of the following items:</p>
<ol>
<li style="text-align: justify;">A target CPU: There are many target CPUs available today, each one with its own characteristics. The CPU is selected according to the requirements of the final product, such as processing power, power consumption, opcode compatibility and more.</li>
<li style="text-align: justify;">Main RAM: The main memory of the system which stores all the code and data in run-time. There are many types of RAM, such as SDRAM, DDR, DDR2, DDR3, etc. where each device has its own characteristics. Similarly to the CPU, the RAM device is selected according to the system&#8217;s requirements and limitations.</li>
<li style="text-align: justify;">Storage device: Many embedded systems store the code in a storage device such as a Flash device or a Disk. There are many types of devices in various speeds, capacities and data retention abilities.</li>
<li style="text-align: justify;">Power connector: Some boards require DC voltage in order to operate, in this case, they will be provided with an external power supply unit and the power connector&#8217;s shape will be round. Other designs has a built-in power supply and need to be connected directly to the mains power. In such designs, the board will have large capacitors and coils to convert and regulate the power. Note that in this case, you need to make sure that the mains voltage in your location matches the board design (connecting 220v to a 110v design could cause electric shock!). Moreover, this design exposes you to electrical shock if you&#8217;ll touch the power supply area, so be careful.</li>
<li style="text-align: justify;">Expansion sockets: Various board designs implement expansion sockets, such as eSATA, PCIe and other proprietary connections that allows the user to enhance the features of the board with minimum hardware changes (for example, connect an LCD display or another Chip).</li>
<li style="text-align: justify;">Debug ports: Such as Serial port (console, RS-232), JTAG port or any other port.</li>
<li style="text-align: justify;">Communication ports: The board in the picture features an Ethernet port, a 4-port Ethernet switch, and a USB host port. Other consumer electronics devices might have different types of ports, such as coaxial, optical, telephony, USB device or any other proprietary port (like in Apple&#8217;s devices). Wireless board devices also contain one or more antennas.</li>
<li style="text-align: justify;">Reset button: Most boards contain such a button, which resets the system, without the need to physically power-cycle it.</li>
<li style="text-align: justify;">LEDs: Most board design contain one or more LEDs (Light-Emitting-Diodes). These small lights are used as indication or action marks that signal a specific activity, according to the design of the system.</li>
</ol>
<h4>The System&#8217;s software in high-level</h4>
<p style="text-align: justify;">Now that we have reviewed the hardware in high-level, let&#8217;s review the software in high-level. A typical system is composed of the following components:</p>
<ol>
<li style="text-align: justify;">A boot-loader: A short binary code which initializes the basic hardware, and prepares the system for the Operating System startup. The boot-loader usually resides in the first sector(s) of the flash (or storage device) and it is usually very small in size (typically 64KB &#8211; 128KB).</li>
<li style="text-align: justify;">An image: A binary image which contains all the code that runs the product. In some cases, the image is composed of a big linked-executable file, and in other cases (such as Linux OS), the image is made of a Kernel and one of more file-systems which hold all the programs and files. Some products require that there will be two images stored on the storage device, for backup and reliability reasons (to cover a case where the first image gets corrupted for any reason).</li>
<li style="text-align: justify;">Miscellaneous: The storage device can contain other parts, such as parameter sectors which can be stored in small sectors on the top or bottom of the flash device (depends on the flash support), or environment variables that configure the boot-loader (used by u-boot). Other systems might have some other miscellaneous blocks for their own special requirements.</li>
</ol>
<h4>A typical boot process</h4>
<p style="text-align: justify;">When powering up the board, the boot ROM code inside the chip does the most basic hardware setup and jumps to the first address of the storage device. This address contains the first command of the system&#8217;s boot-loader. In its turn, the boot-loader initializes the hardware which is required for the board setup, like memory controller, Ethernet port, Serial port or any other system specific peripheral. Once the hardware is initialized, the boot-loader loads the image which implements the system&#8217;s functionality. There are many variations for this stage, depending on the system type and usage. In some systems, the boot-loader uncompresses the compressed image to the RAM and jumps to the first address of the uncompressed image. Systems which run Linux usually skip this stage because the image contains a strap code which is attached to the start of the image, that does the uncompression work. Other systems load the image through the network (usually with TFTP) or through another standard or proprietary bus.</p>
<p style="text-align: justify;">Once the boot-loader jumps to the image&#8217;s first address, it is no longer active, and the control is passed to the Operating system. The boot process of an OS is usually very complex and there are many books about the subject. In general, it initializes the OS sub-systems (such as memory management, file systems, etc) and initializes the rest of uninitialized hardware by calling the drivers&#8217; <em>init </em>functions.</p>
<p style="text-align: justify;">Once the OS has finished its initialization, it starts all the system&#8217;s programs or tasks which implement the product&#8217;s functionality.</p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/zGbLBY-zPyE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/typical-embedded-system-description/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/typical-embedded-system-description/</feedburner:origLink></item>
		<item>
		<title>Bitwise Operations</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/VBi-KOBSN9M/</link>
		<comments>http://www.rt-embedded.com/blog/archives/bitwise-operations/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 13:01:50 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[and]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[left]]></category>
		<category><![CDATA[not]]></category>
		<category><![CDATA[operation]]></category>
		<category><![CDATA[or]]></category>
		<category><![CDATA[right]]></category>
		<category><![CDATA[shift]]></category>
		<category><![CDATA[xor]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=929</guid>
		<description><![CDATA[<p style="text-align: justify;">Bitwise operations are widely used in embedded systems, both in assembly code and in C code. Bitwise operations are used for reading and writing hardware registers, enabling or disabling hardware features, setting or masking interrupts, writing  values to GPIO pins and many other uses. Bitwise operations are different than the corresponding logical operations. [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-thumbnail wp-image-942" title="bitwise" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/08/bitwise-150x150.jpg" alt="" width="150" height="150" />Bitwise operations are widely used in embedded systems, both in assembly code and in C code. Bitwise operations are used for reading and writing hardware registers, enabling or disabling hardware features, setting or masking interrupts, writing  values to GPIO pins and many other uses. Bitwise operations are different than the corresponding logical operations. In this article I will cover the basic bitwise operations and provide some macros for the most common bitwise operations and some common practices when using them.</p>
<p style="text-align: justify;"><span id="more-929"></span></p>
<h4>The OR Operation</h4>
<p style="text-align: justify;">The bitwise OR operation takes two arguments (usually two register values). Its output is composed of set of bits which are either 1&#8242;s in the first argument OR 1&#8242;s in the second argument. For example, given the first argument value (in binary) of 01110000 and second argument value of 10011101, the result would be 11111101. The bitwise OR operation can be treated as a bit level carry-less addition operation, according to the following truth table:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="197" valign="top"><strong>Arg 1</strong></td>
<td width="197" valign="top"><strong>Arg 2</strong></td>
<td width="197" valign="top"><strong>Result</strong></td>
</tr>
<tr>
<td width="197" valign="top">0</td>
<td width="197" valign="top">0</td>
<td width="197" valign="top">0</td>
</tr>
<tr>
<td width="197" valign="top">0</td>
<td width="197" valign="top">1</td>
<td width="197" valign="top">1</td>
</tr>
<tr>
<td width="197" valign="top">1</td>
<td width="197" valign="top">0</td>
<td width="197" valign="top">1</td>
</tr>
<tr>
<td width="197" valign="top">1</td>
<td width="197" valign="top">1</td>
<td width="197" valign="top">1</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The bitwise OR operator is &#8220;|&#8221; (a single pipe), and it has a different meaning than the logical OR operation, which is marked as a double pipe &#8220;||&#8221;.  The Logical OR operation is usually used in conditional clauses, and the Bitwise OR operation is used for changing data (usually, setting 1 bits in a register). For example, suppose that <em>a</em> is an 8-bit register and we want to set the 4 high bits regardless of the actual data it contains, we can use the following command:</p>
<pre style="text-align: justify;">a = a | 0xf0;</pre>
<p style="text-align: justify;">or in short writing:</p>
<pre style="text-align: justify;">a |= 0xf0;</pre>
<p>The Bitwise OR operation precedes the Logical OR operation.</p>
<h4>The AND Operation</h4>
<p style="text-align: justify;">The bitwise AND operation takes two arguments (usually two register values). Its output is composed of set of bits which are 1&#8242;s in the first argument AND 1&#8242;s in the second argument. For example, given the first argument value (in binary) of 01110000 and second argument value of 10011101, the result would be 00010000. The bitwise AND operation can be treated as a bit level multiplication operation, according to the following truth table:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="197" valign="top"><strong>Arg 1</strong></td>
<td width="197" valign="top"><strong>Arg 2</strong></td>
<td width="197" valign="top"><strong>Result</strong></td>
</tr>
<tr>
<td width="197" valign="top">0</td>
<td width="197" valign="top">0</td>
<td width="197" valign="top">0</td>
</tr>
<tr>
<td width="197" valign="top">0</td>
<td width="197" valign="top">1</td>
<td width="197" valign="top">0</td>
</tr>
<tr>
<td width="197" valign="top">1</td>
<td width="197" valign="top">0</td>
<td width="197" valign="top">0</td>
</tr>
<tr>
<td width="197" valign="top">1</td>
<td width="197" valign="top">1</td>
<td width="197" valign="top">1</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The bitwise AND operator is &#8220;&amp;&#8221; (a single ampersand), and it has a different meaning than the logical AND operation, which is marked as a double ampersand &#8220;&amp;&amp;&#8221;.  The Logical AND operation is usually used in conditional clauses, and the Bitwise AND operation is used for changing data (usually, masking or clearing bits in a register). For example, suppose that <em>a</em> is an 8-bit register and we want to clear (or mask) the 4 high bits regardless of the actual data it contains, we can use the following command:</p>
<pre>a = a &amp; 0xf0;</pre>
<p>or in short writing:</p>
<pre>a &amp;= 0xf0;</pre>
<p>The Bitwise AND operation precedes the Logical AND operation.</p>
<h4>The NOT Operation</h4>
<p style="text-align: justify;">The bitwise NOT operation takes one argument (usually a register value). Its output is composed of set of bits which have the opposite value of its input argument. For example, given the first argument value (in binary) of 01110000, the result would be 10001111. The bitwise NOT operation can be treated as a bit level reverse (complement) operation, according to the following truth table:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="197" valign="top"><strong>Arg</strong></td>
<td width="197" valign="top"><strong>Result</strong></td>
</tr>
<tr>
<td width="197" valign="top">0</td>
<td width="197" valign="top">1</td>
</tr>
<tr>
<td width="197" valign="top">1</td>
<td width="197" valign="top">0</td>
</tr>
</tbody>
</table>
<p>The bitwise NOT operator is &#8220;~&#8221; (a Tilda), and it has a different meaning than the logical NOT operation, which is marked as an exclamation mark &#8220;!&#8221;.  The Logical NOT operation is usually used in conditional clauses, and the Bitwise NOT operation is used for reversing a register&#8217;s data. Note that in C, a clause is <em>False</em> when its evaluation is 0, and <em>True </em>if its evaluation is not 0 (it could be any other value which is not 0, not necessarily a 1). Therefore, the <em>True </em>value is evaluated as &#8220;!0&#8243;.  On the contrary, if an 8-bit register <em>a</em> has the value 0 (or 00000000 in binary), and we want to negate all its bits, the !<em>a</em> operation will not negate its bits, but change <em>a</em> to a value that evaluates as <em>True</em>. In order to negate <em>a</em>&#8216;s bits, we should use the ~<em>a</em> command. The result would be 255 (or 11111111 in binary). As an example, we can use the following command:</p>
<pre>a = ~a;</pre>
<p>In order to initialize a register of any size to all 1&#8242;s, use the following syntax:</p>
<pre>a = ~0;</pre>
<p>The Bitwise complement operation precedes the Logical negation operation.</p>
<h4>The Exclusive OR (XOR) Operation</h4>
<p style="text-align: justify;">The bitwise XOR operation takes two arguments (usually two register values). Its output is composed of set of bits which are 1&#8242;s only in bit indexes that the two arguments differ. In other words, an output bit will be set when the two input bits are different. For example, given the first argument value (in binary) of 01110000 and second argument value of 10011101, the result would be 11101101. The bitwise XOR operation can be treated as a bit level addition modulo 2 operation, according to the following truth table:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="197" valign="top"><strong>Arg 1</strong></td>
<td width="197" valign="top"><strong>Arg 2</strong></td>
<td width="197" valign="top"><strong>Result</strong></td>
</tr>
<tr>
<td width="197" valign="top">0</td>
<td width="197" valign="top">0</td>
<td width="197" valign="top">0</td>
</tr>
<tr>
<td width="197" valign="top">0</td>
<td width="197" valign="top">1</td>
<td width="197" valign="top">1</td>
</tr>
<tr>
<td width="197" valign="top">1</td>
<td width="197" valign="top">0</td>
<td width="197" valign="top">1</td>
</tr>
<tr>
<td width="197" valign="top">1</td>
<td width="197" valign="top">1</td>
<td width="197" valign="top">0</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The bitwise XOR operator is &#8220;^&#8221; (an up arrow) register) and it mainly used for computations (such as CRC or parity) and cryptographic functions. Sometimes, it is used as a means to clear a register (think about using XOR-ing a register with itself). As an example, we can use the following command to clear a register:</p>
<pre>a = a ^ a;</pre>
<h4>The Shift Left/Shift Right Operations</h4>
<p style="text-align: justify;">The Shift operations move a register bits either to the left or to the right. The Right-Shift operator is &#8220;&gt;&gt;&#8221; (greater-greater than sign), and the Left-Shift operator is &#8220;&lt;&lt;&#8221; (smaller-smaller than sign). A bit which is moved beyond the register bit capacity (for example, the first bit before doing a right-shift) is lost forever, and a new inserted bit (for example, the first bit right after doing a shift-left) is always assigned with the value 0. The shift operations are mostly used to convert bit indexes to numbers or masks, and have another interesting property; Left shifting is equal to multiplication by 2 and Right shift is equal to division by 2 (without remainder). Repeating these action will raise the power of 2 of the same operation (for example, 1024 &gt;&gt; 3 == 1024 / 2^3 == 128). The compilers are aware of this property, and convert any division or multiplication by the power of 2 to an optimized shift operation.</p>
<h3 style="text-align: justify;">Typical uses and macros for bit setting</h3>
<p>The following section contains various examples for working with bit fields efficiently.</p>
<h4>Bit Mask &#8211; Convert a bit index to a number</h4>
<p>The following macro converts a bit index to a number. It is a simple macro that helps the programmer to write readable code for accessing specific bits in a register.</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre><em>/* Get the bit mask from a given bit index */</em>
<strong>#define BIT_MASK</strong>( bit ) \
    ( 1 &lt;&lt; bit )</pre>
</td>
</tr>
</tbody>
</table>
<p>In many cases, the hardware data sheet specifies which bits to use. Instead of calculating the value and using hard coded numbers, it is a better approach to use this macro. This way the code is written more easily and more readable.</p>
<h4>Read a bit field value</h4>
<p>The following macro reads a bit field value from a specific offset in a register:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre><em>/* Read a bit field value from a register.
   input:
     reg: register
     offset: offset of the bits in the register
     bits: amount of required bits

   output: the required value
*/</em>
<strong>#define READ_VALUE</strong>( reg, offset, bits ) \
    ( ( reg &gt;&gt; offset ) &amp; ( ( 1 &lt;&lt; bits ) - 1 ) )</pre>
</td>
</tr>
</tbody>
</table>
<p>The macro shifts the register value to the required field offset and masks the data to show only the required bits.</p>
<h3>Write a bit field value</h3>
<p>The following macro writes a bit field value to a specific offset in a register:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre><em>/* Write a bit field value to a register.
   input:
     reg: register
     offset: offset of the bits in the register
     bits: amount of required bits
     value: the value to be written

   output: none
*/</em>
<strong>#define WRITE_VALUE</strong>( reg, offset, bits, value ) \
<strong>do</strong> { \
    reg &amp;= ~( ( ( 1 &lt;&lt; bits ) - 1 ) &lt;&lt;  offset ); \
    reg |= ( value &amp; ( ( 1 &lt;&lt; bits ) - 1 ) ) &lt;&lt;  offset; \
} <strong>while</strong> ( 0 );</pre>
</td>
</tr>
</tbody>
</table>
<p>The macro works in two steps. First step clears the required bit field with the amount of the required bits, and the second step writes the value to the register. First it masks the value to fit to the bit field size and then it shifts the value to the correct offset.</p>
<h4>Examples</h4>
<p>Let’s see an example of usage (assuming the macros are defined in rte-bitwise.h header file):</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre><strong>#include</strong> &lt;stdio.h&gt;
<strong>#include</strong> &lt;stdlib.h&gt;
<strong>#include</strong> "rte-bitwise.h"</pre>
<pre><strong>int main</strong>( <strong>int </strong>argc, <strong>char </strong>*argv[] )
{
  <strong>int </strong>a = 0x48F03456;
  <strong>int </strong>b = 0;

  <strong>printf</strong>( "a = 0x%x\n", a );

  <em>/* Write 0x2 in bit field that starts at offset 12, with length 3</em><em> */</em>
  <strong>WRITE_VALUE</strong>( a, 12, 3, 0x2 );

  <strong>printf</strong>( "a = 0x%x\n", a );

  <em>/* Read the value of bit field 12..13 (start at 12, length 2) */</em>
  <strong>printf</strong>( "field 12..13 = 0x%x\n", <strong>READ_VALUE</strong>( a, 12, 2 ) );

  /* Generate a bitmask with 4 bits */
  b = BIT_MASK( 31 ) | BIT_MASK( 12 ) | BIT_MASK( 13 ) | BIT_MASK( 1 );
  <strong>printf</strong>( "b = 0x%x\n", b ); 

  <strong>return </strong>0;
}</pre>
</td>
</tr>
</tbody>
</table>
<p>The number in register a is a random number, which was made for the purpose of the example. Let’s compile and run this program, and see what are the results:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre># ./bitwise
a = 0x48f03456
a = 0x48f02456
field 12..13 = 0x2
b = 0x80003002</pre>
</td>
</tr>
</tbody>
</table>
<p>The results show that the macros work as expected. We can see that bits 12..13 were changed from 3 to 2, and that they are read correctly. We can also see that the mask that was generated matches the bits we requested.</p>
<p style="text-align: justify;">For conclusion, we now understand the difference between logical and bitwise operations. The provided macros are correct and efficient, and it is recommended to use them in your project when accessing hardware registers. If you have other ideas of useful bitwise macro, please write a comment and I&#8217;ll be happy to add them to the post.</p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/VBi-KOBSN9M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/bitwise-operations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/bitwise-operations/</feedburner:origLink></item>
		<item>
		<title>Enabling Core Dumps in Embedded Systems</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/-EbjY42ilbo/</link>
		<comments>http://www.rt-embedded.com/blog/archives/enabling-core-dumps-in-embedded-systems/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 16:02:08 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[core]]></category>
		<category><![CDATA[core_pattern]]></category>
		<category><![CDATA[dump]]></category>
		<category><![CDATA[prctl]]></category>
		<category><![CDATA[PR_SET_DUMPABLE]]></category>
		<category><![CDATA[RLIMIT_CORE]]></category>
		<category><![CDATA[setrlimit]]></category>
		<category><![CDATA[ulimit]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=899</guid>
		<description><![CDATA[<p style="text-align: justify;">Core dumps are the standard Linux way for post-mortem analysis of crashed applications. Given some preconditions, the core dumps can provide a detailed back trace and shed some light about the last whereabouts of the crashed application.</p>
<p style="text-align: justify;">The core dump itself is an executable file format, and it is generated by the [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><a href="http://www.rt-embedded.com/blog/wp-content/uploads/2010/08/Stop.jpg"><img class="alignright size-thumbnail wp-image-905" title="Stop" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/08/Stop-150x150.jpg" alt="" width="150" height="150" /></a>Core dumps are the standard Linux way for post-mortem analysis of crashed applications. Given some preconditions, the core dumps can provide a detailed back trace and shed some light about the last whereabouts of the crashed application.</p>
<p style="text-align: justify;">The core dump itself is an executable file format, and it is generated by the Kernel. The core dump contains a list of memory sections which were accessible by the process, and their memory image. This allows you to analyze the core dump offline, on a host machine, using gdb which was compiled for the appropriate target.</p>
<p style="text-align: justify;">Core dump is not enabled by default in embedded systems mainly due to memory limitations. In this article I will explain what is required in order to enable core dumps support on Embedded Linux/uClibc/Busybox platforms for a specific debug task.</p>
<p style="text-align: justify;"><span id="more-899"></span>As I mentioned, core dumps are not enabled by default in embedded systems, which usually have a very limited storage space and more RAM than non-volatile storage. The following steps describe what is needed in order to enable core dumps in your platform.</p>
<h4 style="text-align: justify;">Enable Kernel support for Core Dumps</h4>
<p>Core dump support in the Kernel can be disabled in compilation time in order to reduce the kernel size. Without this support, no core dump will ever be generated. In order to enable the Kernel&#8217;s support for Core dumps, enable the following option in the Kernel configuration menu:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>General setup  ---&gt;
      Configure standard kernel features (for small systems)  ---&gt;
            Enable ELF core dumps</pre>
</td>
</tr>
</tbody>
</table>
<p>Enabling this option adds about 4KB to the final kernel size.</p>
<h4>Enable debug mode in uClibc</h4>
<p>The uClibc library must be configured differently in order to support the back trace. In case your application is multi-threaded, then you need to enable to following configuration options:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>General Library Settings  ---&gt;
     Build pthreads debugging support</pre>
</td>
</tr>
</tbody>
</table>
<p>Also, configure the uClibc to skip the library stripping, in order to retain its symbols:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>uClibc development/debugging options  ---&gt;
      Strip libraries and executables</pre>
</td>
</tr>
</tbody>
</table>
<h4>Enable Core Dump support in Busybox</h4>
<p>The Busybox provides the <em>init</em> process of the system, which is the father of all processes. The Busybox can be configured to enable core dumps to all its children by inheritance. Actually, it sets the <em>init</em> process as “dumpable”, and sets the core dump size limitation to “unlimited”. All the children of <em>init</em> will inherit these properties and allow core dumps. There are two actions required in order to enable the Busybox support for core dumps. First, enable this option in the Busybox configuration menu:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>Init Utilities  ---&gt;
     Support dumping core for child processes (debugging only)</pre>
</td>
</tr>
</tbody>
</table>
<p>Second, create an empty file in the target’s filesystem’s root directory, called “<em>.init_enable_core</em>“.</p>
<h4>Setup the Core dump size limitation</h4>
<p>In systems without Busybox, it is required to setup the Core dump size limitation. By default, the size is 0, and it means that the Core dump is disabled. In order to get a readable (non-truncated) Core dump, setup the limitation to “unlimited” using the <em>ulimit</em> application:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre># ulimit -c unlimited</pre>
</td>
</tr>
</tbody>
</table>
<h4>Setup the Core file name and path</h4>
<p>By default,  the Core dump file is named <em>core</em>, and it is created in the current working directory of the process. There are cases where a process’s current working directory points to a read only filesystem. In this case, the Core dump file could not be created. Furthermore, the default file name does not indicate what was the process name or id, and by listing the file, you can not which process created it. The <em>/proc/sys/kernel/core_pattern</em> file can be set to define a template that is used to name core dump files. The template can contain % specifiers which are substituted by the following values when a core file is created:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>%%  A single % character
%p  PID of dumped process
%u  real UID of dumped process
%g  real GID of dumped process
%s  number of signal causing dump
%t  time of dump (seconds since 0:00h, 1 Jan 1970)
%h  hostname
%e  executable filename</pre>
</td>
</tr>
</tbody>
</table>
<p>This file allows you to setup a different output directory as well. This option allows you to configure the Kernel to generate the files in a writable filesystem, such as a RAM disk, which could have enough storage space to hold the core dump file. Use “<em>echo “your_pattern</em>” &gt; <em>/proc/sys/kernel/core_pattern</em>” to change the Core dump file name pattern.</p>
<p>The <em>/proc/sys/kernel/core_uses_pid </em>file determines if the Core dump file name will end with .PID.</p>
<h4>Setup the Compilation flags of your debugged Application(s)</h4>
<p>In order to make an application debugable, it must be compiled a bit differently, using some compilation flags that create debug symbols and enable support for stack frame.</p>
<ol>
<li>The applications needs to be compiled with the “<em>-g -ggdb</em>” flags in gcc, in order to enable debug symbols.</li>
<li>The optimization level: Optimization levels higher than -O1 trigger frame pointer omitting, which makes back tracing impossible. The reason is because the frame pointer can be reused for other purposes, and the fact that saving the frame pointer requires additional opcodes. In order to keep the frame pointer in higher optimization levels, use the “<em>-fno-omit-frame-pointer</em>” flag in gcc. If possible, reduce the optimization level to -O1 for the purpose of the debug. Higher optimizations may alter the final binary and omit or inline some functions. The -O0 optimization level might break your application, so I don’t recommend using it.</li>
<li>If the target platform is ARM and the binaries are in Thumb mode, add the following flags in gcc to keep the stack frame in the standard form: “<em>-mtpcs-frame -mtpcs-leaf-frame</em>“.</li>
</ol>
<h4>Enable Core Dumps manually in your Application(s)</h4>
<p>It is possible to set your application as “Dumpable” and its limit as “Unlimited” using standard system calls.</p>
<p>The “Dumpable” state can be set or retrieved by the <em>prctl </em>system call, using the PR_SET_DUMPABLE and PR_GET_DUMPABLE flags accordingly. When using the set command, the second argument determines if the process is “Dumpable” (1-yes, 0-no). Here’s an example code snippet:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>{
    #include &lt;sys/prctl.h&gt;

    <strong>int</strong> ret;

    /* Get the Dumpable state */
    ret = <strong>prctl</strong>( PR_GET_DUMPABLE, 0, 0, 0, 0 );
    <strong>printf</strong>( "PR_GET_DUMPABLE returned %d", ret );

    /* Set the Dumpable state to be enabled */
    ret = <strong>prctl</strong>( PR_SET_DUMPABLE, 1, 0, 0, 0 );
    <strong>printf</strong>( "PR_SET_DUMPABLE returned %d", ret );

    /* Get the Dumpable state again, make sure it was set */
    ret = <strong>prctl</strong>( PR_GET_DUMPABLE, 0, 0, 0, 0 );
    <strong>printf</strong>( "PR_GET_DUMPABLE returned %d", ret );
}</pre>
</td>
</tr>
</tbody>
</table>
<p>The limitation of a core dump can be set manually by the application using the <em>setrlimit</em> function call, using the RLIMIT_CORE flag. It receives a structure with two fields. The first sets the current limitation, and the second sets the maximum allowed limitation. The fields accept any number, and RLIM_INFINITY to specify “Unlimited”. Here’s an example code snippet:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>{
    #include &lt;sys/time.h&gt;
    #include &lt;sys/resource.h&gt;

    <strong>int</strong> ret;
    <strong>struct</strong> rlimit rlim;

    /* Get the core dump limitation */
    ret = <strong>getrlimit</strong>(RLIMIT_CORE, &amp;rlim);
    <strong>printf</strong>( "RLIMIT_CORE returned %d (%d, %d)", ret, rlim.rlim_cur, rlim.rlim_max );

    /* Set the core dump limitation to be unlimited */
    rlim.rlim_cur = RLIM_INFINITY;
    rlim.rlim_max = RLIM_INFINITY;
    ret = <strong>setrlimit</strong>(RLIMIT_CORE, &amp;rlim);
    <strong>printf</strong>( "RLIMIT_CORE returned %d", ret );

    /* Get the core dump limitation again */
    ret = <strong>getrlimit</strong>(RLIMIT_CORE, &amp;rlim);
    <strong>printf</strong>( "RLIMIT_CORE returned %d (%d, %d)", ret, rlim.rlim_cur, rlim.rlim_max );
}</pre>
</td>
</tr>
</tbody>
</table>
<h4>Custom Signal Handlers</h4>
<p>Unfortunately, if your application is equipped with a customized signal handler, no core dump will be generated, because it is generated only by the default signal handlers. In case your application has a custom signal handler, disable it before starting to debug, otherwise, no core dump will be generated. Some sources in the Internet mention that restoring the default signal handler inside the signal handler <em>after</em> the exception has occurred, and sending it again in a loopback can trigger a core dump. In the tests I did, it did generate a core dump, but the only thing I saw in the core dump was the code that my handler executed (i.e. the calls to signal and kill), so this did not help me. Perhaps on other platforms this trick works better.</p>
<h3>Conclusions</h3>
<p>There are many actions you need to do in order to enable core dumps in Embedded Linux system. Most of them can be automated in your Makefiles and configuration system when a Debug-Mode option is enabled. When a process crashes and a Core dump was generated successfully, the following message will appear:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>Segmentation fault (core dumped)</pre>
</td>
</tr>
</tbody>
</table>
<p>In the next step, download the generated core dump to the host machine and run gdb in order to analyze it. Use the <em>core</em> command to load it, and <em>file</em> command to load the symbols of your application. Use the <em>bt full</em> command to get a complete back trace.</p>
<h4>Resources</h4>
<p><a href="http://linux.die.net/man/2/prctl" target="_blank">http://linux.die.net/man/2/prctl</a><br />
<a href="http://linux.die.net/man/7/signal" target="_blank">http://linux.die.net/man/7/signal</a><br />
<a href="http://linux.die.net/man/2/setrlimit" target="_blank">http://linux.die.net/man/2/setrlimit</a><br />
<a href="http://linux.die.net/man/5/core" target="_blank">http://linux.die.net/man/5/core</a><br />
<a href="http://www.gentoo.org/proj/en/qa/backtraces.xml" target="_self">http://www.gentoo.org/proj/en/qa/backtraces.xml</a><br />
<a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka12961.html" target="_self">http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka12961.html</a></p>
<p>Coretrace debugger tool: <a href="http://www.arbetsmyra.dyndns.org/coretrace/" target="_blank">http://www.arbetsmyra.dyndns.org/coretrace/</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/-EbjY42ilbo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/enabling-core-dumps-in-embedded-systems/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/enabling-core-dumps-in-embedded-systems/</feedburner:origLink></item>
		<item>
		<title>Open-Source Licenses</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/PK7sI-nmkoY/</link>
		<comments>http://www.rt-embedded.com/blog/archives/open-source-licenses/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 04:15:07 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Reviews]]></category>
		<category><![CDATA[bsd]]></category>
		<category><![CDATA[CreativeCommons]]></category>
		<category><![CDATA[general]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[gpl]]></category>
		<category><![CDATA[lgpl]]></category>
		<category><![CDATA[license]]></category>
		<category><![CDATA[mit]]></category>
		<category><![CDATA[open]]></category>
		<category><![CDATA[public]]></category>
		<category><![CDATA[source]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=752</guid>
		<description><![CDATA[<p style="text-align: justify;">The Open Source concept is not new, and has been studied and researched by professionals and by the academia for quite a while.  Open Source software products are the outcome of collaboration and cooperation of individuals programmers from all over the world, who gather in communities.  The communities are non-profit organizations that distribute [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-756" title="heckert_gnu_small" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/heckert_gnu_small.png" alt="" width="145" height="140" />The Open Source concept is not new, and has been studied and researched by professionals and by the academia for quite a while.  Open Source software products are the outcome of collaboration and cooperation of individuals programmers from all over the world, who gather in communities.  The communities are non-profit organizations that distribute and support their software without any charge. The term Open Source, also referred as Free Software, constitutes the main concept of the Open Source movement, which believes that the user must have the rights to use, modify or redistribute the software as he wishes, while granting the same rights to the users of his derivative work or redistribution. The term &#8220;Free&#8221; is referred as &#8220;Free Speech&#8221; and not &#8220;Free Beer&#8221;.</p>
<p style="text-align: justify;">Nowadays, Open Source software is widely used by for-profit organizations. In this article, I will provide an overview of the most used Open Source Licenses, the rights that they grant, and the obligations they require.</p>
<p style="text-align: justify;"><span id="more-752"></span></p>
<p style="text-align: justify;">There are two main Open Source organizations that support the Free/Open Source concept:</p>
<ul>
<li><a href="http://www.fsf.org/" target="_blank">The Free Software Foundation</a> &#8211; Was founded in 1985 and promotes the idea of the freedom for the users, to use, modify, copy and redistribute the software without restrictions.</li>
<li><a href="http://www.opensource.org/" target="_blank">The Open Source Initiative</a> &#8211; Was founded in 1998, and agrees with the general concept of FSF, but believes that the cause of free software is about collaboration and creation of better software for everyone.</li>
</ul>
<h4>Open Source Licenses</h4>
<ul>
<li style="text-align: justify;"><strong>GNU General Public License</strong>: The most used and common open source license available. The GNU GPL is the most restrictive license available today, and it has introduced the &#8220;Copyleft&#8221; concept, where rights that were usually reserved by commercial firms are now granted to the users. The GNU GPL license grants the users the right to; retrieve the source code, modify it, copy it, build upon it a new product and redistribute it, freely. On the other hand, the GNU GPL does not allow the user to change the license terms, and requires that any derivative work or redistribution must retain the original license terms. Moreover, the GNU GPL does not allow mixing proprietary code (private software that is not free) with a GPL licensed software, meaning that any private changes to a GPL software, or usage by a other proprietary code is prohibited. In this case, the GPL license has a &#8220;Viral&#8221; property that &#8220;infects&#8221; any other piece of software which uses a GPL licensed software, with the GPL license. In other words, any software that links with, or adds functionality to a GPL licensed software must be licensed with GPL. The GPL also discourages software patents in this way. The GPL allows the distribution of proprietary software and GPL software in the same media. The most common open source project that is licensed with GPL is the Linux operating system.</li>
</ul>
<ul>
<li style="text-align: justify;"><strong>GNU Lesser General Public License</strong>: This is a weaker version of GPL, that is usually used by libraries. The license is similar to the GPL, with the only exception that it allows to link against it without forcing the linked code to be released under the GPL or compatible license. It also requires that a downstream user be able to link it against a newer version of the library (say, one with bug fixes). This means that you either have to dynamically link it, or provide object files for your proprietary code if you statically link it. In other words, proprietary code can use and call functions that are provided by an LGPL licensed software without the &#8220;viral&#8221; effect. A common open source project that is licensed with LGPL is the uClibc library.</li>
</ul>
<ul>
<li style="text-align: justify;"><em> </em><strong>Barkley Software Distribution License</strong>: Another widely used type of license is the BSD license, which was first used by the Barkley University. This license, with some minor modifications from the original license,  is the most permissive license available. Similarly to GPL, it allows the user to use, modify and redistribute the software. But unlike the GPL, any derivative work could remain private, and it is possible to mix proprietary code with the BSD licensed code. The only requirement this license has is to give the credit to the original authors of the software. The main concept of this license was born from the fact the in the 90&#8242;s, many software projects were funded by the US government, so you can actually consider the software as if it is owned by the public. Some open source software that use BSD-style license add some more requirements from the user, such as the prohibition to use the software name as a means to promote a derivative software product.</li>
</ul>
<ul>
<li style="text-align: justify;"><strong>Massachusetts Institute of Technology License</strong>: Another permissive license, similar license to the BSD license.</li>
</ul>
<ul>
<li style="text-align: justify;"><strong>Artistic License</strong>: The Artistic license was originally developed and used by the perl project, and is commercially oriented. The license allows to keep modifications as private, but prohibits to sell the software, unless it is bundled with a bigger software package.</li>
</ul>
<ul>
<li style="text-align: justify;"><strong>Netscape PL/Mozilla PL</strong>: The NPL/MPL licenses were developed in 1998 by the Netscape corporation, and present a trade-off approach between the restrictive GPL and the permissive BSD. Similarly to GPL, they grant the users the freedom to use , modify and redistribute the software while retaining the original license, and similarly to BSD, they permit mixing and usage of proprietary software. The NPL has another special property that gave Netscape extra privileges over derivative work. It allowed Netscape to reuse an open source derivative work, modify it privately and sell it. Of course, this license was not very popular due to the fact the communities felt that Netscape was going to harvest their efforts for its business. The MPL license, which does not contain this property is more popular nowadays.</li>
</ul>
<ul>
<li style="text-align: justify;"><strong>CreativeCommons License</strong>: A rather new type of license that was developed in order to protect any type of work (text, pictures, music, code) while granting some rights to the users and reserving some rights to the owner. The license has a few flavors where the owner of the work can decide which right to grant, and which right to reserve, such as allowing or disallowing redistribution, modification and commercial use.</li>
</ul>
<h4>Misconceptions about other Licenses</h4>
<p>There are other types of licenses that are used by software authors, which are not considered to be Open Source licenses, due to the fact that they lack one or more properties of the Open Source requirements:</p>
<ul>
<li style="text-align: justify;"><strong>Public Domain</strong>: Public domain license is not an open source license. Indeed, every user can obtain, modify and redistribute the code, but since the owner has waived his rights on the software, any other user can keep the changes private and even sell the software. Therefore, public domain software is not considered as open source.</li>
</ul>
<ul>
<li style="text-align: justify;"><strong>Freeware</strong>: Freewares are usually provided in the binary form only, without access to the source code. Therefore, freeware software is not considered as open source.</li>
</ul>
<ul>
<li style="text-align: justify;"><strong>Shareware</strong>: Shareware are usually provided in the binary form only. Furthermore, users of Shareware are required to pay some amount of money to the author, otherwise, its function is limited. Therefore, freeware software is not considered as open source.</li>
</ul>
<h4>Warranty and Liability</h4>
<p style="text-align: justify;">When a commercial firm or a private user wishes to use an open source software, they must be aware that the software comes AS-IS, with no warranty or liability, unlike proprietary, of-the-shelf software, which is provided with some kind of warranty. The authors and contributors are not  liable for any of kind of damage that may rise from the usage of the software.</p>
<p style="text-align: justify;"><strong>Resources</strong>:</p>
<ul>
<li>The Free Software Organization: <a href="http://www.fsf.org/" target="_blank">http://www.fsf.org/</a></li>
<li>The Open Source Initiative: <a href="http://www.opensource.org/" target="_blank">http://www.opensource.org/</a></li>
<li>GNU Licenses: <a href="http://www.gnu.org/licenses/" target="_blank">http://www.gnu.org/licenses/</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/PK7sI-nmkoY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/open-source-licenses/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/open-source-licenses/</feedburner:origLink></item>
		<item>
		<title>Getting a process call-stack using pstack</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/RGSH5ZmbmvU/</link>
		<comments>http://www.rt-embedded.com/blog/archives/call-stack-using-pstack/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 15:03:37 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[call]]></category>
		<category><![CDATA[pstack]]></category>
		<category><![CDATA[stack]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=718</guid>
		<description><![CDATA[<p style="text-align: justify;">Ever wanted to see (in run-time) which function a program is currently executing, or what is the current calling stack of a program? I&#8217;m sure you do. In many cases, it is very helpful to see a back trace of all the calling functions in a program, at any given time. Normally, this is a [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-730" title="ARM-stack-frames" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/ARM-stack-frames1.png" alt="" width="220" height="180" />Ever wanted to see (in run-time) which function a program is currently executing, or what is the current calling stack of a program? I&#8217;m sure you do. In many cases, it is very helpful to see a back trace of all the calling functions in a program, at any given time. Normally, this is a basic feature when you are using a debugger (either a JTAG based hardware debugger or a software based debugger like gdb). However, in this post I would like to show you that this is also possible, with some preconditions, to be done without any attached debugger. It could be useful to remotely debug units in the field or in the lab, where any extra debug equipment may be unavailable.</p>
<p style="text-align: justify;"><span id="more-718"></span>In this post, I would like to present the <strong>pstack</strong> utility, which displays the calling stack of a running program. First, let&#8217;s see what it does, and later we&#8217;ll review how it works in high level. In the following program, there is a nested call from <em>main( )</em> to <em>a( )</em> to <em>b( )</em> to <em>c( )</em>, which remains there forever. Let&#8217;s assume that the following program is a very complicated program which appears to be running an infinite loop (the latter is indeed correct). Normally, without a debugger, we could not have any means to find out which function is causing this loop, or in other words, we can&#8217;t know what the program is doing (or doing wrong).</p>
<table style="width: 467px; height: 401px;" cellspacing="0" cellpadding="0" width="467">
<tbody>
<tr>
<td>
<pre>/* a very simple program */
<strong>int</strong> <strong>c</strong>( <strong>int</strong> cc )
{
    <strong>while</strong>(cc);
    <strong>return</strong> 0;
}

<strong>int</strong> <strong>b</strong>( <strong>int</strong> bb )
{
    <strong>return</strong> <strong>c</strong>(bb);
}

<strong>int</strong> <strong>a</strong>( <strong>int</strong> aa )
{
    <strong>return</strong> <strong>b</strong>(aa);
}

<strong>int</strong> <strong>main</strong>( <strong>int</strong> argc, <strong>char</strong> *argv[] )
{
    <strong>return</strong> <strong>a</strong>(1);
}</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Now we compile and run it on the target. If we start this program in the foreground, it will hang our console because it never ends. So let&#8217;s move it to the background, and activate the pstack utility in order to understand what is going on. The pstack utility accepts the process id of the required process as an input parameter. Therefore, we&#8217;ll need to run <em>ps</em> first in order to get it. Here&#8217;s the output of pstack on our program:</p>
<table style="width: 466px; height: 18px;" cellspacing="0" cellpadding="0" width="466">
<tbody>
<tr>
<td><code># ./pstack 281922<br />
8192: ./test<br />
(No symbols found in /lib/libc.so.0)<br />
(No symbols found in /lib/ld-uClibc.so.0)<br />
0x000083ec: c() [at 0x000083d4]<br />
0x00008420: b() [at 0x00008404]<br />
0x0000844c: a() [at 0x00008430]<br />
0x0000847c: main() [at 0x0000845c]</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The pstack output displays the following information (from bottom to top):</p>
<ul>
<li>
<div style="text-align: justify;">Return address to the <em>main( )</em> function and its start address.</div>
</li>
<li>
<div style="text-align: justify;">
<div>Return address to a<em>( )</em> function and its start address.</div>
</div>
</li>
<li>
<div style="text-align: justify;">
<div>
<div>Return address to b<em>( )</em> function and its start address.</div>
</div>
</div>
</li>
<li>
<div style="text-align: justify;">
<div>
<div>
<div>Current execution address in c<em>( )</em> function and its start address.</div>
</div>
</div>
</div>
</li>
<li>
<div style="text-align: justify;">
<div>
<div>
<div>A warning that the C library was stripped (this isn&#8217;t critical, unless you want to see the function calls inside them as well).</div>
</div>
</div>
</div>
</li>
</ul>
<p style="text-align: justify;">We can see that our program is running the <em>c( )</em> function, and if we&#8217;ll run pstack again, we&#8217;ll probably see a similar output. In the next step we can use either <em>objdump</em> or <em>addr2line</em> applications  to find the exact source code. Here&#8217;s an example:</p>
<table cellspacing="0" cellpadding="0" width="466">
<tbody>
<tr>
<td><code># addr2line -e ~/test 0x83d4<br />
/home/hai/test.c:3</code></td>
</tr>
</tbody>
</table>
<p>As <em>addr2line</em> suggests, we are stuck at line 3, which is the endless <em>while</em>  loop.</p>
<h4>Limitations of pstack utility</h4>
<p style="text-align: justify;">The original pstack utility supports only the x86 32-bit platform. You can find pstack utility in every Linux distribution for PCs, but not in any embedded platform. However, there are patches in the web that enable support for other platforms (a link for a patch for ARM platform will be given later).</p>
<h4>Prerequisites of pstack utility</h4>
<p>The pstack utility requires the following items enabled on your program in order to fully function and provide all the information:</p>
<ol>
<li>The program must be compiled with gcc (GNU compiler collection).</li>
<li>The program must not be stripped. Otherwise, no symbols will be available and no functions name will be displayed. Instead you&#8217;ll get only question marks.</li>
<li>The program must retain the frame pointer. Otherwise, the pstack will not be able to crawl in the stack. When enabling the compiler optimizations (-O[s123]), the compiler throws away the frame pointer in order to reduce code size and reuse the register for other purposes. In order to retain it while enabling the optimizations, use the <em>-fno-omit-frame-pointer</em> flag.</li>
<li>When debugging a multi-threaded application, the pthread library must be compiled with debug support (Enabled the PTHREADS_DEBUG_SUPPORT define in uClibc).</li>
<li>For ARM platforms, when the program is compiled in Thumb mode, the compiler must be configured to generate a stack frame that is compliant with the Thumb Procedure Call Standard for all the functions. This option is required by pstack utility in order to crawl the stack. The following flags need to be enabled: <em>-mtpcs-frame -mtpcs-leaf-frame</em>. Note that when you enable these flags, the compiler will add about 20 additional opcodes in order to create the standard stack structure.</li>
</ol>
<h4>How does it work?</h4>
<p style="text-align: justify;">In order to understand how it works, we first need to understand the structure of the stack frame of each function. However, each platform has its own stack frame structure and therefore, I will only describe the ARM stack frame structure (there is plenty of information in the web). When working with frame pointer, the compiler generates code that stores it, among other registers in the frame in the following structure:</p>
<p style="text-align: center;"><img class="size-full wp-image-726  aligncenter" title="ARM-stack-frame" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/ARM-stack-frame.png" alt="" width="351" height="243" /></p>
<p style="text-align: justify;">When entering a function, the code pushes the following registers to the stack: frame pointer (fp), stack pointer image (ip), link register (lr) and the program counter (pc). In the end of the function, the code restores the registers, where the frame pointer gets the last frame pointer, the stack pointer gets the saved stack pointer image, and the program counter gets the link register value, which is actually the return address. The ptrace utility, connects to the required process and first reads the frame pointer and program counter registers. The first provides an address to the stack frame and the latter provides the current executing address. It then shows what is the current address that the program executes. Using the frame pointer address, it reads the next frame in a loop until the next frame pointer equals to 0. In each frame in the loop, it extracts the link register in order to resolve of the return address. Per each return address, it browsed the symbol table in order to match it with a corresponding function, and then prints the function name. </p>
<h4>Resources</h4>
<ul>
<li>The original pstack source code can be downloaded from <a href="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/pstack_1.2.orig.tar.gz" target="_blank">here</a>.</li>
<li>A patch for Little and Big endians ARM/Thumb code can be downloaded from <a href="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/pstack-1.2-arm.patch" target="_blank">here</a>. Download and extract the original source code, download and save the patch file in the same directory, and run &#8220;<em>patch -i pstack-1.2-arm.patch</em>&#8221; to enable the ARM support.</li>
<li>GCC ARM options:  <a href="http://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html">http://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html</a></li>
<li>pstack at debian: <a href="http://packages.debian.org/source/stable/pstack">http://packages.debian.org/source/stable/pstack</a></li>
<li>pstack at SourceForge: <a href="http://sourceforge.net/projects/bsd-pstack/">http://sourceforge.net/projects/bsd-pstack/</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/RGSH5ZmbmvU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/call-stack-using-pstack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/call-stack-using-pstack/</feedburner:origLink></item>
		<item>
		<title>Writing Efficient C Code for Embedded Systems</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/ZX7qEl8qIkQ/</link>
		<comments>http://www.rt-embedded.com/blog/archives/writing-efficient-c-code-for-embedded-systems/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 19:57:51 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[efficient]]></category>
		<category><![CDATA[optimize]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[size]]></category>
		<category><![CDATA[speed]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=653</guid>
		<description><![CDATA[<p style="text-align: justify;">The traditional definition of efficiency has two aspects: speed and size. In most cases, optimizing the first aspect causes a degradation in the other, and it&#8217;s a matter of balancing between the two, according to the specific needs. Per each embedded system, or even a software module, the appropriate strategy must be balanced [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-692" title="board" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/board.jpg" alt="" width="280" height="211" />The traditional definition of efficiency has two aspects: speed and size. In most cases, optimizing the first aspect causes a degradation in the other, and it&#8217;s a matter of balancing between the two, according to the specific needs. Per each embedded system, or even a software module, the appropriate strategy must be balanced between the two. Nowadays, there is a new dimension to this definition; power. In this article, I am going to discuss about the traditional aspects. In the years I&#8217;ve been working as a software engineer, I gained a lot of experience about C code efficiency. I saw how changing a few lines of code makes the difference, either in performance, final size or memory consumption. The examples I&#8217;ll show here were written in C and tested on an ARM platform. You should expect similar behavior by other processors. I might update this article from time to time with some more tips, so it is recommended to bookmark it and catch up.</p>
<p style="text-align: justify;"><span id="more-653"></span></p>
<h4 style="text-align: justify;">Global variables</h4>
<p style="text-align: justify;">The use of global variables is not recommended in most cases, but there are cases where we must use them. In some cases, we declare large tables/arrays which are used later by the software. When using global variables, follow the following guidelines:</p>
<ul>
<li>
<div style="text-align: justify;">Tables/Arrays which never change, should be defined as <strong>const</strong>. When defining them as constant, they will be moved to the code section. If this table/array was defined in a shared library, without the const directive, the table would be copied per each instance of a linked process. However, with the const directive, it will have only a single instance in memory.</div>
</li>
<li>
<div style="text-align: justify;">Reading and writing global variables require additional opcodes (load the global address, load the value from the address, and then store the value back). If possible, try to use local variables, or use a <strong>local copy</strong>.</div>
</li>
<li>
<div style="text-align: justify;">Declare all globals as <strong>static</strong>, unless other C files need to see them (not recommended at all. You can use a local read and write functions instead, and keep them static).</div>
</li>
</ul>
<h4 style="text-align: justify;">Variable types, signess and localization</h4>
<p style="text-align: justify;">In is very important to select the appropriate variable types you are using. There are a few rules of thumb in this case:</p>
<ul>
<li>
<div style="text-align: justify;">Try to use variables in the native size of the processor (like <strong>int</strong> for 32-bit processors). For a typical 32-bit processor, the use of <strong>short int</strong> or <strong>char </strong>types is less recommended. There are processors which need to read a 32-bit word anyway and do some shifting and masking, just to get the right value. Other pipeline processors which have 16-bit or 8-bit access opcodes, may read 32-bit anyway.</div>
</li>
<li>
<div style="text-align: justify;">If you don&#8217;t require negative values, use <strong>unsigned</strong> variables. Unsigned variables yield <strong>better performance</strong>, especially when using division and multiplications.</div>
</li>
<li>
<div style="text-align: justify;">Declare variables <strong>only when you really need them</strong> and not in the begining of the function. This will allow the compiler to better utilize the internal registers and avoiding assignment of a register to a variable which is used only later.</div>
</li>
</ul>
<h4 style="text-align: justify;">Division and Modulus</h4>
<p style="text-align: justify;">The division and modulus arithmetic operations require extensive CPU cycles. In ARM platform, they are done by software, and it is also known that in other processors, these operations are much slower than any other arithmetic operations.</p>
<p style="text-align: justify;">The modulus operation can be implemented much more efficiently, in some cases, using a simply counter:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="590">Bad example:</td>
</tr>
<tr>
<td valign="top" width="590">
<pre><strong>int</strong> <strong>tick_after_100_cycles_mod</strong>( <strong>void</strong> )
{
    <strong>static</strong> <strong>unsigned</strong> <strong>int</strong> i = 0;

    <strong>if</strong>( i % 100 == 0) {
        <strong>return</strong> 1;
    } <strong>else</strong> {
        i++;
    }

    <strong>return</strong> 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="590">Good example:</td>
</tr>
<tr>
<td valign="top" width="590">
<pre><strong>int</strong> <strong>tick_after_100_cycles_cnt</strong>(
    <strong>void</strong> )
{
    <strong>static</strong> <strong>unsigned</strong> <strong>int</strong> i = 0;

    <strong>if</strong>( i &gt;= 100) {
        <strong>return</strong> 1;
    } <strong>else</strong> {
        i++;
    }

    <strong>return</strong> 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<p>The division operation is extremely efficient if the divider is a natural number in <strong>power of two</strong> (2,4,8,16,32&#8230;). The reason for this is the binary representation of the numbers inside the processor, where each division by two equals to a single right shift. In this case, if you are dividing a variable with a hard coded value in the power of two, the compiler will automatically convert it to a shift operation. In case you are dividing by a variable, the compiler can not know what values could be used in it and will not optimize this division, so you&#8217;ll have to help it by doing the shift operation manually. Here are a few examples and the corresponding output for ARM:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="590">
<pre>/* Results in a call to division opcode or function */
<strong>int</strong> <strong>test_div</strong>( <strong>unsigned</strong> num, <strong>unsigned</strong> div )
{
    <strong>return</strong> num / div;
}

/* Results in an optimized automatic shift right */
<strong>int</strong> <strong>test_div_hardcoded_power_2</strong>( <strong>unsigned</strong> num )
{
    <strong>return</strong> num / 8;
}

/* Results in an optimized automatic shift */
<strong>int</strong> <strong>test_div_shift_right</strong>( <strong>unsigned</strong> num, <strong>unsigned</strong> powerof_two )
{
    <strong>return</strong> num &gt;&gt; powerof_two;
}</pre>
</td>
</tr>
<tr>
<td valign="top" width="590">
<pre>00000000 &lt;test_div&gt;:
   0:   e52de004        push    {lr}            ; (str lr, [sp, #-4]!)
   4:   e24dd004        sub     sp, sp, #4      ; 0x4
   8:   ebfffffe        bl      0 &lt;__aeabi_uidiv&gt;
   c:   e28dd004        add     sp, sp, #4      ; 0x4
  10:   e8bd8000        pop     {pc}

00000014 &lt;test_div_hardcoded_power_2&gt;:
  14:   e1a001a0        lsr     r0, r0, #3
  18:   e12fff1e        bx      lr

0000001c &lt;test_div_shift_right&gt;:
  1c:   e1a00130        lsr     r0, r0, r1
  20:   e12fff1e        bx      lr</pre>
</td>
</tr>
</tbody>
</table>
<p>We can see that the functions that use shift operation are very efficient, while the function with the real division calls the external <em>__aeabi_uidiv( )</em> function to calculate the result.</p>
<h4 style="text-align: justify;">For loops</h4>
<p style="text-align: justify;">For loops are widely used in the code. The natural human thinking makes us write our for loops from 0 to the maximum value. However, in case the order of the index is not important, it is more efficient to do the for loop from top to 0. The reason is due to an extra comparison opcode required when not comparing with 0. Per each incrementing for iteration, the CPU needs to check that we have reached the maximum value, and then break. However, if the for loop is decrementing, the comparison is not required because the result of the decrement action will trigger the Z (zero) flag, and the CPU will know when to break just by looking at it.</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="590">
<pre><strong>extern</strong> <strong>void</strong> <strong>foo</strong>(
    <strong>int</strong> );

<strong>void</strong> <strong>test_incrementing_for_loop</strong>( <strong>void</strong> )
{
    <strong>int</strong> i;

    <strong>for</strong>(i=0; i&lt;100;i++) {
        <strong>foo</strong>(i);
    }
}

<strong>void</strong> <strong>test_decrementing_for_loop</strong>( <strong>void</strong> )
{
    <strong>int</strong> i;

    <strong>for</strong>(i=100; i; i--) {
        <strong>foo</strong>(i);
    }
}</pre>
</td>
</tr>
<tr>
<td valign="top" width="590">
<pre>00000000 &lt;test_decrementing_for_loop&gt;:
   0:   e92d4010        push    {r4, lr}
   4:   e3a00064        mov     r0, #100        ; 0x64
   8:   ebfffffe        bl      0 &lt;foo&gt;
   c:   e3a04063        mov     r4, #99 ; 0x63
  10:   e1a00004        mov     r0, r4
  14:   ebfffffe        bl      0 &lt;foo&gt;
  18:   e2544001        subs    r4, r4, #1      ; 0x1
  1c:   1afffffb        bne     10 &lt;test_decrementing_for_loop+0x10&gt;
  20:   e8bd8010        pop     {r4, pc}

00000024 &lt;test_incrementing_for_loop&gt;:
  24:   e92d4010        push    {r4, lr}
  28:   e3a00000        mov     r0, #0  ; 0x0
  2c:   ebfffffe        bl      0 &lt;foo&gt;
  30:   e3a04001        mov     r4, #1  ; 0x1
  34:   e1a00004        mov     r0, r4
  38:   e2844001        add     r4, r4, #1      ; 0x1
  3c:   ebfffffe        bl      0 &lt;foo&gt;
  40:   e3540064        cmp     r4, #100        ; 0x64
  44:   1afffffa        bne     34 &lt;foo+0x34&gt;
  48:   e8bd8010        pop     {r4, pc}</pre>
</td>
</tr>
</tbody>
</table>
<p>We can see that the decrementing for loop has one less opcode.</p>
<p style="text-align: justify;">In some cases, when short loops are required, it could be more efficient (in terms of speed) to unroll the loop and save all the loop overhead. The compiler can be configured to do that automatically when using the -O3 optimization flag.</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="590">
<pre><strong>extern</strong> <strong>void</strong> <strong>foo</strong>( <strong>int</strong> );

<strong>void</strong> <strong>for_loop</strong>( <strong>void</strong> )
{
    <strong>int</strong> i;

    <strong>for</strong>(i=4; i; i--) {
        <strong>foo</strong>(i);
    }
}

<strong>void</strong> <strong>unrolled_loop</strong>( <strong>void</strong> )
{
    <strong>foo</strong>(4);
    <strong>foo</strong>(3);
    <strong>foo</strong>(2);
    <strong>foo</strong>(1);
    <strong>foo</strong>(0);
}</pre>
</td>
</tr>
<tr>
<td valign="top" width="590">
<pre>00000000 &lt;unrolled_loop&gt;:
   0:   e52de004        push    {lr}            ; (str lr, [sp, #-4]!)
   4:   e3a00004        mov     r0, #4  ; 0x4
   8:   e24dd004        sub     sp, sp, #4      ; 0x4
   c:   ebfffffe        bl      0 &lt;foo&gt;
  10:   e3a00003        mov     r0, #3  ; 0x3
  14:   ebfffffe        bl      0 &lt;foo&gt;
  18:   e3a00002        mov     r0, #2  ; 0x2
  1c:   ebfffffe        bl      0 &lt;foo&gt;
  20:   e3a00001        mov     r0, #1  ; 0x1
  24:   ebfffffe        bl      0 &lt;foo&gt;
  28:   e3a00000        mov     r0, #0  ; 0x0
  2c:   e28dd004        add     sp, sp, #4      ; 0x4
  30:   e49de004        pop     {lr}            ; (ldr lr, [sp], #4)
  34:   eafffffe        b       0 &lt;foo&gt;

00000038 &lt;for_loop&gt;:
  38:   e92d4010        push    {r4, lr}
  3c:   e3a00004        mov     r0, #4  ; 0x4
  40:   ebfffffe        bl      0 &lt;foo&gt;
  44:   e3a04003        mov     r4, #3  ; 0x3
  48:   e1a00004        mov     r0, r4
  4c:   ebfffffe        bl      0 &lt;foo&gt;
  50:   e2544001        subs    r4, r4, #1      ; 0x1
  54:   1afffffb        bne     48 &lt;foo+0x48&gt;
  58:   e8bd8010        pop     {r4, pc}</pre>
</td>
</tr>
</tbody>
</table>
<p>In the result we can see that the unrolled loop is <strong>longer</strong>. However, if we take a closer look, we can see that in the unrolled loop runs <strong>faster</strong>, because each iteration is done with 2 opcodes, compared to 4. In the unrolled for loop, we have 2 opcodes; loading a value and calling <em>foo( ).</em> In the regular for loop, we have 4 opcodes; loading a value, calling <em>foo( )</em>, reducing the register by 1 and comparing the result to 0 (with an incrementing for loop there could be even another opcode).</p>
<h4 style="text-align: justify;">If-Else and Switch</h4>
<p style="text-align: justify;">In many cases, we use branching in our code. We all know the cases where we need to add some more conditions when the code grows or new features are added. We might end up with a multiple if-else-if-else clauses. Long if-else clauses are not efficient because for the worst case scenario, where the last comparison is positive, the CPU must check all other possibilities. In this case it is more efficient to use a switch clause which is implemented as a lookup table. The compiler generates a list of addresses and the CPU jumps directly to the correct one using the index in the switch.</p>
<p style="text-align: justify;">If it is not possible to use switch, it may be possible to apply the <strong>binary search</strong> strategy. If the range is large, we can divide it to sub areas, and increase the performance by reducing the amount of tests in the worst case scenario.</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="590">
<pre>/* Bad in worst case scenario */
<strong>if</strong>(i==1) {
    <strong>do_something1</strong>();
} <strong>else</strong> <strong>if</strong>(i==2) {
    <strong>do_something2</strong>();
} <strong>else</strong> <strong>if</strong>(i==3) {
    <strong>do_something3</strong>();
} <strong>else</strong> <strong>if</strong>(i==4) {
    <strong>do_something4</strong>();
} <strong>else</strong> <strong>if</strong>(i==5) {
    <strong>do_something5</strong>();
} <strong>else</strong> <strong>if</strong>(i==6) {
    <strong>do_something6</strong>();
} <strong>else</strong> <strong>if</strong>(i==7) {
    <strong>do_something7</strong>();
} <strong>else</strong> <strong>if</strong>(i==8) {
    <strong>do_something8</strong>();
}

/* Improved version */
<strong>if</strong>(i&lt;=4) {
    <strong>if</strong>(i==1) {
        <strong>do_something1</strong>();
    } <strong>else</strong> <strong>if</strong>(i==2) {
        <strong>do_something2</strong>();
    } <strong>else</strong> <strong>if</strong>(i==3) {
        <strong>do_something3</strong>();
    } <strong>else</strong> <strong>if</strong>(i==4) {
        <strong>do_something4</strong>();
} <strong>else</strong> {
    <strong>if</strong>(i==5) {
        <strong>do_something5</strong>();
    } <strong>else</strong> <strong>if</strong>(i==6) {
        <strong>do_something6</strong>();
    } <strong>else</strong> <strong>if</strong>(i==7) {
        <strong>do_something7</strong>();
    } <strong>else</strong> <strong>if</strong>(i==8) {
        <strong>do_something8</strong>();
    }
}</pre>
</td>
</tr>
</tbody>
</table>
<p>We can see that for case i == 5, it takes 5 comparisons in version 1 and 2 comparisons in version 2, and in case i == 8, it takes 8 comparisons in version 1 and 5 comparisons in version 2. The binary break can be even finer if required.</p>
<h4 style="text-align: justify;">Lazy Evaluation</h4>
<p style="text-align: justify;">Another principle in the if-else calculation of C is referred as &#8220;<strong>Lazy Evaluation</strong>&#8220;. In multiple conditional clauses, where there is a need to check various conditions, the generated code will evaluate only the minimum conditions which are required to satisfy the clause, from left to right. For example, in a multiple OR clause, it is sufficient that only one of the conditions becomes true to satisfy the clause, and in a multiple AND clause, it is sufficient that only on of the conditions become false to negate the clause. We can utilize this property to put the &#8220;easy&#8221; or &#8220;trivial&#8221; checks first, and thus avoiding some work.</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="590">
<pre><strong>int</strong> <strong>check_something_1</strong>( <strong>int</strong> i )
{
    <strong>if</strong>( i &gt; 99 &amp;&amp; i % 100 == 0 ) {
        <strong>return</strong> 1;
    }

    <strong>return</strong> 0;
}

<strong>int</strong> <strong>check_something_2</strong>( <strong>int</strong> i )
{
    <strong>if</strong>( i == 0 || i % 100 == 0 ) {
        <strong>return</strong> 1;
    }

    <strong>return</strong> 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<p>In the first function, we first check if i is bigger than 99, and only then perform the modulus calculation. So actually, the modulus will be skipped every time we call this function with 0-99. In the second example, we will skip the modulus when i equals 0. In both cases, it would be wrong to switch between the conditions because the &#8220;easy&#8221; condition is first.</p>
<h4>Return Value checking</h4>
<p>Another thing that could slightly optimize the code is to check return values of functions by <span style="text-decoration: underline;">comparing to 0</span>. Since comparison to zero is native to the CPU, it will usually save one or two opcodes. If a function returns 0 on success and -1 for failure, don&#8217;t check if the return value is -1. Instead, check if the return value is less than 0. Another example; If a function returns OK or NOK, compare the return value against the enumeration which is equal to 0.</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>/* returns 0 on success, -1 on failure */
extern int foo( int i );

/* Good example 1: Good path */
if( foo( 7 ) == 0 ) {
    /* Good path: do something */
}

/* Good example 2: Bad path */
if( foo( 7 ) != 0 ) {
    /* Bad path: do something */
}

/* Good example 3: Bad path */
if( foo( 7 ) &lt; 0 ) {
    /* Bad path: do something */
}

/* Bad example */
if( foo( 7 ) == -1 ) {
    /* Bad path */
}</pre>
</td>
</tr>
</tbody>
</table>
<h4 style="text-align: justify;">Lookup tables</h4>
<p style="text-align: justify;">Lookup tables can speed up the processing but increase the size by actually having all the answers in advance without the need to calculate them. Here&#8217;s an example that beats both size and speed:</p>
<table style="width: 624px; height: 1142px;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="590">
<pre><strong>char</strong> <strong>get_char</strong>( <strong>unsigned</strong> <strong>int</strong> i )
{
    <strong>switch</strong>(i) {
    <strong>case</strong> 0:
        <strong>return</strong> 'r';
    <strong>case</strong> 1:
        <strong>return</strong> 't';
    <strong>case</strong> 2:
        <strong>return</strong> '-';
    <strong>case</strong> 3:
        <strong>return</strong> 'e';
    <strong>case</strong> 4:
        <strong>return</strong> 'm';
    <strong>case</strong> 5:
        <strong>return</strong> 'b';
    <strong>default</strong>:
        <strong>return</strong> '\0';
    }
}

<strong>static</strong> <strong>const</strong> <strong>char</strong> lookup_table[] = { 'r', 't', '-', 'e','m','b' };

<strong>char</strong> <strong>get_char_lookup</strong>( <strong>unsigned</strong> <strong>int</strong> i )
{
    <strong>if</strong>(i&gt;=<strong>sizeof</strong>(lookup_table)) {
        <strong>return</strong> '\0';
    }
    <strong>return</strong> lookup_table[i];
}</pre>
</td>
</tr>
<tr>
<td valign="top" width="590">
<pre>00000000 &lt;get_char&gt;:
   0:   e3500005        cmp     r0, #5  ; 0x5
   4:   979ff100        ldrls   pc, [pc, r0, lsl #2]
   8:   ea000005        b       24 &lt;get_char+0x24&gt;
   c:   0000002c        .word   0x0000002c
  10:   00000034        .word   0x00000034
  14:   0000003c        .word   0x0000003c
  18:   00000044        .word   0x00000044
  1c:   0000004c        .word   0x0000004c
  20:   00000054        .word   0x00000054
  24:   e3a00000        mov     r0, #0  ; 0x0
  28:   e12fff1e        bx      lr
  2c:   e3a00072        mov     r0, #114        ; 0x72
  30:   e12fff1e        bx      lr
  34:   e3a00074        mov     r0, #116        ; 0x74
  38:   e12fff1e        bx      lr
  3c:   e3a0002d        mov     r0, #45 ; 0x2d
  40:   e12fff1e        bx      lr
  44:   e3a00065        mov     r0, #101        ; 0x65
  48:   e12fff1e        bx      lr
  4c:   e3a0006d        mov     r0, #109        ; 0x6d
  50:   e12fff1e        bx      lr
  54:   e3a00062        mov     r0, #98 ; 0x62
  58:   e12fff1e        bx      lr

0000005c &lt;get_char_lookup&gt;:
  5c:   e3500005        cmp     r0, #5  ; 0x5
  60:   959f3008        ldrls   r3, [pc, #8]    ; 70 &lt;get_char_lookup+0x14&gt;
  64:   83a00000        movhi   r0, #0  ; 0x0
  68:   97d30000        ldrbls  r0, [r3, r0]
  6c:   e12fff1e        bx      lr
  70:   00000000        .word   0x00000000</pre>
</td>
</tr>
</tbody>
</table>
<p>We can see that the lookup table implementation is fast and small.</p>
<h4>Data caching and avoiding calling other functions/system calls</h4>
<p style="text-align: justify;">My meaning here is not for the actual CPU cache. Sometimes, we call some functions or system calls in order to perform some calculation or get some data. If possible, try to reduce and eliminate the use of these, especially system calls which cause a context switch and data copying (relatively slow actions).</p>
<p style="text-align: justify;">In this example, we can skip calling <em>getpid( )</em> starting from the second time we call this function:</p>
<table style="width: 627px; height: 656px;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="590">
<pre>#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;

pid_t <strong>my_getpid</strong>( <strong>void</strong> )
{
    pid_t pid = <strong>getpid</strong>();
    <strong>return</strong> pid;
}

pid_t <strong>my_getpid_optimized</strong>( <strong>void</strong> )
{
    <strong>static</strong> pid_t pid = -1;

    <strong>if</strong>(pid&lt;0) {
         pid = <strong>getpid</strong>();
    }
    <strong>return</strong> pid;
}</pre>
</td>
</tr>
<tr>
<td valign="top" width="590">
<pre>00000000 &lt;my_getpid_optimized&gt;:
   0:   e92d4010        push    {r4, lr}
   4:   e59f4020        ldr     r4, [pc, #32]   ; 2c &lt;my_getpid_optimized+0x2c&gt;
   8:   e5943000        ldr     r3, [r4]
   c:   e3530000        cmp     r3, #0  ; 0x0
  10:   ba000001        blt     1c &lt;my_getpid_optimized+0x1c&gt;
  14:   e5940000        ldr     r0, [r4]
  18:   e8bd8010        pop     {r4, pc}
  1c:   ebfffffe        bl      0 &lt;getpid&gt;
  20:   e5840000        str     r0, [r4]
  24:   e5940000        ldr     r0, [r4]
  28:   e8bd8010        pop     {r4, pc}
  2c:   00000000        .word   0x00000000

00000030 &lt;my_getpid&gt;:
  30:   eafffffe        b       0 &lt;getpid&gt;</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">While the <em>my_getpid( ) </em>function looks a lot shorter (both in the code and in the assembly), it is actually <strong>much slower</strong> than the other function. The reason is because the <em>my_getpid( )</em> function will initiate a system call each time it is called, and on the other hand, the <em>my_getpid_optimized( ) </em>function, will call the system call only once. The rest of the times will return the stored process id. In this example, we can see the tradeoff between speed and size.</p>
<h4 style="text-align: justify;">Efficient search and sort functions</h4>
<p style="text-align: justify;">The uClibc library (and probably other standard C libraries) provide very efficient search and sort functions, so if you need to perform searches or sorts on any type of variable, you don&#8217;t need to write an algorithm from scratch, but use the existing ones. The binary search function is called <em>bsearch( )</em>, and the quick sort function is called <em>qsort( )</em>. When using these functions, you need to specify the size of your element, and a comparison function which will be used by the algorithms to perform their actions. Here&#8217;s an example for quick sorting an array of elements. The elements are made of an index and a name. We define the sort criteria as the index number, and write the comparison function accordingly.</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="590">
<pre>#include &lt;stdlib.h&gt;

/* Entry element */
<strong>struct</strong> entry_t {
    <strong>int</strong> index;
    <strong>char</strong> name[ 10 ];
};

<strong>static</strong> <strong>int</strong> <strong>mycmp</strong>( <strong>const</strong> <strong>void</strong> *a, <strong>const</strong> <strong>void</strong> *b )
{
    /* Compare the elements by indexes */
    <strong>if</strong>(((<strong>struct</strong> entry_t *)a)-&gt;index == ((<strong>struct</strong> entry_t *)b)-&gt;index) {
        <strong>return</strong> 0;
    }
    <strong>return</strong> ((<strong>struct</strong> entry_t *)a)-&gt;index &gt; ((<strong>struct</strong> entry_t *)b)-&gt;index;
}

/* Maximum elements */
#define MAX_ENTRIES 100

/* The array to be sorted */
<strong>struct</strong> entry_t entries_array[ MAX_ENTRIES ];

<strong>int</strong> <strong>do_sort</strong>( <strong>void</strong> )
{
    /* Sort the array */
    <strong>qsort</strong>( &amp;entries_array, MAX_ENTRIES, <strong>sizeof</strong>( <strong>struct</strong> entry_t ), mycmp );
    <strong>return</strong> 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<h4 style="text-align: justify;">Inline functions</h4>
<p style="text-align: justify;">Inline functions are functions which are copied inside the calling function thus avoiding the function call overhead. It could be used to increase the performance of a specific critical area. Note that inlines create code duplication per each call to them, so use them wisely.</p>
<h4 style="text-align: justify;">Bitmaps</h4>
<p style="text-align: justify;">A bitmap is usually a register (32-bit) variable that can represent data. The 32-bits can be divided to represent up to 32 pieces of data, wit 1 bit per piece which represent 2 states (on or off, enabled or disabled). The less data pieces represented, the more data can be saves (for 16 pieces, there are 2 bits which represent 4 different states). Sometimes bitmaps can be very efficient comparing to other data types, mainly because the single memory access, and the fact that data can be manipulated using bit-wise operations (&amp;, |, ~, ^). For example, suppose we need to keep a list of objects (like interfaces, devices, etc.), and we want to keep track which of them has a specific property enabled (like, link, connectivity, etc.). Usually, we will use an array or any other data type to store this information. If the list is 32 entries long or less, we can store it in a register, and mark the property using a single bit. Each object will have its own offset in the register. Enabling a property is a single OR operation in the object&#8217;s offset, and disabling a property is a single AND operation in the object&#8217;s offset.</p>
<h4 style="text-align: justify;">Compiler optimizations</h4>
<p style="text-align: justify;">Don&#8217;t forget to enable the appropriate compiler optimizations. You can read about it <a href="http://www.rt-embedded.com/blog/archives/using-gcc-part-2/" target="_blank">here</a>.</p>
<h4 style="text-align: justify;">Resources</h4>
<p style="text-align: justify;">Quick sort: <a href="http://linux.die.net/man/3/qsort">http://linux.die.net/man/3/qsort</a><br />
Binary search: <a href="http://linux.die.net/man/3/bsearch">http://linux.die.net/man/3/bsearch</a><br />
Using gcc: <a href="http://www.rt-embedded.com/blog/archives/using-gcc-part-2/">http://www.rt-embedded.com/blog/archives/using-gcc-part-2/</a></p>
<p style="text-align: justify;">
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/ZX7qEl8qIkQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/writing-efficient-c-code-for-embedded-systems/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/writing-efficient-c-code-for-embedded-systems/</feedburner:origLink></item>
		<item>
		<title>Working with Kconfig</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/c9Wijt7dfi8/</link>
		<comments>http://www.rt-embedded.com/blog/archives/working-with-kconfig/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 04:18:40 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[defconfig]]></category>
		<category><![CDATA[kconfig]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[menuconfig]]></category>
		<category><![CDATA[oldconfig]]></category>
		<category><![CDATA[xconfig]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=629</guid>
		<description><![CDATA[The Kconfig mechanism is today’s standard configuration mechanism and it is used by leading open source projects, such as the Linux kernel, Busybox and uClibc. The Kconfig has a basic configuration syntax that allows you to add configuration options of various types, create dependencies and write a few lines of description. The Kconfig utilities know [...]]]></description>
				<content:encoded><![CDATA[<div class="mceTemp mceIEcenter" style="text-align: justify;"><a href="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/Screenshot-xconfig-header.png"><img class="size-full wp-image-638 alignright" title="Screenshot - xconfig header" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/Screenshot-xconfig-header.png" alt="" width="297" height="195" /></a>The Kconfig mechanism is today’s standard configuration mechanism and it is used by leading open source projects, such as the <a href="http://kernel.org/" target="_blank">Linux kernel</a>, <a href="http://www.busybox.net/" target="_blank">Busybox</a> and <a href="http://www.uclibc.org/" target="_blank">uClibc</a>. The Kconfig has a basic configuration syntax that allows you to add configuration options of various types, create dependencies and write a few lines of description. The Kconfig utilities know how to read and parse the configuration files, and create project configuration files (usually by the name of <em>.config </em>for Makefiles and <em>autoconf.h</em> for source files). Other advantages this method has, are automatic menu generation (for both graphical and text based consoles), and ease of configuration management. With Kconfig, there is no need to specify any build flags to the project’s make. In this article I will show how to use the Kconfig to configure your Linux kernel, and how to easily modify the kernel’s Kconfig files in order to add a new driver somewhere in the tree. These methods of configuration can be also ported to any other projects.</div>
<p><span id="more-629"></span></p>
<p style="text-align: justify;">The Linux kernel is instrumented with a Kconfig file almost per each directory. Each Kconfig file configures its own level. For example, you’ll find a Kconfig in each driver directory or arch directory. Each one of them configures a driver or architecture and includes other Kconfig files in the levels below it.</p>
<h4 style="text-align: justify;">Configuring the Linux Kernel</h4>
<p style="text-align: justify;">There are 4 main ways to configure the kernel:</p>
<p style="text-align: justify;">
<ul>
<li>Running “<strong>make menuconfig</strong>”: Will show a text based menu configuration of the kernel. It also applicable for configuring a Linux kernel using telnet/ssh connections to a build server. This configuration option will read the existing configuration in case it exists. Otherwise, it will initialize all the menu items with their default values. When browsing in the menus, you could see that some configuration options are not marked, some are mark with star [*] and some are marked with [M]. These options mean: Disabled, Enabled and Modulized, accordingly. The difference between Enabled and Modulized, is the fact that when a driver or a feature is Enabled, it will be loaded automatically with the kernel, as an integral part of it without an option to remove. The Modulized option enables hot plug and unplug of the driver/feature, thus allowing you to use the memory for other tasks, in case it is not required.</li>
</ul>
<p class="mceTemp mceIEcenter" style="text-align: center;">
<dl class="wp-caption aligncenter" style="width: 632px;">
<dt class="wp-caption-dt"><img title="Screenshot - menuconfig" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/Screenshot-menuconfig.png" alt="" width="622" height="474" /></dt>
<dd class="wp-caption-dd">Screenshot: menuconfig (textual configuration menu)</dd>
</dl>
<ul>
<li>Running “<strong>make xconfig</strong>”: Similar to option 1, but uses a graphical interace. It will not work with telnet/ssh connections, and it requires the “<em>qt3-devel</em>” package to be installed in the machine.</li>
</ul>
<div class="wp-caption aligncenter" style="width: 763px"><img title="Screenshot - xconfig" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/Screenshot-xconfig.png" alt="" width="753" height="495" /><p class="wp-caption-text">Screenshot: xconfig (graphical configuration menu)</p></div>
<ul>
<li style="text-align: left;">Running “<strong>make defconfig</strong>”: Will read and parse all the configuration files and assign default values to the configuration variables. In case a default value is not available, the program will prompt for a selection.</li>
<li style="text-align: left;">Running “<strong>make oldconfig</strong>”: In case you were editing the configuration file (.config) manually, it is required to run this command in order for the setting to take effect.</li>
</ul>
<h4 style="text-align: left;">The Kconfig syntax</h4>
<p style="text-align: left;">The Kconfig syntax is very simple and it is documented in the Documentation/kbuild/kconfig-language.txt file. Let’s review some of the basics by a few examples:</p>
<ul>
<li style="text-align: left;">An example from drivers/video/Kconfig:</li>
</ul>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>config FB_CIRRUS<br />
tristate "Cirrus Logic support"<br />
depends on FB &amp;&amp; (ZORRO || PCI)<br />
select FB_CFB_FILLRECT<br />
select FB_CFB_COPYAREA<br />
select FB_CFB_IMAGEBLIT<br />
---help---<br />
This enables support for Cirrus Logic GD542x/543x based boards on<br />
Amiga: SD64, Piccolo, Picasso II/II+, Picasso IV, or EGS Spectrum.</code></td>
</tr>
</tbody>
</table>
<p class="mceTemp mceIEcenter" style="text-align: center;">
<p class="mceTemp mceIEcenter" style="text-align: center;">
<p class="mceTemp mceIEcenter" style="text-align: center;">
<p style="text-align: left;">
<p style="text-align: justify;">
<p class="mceTemp mceIEcenter">
<p class="mceTemp mceIEcenter">
<p style="text-align: justify;">The example, defines a new variable in the name of CONFIG_FB_CIRRUS (note that the CONFIG_ prefix is added automatically and should be omitted from the Kconfig file itself). Let’s jump to the <em>help</em> section for a second. We can read that this configuration option enables support for various Cirrus Logic video cards. In the second line, we can see that this variable is in the type of “<em>tristate</em>”. It means that this configuration option has 3 possible states; Disabled, Enabled and Modulized. Each configuration option must have a type. The available types are: &#8220;<em>bool</em>&#8220;, &#8220;<em>tristate</em>&#8220;, &#8220;<em>string</em>&#8220;, &#8220;<em>hex</em>&#8221; or &#8220;<em>int</em>&#8220;. Each type has its own properties. The <em>bool</em> type can be either Enabled or Disabled. The <em>string</em> type will contain a list of characters. The <em>hex</em> type will contain a hexadecimal representation of a number (usually used to assign hardware address and ids) and the <em>int</em> type contains numbers. Right next to the type, you’ll find the prompt string that represents the configuration option in the menu (or in the prompt). Next, shows the dependency of this configuration option. It means that this option will not be available for selection if the CONFIG_FB option is not enabled and either CONFIG_ZORRO or CONFIG_PCI are enabled. In this way, the driver developer can be sure that this driver will not be available unless the Frame Buffer framework is enabled and the target has either Zorro or PCI interfaces enabled (otherwise, the driver will fail to compile or run). The next lines, with the <em>select</em> option, show options that the driver needs in order to operate correctly. In this example, we see that the driver requires a few Frame Buffer options to be enabled.</p>
<ul>
<li>An example from net/Kconfig:</li>
</ul>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>menu "Networking options"source "net/packet/Kconfig"<br />
source "net/unix/Kconfig"<br />
source "net/xfrm/Kconfig"<br />
source "net/iucv/Kconfig"config INET<br />
bool "TCP/IP networking"<br />
---help---<br />
These are the protocols used on the Internet and on most local<br />
Ethernets. It is highly recommended to say Y here (this will enlarge<br />
your kernel by about 400 KB), since some programs (e.g. the X window<br />
system) use TCP/IP even if your machine is not connected to any<br />
other computer. You will get the so-called loopback device which<br />
allows you to ping yourself (great fun, that!).<br />
…<br />
…<br />
endmenu </code></td>
</tr>
</tbody>
</table>
<p class="mceTemp mceIEcenter">
<p class="mceTemp mceIEcenter">
<p class="mceTemp mceIEcenter">
<p style="text-align: justify;">
<p class="mceTemp mceIEcenter">
<p class="mceTemp mceIEcenter">
<p class="mceTemp mceIEcenter">
<p style="text-align: justify;">This example shows how to create a submenu. In this example, a sub menu in the name of “Networking options” will be created, and a bunch of related configuration options will be placed inside it. This menu also includes some other Kconfig files using the keyword “<em>source</em>”.</p>
<p style="text-align: justify;">You can browse the Kconfig files in the kernel and learn from the many examples available. That’s the best way to learn.</p>
<h4 style="text-align: justify;">Adding a new configuration option</h4>
<p style="text-align: justify;">In order to add a new driver or feature to the Kernel, first we’ll need to select the appropriate location inside the tree to store the code. Once selected, we’ll need to add a new configuration option in the corresponding Kconfig file, and update the directory’s Makefile to compile our new code, once our configuration option has been selected.</p>
<p style="text-align: justify;">For our example, let’s add a new driver under the networking drivers. In the following example, our new driver contains two files; <em>my_obj_file1.c</em> and <em>my_obj_file2.c</em>. In order to add this driver in the kernel, we need to do the following:</p>
<p style="text-align: justify;">
<ul>
<li>Select a <strong>unique</strong> configuration option name, (or names, in case the driver/feature has additional configuration parameters). Otherwise, there will be a collision and a configuration mess. You can grep the <em>.config </em>file to make sure your selected name is not taken. For this example, the name is <strong>CONFIG_RT_EMBEDDED_TEST_DRIVER</strong>.</li>
<li>Define the configuration option type(s). The main feature/driver can be Boolean type (Enabled/Disabled), or Tristate type (Enabled/Disabled/Modulized). Other configuration types are driver specific (like hardware address, number of buffers, identification string, etc.).</li>
<li>Edit the appropriate Kconfig file. In this example, we will edit the drivers/net/Kconfig file:</li>
</ul>
<p style="text-align: center;"><img class="aligncenter" title="Screenshot - new option" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/Screenshot-new-option.png" alt="" width="624" height="475" /></p>
<p style="text-align: justify;">
<ul>
<li style="text-align: left;">Next, we add our new configuration option in the appropriate Makefile in order to build the code when it is enabled. In this example, we will edit the drivers/net/Makefile:</li>
</ul>
<p style="text-align: center;"><img class="aligncenter" title="Screenshot - new option makefile" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/Screenshot-new-option-makefile.png" alt="" width="625" height="473" /></p>
<ul>
<li style="text-align: left;">Now the new driver is a part of the Linux kernel. In the next step we must re-configure the kernel because its configuration has been upgraded with a new feature. We can use either configuration possibilities listed in the begining of this article, except for option 4 (its a new configuratin option which is not listed in the default configuration file yet). When using either menu configuration options, the new item will appear with <strong>[NEW]</strong> marking near it. When using oldconfig option, it will prompt for your selection, while capitaling the default option. In our example, since the default mode is Modulized, we will see n/y/M.</li>
</ul>
<p style="text-align: center;"><img class="aligncenter" title="Screenshot - new option menuconfig" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/07/Screenshot-new-option-menuconfig.png" alt="" width="624" height="474" /></p>
<ul>
<li style="text-align: left;">Here we need to select what to do with the new option, according to our requirements.</li>
</ul>
<p style="text-align: left;"><span style="text-decoration: underline;">Resources</span></p>
<ul>
<li>Kconfig description: <a href="http://lxr.linux.no/linux+v2.6.34.1/Documentation/kbuild/kconfig.txt">http://lxr.linux.no/linux+v2.6.34.1/Documentation/kbuild/kconfig.txt</a></li>
<li>The Kconfig language: <a href="http://lxr.linux.no/linux+v2.6.34.1/Documentation/kbuild/kconfig-language.txt">http://lxr.linux.no/linux+v2.6.34.1/Documentation/kbuild/kconfig-language.txt</a></li>
</ul>
<p style="text-align: left;">
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/c9Wijt7dfi8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/working-with-kconfig/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/working-with-kconfig/</feedburner:origLink></item>
		<item>
		<title>Creating and using proc files</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/3StQM74gCR0/</link>
		<comments>http://www.rt-embedded.com/blog/archives/creating-and-using-proc-files/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 15:21:27 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[proc filesystem file read write create]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=591</guid>
		<description><![CDATA[<p style="text-align: justify;">The proc files implement the simplest method of communication and data exchange between the Linux kernel or its drivers, and the user space applications or human users. The kernel provides many proc files for setting various settings, and getting plenty of information (see the article about process procs). Each driver or loadable kernel module can [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-140" title="proc" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/03/proc.jpg" alt="" width="280" height="218" />The <em>proc</em> files implement the simplest method of communication and data exchange between the Linux kernel or its drivers, and the user space applications or human users. The kernel provides many <em>proc</em> files for setting various settings, and getting plenty of information (see the article about <a href="http://www.rt-embedded.com/blog/archives/process-information-in-the-proc-filesystem/">process procs</a>). Each driver or loadable kernel module can add more proc files in the <em>/proc</em> directory, in order to set its specific parameters or to get its specific information. The <em>proc</em> files are not really files, but referred as &#8220;pseudo-files&#8221;, where all the <em>/proc</em> directory is a &#8220;pseudo-filesystem&#8221;. The reason for this name convention is the fact that unlike other filesystems, these files do not really exist. Instead, the kernel (or each driver which needs a proc file) registers a file, defines its permissions and implements a read and/or write functions. The read function will be invoked whenever a user space application wishes to read information, so the information that is read is actually generated by the kernel upon request. The same applies for the write function.  </p>
<p style="text-align: justify;"><span id="more-591"></span>Now that we know what a <em>proc</em> file is, let&#8217;s see how we can create and use it. First, in order to gain access to the <em>proc</em> functions and types, you will need to include the <em>linux/proc_fs.h</em> header file.  </p>
<h4>Creating a new sub directory in /proc</h4>
<p>If you wish to place your <em>proc</em> files in a designated sub directory, it is possible to create one. This is usually done when your driver has more than one file, and therefore, it is easier to access them and a cleaner implementation. The following example function creates a newdirerctory under <em>/proc</em> directory in a given name, and returns a pointer. Note the <em>S_IFDIR</em> flags which marks this entry as a directory (defined at linux/stat.h file).</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code><strong>struct</strong> proc_dir_entry *make_proc_dir( <strong>char</strong> *name )<br />
{<br />
   <strong>struct</strong> proc_dir_entry *my_proc_dir = <strong>NULL</strong>;</code><code>   </code><code>   </code></p>
<p><code>   /* Create a new directory */<br />
   my_proc_dir = <strong>create_proc_entry</strong>( name, S_IFDIR, <strong>NULL</strong> );</code><code>   </code><code>   </code></p>
<p><code>   <strong>if</strong> (my_proc_dir)<br />
   {<br />
</code><code>      /* No Read and Write functions here */<br />
      </code><code>my_proc_dir-&gt;read_proc  = <strong>NULL</strong>;<br />
      my_proc_dir-&gt;write_proc = <strong>NULL</strong>;<br />
   }   <br />
   <strong>return</strong> my_proc_dir;<br />
}</code></td>
</tr>
</tbody>
</table>
<h4>Registering a new proc file</h4>
<p>The next stage, is to actually create a file entry. As I mentioned, the <em>proc</em> files are not real files, but yet we need to declare them by registering them with the <em>proc </em>filesystem. In this example, the file &#8220;rt-embedded&#8221; will be created under <em>/proc</em>.  It is possible to create a file deeper in the tree simply by specifing the desired location (for example; <em>net/rt-embedded</em>  will create the file under <em>/proc/net</em>).  The file is created with 0644 permissions, means that the owner user can read and write, and the rest can read only (don&#8217;t forget the linux/stat.h file). The last parameter is optional and denotes the entry parent. Once the entry is created, we need to specify the read and write functions, according to the permissions we declared. We&#8217;ll see then next.</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<div><code><strong>struct</strong> proc_dir_entry *my_proc_file = <strong>NULL</strong>;</code></div>
<p> <code>/* Create a proc file */<br />
my_proc_file = <strong>create_proc_entry</strong>("rt-embedded", S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH, NULL);</code> </p>
<div><code><strong>if</strong> (my_proc_file)<br />
{<br />
  /* Setup the Read and Write functions */</code></div>
<div><code>  my_proc_file-&gt;read_proc  = my_proc_read;<br />
  my_proc_file-&gt;write_proc = my_proc_write;<br />
}</code></div>
</td>
</tr>
</tbody>
</table>
<h4>Implementing the read function</h4>
<p>Once a file is registered, its contents is generated upon request by a &#8220;read&#8221; function, which fetches all the required information from the driver&#8217;s internal data structures and formats the data in a human readable form. The read functions must be implemented only if there is a read access to the file, or in other words, when this file provides information. The read function&#8217;s prototype is a bit complicated. Let&#8217;s see the example first:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td> <code><strong>int </strong>my_proc_read( <strong>char</strong> *buf, <strong>char</strong> **start, <strong>off_t</strong> offset, <strong>int</strong> count, <strong>int *</strong>eof )<br />
{<br />
    /* Initialize the total length */<br />
    int len = 0;</code><code>    /* Format the data in the buffer */<br />
    len += <strong>sprintf</strong>( buf + len, "my first line of proc data\n" );<br />
    len += <strong>sprintf</strong>( buf + len, "my second line of proc data\n" );      <br />
   <br />
    /* If our data length is smaller than what<br />
       the application requested, mark End-Of-File */<br />
    <strong>if</strong>( len &lt;= count + offset )<br />
        *eof = 1;</code><code>    /* Initialize the start pointer */<br />
    *start = buffer + offset; </code></p>
<div><code>    /* Reduce the offset from the total length */<br />
    len -= offset;<br />
   <br />
    /* Normalize the len variable */<br />
    <strong>if</strong>( len &gt; count )<br />
        len = count; </code></div>
<div><code>    <strong>if</strong>( len &lt; 0 )<br />
        len = 0;</code></div>
<div> </div>
<div><code>    <strong>return</strong> len;<br />
}</code></div>
</td>
</tr>
</tbody>
</table>
<p>There are many bad implementations of the read function which ignore all the parameters. Here&#8217;s the description of each parameter:  </p>
<ul>
<li>buf: A pointer to a buffer that the kernel allocated for the proc file data.</li>
<li>start: A double pointer to indicate the start data.</li>
<li>offset: The offset of the data, from the start, that is currently requested.</li>
<li>count: The amount of bytes the reader wants.</li>
<li>eof: End-Of-File flag.</li>
</ul>
<p>When calling the read function, the kernel usually allocates a page of memory (usually 4KB) for the data. In case the reading application allocates a small buffer, it might require that this function will be called <span style="text-decoration: underline;">multiple times</span> in order to complete reading the entire data. Therefore, the kernel also provides the count, start and offset variables. The read function must honor the offset variable and set the start pointer to the appropriate offset, as requested. Only when the complete data has been transfered, then the read function turns the EOF flag on. The kernel will not call the read function again for this purpose, since the function marked that it has no more data to send.  </p>
<h4>Implementing the write function</h4>
<p>The write function is used only in case the kernel or driver allows data to be set by the caller. For example, if you have 8 LEDs on your target, your LED driver can accept writing a number in a designated <em>proc</em> file which will light up a specific LED. Implementing this function is not mandatory in case the <em>proc</em> file is in read-only mode. When working with the write function, the data needs to be copied from the user&#8217;s address space to the kernel&#8217;s user space.</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code><strong>int</strong> my_proc_write( <strong>struct</strong> file *file, <strong>const</strong> <strong>char</strong> __user *buffer, <strong>unsigned long </strong>count, <strong>void</strong> *data )<br />
{<br />
    <strong>char</strong> *page;<br />
    <strong>int</strong> size = sizeof(<strong>int</strong>); /* Example, we require to read a buffer in the size of an integer */<br />
    <strong>int</strong> my_data = 0;</code><code>    /* Make sure that the data is in the right size */<br />
    <strong>if</strong> (count &lt; size)<br />
       <strong>return</strong> -EINVAL;</code><code>    </code><code>    </code><code>    /* Allocate memory for the kernel buffer */<br />
    page = (<strong>char</strong> *) vmalloc(size);<br />
    <strong>if</strong> (!page)<br />
       <strong>return</strong> -ENOMEM;</code><code>    </code></p>
<p><code>    /* Copy the data from the user space */ <br />
    <strong>if</strong> (<strong>copy_from_user</strong>(page, buffer, size)) {<br />
       vfree(page);<br />
       <strong>return</strong> -EFAULT;<br />
    }</code></p>
<div><code>    /* Now do something with the data, here we just print it */<br />
    <strong>sscanf</strong>( page,"%d",&amp;my_data );<br />
    <strong>printk</strong>( "User has sent the value of %d\n", my_data );</code></div>
<div><code>    </code></div>
<div><code>    /* Free the allocated memory */<br />
    <strong>vfree</strong>(page); </code></div>
<p><code>    <strong>return</strong> count;<br />
}</code></td>
</tr>
</tbody>
</table>
<p>The <em>file</em> and <em>data</em> parameters are not required for general use. The kernel sends the user space buffer in the <em>buffer</em> variable and its size in the <em>count</em> variable. We need to verify that the data that was sent is in the right size, and in case we transfer structures of data, we can add a <em>magic number</em> member which will hold a constant value, just to make sure that the expected data type was actually sent. Then, we must copy the data to the kernel space because it is impossible to access it as is with the user space pointer. After we&#8217;ve copied the data, we can start parsing and working on the data. The return value tells the kernel if we failed (by returning a negative value) or processed all the data (by returning the <em>count</em> number). </p>
<h4>Removal of a proc file</h4>
<p>It is possible to remove a <em>proc</em> file from the <em>proc</em> filesystem. This function is mostly used by Loadable Kernel modules which are in the process for shutting down during removal (using <em>rmmod</em>). In order to remove a proc file, just use the following function with the file name:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code> <strong>remove_proc_entry</strong>( name, <strong>NULL</strong> );</code></td>
</tr>
</tbody>
</table>
<h4>Usage example from a user space application</h4>
<p>Now let&#8217;s see how we can access these <em>proc</em> files. The simplest way to access them is manually, in the Linux shell. In order to read a contents of a <em>proc</em> file, we simply use the <em>&#8220;cat&#8221;</em> command and the file name, for example: <em>cat /proc/my_proc_file</em>. In order to write data to a <em>proc</em> file, we simply use the &#8220;<em>echo</em>&#8221; command with redirection of the output to the destination file, for example: <em>echo &#8220;write data&#8221; &gt; /proc/my_proc_file</em>.  </p>
<p style="text-align: justify;">We can also access these files from our user space applications. The process is actually quite similar and very simple. Each program who wants to read (or write) data, is required to open the file in the standard way (either with stream access; <em>fopen</em>, or descriptor access; <em>open</em>). Then, it reads the contents of the file to a buffer and then it closes it. The data, is always in human readable text mode. Therefore, the program needs to convert it back to binary representation in case it needs to work or analyze the data.  </p>
<h4>Other proc helper functions</h4>
<p>The kernel provides some more <em>proc</em> APIs that may short-cut or encapsulate some of the actions or parameters when using the standard <em>create_proc_entry</em> function. Some examples of such APIs:  </p>
<ul>
<li>proc_mkdir</li>
<li>proc_symlink</li>
<li>proc_net_create</li>
<li>create_proc_read_entry</li>
</ul>
<p> Refer to the <em>linux/proc_fs.h</em> file for the complete list of available API.</p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/3StQM74gCR0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/creating-and-using-proc-files/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/creating-and-using-proc-files/</feedburner:origLink></item>
		<item>
		<title>PCD – Process Control Daemon</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/m20CNpuU-4M/</link>
		<comments>http://www.rt-embedded.com/blog/archives/pcd/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 12:00:54 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[boot]]></category>
		<category><![CDATA[Control]]></category>
		<category><![CDATA[Daemon]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[monitor]]></category>
		<category><![CDATA[pcd]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[rcs]]></category>
		<category><![CDATA[real]]></category>
		<category><![CDATA[recovery]]></category>
		<category><![CDATA[rt]]></category>
		<category><![CDATA[Startup]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=48</guid>
		<description><![CDATA[



<p style="text-align: center;">DOCUMENTATION</p>


<p style="text-align: center;">EXAMPLES</p>




What is PCD?
<p style="text-align: justify;">PCD (Process Control Daemon), is an open source, light-weight, system level process manager for Embedded Linux based products (Consumer electronics, network devices, and more). With PCD in control, it is guaranteed that the system&#8217;s boot up time will be reduced, and its reliability, availability and robustness will [...]]]></description>
				<content:encoded><![CDATA[<table style="height: 18px;" border="1" cellspacing="0" cellpadding="0" width="669">
<tbody>
<tr>
<td width="197" valign="top">
<p style="text-align: center;"><a href="http://www.rt-embedded.com/blog/pcd-process-control-daemon/pcd-documentation/" target="_self">DOCUMENTATION</a></p>
</td>
<td width="197" valign="top">
<p style="text-align: center;"><a href="http://www.rt-embedded.com/blog/wp-content/uploads/2010/10/examples.tar.bz2" target="_self">EXAMPLES</a></p>
</td>
</tr>
</tbody>
</table>
<h2 style="text-align: left;">What is PCD?</h2>
<p style="text-align: justify;">PCD (<em><strong>Process Control Daemon</strong>)</em>, is an open source, light-weight, system level process manager for Embedded Linux based products (Consumer electronics, network devices, and more). With PCD in control, it is guaranteed that the system&#8217;s boot up time will be reduced, and its reliability, availability and robustness will be enhanced.</p>
<h3 style="text-align: left;">Why do you need PCD in your Embedded Linux based product?</h3>
<p style="text-align: justify;"><img class="alignright size-full wp-image-316" title="PCD - Process Control Daemon" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/pcd1.jpg" alt="PCD - Process Control Daemon" width="158" height="215" />With PCD driving your product, you will gain:</p>
<ul style="text-align: justify;">
<li style="text-align: left;">Enhanced control and monitoring on all the processes in the system.</li>
<li style="text-align: left;">Simple, yet powerfull means to configure each process and service in the system:
<ul>
<li>When to start each process, configure its dependencies.</li>
<li>What resource or condition to check, per each process, in order to verify its successful startup.</li>
<li>What recovery action to take when a process fails to start or crashes during its work.</li>
</ul>
</li>
<li style="text-align: left;">Reduced system startup time
<ul>
<li>Rules are started as soon as their start condition is satisfied.</li>
<li>No need for non-deterministic delays. Dependencies are well defined and enforced.</li>
<li>Rules without inter-dependency are started in parallel.</li>
</ul>
</li>
<li style="text-align: left;">Enhanced debug capabilities of faulty processes
<ul>
<li>Exception handlers and logs provide useful and detailed information about process crashes, such as the process name and id, exception description, date and time, fault address, register dump, map file and more.</li>
<li>Crash log storage in non-volatile memory for post-mortem/offline analysis.</li>
</ul>
</li>
<li style="text-align: left;">Improvement in system stability and robustness.</li>
<li style="text-align: left;">Generation of a graphic representation of the system startup sequence for further analysis.</li>
</ul>
<p style="text-align: justify;"><span id="more-48"></span></p>
<h3 style="text-align: left;">Some Background</h3>
<p style="text-align: justify;">The PCD project was started due to a need for a process controller which was missing in one of TI&#8217;s broadband solutions. In the early stages of the software development, the system was started using scripts, where the <em>init.d/rcS</em> script was doing some of the work, and it was calling other scripts which loaded all the processes, drivers and services. Needless to say, this method was far from fulfilling the required task, especially for a mass-production product. There were cases where we were not able to determine when a driver was initialized, so we added sleeps. In other cases, some process has crashed and we didn&#8217;t even notice it, but experienced unexpected system behavior. The debugging capabilities were also minimal. It is not trivial to work with gdbserver on an embedded target, it requires a stable communication channel and symbols.</p>
<p style="text-align: justify;">Eventually, the PCD project has been initiated to provide an adequate solution to all these issues, and it has improved the product&#8217;s quality, robustness and stability. We were able to catch and fix all the crashes early using the extended debug information. Furthermore, it has reduced the boot up time significantly because the sleeps were not required anymore, and due to activation of many non-depended processes in parallel.</p>
<p style="text-align: justify;">The PCD was originally designed and developed by <a href="http://il.linkedin.com/in/haishalom" target="_blank">Hai Shalom</a> as a proprietary solution for Texas Instruments. During the last stages of the development, the PCD project was released by TI as an open source software and the project&#8217;s community is in process of growing. New programmers which find the PCD useful and interesting project are starting to contribute to the project with their ideas and innovations.</p>
<h3 style="text-align: justify;">Supported Architectures</h3>
<p style="text-align: justify;">The PCD is a user space application, therefore its exposure to the actual architecture is limited. However, supported platforms have an enhanced and detailed crash dump which displays all registers, thus allowing an easier debug.</p>
<p style="text-align: justify;">The PCD fully supports the following architectures:</p>
<ul>
<li><strong>ARM</strong></li>
<li><strong>MIPS</strong></li>
<li><strong>x86</strong></li>
<li><strong>x64</strong></li>
</ul>
<p>Non-supported architectures should also work, but without the enhanced exception details.<br />
If PCD does not support your platform, it is possible to support it if you&#8217;ll send the hardware and BSP. Please use the <a href="../contact/">contact page</a> to contact me.</p>
<h3 style="text-align: left;">Already have a process controller in your product?</h3>
<p style="text-align: justify;">You can still use the PCD in &#8220;crash-daemon only&#8221; mode. In this mode, the PCD will not start your system nor monitor its processes. Instead, it will remain idle until one of your application crashes, and when that happens, the PCD will generate and log the detailed crash dump which could help you isolate and fix the bug. There is no need for any rules file, and in case you&#8217;ll like using PCD, you might consider upgrading your existing process controller.</p>
<h3 style="text-align: left;">PCD High Level Presentation (updated)</h3>
<p style="text-align: left;">The following slides provide a high level overview on the PCD project.</p>
<p style="text-align: center;"><iframe src="http://www.slideshare.net/slideshow/embed_code/7593162" width="640" height="480" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
<div style="padding:5px 0 12px"> </div>
</p></div>
<h3 style="text-align: justify;">Integrate PCD in 4 steps</h3>
<p style="text-align: justify;">Thinking about the integration effort? It&#8217;s not that difficult. Follow these 4 steps in order to integrate PCD in your Linux based system:</p>
<h4 style="text-align: justify;">Step 1: Get introduced</h4>
<ol>
<li>
<div style="text-align: justify;">Download the PCD source code from the <a href="http://sourceforge.net/projects/pcd/files/pcd-sdk-latest.tar.bz2/download" target="_blank">PCD project page</a> at SourceForge.</div>
</li>
<li>
<div style="text-align: justify;">Download the PCD <a href="http://sourceforge.net/projects/pcd/files/docs/Process%20Control%20Daemon%20-%20High-Level%20design%20document.pdf/download" target="_blank">documentation</a> and <a href="http://sourceforge.net/projects/pcd/files/docs/Process%20Control%20Daemon%20-%20OSS.ppt/download" target="_blank">training presentation</a>.</div>
</li>
<li>
<div style="text-align: justify;">Unpack, compile and install the PCD in your target&#8217;s file system.</div>
</li>
</ol>
<p><a href="http://downloads.sourceforge.net/project/pcd/pcd-sdk-latest.tar.bz2?use_mirror=switch" target="_blank"><img class="alignnone" title="Download PCD now!" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/download-pcd-now.jpg" alt="Download PCD now!" width="258" height="63" /></a></p>
<h4 style="text-align: justify;">Step 2: Write rules</h4>
<ol>
<li style="text-align: justify;">Determine the order and dependencies of each process, daemon or service.</li>
<li style="text-align: justify;">Determine what is the condition for a successful init of each process.</li>
<li style="text-align: justify;">Determine the recovery actions to take if they fail.</li>
<li style="text-align: justify;">Write a rule per each process using the PCD&#8217;s simple syntax.</li>
<li style="text-align: justify;">Run <em>pcdparser</em> to check your rules file for syntax errors.</li>
<li style="text-align: justify;">Generate a dependency graph which describes your system&#8217;s startup.</li>
<li style="text-align: justify;">Install the rules file(s) in the target&#8217;s file system.</li>
</ol>
<p style="text-align: center;"><img class="aligncenter" title="pcd-rules-to-graph" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/pcd-rules-to-graph1.jpg" alt="pcd-rules-to-graph" width="633" height="242" /></p>
<h4 style="text-align: justify;">Step 3: Enhance your processes</h4>
<ol>
<li style="text-align: left;">Install the PCD&#8217;s exception handlers in your processes (you&#8217;ll gain a detailed crash log).</li>
<li style="text-align: left;">Replace any <em>fork-exec</em> or <em>system( )</em> calls with the PCD API with the corresponding rule.</li>
<li style="text-align: left;">Send a &#8220;<em>process ready event</em>&#8221; to the PCD when your process has completed its initialization.</li>
</ol>
<h4 style="text-align: justify;">Step 4: Activate PCD</h4>
<ol>
<li>Remove any script based startup. Remove explicit calls to daemons and processes.</li>
<li>Remove any delays and sleeps which were used for process synchronization.</li>
<li>Add one call to PCD along with your new rule file(s).</li>
</ol>
<h4 style="text-align: justify;">That&#8217;s it!</h4>
<p style="text-align: justify;">Your system will now be booted with the PCD synchronizing and monitoring everything. In case of an error, it will provide a detailed crash log and take the necessary steps in order to recover the system.</p>
<h3><img class="alignright" title="heckert_gnu_small" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/heckert_gnu_small.png" alt="heckert_gnu_small" width="145" height="140" />PCD Licensing</h3>
<ul>
<li>The PCD project is licensed under the GNU Lesser General Public License version 2.1 (<strong>LGPLv2.1</strong>)<strong>,</strong> as published by the Free Software Foundation.</li>
<li>To view a copy of this license, visit <a title="http://www.gnu.org/licenses/lgpl-2.1.html#SEC1" href="http://www.gnu.org/licenses/lgpl-2.1.html#SEC1" target="_parent">http://www.gnu.org/licenses/lgpl-2.1.html#SEC1</a></li>
<li>There are <strong>no</strong> purchase costs or royalty fees.</li>
<li>This license allows commercial use without the need to expose proprietary source code. Any enhancements or features that are added to the PCD must be submitted back to the community.</li>
</ul>
<h3 style="text-align: justify;">PCD Resources</h3>
<p style="text-align: justify;">Browse the PCD website, join the community, request features, report bugs, discuss in the forum and get help:</p>
<ul style="text-align: justify;">
<li style="text-align: left;">PCD project page at SourceForge: <a title="http://sourceforge.net/projects/pcd/" href="http://sourceforge.net/projects/pcd/">http://sourceforge.net/projects/pcd/</a></li>
<li style="text-align: left;">PCD presentations: <a href="http://www.slideshare.net/haish" target="_blank">http://www.slideshare.net/haish</a></li>
<li style="text-align: left;">PCD Design document: <a href="http://sourceforge.net/projects/pcd/files/docs/PCD-High-Level-Design-Document.pdf/download">http://sourceforge.net/projects/pcd/files/docs/PCD-High-Level-Design-Document.pdf/download</a></li>
<li style="text-align: left;">PCD Documentation <a href="http://www.rt-embedded.com/blog/pcd-process-control-daemon/pcd-documentation/" target="_blank">http://www.rt-embedded.com/blog/pcd-process-control-daemon/pcd-documentation/</a></li>
<li style="text-align: left;">Examples for PCD debug capabilities: <a href="http://www.rt-embedded.com/blog/archives/resolving-crashes-and-segmentation-faults/" target="_blank">Resolving segmentation faults with PCD</a>, <a href="http://www.rt-embedded.com/blog/archives/resolving-alignment-traps/" target="_blank">Resolving alignment traps with PCD</a>.</li>
</ul>
<h3 style="text-align: justify;">Need more information, training or support?</h3>
<p style="text-align: justify;">If you are managing a commercial project, it is possible to receive a dedicated, on demand PCD training and support. Please use the <a href="http://www.rt-embedded.com/blog/contact/">contact page</a> for more details.</p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/m20CNpuU-4M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/pcd/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/pcd/</feedburner:origLink></item>
		<item>
		<title>Size optimization – Part 3</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/_20OFTxYoMU/</link>
		<comments>http://www.rt-embedded.com/blog/archives/size-optimization-part-3/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 16:16:54 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[mklibs]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[reduction]]></category>
		<category><![CDATA[shared]]></category>
		<category><![CDATA[size]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=563</guid>
		<description><![CDATA[<p style="text-align: justify;">Now that you&#8217;ve optimized your applications (programs) and archives (static libraries), we&#8217;ll discuss how to optimize your shared libraries. Unlike archives which are used only during link time on the host machine, shared libraries reside on the target&#8217;s file system, and cannot be reduced using the the same techniques. Furthermore, when you create a [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-569" title="weight-3" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/06/weight-3.jpg" alt="" width="200" height="171" />Now that you&#8217;ve optimized your applications (programs) and archives (static libraries), we&#8217;ll discuss how to optimize your shared libraries. Unlike archives which are used only during link time on the host machine, shared libraries reside on the target&#8217;s file system, and cannot be reduced using the the same techniques. Furthermore, when you create a shared library, you can not know which functions will be used by the applications and which functions are not used. It is also not trivial to figure out the dependency between the library&#8217;s functions (which function requires another one inside the library). Therefore, shared libraries always contain the full set of functions. The question that we ask is; how much storage space is wasted for unused code of a shared library?</p>
<p style="text-align: justify;"><span id="more-563"></span></p>
<p style="text-align: justify;">Assuming we always use small and light-weighted libraries, it may not be a serious issue, although the sum of many small libraries may add up eventually. However, there are cases where large libraries are used, for example OpenSSL. In terms of embedded systems, this library is huge and requires a large part of the system&#8217;s storage. In most cases, we don&#8217;t need the full functional set of this library. Some libraries (OpenSSL too) allow a level of configuration which allows the user to disable some of its features to reduce the final size. We can also work hard to manually remove some objects from the library according to our current needs. But what happens if we add a new program that requires some functionality that we already removed? We&#8217;ll end up with a broken system due to unresolved symbols. How can we automate shared library reduction? Here&#8217;s the answer: use <strong>mklibs</strong>.</p>
<h4 style="text-align: justify;">What is mklibs?</h4>
<p style="text-align: justify;">mklibs is a script which was originally used for creating bootable floppy diskettes. Due to the fact that these historical storage medias were very limited in size, it was necessary to save only the needed code, and nothing else. The Internet is full of variants of this script, some variants use shell syntax and others use Python syntax. In the resources section I provided links for some variants. Nowadays, in modern embedded systems we have a similar problem. Embedded systems also have limited storage, not because of technological limitation, but due to the final cost. Smaller storage devices are simply cheaper.</p>
<h4 style="text-align: justify;">How does it work?</h4>
<p>The mklibs uses recursive iterations on all the program&#8217;s symbols and share library simbols as follows (quoting from the script):</p>
<ul>
<li>Gather all unresolved symbols and libraries needed by the programs  and reduced libraries.</li>
<li>Gather all symbols provided by the already reduced libraries (none on the first pass).</li>
<li>If all symbols are provided:
<ul>
<li>we are done.</li>
</ul>
</li>
<li>Else:
<ul>
<li>Go through all libraries and remember what symbols they provide.</li>
<li>Go through all unresolved/needed symbols and mark them as used.</li>
<li>For each library:
<ul>
<li>Find <em>pic</em> file (if not present copy and strip the share library).</li>
<li>Compile in only used symbols.</li>
<li>Strip.</li>
<li>Back to the top.</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>How can it be used?</h4>
<p>Please note that it may not be so trivial to use this script in your system, there are plenty of potential issues that will prevent the script from completing its task, or the outcome to actually work on the target. Here are a few pointers that will help you (I spent a lot of time figuring out everything):</p>
<ol>
<li>Your shared libraries must be created with the <code>soname</code> flag enabled. This flag embeds the shared library name inside the shared library itself. Here&#8217;s an example: &#8220;<code>-Wl,--soname,$(so_target)</code>&#8220;, where the <code>so_target</code>variable contains the full library&#8217;s name (such as libmath.so).</li>
<li>Per each library you must create a <em>pic</em> file, which is actually a static library version of your shared library, compiled with function and data sections (see <a href="http://www.rt-embedded.com/blog/archives/size-optimization-part-2/" target="_blank">part 2</a> for more details). The archive name must be with the same name of the shared library, with the suffix <code>_pic</code> and the extension <code>.a</code>. In order to accomplish this, you need to use the linker to create a single object file from the list of library objects, and then call the archiver to create an archive from this linked object. For example, the following command will create a single object out of the library&#8217;s objects: &#8220;<code>ld -r -o $(so_target)_pic.o $(objs) $(LDFLAGS)</code>&#8220;, the following command to create an archive: &#8220;<code>ar rc $(so_target)_pic.a $(so_target)_pic.o</code>&#8220;. The last command is required after the archive is created: &#8220;<code>ranlib $(so_target)_pic.a</code>&#8220;.</li>
</ol>
<p>Once the script encounter a shared library with the name embedded inside and a matching <em>pic</em> file, it will be able to maximize the reduction of this library. Otherwise, it will only strip it, which will not reduce it size further (stripping is usually done anyway).</p>
<p>The following parameters are supported by the script. Note that this list may change between the various variants:</p>
<p style="padding-left: 30px;"><code>-d, --dest-dir DIRECTORY create libraries in DIRECTORY<br />
-D, --no-default-lib omit default libpath ( /lib/ : /usr/lib/ : /usr/X11R6/lib/ )<br />
-L DIRECTORY[:DIRECTORY]... add DIRECTORY(s) to the library search path<br />
--ldlib LDLIB use LDLIB for the dynamic linker<br />
--libc-extras-dir DIRECTORY look for libc extra files in DIRECTORY<br />
--target TARGET prepend TARGET to the gcc and binutils calls<br />
--root ROOT search in ROOT for library rpaths<br />
-v, --verbose explain what is being done<br />
-h, --help display this help and exit</code></p>
<p>For embedded systems, the following flags are recommended:</p>
<ol>
<li>-D: You must configure the script not to use the host&#8217;s libraries.</li>
<li>-L &lt;directory&gt;: Specify where your libraries reside.</li>
<li>&#8211;ldlib &lt;loader library&gt;: Specify where is the loader library. For uClibc systems, the loader is called ld-uClibc-0.9.XX.so, where XX stands for the uClibc version (28,29,30&#8230;).</li>
<li>&#8211;libc-extras-dir &lt;libc directory&gt;: Specify where the uClibc libraries reside. In uClibc systems, they reside in the uClibc directory, under lib/.</li>
<li>&#8211;target &lt;CROSS&gt;: Specify your cross compiler prefix.</li>
<li>-d &lt;dest directory&gt;: Specify the destination output directory where all the reduced libraries will be stored.</li>
</ol>
<p>If you are using uClibc in your system, you should consider using the uClibc version of mklibs (see link in the resources section).</p>
<h4>Estimated size reduction</h4>
<p>If your system uses large libraries, especially ones that came from Open Source, and designed for general purposes, there is a good chance that you&#8217;ll see a lot of reduction in size. In the platform I am working with, I was able to reduce the final size of the OpenSSL library by 25%. It means that there were a few hundred bytes which were not used, just sitting on the precious and limited storage media. On the other hand, if most of the libraries are light weighted, and locally written, usually there are no excess functions which are not used, as most are used by design.</p>
<h4>Other pointers</h4>
<p>The script will give you hard time until it will produce the required results. However, if your system uses open source libraries and is limited with storage, this script will produce the most of size reduction. In case there is a shared library which is breaking due to this optimization process (it is possible), you can disable the optimization on this specific library by removing its <em>pic</em> file. As I mentioned, without a <em>pic</em> file, the script will only strip the library and keep it intact.</p>
<p style="text-align: left;"><span style="text-decoration: underline;">Resources</span>:<br />
mklibs from debian: <a href="http://ftp.debian.org/pool/main/m/mklibs/">http://ftp.debian.org/pool/main/m/mklibs/</a><br />
mklibs with uClibc fixes: <a href="http://cristi.indefero.net/p/buildroot-cristi/source/tree/8a25f1c4e78f02b1e722ccc2f659c025ba662296/toolchain/mklibs/mklibs.py">http://cristi.indefero.net/p/buildroot-cristi/source/tree/8a25f1c4e78f02b1e722ccc2f659c025ba662296/toolchain/mklibs/mklibs.py</a><br />
<a href="http://tracker.coreboot.org/trac/buildrom/browser/buildrom-devel/bin/mklibs.py">http://tracker.coreboot.org/trac/buildrom/browser/buildrom-devel/bin/mklibs.py</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/_20OFTxYoMU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/size-optimization-part-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/size-optimization-part-3/</feedburner:origLink></item>
		<item>
		<title>Size optimization – part 2</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/GaXqcR-Iqk0/</link>
		<comments>http://www.rt-embedded.com/blog/archives/size-optimization-part-2/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 16:38:31 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[section]]></category>
		<category><![CDATA[size]]></category>
		<category><![CDATA[static]]></category>
		<category><![CDATA[whole]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=537</guid>
		<description><![CDATA[<p style="text-align: justify;">The tips and information in part 1 are too general and common, and it is highly likely that you&#8217;ve already implemented them. In this article, I will show how we can reduce the size of static libraries (archives) and applications (programs) by specifying advanced compilation flags which utilize the special properties of archives and applications.    </p>
<p style="text-align: [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-543" title="weight-2" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/06/weight-2.jpg" alt="" width="200" height="171" />The tips and information in part 1 are too general and common, and it is highly likely that you&#8217;ve already implemented them. In this article, I will show how we can reduce the size of static libraries (archives) and applications (programs) by specifying advanced compilation flags which utilize the special properties of archives and applications.    </p>
<p style="text-align: justify;"><span id="more-537"></span>    </p>
<h3 style="text-align: left;">Optimizing the static library (archive)</h3>
<p style="text-align: justify;"><a href="http://www.rt-embedded.com/blog/archives/libraries/" target="_blank">Static libraries</a> are actually archives of objects. An archive can contain one or more objects, where each object can contain one or more functions. When an application requires a specific function from an archive, it links with it, and the linker copies the entire object which contains this function from the archive to the application. This could cause an excess duplication of an unneeded code, in case this object contains some other functions and data, which is usually the case. In order to optimize the link process here, and copy the required function <strong>only</strong>, we can tell the compiler to put each function and global data variable in a separated section inside the object.  Although this action slightly increases the size of the archive, it will produce better results in the target. Don&#8217;t forget that the archive is not a part of the final file system in the target. This option allows the linker to select only the relevant function&#8217;s code from the archive, without copying the entire object - thus reducing the final size of the application. The compiler flags for this options are given per each source code compilation:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>--ffunction-sections --fdata-sections</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">Note that all the optimization techniques specified in <a href="http://www.rt-embedded.com/blog/archives/size-optimization-part-1/" target="_blank">part 1</a> can be also applied for static libraries.    </p>
<h3 style="text-align: left;">Optimizing the application</h3>
<p style="text-align: justify;">The static library optimization which I described above can be also applied for applications, although it will yield less reduction because most of the functions inside the application are usually used (unused functions are usually removed by the authoer). When the compiler finishes to compile all the source files and you have a list of objects which need to be linked for the final application, you need to instruct the linker to clean the unused sections. If you remember, enabling the function and data sections allows the linker to remove any unused functions and data variables. The following command needs to be specified in the final link process of your application:   </p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>-Wl,--gc-sections</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">Another property of an application in Linux environment is that the application is a single entity without any connections to other applications. This property allows us to instruct the compiler to look at all the application&#8217;s code as a single entity (a whole program) and enable some more agressive optimizations which can be enabled only when the compiler sees the whole program (not possible when compiling single source files). This option is available from gcc version 4.1 and newer. In case you are using Makefiles which compile each source file separately, you will have to change it if you want to enable this option. Instead of compiling each source file, you need to compile the whole application in a single command line, while specifying all the source files and compilation flags, and the following flag to enable the optimization:   </p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>--combine</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">The result of this action is a large and optimized object file. Next, when you need to link the object, specify the following flag:   </p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>-fwhole-proram</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">While this method reduces the size of the application, it also has some drawbacks. One of them is lack of dependency, because it requires to recompile the entire source files (although in a single command) per each change in one of the sources. This method also requires more CPU power and memory in the host computer.    </p>
<p style="text-align: justify;">Here&#8217;s an example Makefile snippet for this method:   </p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># Makefile snippet for enabling the whole-program optimization.<br />
# Author: Hai Shalom, <a href="http://www.rt-embedded.com/">http://www.rt-embedded.com/</a><br />
#<br />
# Assuming the following variables:<br />
# TARGET - contains the target application name<br />
# CFLAGS - contains the project default C flags<br />
# LDFLAGS - contains the project default linker flags<br />
# SRC - contains the application source files<br />
# LIBS - contains the libraries the application links with</p>
<p></code><code></p>
<div><code>$(TARGET)_combined.o: $(SRC)<br />
 @echo "Compiling  $(SRC)"<br />
 @$(CC) $(CFLAGS) -c --combine $(SRC) -o $@</code></div>
<p><code><code>$(TARGET): $(TARGET)_combined.o<br />
 @echo "Linking  $@"<br />
 @$(CC) -o $@ $(TARGET)_combined.o $(LDFLAGS) -Wl,--gc-sections -fwhole-program -Wl,--sort-common -Wl,--sort-section,alignment -Wl,--start-group $(LIBS) -Wl,--end-group</code></code></p>
<p></code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">The last method to squeeze the last unused bytes in an application is to instruct the linker to sort the sections and remove unneeded padding&#8217;s. The following flags need to be added to the final link command:   </p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>-Wl,--sort-common -Wl,--sort-section,alignment</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">Note that all the optimization techniques specified in <a href="http://www.rt-embedded.com/blog/archives/size-optimization-part-1/" target="_blank">part 1</a> can be also applied for applications.</p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/GaXqcR-Iqk0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/size-optimization-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/size-optimization-part-2/</feedburner:origLink></item>
		<item>
		<title>Size optimization – part 1</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/Fopp8NR0yfM/</link>
		<comments>http://www.rt-embedded.com/blog/archives/size-optimization-part-1/#comments</comments>
		<pubDate>Sun, 30 May 2010 15:52:32 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[reduction]]></category>
		<category><![CDATA[size]]></category>
		<category><![CDATA[strip]]></category>
		<category><![CDATA[symobls]]></category>
		<category><![CDATA[thumb]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=520</guid>
		<description><![CDATA[<p style="text-align: justify;">In embedded systems, size does matter. Embedded products are usually limited in resources of RAM and storage (usually Flash) and the cost pressure forces you to think about creative ways to reduce the overall size of the binary applications and libraries, without reducing the features and functionality. In the years I&#8217;ve been working [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-528" title="weight-1" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/05/weight-1.jpg" alt="" width="200" height="171" />In embedded systems, size <strong>does </strong>matter. Embedded products are usually limited in resources of RAM and storage (usually Flash) and the cost pressure forces you to think about creative ways to reduce the overall size of the binary applications and libraries, without reducing the features and functionality. In the years I&#8217;ve been working in the embedded systems business, I often deal with requirements to reduce the overall size of the application due to system limitation. Therefore, I have a lot of experience in this field which I am going to share with you in this &#8220;Size optimization&#8221; series. The series will include information about size optimization and reduction,  general tips, optimizing applications, static libraries, shared libraries, file systms and the Linux kernel.</p>
<p style="text-align: justify;"><span id="more-520"></span></p>
<h3 style="text-align: justify;">Background</h3>
<p style="text-align: justify;">Embedded systems are usually equipped with limited amount of RAM (32MB &#8211; 128MB) and even more limited amount of flash memory (1MB &#8211; 16MB), where the sizes of the devices grow in the power of 2. So if your application image requires 4.1MB of storage, your product must be equipped with a 8MB flash device instead of a cheaper 4MB device, if the image was just a bit smaller. In the following series of articles, I&#8217;m going to show some techniques to reduce the binary size without actually changing the code itself, which could yield also better results. I will dedicate one or more article about writing a more efficient code as well. The input for this purpose is the source code which is refered as a &#8220;black box&#8221;.</p>
<h3 style="text-align: justify;">General tips for size optimization</h3>
<p style="text-align: justify;">The following tips will yield smaller binary output.</p>
<h4 style="text-align: justify;">Configure the compiler</h4>
<p style="text-align: justify;">The compiler can be configured to produce smaller binaris by applying optimizations to the assembly code that result in smaller output. As described in the &#8220;<a href="http://www.rt-embedded.com/blog/archives/using-gcc-part-2/" target="_blank">using the gcc</a>&#8221; article, enable this option by adding the &#8220;-Os&#8221; flag in the command line. In case another optimization level is used (-Ox, where x could be 0,1,2,3), you&#8217;ll need to replace it. Note that replacing the optimization with size optimization may also result in performance impact, especially if levels 2 or 3 were previously used. These levels configure the compiler to modify the code to yield better performance, and level 3 even increases the output size in order to improve performance (like loop unfolding for example). The size optimization works on the other direction, where the performance is less important than the actual output size. This option is mostly used when compiling a boot loader which must be confined to the first 1 or few sectors in the flash, but can be also used for compiling the kernel and user space applications. The following table shows the output size (in bytes) of an application which was compiled with various optimization levels:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="148" valign="top"><strong>No optimization</strong></td>
<td width="148" valign="top"><strong>-O2</strong></td>
<td width="148" valign="top"><strong>-O3</strong></td>
<td width="148" valign="top"><strong>-Os</strong></td>
</tr>
<tr>
<td width="148" valign="top">503,751</td>
<td width="148" valign="top">466,469</td>
<td width="148" valign="top">522,369</td>
<td width="148" valign="top">449,817</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">As we can see from this table, the level 2 optimization has the best trade-off between size and performance, comparing to &#8220;no optimiztion&#8221;. The size in level 2 optimization has been reduced by 8%. Level 3 optimization has actually increased the size by 4% (with the supposed performance increase) and level s has reduced the file size by 11% (with some impact of performance).</p>
<h4 style="text-align: justify;">Stripping the output</h4>
<p style="text-align: justify;">By default, and especially when the <em>-g </em>flag was used in the compilation command line, the output contains some debug symbols which are useful for debugging and analysis. However, in you final target, these symbols are not required and can be removed (stripped) in order to reduce the size of the image. In order to remove these symbols, use the <em>strip</em>application which comes in the binutils package with the <em>&#8211;strip-unneeded</em> flag. The following table shows the size differences (in bytes) before and after stripping:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="295" valign="top"><strong>Before stripping</strong></td>
<td width="295" valign="top"><strong>After stripping</strong></td>
</tr>
<tr>
<td width="295" valign="top">449,817</td>
<td width="295" valign="top">162,916</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Stripping the output is safe and has no effect on the performance. It does not change the behavior of the application, just removes unused data.</p>
<h4 style="text-align: justify;">Compiling in Thumb mode (ARM platforms only)</h4>
<p style="text-align: justify;">The ARM family of CPUs provides an option to work in a reduced instruction set, which is 16-bit in length instead of 32-bit. This instruction set is referred as &#8220;<a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0135a/BABFCAHG.html" target="_blank">Thumb mode</a>&#8220;.  This mode actually uses a compressed instruction set and thus resulting with a smaller code. The Thumb mode may reduce the output size by up to 30%. The performance figures are not always clear here. In case the CPU is 16-bit native, it will yield better performance. However, on a 32-bit CPU, the thumb mode may either reduce the performance or not impact it at all (it&#8217;s application depended). In a mixed instruction system, you also need to configure the compiler to produce interwork code, which is required when switching from the ARM mode to Thumb mode and vice versa. Therefore, in order to produce a Thumb output, use the &#8220;<em>-mthumb -mthumb-interwork</em>&#8221; flags. Note that you can&#8217;t compile the Linux kernel in Thumb mode and most of boot loaders due to use of ARM assembly code and other reasons. However, it can be very useful for user space applications and libraries.</p>
<p style="text-align: justify;"><span style="text-decoration: underline;">Resources:<br />
</span><a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0135a/BABFCAHG.html">http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0135a/BABFCAHG.html</a><br />
<a href="http://www.rt-embedded.com/blog/archives/using-gcc-part-2/">http://www.rt-embedded.com/blog/archives/using-gcc-part-2/</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/Fopp8NR0yfM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/size-optimization-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/size-optimization-part-1/</feedburner:origLink></item>
		<item>
		<title>Compilation warnings management</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/Pq1UrO8caWo/</link>
		<comments>http://www.rt-embedded.com/blog/archives/compilation-warnings-management/#comments</comments>
		<pubDate>Sun, 23 May 2010 12:19:42 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Opinions]]></category>
		<category><![CDATA[compilation]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[Wall]]></category>
		<category><![CDATA[warnings]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=495</guid>
		<description><![CDATA[<p style="text-align: justify;">One measurement of the quality of your project&#8217;s code is the amount of outstanding compilation warnings. In my opinion, these compilation warnings could potentially be responsible for a system crash and unexplained behaviour, and are one of the top 5 destructive bugs. If you are a project manager, it is your interest to eliminate [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-502" title="warning-sign" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/05/warning-sign.gif" alt="" width="200" height="176" />One measurement of the quality of your project&#8217;s code is the amount of outstanding compilation warnings. In my opinion, these compilation warnings could potentially be responsible for a system crash and unexplained behaviour, and are one of the <a href="http://www.rt-embedded.com/blog/archives/5-most-destructive-bugs/" target="_blank">top 5 destructive bugs</a>. If you are a project manager, it is your interest to eliminate them, especially if your deliverable is the actual source code. As a customer, a software deliverable with plenty of compilation warnings appears to be non-professional just by looking at the compilation process. If you are a developer, it is your aim to deliver warning-less code.</p>
<p><span id="more-495"></span></p>
<p style="text-align: justify;">In this article, I will provide a few methods and tips for warning management, meant mostly for project managers. As I mentioned earlier, code that compiles cleanly looks better and more professional. It is even more important if you deliver source code to your customers. Compilation warnings could sometimes indicate a real bug or a potential issue. Furthermore, if your project is already full of warnings, you have no control of new warnings which are introduced in each code change. Since the screen is already so full of warnings, you&#8217;ll most probably miss new ones. This is the way to complete chaos in your project in terms of warning management and control. If you want to gain control over this issue and eliminate the warnings in your project, follow the following steps.</p>
<h3 style="text-align: justify;">1st step: Assess the amount of warnings</h3>
<p style="text-align: justify;">In the projects I manage, I don&#8217;t allow any warnings. However, suppose your current project contains warnings, first you need to assess the number of warnings in high-level. You can later divide the numbers to groups, according to the corresponding component. With these numbers at hand, you can assign warning-fixing tasks to your staff. The following shell script will help you getting the warning numbers. It will clean and compile the component in question and provide the number of outstanding warning count. You should allocate some time before each delivery deadline, and run this script, to make sure your project is meeting the warning count goals. In order to use this script, you need to specify the target name and optionally, a file name to contain the list of warnings. If you don&#8217;t have a target, specify &#8220;all&#8221;.</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># !/bin/sh<br />
# warning_count.sh - Count the warnings in a specified target<br />
# Written by Hai Shalom - hai@rt-embedded.com<br />
#<br />
# usage:<br />
# warning_count.sh [target] &lt;stderr filename&gt;<br />
#<br />
# The stderr file is optional and can be later used to examine all the warnings.<br />
# If the target supports "target_clean" option, it will be used. Otherwise,<br />
# the scripts cleans the whole system.<br />
#<br />
<strong>if</strong> [ <strong>$1</strong> ]<br />
<strong>then</strong><br />
    target=<strong>$1</strong><br />
<strong>else</strong><br />
    <strong>echo</strong> "$0" [target] \&lt;filename <strong>for</strong> stderr\&gt;<br />
    <strong>echo</strong> e.g.: "$0" all ~/stderr.txt<br />
    <strong>echo</strong> The stderr file is optional and can be later used to examine all the warnings.<br />
    <strong>exit</strong> 2<br />
<strong>fi</strong></code></p>
<p><code><strong>echo</strong> -e "\n=========== Cleaning $target ===========\n"<br />
make "$target"_clean 2&gt; /dev/null<br />
<strong><br />
if</strong> [ <strong>$?</strong> != 0 ]; <strong>then</strong><br />
    make clean 2&gt; /dev/null;<br />
<strong>fi</strong></code></p>
<p><code><strong>echo</strong> -e "\n=========== Building $target ===========\n"<br />
<strong>if</strong> [ <strong>$2</strong> ]<br />
<strong>then</strong><br />
        stderrfile=<strong>$2</strong><br />
<strong>else</strong><br />
        stderrfile=/tmp/warning_count_`whoami`.tmp<br />
<strong>fi</strong></code></p>
<p><code>make $target 2&gt; $stderrfile &amp;&amp; \<br />
cat $stderrfile | grep "warning: " |\<br />
<strong>echo</strong> -e "\n=========== Total warnings:" `wc -l` "===========\n"<br />
<strong><br />
if</strong> [ <strong>$?</strong> != 0 ]; <strong>then</strong><br />
    <strong>echo</strong> -e "\n=========== Compilation failed, aborting! ===========\n";<br />
    <strong>exit</strong> 2;<br />
<strong>fi</strong><br />
<strong>if</strong> [ <strong>$2</strong> ]<br />
<strong>then</strong><br />
    cat $stderrfile | grep "warning: " |\<br />
    <strong>echo</strong> -e "\n=========== Total warnings:" `wc -l` "===========\n" &gt;&gt; $stderrfile<br />
<strong>else</strong><br />
    rm -rf $stderrfile<br />
<strong>fi</strong></code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Once the script has completed the run, you&#8217;ll see the current warning count in your project, and the warning list will be saved in the specified file.  Note that if your Makefile does not specify any warning level to the compiler, many warnings will be inhibited. In case, it is recommended to enable all warnings by adding &#8220;<em>-Wall</em>&#8221; to the CFLAGS variable in your Makefile. The second article about <a href="http://www.rt-embedded.com/blog/archives/using-gcc-part-2/" target="_blank">using gcc</a> provides a nice description about setting the required warning level.</p>
<h3 style="text-align: justify;">2nd step: Assign warnings to developers</h3>
<p style="text-align: justify;">Divide the warnings between your developers in order to reduce the warning count (or eliminate them completely). Pay attention, while most warnings could be a minor issue, such as a missing header file, or an unused variable, some may hide a potential problem. Instruct your staff to consult witheach other or hold brainstorms on warnings they are not sure how to fix. For example, casting a variable may eliminate the warning, but not the problem! In this example, the developer must understand what is the cause for the type mismatch. What if the types are not in the same size? there could be a memory overrun in this case.</p>
<h3 style="text-align: justify;">3rd step: Perform code reviews</h3>
<p style="text-align: justify;">Each warning fix must be reviewed, in order to make sure it was fixed properly.</p>
<h3 style="text-align: justify;">4th step: Lock clean modules</h3>
<p style="text-align: justify;">Once a software module has been cleaned from compilation warnings, it is possible to lock it from accepting new warnings. In order to do that, you can instruct the compiler to <a href="http://www.rt-embedded.com/blog/archives/using-gcc-part-2/" target="_blank">treat warnings as errors</a>. Any new warning which will be introduced will fail the compilation process and the developer will have to fix it immediately in order to proceed with the work. This approach has advantages and disadvantages. The advantages are: It will keep the software module clean from warnings forever, it will force the developer to fix any new warning immediately, otherwise, he may postpone it until there are many warnings, and sometimes may even forget to fix them at all. The big disadvantage of this approach is that it is more difficult to develop this way because the compilation will fail for each small issue which may be irrelevant. If you decide not to use this approach, you shall have to run the script once in a while to make sure that your warning count does not raise.</p>
<h3 style="text-align: justify;">Third party code</h3>
<p style="text-align: justify;">In some cases, you may not want to or could not change some code because it is provided by a third party vendor, or has a special license that prevents you  from changing it. If you verified that these warnings are not affecting your system, you can instruct the compiler to inhibit all warnings from this software module. Again, this approach has advantages and disadvantages. The main advantages are the look of the compilation process and the total warning count of your project. As a big disadvantage I can say that if a real bug is hiding there, you won&#8217;t see it with no warnings.</p>
<p style="text-align: justify;"><span style="text-decoration: underline;">Resources:<br />
</span><a href="http://www.rt-embedded.com/blog/archives/using-gcc-part-2/">http://www.rt-embedded.com/blog/archives/using-gcc-part-2/</a></p>
<p style="text-align: justify;"> </p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/Pq1UrO8caWo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/compilation-warnings-management/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/compilation-warnings-management/</feedburner:origLink></item>
		<item>
		<title>Monitoring process creation, execution and termination</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/R32vGusRWLI/</link>
		<comments>http://www.rt-embedded.com/blog/archives/fork-exec-exit/#comments</comments>
		<pubDate>Mon, 17 May 2010 07:17:17 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[do_exec]]></category>
		<category><![CDATA[do_exit]]></category>
		<category><![CDATA[do_fork]]></category>
		<category><![CDATA[exec]]></category>
		<category><![CDATA[exit]]></category>
		<category><![CDATA[fork]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[monitor]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=458</guid>
		<description><![CDATA[<p style="text-align: justify;">Last week, one of the company&#8217;s customers had a major issue with a mysterious process that is periodically spawned, and when it runs, it allocates a resource and terminates without freeing it. Unfortunately, the allocated resource is a shared memory segment, which is not controlled by the kernel. Unlike dynamic memory allocation which gets cleaned [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-474" title="fork" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/05/fork.jpg" alt="" width="240" height="180" />Last week, one of the company&#8217;s customers had a major issue with a mysterious process that is periodically spawned, and when it runs, it allocates a resource and terminates without freeing it. Unfortunately, the allocated resource is a shared memory segment, which is not controlled by the kernel. Unlike dynamic memory allocation which gets cleaned up when a process terminates, this resource kept on leaking until it was completely exhausted. Once all the shared memory was consumed, the system was unable to operate correctly, although the kernel was still alive, and there was plenty of free RAM. This is just a single scenario where you might need to get a clear picture about the running processes in the system. How can we log and monitor the creation, execution and termination of processes in the system? Just continue reading.</p>
<p><span id="more-458"></span></p>
<p style="text-align: justify;">In many cases, you may encounter a situation when you need to work on a system which you are not completely familiar with. There could be plenty of processes running in the system, which are started using <a href="http://www.rt-embedded.com/blog/archives/processes-background-and-priority/" target="_blank"><em>fork</em> and one of the <em>exec</em> functions</a>. It is not a simple task to debug it, at least until you get more familiar with it. In this article I will show how you can easily patch the kernel to display a message each time a new process is created using <em>fork</em>, when a new process is loaded and executed using one of the <em>exec</em>functions and when a process is about to terminate. These messages could surprise you in the level of details, because the amount of process activity is relatively large, especially during the kernel start-up.</p>
<h3>Patching the <em>fork</em> system call</h3>
<p style="text-align: justify;">When a process wants to create a new child process it uses <em>fork</em>. This function is actually a system call which eventually ends up in kernel/fork.c file, in the function <em><a href="http://lxr.linux.no/linux+v2.6.34/kernel/fork.c#L1351" target="_blank">do_fork</a></em>. This function creates a clone of the calling process, sets up its stack, assigns a new process id and links the new process with its father. Then, it returns the with the new process id. Note that fork only clones the process, but does not start a new one. After the fork, the process is running concurrently in two copies, with minor differences; the process id, the parent process id and the stack.</p>
<p><span style="text-decoration: underline;">To add the patch</span>: Locate the <em>do_fork</em> function in kernel/fork.c file. Look for the <em>copy_process </em>function and place the green code inside the <strong>if</strong> statement.</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>p = <strong>copy_process</strong>(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, nr);<br />
/*<br />
 * Do this prior waking up the new thread - the thread pointer<br />
 * might get invalid after that point, if the thread exits quickly.<br />
 */<br />
<strong>if</strong> (!<strong>IS_ERR</strong>(p)) {<br />
    <strong>struct</strong> completion vfork;<br />
<span style="color: #008000;">    <code><span style="color: #008000;">/* Hai: Display a message when a new process is forked */<br />
    <strong>printk</strong>( KERN_INFO "KERNEL: Process %s (pid = %d) forked a new process,<br />
            pid = %d\n", p-&gt;comm, p-&gt;parent-&gt;pid, p-&gt;pid );</span></code></span></code></td>
</tr>
</tbody>
</table>
<h3>Patching the <em>exec</em> system call</h3>
<p style="text-align: justify;">In case a new program is needed to be loaded, then in the next step, the process calls one of the <a href="http://linux.die.net/man/3/exec" target="_blank"><em>exec</em> system calls</a>. This is the common scenario. Otherwise, if the intention was to actually have two copies of the same program, then the <em>exec </em>system call is not required. All these system calls eventually end up in fs/exec.c file, in the function <a href="http://lxr.linux.no/linux+v2.6.34/fs/exec.c#L1315" target="_blank">do_execve</a>. This function loads the new program code and schedules its new execution. In the right time, it will call its <em>main</em> function.</p>
<p><span style="text-decoration: underline;">To add the patch</span>: Locate the <em>do_execve</em> function in fs/exec.c file. Place the green code in the start of the function, right after the defiinitions of the variables.</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>/*<br />
* sys_execve() executes a new program.<br />
*/<br />
<strong>int</strong> <strong>do_execve</strong>(<strong>char</strong> * filename,<br />
    <strong>char</strong> __user *__user *argv,<br />
    <strong>char</strong> __user *__user *envp,<br />
    <strong>struct</strong> pt_regs * regs)<br />
{<br />
    <strong>struct</strong> linux_binprm *bprm;<br />
    <strong>struct</strong> file *file;<br />
    <strong>int</strong> retval;<br />
    <strong>int</strong> i; <span style="color: #008000;">  <br />
</span></code><code><span style="color: #008000;"><br />
    /* Hai: Display a message when a new process is executed */<br />
    <strong>printk</strong>( KERN_INFO "KERNEL: Executing %s,pid = %d\n",<br />
            filename, current-&gt;pid );<br />
</span></code></td>
</tr>
</tbody>
</table>
<h3>Patching the <em>exit </em>system call</h3>
<p style="text-align: justify;">When a process is about to terminate, the exit system call is activated. This system call eventually ends up in kernel/exit.c file, in the function <a href="http://lxr.linux.no/linux+v2.6.34/kernel/exit.c#L900" target="_blank">do_exit</a>. This function cleans up the process, frees its monitored resources, terminates its services and sends a signal to its father.</p>
<p><span style="text-decoration: underline;">To add the patch</span>: Locate the <em>do_exit</em> function in kernel/exit.c file. Place the green code after the <strong>in_atomic</strong> check.</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code><strong>if</strong> (<strong>unlikely</strong>(<strong>in_atomic</strong>()))<br />
   <strong>printk</strong>(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",<br />
          current-&gt;comm, current-&gt;pid,<br />
          <strong>preempt_count</strong>());<br />
<span style="color: #008000;"><br />
/* Hai: Display a message when a process terminates */<br />
<strong>printk</strong>( KERN_INFO "KERNEL: Process %s (pid = %d) is terminating.\n", tsk-&gt;comm, tsk-&gt;pid );</span></code></td>
</tr>
</tbody>
</table>
<h3>The results</h3>
<p>Here&#8217;s the output of a basic &#8220;hello world&#8221; program, after the patches were applyed on the kernel:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># ./hello<br />
KERNEL: Process sh (pid = 160) forked a new process, pid = 170<br />
KERNEL: Executing ./hello, pid = 170<br />
<strong>Hello, world!</strong><br />
KERNEL: Process hello (pid = 170) is terminating.</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">As you can see, the patches provide detailed logs upon process creation, execution and termination. With this patches, it was possible to debug the system and find which process (among the many in the system) was causing the problem.</p>
<p><span style="text-decoration: underline;">Resources:<br />
<a href="http://linux.die.net/man/3/exec">http://linux.die.net/man/3/exec</a><br />
<a href="http://linux.die.net/man/2/fork">http://linux.die.net/man/2/fork</a><br />
<a href="http://lxr.linux.no/linux">http://lxr.linux.no/linux</a></span></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/R32vGusRWLI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/fork-exec-exit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/fork-exec-exit/</feedburner:origLink></item>
		<item>
		<title>Resolving memory leaks in the Linux kernel</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/fz-UW6sooDk/</link>
		<comments>http://www.rt-embedded.com/blog/archives/memory-leaks-kernel/#comments</comments>
		<pubDate>Mon, 10 May 2010 10:11:50 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[allocator]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[leak]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[slab]]></category>
		<category><![CDATA[slabinfo]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=412</guid>
		<description><![CDATA[<p style="text-align: justify;">The Linux kernel, like any other piece of software, can suffer from memory leaks. However, if you are using a recent version of the kernel, or a &#8220;commercial version&#8221; of it (such as Red-Hat, or MontaVista), the probability for memory leaks which originate from the kernel itself is relatively low (yes, I have high confidence [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-413" title="memleak-kernel" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/05/memleak-kernel.jpg" alt="" width="240" height="200" />The Linux kernel, like any other piece of software, can suffer from <a href="http://www.rt-embedded.com/blog/archives/memory-leaks/" target="_blank">memory leaks</a>. However, if you are using a recent version of the kernel, or a &#8220;commercial version&#8221; of it (such as Red-Hat, or MontaVista), the probability for memory leaks which originate from the kernel itself is relatively low (yes, I have high confidence in it). So what else could eat up the memory in the kernel space? There are a few possibilities for this:</p>
<ul>
<li>Misuse of standard kernel API.</li>
<li>Local modifications which were added to the kernel in your project.</li>
<li>A loadable kernel module.</li>
</ul>
<p><span id="more-412"></span></p>
<p style="text-align: justify;">How do we know that we have a memory leak? first, follow the instructions in my <a href="http://www.rt-embedded.com/blog/archives/memory-leaks/" target="_blank">previous memory leaks post</a>. If your system showed memory leak symptoms, which result in constantly reducing amount of free memory, and on the other hand, the top utility did not show any increasing amount of memory by a specific process, then this leak comes from the kernel space.</p>
<h3>Reading the kernel memory consumption (slab)</h3>
<p style="text-align: justify;">The kernel provides a file in the proc file system which shows its current memory consumption. The file name is <em>/proc/slabinfo</em>. If you&#8217;ll read this file in the first time, you will see a large table with many confusing numbers and you may panic (I know I did in the first time). Don&#8217;t worry, it&#8217;s not so complicated, and I&#8217;m going to explain how to read this file easily. Since this file shows a very long and wide table, it is recommended to redirect the output to a file and analyze it with an editor (use <em>cat /proc/slabinfo &gt; /var/myslabinfo.txt </em>command or similar).</p>
<p style="text-align: justify;">In high level, the memory management subsystem allocates memory in constant block sizes. Each block of memory is referred as an object. The kernel creates pools of objects in various sizes (in embedded systems the standard object sizes vary from 32 bytes to 128K bytes, usually in steps of power of two), and when a kernel code uses <em>kmalloc</em>, it will provide the memory from the appropriate pool (the lowest size pool that satisfies the memory requirement). Furthermore, each driver in the kernel can create its own pool with its specific object memory size, which is optimized for its needs (for example, the networking subsystem creates a pool with objects of 1504 bytes). These pools are also called &#8220;caches&#8221;, where each driver has its own cache. The kernel keeps track of each and every allocated object, so it knows how many objects were allocated in each pool.</p>
<p>Now let&#8217;s see the contents of the <em>slabinfo</em> file. I&#8217;ve truncated it a bit and left only the standard sizes, just to make this example easier:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr><code>name            &lt;active_objs&gt; &lt;num_objs&gt; &lt;objsize&gt; &lt;objperslab&gt; &lt;pagesperslab&gt; : tunables &lt;limit&gt; &lt;batchcount&gt; &lt;sharedfactor&gt; : slabdata &lt;active_slabs&gt; &lt;num_slabs&gt; &lt;sharedavail&gt;<br />
size-131072(DMA)       0      0 131072    1   32 : tunables    8    4    0 : slabdata      0      0      0<br />
size-131072            3      3 131072    1   32 : tunables    8    4    0 : slabdata      3      3      0<br />
size-65536(DMA)        0      0  65536    1   16 : tunables    8    4    0 : slabdata      0      0      0<br />
size-65536             2      2  65536    1   16 : tunables    8    4    0 : slabdata      2      2      0<br />
size-32768(DMA)        0      0  32768    1    8 : tunables    8    4    0 : slabdata      0      0      0<br />
size-32768             0      0  32768    1    8 : tunables    8    4    0 : slabdata      0      0      0<br />
size-16384(DMA)        0      0  16384    1    4 : tunables    8    4    0 : slabdata      0      0      0<br />
size-16384             0      0  16384    1    4 : tunables    8    4    0 : slabdata      0      0      0<br />
size-8192(DMA)         0      0   8192    1    2 : tunables    8    4    0 : slabdata      0      0      0<br />
size-8192              1      1   8192    1    2 : tunables    8    4    0 : slabdata      1      1      0<br />
size-4096(DMA)         0      0   4096    1    1 : tunables   24   12    0 : slabdata      0      0      0<br />
size-4096             16     16   4096    1    1 : tunables   24   12    0 : slabdata     16     16      0<br />
size-2048(DMA)         0      0   2048    2    1 : tunables   24   12    0 : slabdata      0      0      0<br />
size-2048            192    192   2048    2    1 : tunables   24   12    0 : slabdata     96     96      0<br />
size-1024(DMA)         0      0   1024    4    1 : tunables   54   27    0 : slabdata      0      0      0<br />
size-1024             98    100   1024    4    1 : tunables   54   27    0 : slabdata     25     25      0<br />
size-512(DMA)          0      0    512    8    1 : tunables   54   27    0 : slabdata      0      0      0<br />
size-512              64     64    512    8    1 : tunables   54   27    0 : slabdata      8      8      0<br />
size-256(DMA)          0      0    256   15    1 : tunables  120   60    0 : slabdata      0      0      0<br />
size-256             105    105    256   15    1 : tunables  120   60    0 : slabdata      7      7      0<br />
size-192(DMA)          0      0    192   20    1 : tunables  120   60    0 : slabdata      0      0      0<br />
size-192             100    100    192   20    1 : tunables  120   60    0 : slabdata      5      5      0<br />
size-128(DMA)          0      0    128   30    1 : tunables  120   60    0 : slabdata      0      0      0<br />
size-128             794    810    128   30    1 : tunables  120   60    0 : slabdata     27     27      0<br />
size-96(DMA)           0      0     96   40    1 : tunables  120   60    0 : slabdata      0      0      0<br />
size-64(DMA)           0      0     64   59    1 : tunables  120   60    0 : slabdata      0      0      0<br />
size-32(DMA)           0      0     32  113    1 : tunables  120   60    0 : slabdata      0      0      0<br />
size-32             1078   1130     32  113    1 : tunables  120   60    0 : slabdata     10     10      0<br />
size-96              560    560     96   40    1 : tunables  120   60    0 : slabdata     14     14      0<br />
size-64              371    413     64   59    1 : tunables  120   60    0 : slabdata      7      7      0&gt;</code></tr>
</tbody>
</table>
<p>Let&#8217;s focus on the first 4 columns for the sake of our discussion. These columns describe the following:</p>
<ol>
<li>Name: The name of the pool/cache. The name could be <em>size-XXXX</em>, where the XXXX stands for the object size in the pool. The name could also be a meaningful name which was selected by a driver in the kernel.</li>
<li>Active objects: Amount of allocated objects in this pool. For example, in the table above, there are curently 1078 allocated objects in the size of 32 bytes.</li>
<li>Number of objects: The highest amount of objects which were allocated in this pool. This number can be used as a &#8220;watermark&#8221; for understanding how many objects were allocated during the system&#8217;s life.</li>
<li>Object size: The net size of the object in bytes.</li>
</ol>
<p>The rest of columns provide extra information which is not so relevant for memory leaks and I won&#8217;t extend the information on them currently.</p>
<h3>Detecting a memory leak in the kernel</h3>
<p style="text-align: justify;">Assuming we concluded that the memory leak is not originating from the user space, we can use the <em>slabinfo </em>file to detect a memory leak in the kernel. Now, we need to log its output periodically and compare the outputs. You can use a shell script that runs in the background, cats the <em>slabinfo</em> file and sleeps for a minute. In case there is a leak in kernel space, we will see a constantly growing number in the second column of one (or more) of the caches. If this cache has a meaningful name which indicates its origin (name of driver, subsystem, service, etc.) we could try to isolate the leak in this relevant area. However, if the leak comes from one of the general caches, we need some more information in order to proceed.</p>
<h3>Enabling slab debug options</h3>
<p style="text-align: justify;">The kernel provides some useful debug options to debug the slab allocator. In order to enable these options, select the CONFIG_DEBUG_SLAB and CONFIG_DEBUG_SLAB_LEAK kernel configuration options (In the configuration menu, they can be found in the Kernel Hacking menu). Also, compile the kernel with symbols ( CONFIG_DEBUG_INFO option). Unfortunately, there are kernel versions which do not support the leak option or it simply doesn&#8217;t work. The following patch simply extends the <em>slabinfo</em> file with a list of caller functions, which requested an object. The patch is simple and patches the mm/slab.c file, however, it may fail to patch correctly in your Linux version. In this case, it can be easily integrated manually.</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>*** mm/slab.c 2010-05-10 11:21:03.000687000 +0300<br />
--- newmm/slab.c 2010-05-10 11:28:04.000000000 +0300<br />
***************<br />
*** 186,191 ****<br />
--- 186,192 ----<br />
  #define DEBUG  1<br />
  #define STATS  1<br />
  #define FORCED_DEBUG 1<br />
+ #define SLAB_NCALLERS 128 /* Maximum amount of callers */<br />
  #else<br />
  #define DEBUG  0<br />
  #define STATS  0<br />
***************<br />
*** 372,377 ****<br />
--- 373,379 ----<br />
     int node, int *this_cpu);<br />
  static void enable_cpucache(struct kmem_cache *cachep);<br />
  static void cache_reap(void *unused);<br />
+ static void show_symbol(struct seq_file *m, unsigned long address);  /*<br />
   * This function must be completely optimized away if a constant is passed to<br />
***************<br />
*** 499,504 ****<br />
--- 501,513 ----<br />
    */<br />
   int obj_offset;<br />
   int obj_size;<br />
+<br />
+  /* Structure to hold the caller information */<br />
+  struct caller_info {<br />
+   void *caller;<br />
+   int ncalls;<br />
+  } callers[SLAB_NCALLERS];<br />
+  int slab_ncallers;<br />
  #endif<br />
  };***************<br />
*** 3151,3162 ****<br />
--- 3160,3205 ----<br />
   return objp;<br />
  }</p>
<p>+ #if DEBUG<br />
+ /* A new function to lookup a caller in the list */<br />
+ static int<br />
+ lookup_caller(struct caller_info *cp, void *caller, int ncallers)<br />
+ {<br />
+  int l = 0, u = ncallers, i;<br />
+<br />
+  while (l &lt; u) {<br />
+   i = (l + u) / 2;<br />
+   if (cp[i].caller == caller)<br />
+    return i;<br />
+   if (cp[i].caller &lt; caller)<br />
+    l = i + 1;<br />
+   else<br />
+    u = i;<br />
+  }<br />
+  return u;<br />
+ }<br />
+ #endif<br />
+<br />
  static __always_inline void *__cache_alloc(struct kmem_cache *cachep,<br />
        gfp_t flags, void *caller)<br />
  {<br />
   unsigned long irqflags;<br />
   int this_cpu;<br />
   void *objp;<br />
+ #if DEBUG<br />
+  int n;<br />
+<br />
+  /* Lookup the caller in the list, append a caller if not found */<br />
+  n = lookup_caller(cachep-&gt;callers, caller, cachep-&gt;slab_ncallers);<br />
+  if (cachep-&gt;callers[n].caller == caller)<br />
+   cachep-&gt;callers[n].ncalls++;<br />
+  else if (cachep-&gt;slab_ncallers &lt; SLAB_NCALLERS) {<br />
+   memmove(cachep-&gt;callers + n + 1, cachep-&gt;callers + n, sizeof *cachep-&gt;callers * (cachep-&gt;slab_ncallers - n));<br />
+   cachep-&gt;slab_ncallers++;<br />
+   cachep-&gt;callers[n].caller = caller;<br />
+   cachep-&gt;callers[n].ncalls = 1;<br />
+  }<br />
+ #endif</p>
<p>   cache_alloc_debugcheck_before(cachep, flags);</p>
<p>***************<br />
*** 4127,4132 ****<br />
--- 4170,4187 ----<br />
        allochit, allocmiss, freehit, freemiss);<br />
   }<br />
  #endif<br />
+ #if DEBUG<br />
+  /* Show the list of callers */<br />
+  if (cachep-&gt;slab_ncallers) {<br />
+   int n;<br />
+<br />
+   seq_printf(m, " %d callers: ", cachep-&gt;slab_ncallers);<br />
+   for (n = 0; n &lt; cachep-&gt;slab_ncallers; n++) {<br />
+    show_symbol(m, (unsigned long)cachep-&gt;callers[n].caller);<br />
+    seq_printf(m, ":%d ", cachep-&gt;callers[n].ncalls);<br />
+   }<br />
+  }<br />
+ #endif<br />
   seq_putc(m, '\n');<br />
   return 0;<br />
  }</p>
<p></code></td>
</tr>
</tbody>
</table>
<p>Let&#8217;s see a single line (as an example) in the <em>slabinfo</em> file with this extra information, once we&#8217;ve recompiled the kernel:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>name            &lt;active_objs&gt; &lt;num_objs&gt; &lt;objsize&gt; &lt;objperslab&gt; &lt;pagesperslab&gt; : tunables &lt;limit&gt; &lt;batchcount&gt; &lt;sharedfactor&gt; : slabdata &lt;active_slabs&gt; &lt;num_slabs&gt; &lt;sharedavail&gt;<br />
names_cache            2      2   4096    1    1 : tunables   24   12    0 : slabdata      2      2      0 : globalstat       2      2     2    0                                  <br />
0    0    0    0    0 : cpustat   1714      2   1716      0 2 callers: mount_block_root+0x28/0x288:1 getname+0x24/0xe0:1715</code></td>
</tr>
</tbody>
</table>
<p>As we can see, the list of callers was added in the right. Assuming we saw that the number of active objects in the cache in the example is constantly growing, we need to look at the list of callers, and examine each and one of them. It is guaranteed that one of them is causing the memory leak. Per each name in the list, we need to locate its source code. If this function belongs to the kernel, it can be looked up at <a href="http://lxr.linux.no/linux" target="_blank">lxr</a>. Otherwise, if it&#8217;s a function of one of your loadable modules, you can easily locate it in your source files.</p>
<p>The number near the function name is the offeset of the return address from within the calling function. In this example, we can locate the function <em>mount_block_root</em> in mm/do_mounts.c file. We can run the nm utility to resolve the address of the mount_block_root function on do_mounts.o, and then run the <em>objdump</em> utility on the do_mounts.o file, and add the offset to the address of the function to find the exact command. </p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># armeb-linux-uclibceabi-nm -S do_mounts.o | grep mount_block_root<br />
0000012c 00000288 T mount_block_root</code></td>
</tr>
</tbody>
</table>
<p>The address of the function is at 0x12c. Now let&#8217;s add 0&#215;28 to this value, and we got 0&#215;154. Here&#8217;s the output of <em>objdump</em>:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># armeb-linux-uclibceabi-objdump -S do_mounts.o | grep 154: -B 14<br />
void __init mount_block_root(char *name, int flags)<br />
{<br />
 140:   e1a09000        mov     r9, r0<br />
 144:   e1a08001        mov     r8, r1<br />
        char *fs_names = __getname();<br />
 148:   e5930000        ldr     r0, [r3]<br />
 14c:   e3a010d0        mov     r1, #208        ; 0xd0<br />
 150:   ebfffffe        bl      0 &lt;kmem_cache_alloc&gt;static void __init get_fs_names(char *page)<br />
{<br />
        char *s = page;        if (root_fs_names) {<br />
 154:   e59f3220        ldr     r3, [pc, #544]  ; 37c &lt;mount_block_root+0x250&gt;</p>
<p></code></td>
</tr>
</tbody>
</table>
<p>Since 0&#215;154 is the return address, the call was made in the previous address (0&#215;150). We can see that there is a call to <em>kmem_cache_alloc</em> function in this address. The source code shows that the <em>__getname</em> symbol is a macro.</p>
<p>Assuming you&#8217;ve found the function that leaks the memory in this method, it still doesn&#8217;t mean you found the problem. Next, you&#8217;ll need to understand why the leak happens by reviewing the code and all possible flows which use this resource.</p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/fz-UW6sooDk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/memory-leaks-kernel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/memory-leaks-kernel/</feedburner:origLink></item>
		<item>
		<title>Extending your application with Plugins</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/xpWX799N__0/</link>
		<comments>http://www.rt-embedded.com/blog/archives/plugins/#comments</comments>
		<pubDate>Tue, 04 May 2010 07:46:24 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[dlopen]]></category>
		<category><![CDATA[dlsym]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[real]]></category>
		<category><![CDATA[rt]]></category>
		<category><![CDATA[shared]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=355</guid>
		<description><![CDATA[<p style="text-align: justify;">Nowadays, many software products support a mechanism that allows programmers and 3rd party vendors to enhance their features and capabilities without the need to recompile them, or actually, without the need to change any of their existing executables or libraries. This mechanism is usually called &#8220;Plugins&#8221;.  Plugins are very popular today and can be also used [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-378" title="plugin" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/05/plugin.jpg" alt="plugin" width="200" height="150" />Nowadays, many software products support a mechanism that allows programmers and 3rd party vendors to enhance their features and capabilities without the need to recompile them, or actually, without the need to change any of their existing executables or libraries. This mechanism is usually called &#8220;Plugins&#8221;.  Plugins are very popular today and can be also used to customize a program according to the requirements of your sysetm. In this article, I will present the Linux version of Plugins, how to support them in your application and how to write plugins. I will also provide examples of an application and a matching Plugin.</p>
<p><span id="more-355"></span></p>
<h3>What is a Plugin?</h3>
<p style="text-align: justify;">A Plugin is a special kind of <a href="http://www.rt-embedded.com/blog/archives/libraries/" target="_blank">a shared library</a>. It contains code that the main program can execute upon request.  A Plugin library is not linked during compilation, but during run-time. Shared libraries that are linked during the compilation are then loaded automatically by the loader during run-time. Plugin libraries on the other hand, are not loaded automatically, but loaded explicitly upon request of the program which requires them. The Plugin libraries must adhere to the interface which was dictated by the main program and implement all the functions it requires.</p>
<h3>What is the use of Plugins?</h3>
<p>There are many uses for Plugins and many reasons for using them. Here&#8217;s a short list:</p>
<ul>
<li>The easiest and most classic way to enhance a big program.</li>
<li>Providing users or customers a convenient means for hooks in your program without the need to rebuild it.</li>
<li>Plugins enable your users or customers to customize the program&#8217;s behavior upon an event or trigger. </li>
<li>Plugins enable customizations and enhancements even if you do not supply the source code.</li>
<li>Plugins can be supplied with different licensing than the main program. Note that this is in the gray area, because when you compile and link a GPL program, your Plugin is not a part of it (it is not needed, nor linked with the application).</li>
</ul>
<p style="text-align: justify;">In the next sections I will describe the steps for adding Plugin support and how to write a simple plugin. I will provide some code example as well.</p>
<h3>Adding Plugin support in your application</h3>
<h4>1. Define the interface</h4>
<p style="text-align: justify;">When you want to add Plugin support in your application or library, as a first step, you need to define the interface. The interface is the glue between the main program and the Plugin, where all supported Plugins will adhere to this interface in order to function properly. The interface should include the following items:</p>
<ol>
<li>A well defined Plugin init function name. Each Plugin will implement it and the main program will load and call it.</li>
<li>A data structure that contains:
<ul>
<li>Plugin version (could be a single number, or major and minor). This will allow the main program to reject Plugins with unsupported versions, or modify its behavior according to the version (for example, a newly added hook will not be available for older Plugins and the main program will not enable it for them).</li>
<li>Plugin name: Could be used by the main program for display and debug purposes.</li>
<li>A list of handlers for all the available hooks. In other words, a list of function pointers for callbacks. These callbacks will be called by the main program.</li>
</ul>
</li>
<li>A list of all data types and macros that are used by the application and the Plugin.</li>
<li>A Plugin register and unregister functionf which allow the plugin to provide its callbacks to the main program.</li>
</ol>
<p>In this example header file, there is a definition of a plugin interface. All plugins need to include this file:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>#ifndef _PLUGIN_INTERFACE_H_<br />
#define _PLUGIN_INTERFACE_H_#define RTE_MAX_PLUGIN_NAME 32<br />
#define RTE_MIN_SUPPORTED_VERSION_MAJOR 1/* Enumeration of events in the main program */<br />
<strong>typedef</strong> <strong>enum</strong><br />
{<br />
  RTE_EVENT_KEY_PRESSED,<br />
  RTE_EVENT_SOMETHING_HAPPENED,<br />
  RTE_EVENT_SOMETHING_ELSE_HAPPENED,</p>
<p>} rte_event_e;</p>
<p>/* The plugin interface structure */<br />
<strong>typedef</strong> <strong>struct</strong> rte_plugin_t<br />
{<br />
  /* Plugin version */<br />
<strong>  unsigned</strong> <strong>int</strong> version_major;<br />
<strong>  unsigned</strong> <strong>int</strong> version_minor;</p>
<p>  /* Plugin name */<br />
<strong>  char</strong> plugin_name[ RTE_MAX_PLUGIN_NAME ];</p>
<p>  /* Hook handlers */</p>
<p>  /* Plugin start handler */<br />
<strong>  int</strong> (*start)(<strong>void</strong>);</p>
<p>  /* Plugin end handler */<br />
<strong>  void</strong> (*end)(<strong>void</strong>);</p>
<p>  /* A function that asks the plugin if it wants to handle an event */<br />
<strong>  int</strong> (*want_to_handle_event)(rte_event_e event);</p>
<p>  /* A function that handles an event */<br />
<strong>  int</strong> (*handle_event)(rte_event_e event, <strong>int</strong> value);</p>
<p>  /* More functions can be added here */</p>
<p>} rte_plugin_t;</p>
<p>/* Register function */<br />
<strong>extern</strong> <strong>int</strong> <strong>plugin_register</strong>( <strong>const</strong> rte_plugin_t *rte_plugin );</p>
<p>/* Unregister function */<br />
<strong>extern</strong> <strong>int</strong> <strong>plugin_unregister</strong>( <strong>void</strong> );</p>
<p>/* The plugin init function:</p>
<p>Each plugin must have the following function implemented:</p>
<p>int rte_plugin_init( void );</p>
<p>The function prototype is in comment because plugins are optional,<br />
and we don't want to have an unresolved external in link time.<br />
*/</p>
<p>#endif /* _PLUGIN_INTERFACE_H_ */</p>
<p></code></td>
</tr>
</tbody>
</table>
<h4>2. Open, initialize and register the Plugin</h4>
<p style="text-align: justify;">The Loader provides functions that allow you to explicitly open and load a shared library. These functions are referred as the <a href="http://linux.die.net/man/3/dlopen" target="_blank"><em>dl</em> family</a>. The following actions are required in order to open, initialize and register the plugin:</p>
<ol>
<li>Call the <em>dlopen</em> function with the plugin name and the required load flags. The recommended load flags are <strong>RTLD_GLOBAL</strong> which makes the Plugin symbols globally available to the application and  <strong>RTLD_NOW</strong>, which loads all the Plugin symbols immediately. The latter will yield better performance when working with large Plugins. The opposite option which is not recommended, <strong>RTLD_LAZY </strong>may save some memory when loading a Plugin, but it will require more time to load each Plugin symbol upon request.</li>
<li>Resolve the &#8220;well defined Plugin init function&#8221; as defined previously using the <em>dlsym</em> function.</li>
<li>Call this init function. The init function needs to register the Plugin, and it will be described later.</li>
</ol>
<h4>3. Start the Plugin</h4>
<p>Once the Plugin was loaded and registered, call its start function. The plugin may initialize its data structures, allocated resources, display a message and do all the necessary actions it requires in order to start its work.</p>
<h4>4. Changes required in the application Makefile</h4>
<p style="text-align: justify;">In order to add Plugin support to your application, you must link with the loader library (<em>-ldl</em>) and use the <em>-rdynamic</em>  flag which instructs the linker to add all symbols, not only used ones, to the dynamic symbol table. This is required because there could be functions in your application which are dedicated to Plugins only, and not used by the application itself. Without this flag enabled, these functions will be unresolved when you&#8217;ll try to load the Plugin.</p>
<h4>5. Install hooks</h4>
<p style="text-align: justify;">Modify your application to use the Plugin callbacks in the designated locations a Plugin can differentiate or improve. I leave it to you to decide where to add these in your application.</p>
<p style="text-align: justify;">Here&#8217;s a sample code of a program that reads characters from the console. In normal behevior it display the characters on the screen. The program accepts a single  parameter which is the name of the plugin you want to use. In case no plugin is required, don&#8217;t specify anything. Note that the parameter checking and parsing is simple and assumes no errors for the sake of simplicity.</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>#include &lt;stdlib.h&gt;<br />
#include &lt;stdio.h&gt;<br />
#include &lt;string.h&gt;<br />
#include &lt;dlfcn.h&gt;<br />
#include "plugin-interface.h"/* The plugin interface data type */<br />
<strong>typedef</strong> <strong>void</strong> rte_plugin;/* Holds the current active plugin */<br />
<strong>static</strong> rte_plugin* plugin_ptr = <strong>NULL</strong>;</p>
<p>/* The plugin interface */<br />
<strong>static</strong> rte_plugin_t plugin_interface;</p>
<p>/* Plugin init function of the main program */<br />
<strong>static</strong> <strong>int</strong> <strong>plugin_init_main</strong>( <strong>char</strong> *name )<br />
{<br />
<strong>  int</strong> (*plugin_init)(<strong>void</strong>);</p>
<p>  /* Load the plugin */<br />
  plugin_ptr = <strong>dlopen</strong>( name, RTLD_GLOBAL | RTLD_NOW );<br />
<strong>  if</strong>( plugin_ptr == <strong>NULL</strong> )<br />
  {<br />
<strong>    fprintf</strong>( stderr, "Failed to load plugin %s, Error: %s.\n", name, <strong>dlerror</strong>());<br />
<strong>    return</strong> -1;<br />
  }</p>
<p>  /* Initialize the plugin */<br />
  plugin_init = <strong>dlsym</strong> (plugin_ptr, "rte_plugin_init");<br />
<strong>  if</strong>( plugin_init == <strong>NULL</strong> )<br />
  {<br />
<strong>    fprintf</strong>( stderr, "Failed to initialize plugin %s, Error: %s.\n", name, <strong>dlerror</strong>());<br />
<strong>    dlclose</strong>(plugin_ptr);<br />
<strong>    return</strong> -1;<br />
  }</p>
<p>  /* Initialize the plugin */<br />
<strong>  if</strong>( <strong>plugin_init</strong>() &lt; 0 )<br />
  {<br />
<strong>    fprintf</strong>( stderr, "Plugin %s failed to initialize, Error: %s.\n", name, <strong>dlerror</strong>());<br />
<strong>    dlclose</strong>(plugin_ptr);<br />
<strong>    return</strong> -1;<br />
  }</p>
<p><strong>  return</strong> 0;<br />
}</p>
<p>/* Plugin register function, called by the plugins */<br />
<strong>int</strong> <strong>plugin_register</strong>( <strong>const</strong> rte_plugin_t *rte_plugin )<br />
{<br />
<strong>  if</strong>( !plugin_ptr )<br />
  {<br />
<strong>    return</strong> 0;<br />
  }<br />
<strong>  printf</strong>( "Registering plugin: %s\n", rte_plugin-&gt;plugin_name );<br />
<strong>  if</strong>( rte_plugin-&gt;version_major &lt; RTE_MIN_SUPPORTED_VERSION_MAJOR )<br />
  {<br />
<strong>    fprintf</strong>( stderr, "Plugin version (%d.%d) is unsupported\n",<br />
    rte_plugin-&gt;version_major, rte_plugin-&gt;version_minor );<br />
<strong>    return</strong> -1;<br />
  }</p>
<p>  /* Copy the plugin information locally */<br />
<strong>  memcpy</strong>( &amp;plugin_interface, rte_plugin, <strong>sizeof</strong>( rte_plugin_t ) );<br />
<strong>  return</strong> 0;<br />
}</p>
<p>/* Plugin unregister function, called by the plugins */<br />
<strong>int</strong> <strong>plugin_unregister</strong>( <strong>void</strong> )<br />
{<br />
  /* Clear plugin info */<br />
<strong>  memset</strong>( &amp;plugin_interface, 0, <strong>sizeof</strong>( rte_plugin_t ) );<br />
  plugin_ptr = <strong>NULL</strong>;<br />
<strong>  return</strong> 0;<br />
}</p>
<p><strong>int</strong> <strong>main</strong>( <strong>int</strong> argc, <strong>char</strong> *argv[] )<br />
{<br />
  /* Check if caller specified a plugin */<br />
<strong>  if</strong>( argc&gt;=2 )<br />
  {<br />
    /* Initialize the plugin with the first parameter */<br />
<strong>    if</strong>( <strong>plugin_init_main</strong>( argv[1] ) &lt; 0 )<br />
    {<br />
<strong>      fprintf</strong>( stderr, "Failed to initialize plugin %s\n", argv[1] );<br />
<strong>      return</strong> 1;<br />
    }<br />
  }</p>
<p>  /* Main loop */<br />
<strong>  while</strong>( 1 )<br />
  {<br />
<strong>    char</strong> ch = <strong>getchar</strong>();</p>
<p>    /* Check if a plugin was loaded */<br />
<strong>    if</strong>( plugin_ptr )<br />
    {<br />
      /* Check if the plugin want to handle the KEY PRESSED event */<br />
<strong>      if</strong>( plugin_interface.want_to_handle_event<br />
          &amp;&amp; plugin_interface.<strong>want_to_handle_event</strong>( RTE_EVENT_KEY_PRESSED ) )<br />
      {<br />
        /* Let the plugin handle it*/<br />
        plugin_interface.<strong>handle_event</strong>( RTE_EVENT_KEY_PRESSED, ch );<br />
      }<br />
    }<br />
<strong>    else</strong><br />
    {<br />
      /* Perform "normal behavior" */<br />
<strong>      if</strong>( ch == '\n' )<br />
      {<br />
<strong>        printf</strong>( "Good bye\n" );</p>
<p><strong>        if</strong>( plugin_ptr )<br />
        {<br />
          /* Close the plugin */<br />
<strong>          dlclose</strong>(plugin_ptr);<br />
        }<br />
<strong>        return</strong> 0;<br />
      }<br />
<strong>      printf</strong>( "\tNormal program behavior, key %c pressed\n", ch );<br />
    }<br />
  }</p>
<p><strong>  return</strong> 0;<br />
}</p>
<p></code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Now your application supports Plugins. Let&#8217;s see now how to write a compatible Plugin.</p>
<h3>Writing a Plugin</h3>
<p style="text-align: justify;">The Plugin code is in &#8220;the other side&#8221;. It should adhere to the interface dictated by the main program and follow its rules. Here are the steps for writing a Plugin:</p>
<h4 style="text-align: justify;">1. Examine the interface</h4>
<p style="text-align: justify;">Include the interface header file in your plugin source code and examine it. Decide which call backs you want to implement and define their prototypes.</p>
<h4>2. Implement the hook handlers (callbacks)</h4>
<p style="text-align: justify;">For each callback in the interface, implement a function, in the specified prototypes. Some callbacks may be unsupported in your plugin. In this case, you need to find out if the main program allows leaving some callbacks without implementation. If it doesn&#8217;t, just create an empty function that does nothing and returns immediately.</p>
<h4>3. Implement the init function and fill the interface structure</h4>
<p style="text-align: justify;">This is an important step. When the main program loads your plugin, it looks up this function explicitly in order to init  the plugin. Note that the name of the function is important and you must provide the function with the designated name only. Once you&#8217;ve implemented the handlers, create an interface structure and fill it with your callbacks. In case there is a callback you don&#8217;t support, don&#8217;t forget to initialize its field with NULL. Note that the application may require that no callbacks will be NULLs. In this case, just write an empty function as described above. Call the plugin register function the main program provides. Once it returns successfully, then your Plugin is loaded and active, and its handler will be called.</p>
<p style="text-align: justify;">Here&#8217;s an example of a plugin that extends this program: </p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>#include &lt;stdlib.h&gt;<br />
#include &lt;stdio.h&gt;<br />
#include "plugin-interface.h"/* Plugin function prototypes */<br />
<strong>static</strong> <strong>int</strong> <strong>rte_plugin_start</strong>( <strong>void</strong> );<br />
<strong>static</strong> <strong>void</strong> <strong>rte_plugin_end</strong>( <strong>void</strong> );<br />
<strong>static</strong> <strong>int</strong> <strong>rte_plugin_want_to_handle_event</strong>( rte_event_e event );<br />
<strong>static</strong> <strong>int</strong> <strong>rte_plugin_handle_event</strong>( rte_event_e event, <strong>int</strong> value );/* Fill the interface structure */<br />
<strong>static</strong> <strong>const</strong> rte_plugin_t rte_plugin =<br />
{<br />
  2, /* Version major */<br />
  5, /* Version minor */<br />
  "rte_plugin", /* Name */<br />
  rte_plugin_start,<br />
  rte_plugin_end,<br />
  rte_plugin_want_to_handle_event,<br />
  rte_plugin_handle_event<br />
};</p>
<p><strong>int</strong> <strong>rte_plugin_start</strong>( <strong>void</strong> )<br />
{<br />
<strong>  printf</strong>( "Starting the %s plugin\n", rte_plugin.plugin_name );<br />
<strong>  return</strong> 0;<br />
}</p>
<p><strong>void</strong> <strong>rte_plugin_end</strong>( <strong>void</strong> )<br />
{<br />
<strong>  printf</strong>( "Ending the %s plugin\n", rte_plugin.plugin_name );<br />
}</p>
<p><strong>int</strong> <strong>rte_plugin_want_to_handle_event</strong>( rte_event_e event )<br />
{<br />
  /* We want to handle only key pressed events */<br />
<strong>  if</strong>( event == RTE_EVENT_KEY_PRESSED )<br />
  {<br />
<strong>    return</strong> 1;<br />
  }<br />
<strong>  return</strong> 0;<br />
}<br />
<strong><br />
int</strong> <strong>rte_plugin_handle_event</strong>( rte_event_e event, <strong>int</strong> value )<br />
{<br />
  /* Handle the event */<br />
<strong>  if</strong>( event == RTE_EVENT_KEY_PRESSED )<br />
  {<br />
<strong>    printf</strong>( "Plugin event handler, character value = %d\n", value );<br />
  }</p>
<p><strong>  return</strong> 0;<br />
}</p>
<p>/* Init function, must be implemented */<br />
<strong>int</strong> <strong>rte_plugin_init</strong>( <strong>void</strong> )<br />
{<br />
  /* Call the register function in the main program */<br />
<strong>  plugin_register</strong>( &amp;rte_plugin );<br />
<strong>  return</strong> 0;<br />
}</p>
<p></code></td>
</tr>
</tbody>
</table>
<h3 style="text-align: justify;">Compiling and running</h3>
<p style="text-align: justify;">Now let&#8217;s compile the main program and the plugin:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># gcc rte_main.c -ldl -o rte_main -rdynamic -Wall<br />
# gcc rte_plugin.c -o rte_plugin.so -shared -fPIC -Wall</code></td>
</tr>
</tbody>
</table>
<p>The main program filename is <em>rte_main</em> and the plugin file name is <em>rte_plugin.so</em>.</p>
<p>Now, let&#8217;s run the main program without the plugin:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code> # ./rte_main<br />
abcdef<br />
        Normal program behavior, key a pressed<br />
        Normal program behavior, key b pressed<br />
        Normal program behavior, key c pressed<br />
        Normal program behavior, key d pressed<br />
        Normal program behavior, key e pressed<br />
        Normal program behavior, key f pressed<br />
Good bye</code></td>
</tr>
</tbody>
</table>
<p>Now, let&#8217;s run the main program with the plugin:</p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code> # ./rte_main ./rte_plugin.so<br />
Registering plugin: rte_plugin<br />
abcdef<br />
Plugin event handler, character value = 97<br />
Plugin event handler, character value = 98<br />
Plugin event handler, character value = 99<br />
Plugin event handler, character value = 100<br />
Plugin event handler, character value = 101<br />
Plugin event handler, character value = 102<br />
Plugin event handler, character value = 10</code></td>
</tr>
</tbody>
</table>
<p>We can see that we were able to change the behavior of our program to a customized behavior, without the need to rebuild it and without the sources (except for the interface header file).</p>
<p><span style="text-decoration: underline;">Resources:<br />
</span><a href="http://linux.die.net/man/3/dlopen">http://linux.die.net/man/3/dlopen</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/xpWX799N__0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/plugins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/plugins/</feedburner:origLink></item>
		<item>
		<title>Resolving memory leaks in user-space applications</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/_CmGQG9liys/</link>
		<comments>http://www.rt-embedded.com/blog/archives/memory-leaks/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 08:27:07 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[dmalloc]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[leak]]></category>
		<category><![CDATA[leaks]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[real]]></category>
		<category><![CDATA[repair]]></category>
		<category><![CDATA[resolve]]></category>
		<category><![CDATA[rt]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[top]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=306</guid>
		<description><![CDATA[<p style="text-align: justify;">Memory leaks is one of the RT Embedded major bugs, as I described in the article about the 5 most destructive bugs. Memory leak bugs usually kill your system slowly and painfully. Depending on the total amount of RAM and the extent of the memory leak, the system will continue to run and perform well [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="size-full wp-image-338 alignright" title="memory-leak" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/memory-leak.jpg" alt="" width="250" height="167" />Memory leaks is one of the RT Embedded major bugs, as I described in the article about the <a href="http://www.rt-embedded.com/blog/archives/5-most-destructive-bugs/" target="_blank">5 most destructive bugs</a>. Memory leak bugs usually kill your system slowly and painfully. Depending on the total amount of RAM and the extent of the memory leak, the system will continue to run and perform well for a certain amount of time. It could be hours, days, or weeks. During this time, the amount of free memory is constantly decreased. In Linux based systems, when memory is required and there is no free memory, the  kernel will start paging out programs and clearing the page cache. Further memory requests will cause performance impact due to paging out living tasks. Eventually, the kernel will trigger the Out-Of-Memory killer and start killing processes. In this stage, the system may be unusable already. The last stage will cause the kernel to panic and hang/reboot. Many consumer electronics products in the field remain active their whole life, therefore you can&#8217;t allow any memory leaks to happen, or else the customer will have to power cycle the unit from time to time (this is annoying, isn&#8217;t it?). Memory leaks are also hard to notice during unit-tests or lab tests because in these scenarios, the unit is rebooted constantly, as part of the development or testing work.</p>
<p style="text-align: justify;">This article covers a few techniques that will help you isolate and resolve the memory leak in your user-space application. An article about kernel memory leaks will be provided as well.</p>
<p><span id="more-306"></span>Memory leaks occur due to continuous allocation of resources without freeing them, thus reducing the amount of free memory.</p>
<h3>First step: Do we have a memory leak?</h3>
<p style="text-align: justify;">As I described earlier, it is hard to detect memory leaks during development due to the frequent system reboots in the development process. Furthermore, in case the leak is minor it could take plenty of time until the system&#8217;s performance starts to degrade to a noticeable state. Therefore, the first action to take during development, is the code review and flow review. Make sure all memory allocations are coupled with memory free. Cover all if-else and other conditional flows to make sure that the coupling still remains. In case the system starts to slow down after it was running for a while, and/or you see strange messages which contain the phrase &#8220;oom_killer&#8221;, then a memory leak probably exists somewhere. Also, during your tests, you may want to check the amount of free memory in the system periodically (read <a href="http://www.rt-embedded.com/blog/archives/linux-memory-consumption/" target="_blank">here</a> if you don&#8217;t know how) and check the log if the amount reduces over time. If it reduces over time, then a memory leak exists.</p>
<p>Let&#8217;s see the following code example of a program that leaks memory on purpose:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;</pre>
<pre>#define BLOCK_SIZE      ( 128 * 1024 )

<strong>int</strong> <strong>rte_steal_mem</strong>( <strong>void</strong> )
{
    <strong>char</strong> *p;

    /* Allocate memory */
    p = <strong>malloc</strong>( BLOCK_SIZE );

    <strong>if</strong>( p )
    {
        /* Clear it - force real allocation */
        <strong>memset</strong>( p, 0, BLOCK_SIZE );

        <strong>return</strong> 0;
    }
    <strong>return</strong> -1;
}</pre>
<pre><strong>int</strong> <strong>main</strong>( <strong>int</strong> argc, <strong>char</strong> *argv[] )
{
    <strong>while</strong>( 1 )
    {
        /* Call this function */
        <strong>if</strong>( <strong>rte_steal_mem</strong>() != 0 )
        {
            <strong>return</strong> 1;
        }
        /* Wait 2 seconds */
        <strong>sleep</strong>(2);
    }
    <strong>return</strong> 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">This program will allocate additional 128KB of memory each 2 seconds. Now, lets run it in the background and use <em>free</em> utility to periodically check the amount of free memory, in 10 second intervals:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><code># memleak &amp;<br />
# (while true; do free; sleep 10; done)&amp;<br />
total         used         free       shared      buffers<br />
Mem:       118820         9500       109320            0          436<br />
Swap:            0            0            0<br />
Total:       118820         9500       109320<br />
total         used         free       shared      buffers<br />
Mem:       118820        10120       108700            0          436<br />
Swap:            0            0            0<br />
Total:       118820        10120       108700<br />
total         used         free       shared      buffers<br />
Mem:       118820        10760       108060            0          436<br />
Swap:            0            0            0<br />
Total:       118820        10760       108060<br />
total         used         free       shared      buffers<br />
Mem:       118820        11400       107420            0          436<br />
Swap:            0            0            0<br />
Total:       118820        11400       107420<br />
total         used         free       shared      buffers<br />
Mem:       118820        12044       106776            0          436<br />
Swap:            0            0            0<br />
Total:       118820        12044       106776<br />
total         used         free       shared      buffers<br />
Mem:       118820        12684       106136            0          436<br />
Swap:            0            0            0<br />
Total:       118820        12684       106136</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">As we can see in the &#8220;<em>free</em>&#8221; column, the amount of free memory is reduced in each iteration (marked in red) and the &#8220;<em>used</em>&#8221; column shows an increasing amount of memory. This is the expected scenario in case there is a memory leak. Again, the extent of the leak depends on the bug itself and its occurrence intervals.</p>
<h3>Second step: Isolate the leaking process</h3>
<p style="text-align: justify;">The TOP application provides a per-process breakdown of memory allocations. Let&#8217;s take a look on the following snapshots, where the first two were taken immediately after the <em>memleak</em> process was activated, and the third one was taken a few minutes later:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<div><code>##### First top iteration #####</code></div>
<p><code>Mem: 9680K used, 109140K free, 0K shrd, 436K buff, 848K cached<br />
CPU:   0% usr   0% sys   0% nic 100% idle   0% io   0% irq   0% sirq<br />
Load average: 0.00 0.00 0.00 1/36 263<br />
PID  PPID USER     STAT   VSZ %MEM %CPU COMMAND<br />
171     1 root     S      572   0%   0% /bin/sh<br />
1     0 root     S      564   0%   0% init<br />
263   171 root     R      564   0%   0% top<br />
262   171 root     S      552   0%   0% memleak<br />
170     1 root     S      420   0%   0% watchdog_rt -t 15 /dev/watchdog<br />
101    13 root     SW&lt;      0   0%   0% [ti_spi.0]<br />
12     1 root     SW&lt;      0   0%   0% [khelper]<br />
144    13 root     SW&lt;      0   0%   0% [IRQ 8]<br />
21    13 root     SW&lt;      0   0%   0% [khubd]<br />
163     1 root     SWN      0   0%   0% [jffs2_gcd_mtd8]<br />
88     1 root     SW       0   0%   0% [mtdblockd]<br />
7     1 root     SW       0   0%   0% [softirq-block/0]<br />
11     1 root     SW&lt;      0   0%   0% [events/0]<br />
5     1 root     SW       0   0%   0% [softirq-net-tx/]<br />
2     1 root     SW       0   0%   0% [posix_cpu_timer]<br />
3     1 root     SW       0   0%   0% [softirq-high/0]<br />
4     1 root     SW       0   0%   0% [softirq-timer/0]<br />
6     1 root     SW       0   0%   0% [softirq-net-rx/]<br />
59    13 root     SW&lt;      0   0%   0% [aio/0]<br />
84    13 root     SW&lt;      0   0%   0% [IRQ 41]</code></p>
<div><code>##### Second top iteration #####</code></div>
<div><code>Mem: 10068K used, 108752K free, 0K shrd, 436K buff, 848K cached<br />
CPU:   0% usr   0% sys   0% nic  99% idle   0% io   0% irq   0% sirq<br />
Load average: 0.00 0.00 0.00 1/36 263<br />
PID  PPID USER     STAT   VSZ %MEM %CPU COMMAND<br />
263   171 root     R      564   0%   0% top<br />
<span style="color: #ff6600;">  262   171 root     S      936   1%   0% memleak<br />
</span>  171     1 root     S      572   0%   0% /bin/sh<br />
1     0 root     S      564   0%   0% init<br />
170     1 root     S      420   0%   0% watchdog_rt -t 15 /dev/watchdog<br />
101    13 root     SW&lt;      0   0%   0% [ti_spi.0]<br />
12     1 root     SW&lt;      0   0%   0% [khelper]<br />
144    13 root     SW&lt;      0   0%   0% [IRQ 8]<br />
21    13 root     SW&lt;      0   0%   0% [khubd]<br />
163     1 root     SWN      0   0%   0% [jffs2_gcd_mtd8]<br />
88     1 root     SW       0   0%   0% [mtdblockd]<br />
7     1 root     SW       0   0%   0% [softirq-block/0]<br />
11     1 root     SW&lt;      0   0%   0% [events/0]<br />
5     1 root     SW       0   0%   0% [softirq-net-tx/]<br />
2     1 root     SW       0   0%   0% [posix_cpu_timer]<br />
3     1 root     SW       0   0%   0% [softirq-high/0]<br />
4     1 root     SW       0   0%   0% [softirq-timer/0]<br />
6     1 root     SW       0   0%   0% [softirq-net-rx/]<br />
59    13 root     SW&lt;      0   0%   0% [aio/0]<br />
84    13 root     SW&lt;      0   0%   0% [IRQ 41]</code></div>
<div></div>
<div><code>##### top output after a few minutes #####Mem: 65212K used, 53608K free, 0K shrd, 436K buff, 848K cached<br />
CPU:   0% usr   0% sys   0% nic  99% idle   0% io   0% irq   0% sirq<br />
Load average: 0.00 0.00 0.00 1/36 263<br />
PID  PPID USER     STAT   VSZ %MEM %CPU COMMAND<br />
263   171 root     R      564   0%   0% top<br />
144    13 root     SW&lt;      0   0%   0% [IRQ 8]<br />
<span style="color: #ff0000;">262   171 root     S    55976  47%   0% memleak</span><br />
171     1 root     S      572   0%   0% /bin/sh<br />
1     0 root     S      564   0%   0% init<br />
170     1 root     S      420   0%   0% watchdog_rt -t 15 /dev/watchdog<br />
101    13 root     SW&lt;      0   0%   0% [ti_spi.0]<br />
12     1 root     SW&lt;      0   0%   0% [khelper]<br />
21    13 root     SW&lt;      0   0%   0% [khubd]<br />
163     1 root     SWN      0   0%   0% [jffs2_gcd_mtd8]<br />
88     1 root     SW       0   0%   0% [mtdblockd]<br />
7     1 root     SW       0   0%   0% [softirq-block/0]<br />
11     1 root     SW&lt;      0   0%   0% [events/0]<br />
5     1 root     SW       0   0%   0% [softirq-net-tx/]<br />
2     1 root     SW       0   0%   0% [posix_cpu_timer]<br />
3     1 root     SW       0   0%   0% [softirq-high/0]<br />
4     1 root     SW       0   0%   0% [softirq-timer/0]<br />
6     1 root     SW       0   0%   0% [softirq-net-rx/]<br />
59    13 root     SW&lt;      0   0%   0% [aio/0]<br />
84    13 root     SW&lt;      0   0%   0% [IRQ 41]</code></div>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Take a look at the VSZ and %MEM columns which show the amount of memory allocated and the percentage from the system memory. In the first iteration, the <em>memleak </em>process looks normal, like all the other processes. In the second iteration (marked in orange), it starts to be noticable, and in the third output (marked in red), we can see that the <em>memleak</em> process has allocated about 57MB which is 47% of the system&#8217;s memory!</p>
<p style="text-align: justify;">Now we know which process is leaking. In case no process shows growing numbers, then the leak could be in the kernel code. Click <a href="http://www.rt-embedded.com/blog/archives/memory-leaks-kernel/" target="_blank">here</a> in order to find out how to detect and fix these.</p>
<h3>Third step: Open the code, find and fix the leak</h3>
<h4>The traditional approach &#8211; Code reviews</h4>
<p style="text-align: justify;">As I mentioned earlier, the first approach is to try to avoid memory leaks by performing code reviews and flow covers. However, sometimes, a post release code review (once we&#8217;ve isolated the process) can reveal the leaking location inside the process. Here are some points to consider:</p>
<ul>
<li>Make sure all memory allocations are coupled with memory free.</li>
<li>Make sure all other resource allocations are coupled with resource release (such as fopen-&gt;fclose, open-&gt;close, etc.).</li>
<li>Cover all if-else, switch-case and other conditional flows, including erroneous flows, to make sure that the coupling still remains.</li>
<li>There are cases where a function allocates memory for the caller. Make sure that you free the memory once it is not needed anymore.</li>
<li>Make sure that a pointer that holds an address to a dynamically allocated memory is not erased or overrun by another value. If this will happen, you will not be able to free this resource.</li>
</ul>
<p>If all of that does not help, you can consider using code inspection tools, or the follow open source library.</p>
<h4>Replacing the malloc function with a debug version</h4>
<p>We can try to apply a nice trick to figure out the caller, by replacing the standard <em>malloc</em> function with a macro. The macro will undefine the original <em>malloc</em> address and will show some useful information about the caller, and how much memory it required. It is most useful to put this macro in a common header file, or in a specific C file that you suspect. Note that the beauty of this macro is in the fact that it will not harm the program, every memory allocation will still be served correctly. However, it will slow down the program due to the prints. The macro that we use is highlighted in the following example code:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

<strong>#define __malloc malloc</strong>
<strong>#undef malloc</strong>
<strong>#define malloc( _size ) \ ({ printf( "%s: Calling malloc(%d) from line %d, filename: %s\n", __func__, _size, __LINE__, __FILE__ ); \   __malloc( _size ); })</strong>

int main( int argc, char *argv[] )
{
    void *p;

    p = malloc( 1024 );
    memset( p, 0, 1024 );
    printf( "p=%p\n", p );
    free(p);

    return 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<p>And here is the output of this file:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">$ ./mem
main: Calling malloc(1024) from line 13, filename: mem.c
p=0x9c06008</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Any doubt who was the caller&#8230;? Note that same macro technique can be applied on <em>free</em> or any other allocation function. If a specific caller is constantly allocating memory (for example; in a loop), it will be very easy to catch it here. However, if the leak is minimal and we need more analysis to apply, the macro can be extended to also print the address of the allocated memory, and so will the <em>free</em> macro replacement. Using an offline processing tool (such as Microsoft Excel), we can then analyze the output of the macros and compare allocations against releases of the same address. Then we could track who was the caller of an allocation which wasn&#8217;t freed. If it sounds too complicated, then the next section describes a tool that may automate some of the work.</p>
<h4>Using dmalloc to debug your application</h4>
<p>The dmalloc is a dynamic memory allocation analysis tool that replaces the traditional <em>malloc</em> (including all its derivatives) and <em>free</em> functions with debug enhanced versions, which will allow you to detect:</p>
<ul>
<li>Memory leaks.</li>
<li>Memory overruns.</li>
<li>Mutiple calls for <em>free</em> on the same pointer.</li>
<li>Freeing a NULL pointer.</li>
<li>Using a freed resource.</li>
</ul>
<p style="text-align: justify;">The tool can be configured in run time, and also provides statistics of memory allocation during the program&#8217;s life and after it exits. The provided log is detailed and contains file names and line numbers.</p>
<p style="text-align: justify;">Once you&#8217;ve downloaded and compiled the dmalloc package, you&#8217;ll see that it created an executable and a library. The executable is used to configure the dmalloc in runtime and the library contains the debug enhanced versions of the allocation/release functions.</p>
<p style="text-align: justify;">In order to facilitate the dmalloc library in your program, you need to do some minor modifications to its source files and makefiles. In each source file that you are interested to enable the dmalloc debugging, you need to include the library&#8217;s header file. This include directive can be <em>ifdef&#8217;</em>ed to be enabled only when you enable debug mode. For example:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><code>#ifdef ENABLE_DMALLOC_DEBUG<br />
#include "dmalloc/dmalloc.h"<br />
#endif </code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">And in the makefile, you need to link with the dmalloc library as in this example:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><code>ifdef ENABLE_DMALLOC_DEBUG<br />
LDFLAGS += -ldmalloc<br />
endif </code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Note that multithreaded applications require to link with the thread aware version of the library. In this example you need to compile your application with the ENABLE_DMALLOC_DEBUG flag enabled in order to enable the dmalloc infrastructure for debug purposes.</p>
<p style="text-align: justify;">Once you&#8217;ve compiled and linked your application with dmalloc enabled, boot the system and configure the tool with your requested settings. When your system finished booting, configure the dmalloc with your required options, such as shell type, monitor intensity, poll intervals, log file and other advanced options according to your requirements. Run the dmalloc application with the <em>&#8211;usage </em>flag to see other flags. If you are using an embedded system with BusyBox, use the -<em>b</em> flag, and then copy-and-paste the output text that the dmalloc prints. This procedure is required in order to setup an environment variable which will be visible to the program you want to debug. Here&#8217;s an example:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><code># dmalloc -b high -i 1 -l /dev/console<br />
DMALLOC_OPTIONS=debug=0x4f4ed03,inter=1,log=/dev/console<br />
export DMALLOC_OPTIONS </code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">In this example, the dmalloc is initialized to print an init string for borne shell types, high monitoring in 1 seconds intervals and display the log in the cosole (this could be replaced by any file name). Don&#8217;t forget you need to copy-and-paste the output to make this configuration effective. Now you are ready to run your program. As an example, let&#8217;s modify our <em>memleak</em> program to do 5 iterations and then exit. Let&#8217;s see what statistics are displayed when the program exits:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><code># memleak<br />
29: 5: Dmalloc version '5.5.2' from 'http://dmalloc.com/'<br />
29: 5: flags = 0x4f4ed03, logfile '/dev/console'<br />
29: 5: interval = 1, addr = 0, seen # = 0, limit = 0<br />
29: 5: starting time = 19<br />
29: 5: process pid = 207<br />
29: 5: Dumping Chunk Statistics:<br />
29: 5: basic-block 4096 bytes, alignment 8 bytes<br />
29: 5: heap address range: 0x4007000 to 0x4194000, 1626112 bytes<br />
29: 5:     user blocks: 165 blocks, 655360 bytes (94%)<br />
29: 5:    admin blocks: 4 blocks, 16384 bytes (2%)<br />
29: 5:    total blocks: 169 blocks, 692224 bytes<br />
29: 5: heap checked 6<br />
29: 5: alloc calls: malloc 5, calloc 0, realloc 0, free 0<br />
29: 5: alloc calls: recalloc 0, memalign 0, valloc 0<br />
29: 5: alloc calls: new 0, delete 0<br />
29: 5:   current memory in use: 655360 bytes (5 pnts)<br />
29: 5:  total memory allocated: 655360 bytes (5 pnts)<br />
29: 5:  max in use at one time: 655360 bytes (5 pnts)<br />
29: 5: max alloced with 1 call: 131072 bytes<br />
29: 5: max unused memory space: 20480 bytes (3%)<br />
29: 5: top 10 allocations:<br />
29: 5:  total-size  count in-use-size  count  source<br />
29: 5:      655360      5      655360      5  /home/hai/rte/memleak.c:13<br />
29: 5:      655360      5      655360      5  Total of 1<br />
29: 5: Dumping Not-Freed Pointers Changed Since Start:<br />
29: 5:  not freed: '0x40ef008|s1' (131072 bytes) from '/home/hai/rte/memleak.c:13'<br />
29: 5:  not freed: '0x4110008|s1' (131072 bytes) from '/home/hai/rte/memleak.c:13'<br />
29: 5:  not freed: '0x4131008|s1' (131072 bytes) from '/home/hai/rte/memleak.c:13'<br />
29: 5:  not freed: '0x4152008|s1' (131072 bytes) from '/home/hai/rte/memleak.c:13'<br />
29: 5:  not freed: '0x4173008|s1' (131072 bytes) from '/home/hai/rte/memleak.c:13'<br />
29: 5:  total-size  count  source<br />
29: 5:      655360      5  /home/hai/rte/memleak.c:13<br />
29: 5:      655360      5  Total of 1<br />
29: 5: ending time = 29, elapsed since start = 0:00:10 </code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Let&#8217;s see the useful debug information which the dmalloc provided:</p>
<ul>
<li>General information</li>
<li>Heap statistics</li>
<li>Total calls for each allocation or free functions</li>
<li>Allocation statistics</li>
<li>Top 10 allocations</li>
<li>And a list of non-freed pointers including file name and line number (This is great for us!).</li>
</ul>
<p style="text-align: justify;">With this information, we can isolate and fix the memory leak immediately. In case your program does not exist (in case of a daemon for example), you can show these statistics by calling the statistics function <em>dmalloc_log_stats( ), </em>which is actually the same function that is automatically called upon process termination. Your program can call this function periodically or by external request (such as an IPC action).</p>
<p style="text-align: justify;">Although it is not relevant directly to memory leaks, lets also see what happens when the program overflows a buffer. Let&#8217;s change the <em>memset</em> call in out <em>memleak</em> program to write BLOCK_SIZE+1 bytes (1 extra byte). Here&#8217;s the output:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><code>66: 2: Dmalloc version '5.5.2' from 'http://dmalloc.com/'<br />
66: 2: flags = 0x4f4ed03, logfile '/dev/console'<br />
66: 2: interval = 1, addr = 0, seen # = 0, limit = 0<br />
66: 2: starting time = 64<br />
66: 2: process pid = 209<br />
66: 2:   error details: checking user pointer<br />
66: 2:   pointer '0x40ef008' from 'unknown' prev access '/home/hai/rte/memleak.c:13'<br />
66: 2:   dump of proper fence-top bytes: '\372\312\336i'<br />
66: 2:   dump of '0x40ef008'+131056: '\000\000\000\000\000\000\000\000\000\000\000\312\336i'<br />
66: 2: ERROR: _dmalloc_chunk_heap_check: failed OVER picket-fence magic-number check (err 27)<br />
debug-malloc library: dumping program, fatal error<br />
Error: failed OVER picket-fence magic-number check (err 27)<br />
Aborted </code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">As we can see, the dmalloc caught the overflow and terminated the program. We can see when the buffer was allocated and the data that was overflowing.</p>
<p style="text-align: justify;">You can download the dmalloc source code from <a href="http://dmalloc.com/releases/" target="_blank">here</a>, and consult its detailed documentation <a href="http://dmalloc.com/docs/latest/online/dmalloc_toc.html" target="_blank">here</a>.</p>
<h3>Forth step: Validate your fix</h3>
<p style="text-align: justify;">Congratulations, you found and fixed the memory leak. The last step is to repeat step 1 and make sure the system does not show memory degradation over time, and your&#8217;e done. <span style="text-decoration: underline;">Resources</span>:<br />
<a href="http://dmalloc.com/">http://dmalloc.com/</a><br />
<a href="http://linux.die.net/man/1/top">http://linux.die.net/man/1/top</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/_CmGQG9liys" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/memory-leaks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/memory-leaks/</feedburner:origLink></item>
		<item>
		<title>Measuring the CPU load</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/iVyRtc8Jbio/</link>
		<comments>http://www.rt-embedded.com/blog/archives/measuring-the-cpu-load/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 11:13:39 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[calculate]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[iostat]]></category>
		<category><![CDATA[load]]></category>
		<category><![CDATA[measure]]></category>
		<category><![CDATA[real]]></category>
		<category><![CDATA[rt]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[top]]></category>
		<category><![CDATA[usage]]></category>
		<category><![CDATA[vmstat]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=245</guid>
		<description><![CDATA[<p style="text-align: justify;">The CPU in embedded systems is an important resource. The CPU type, model and processing power is carefully selected during the project&#8217;s first steps. A system that its CPU is busy most of the time is not healthy, and a system that its CPU is idle most of the time is wasteful. Assuming we&#8217;re [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-270" title="CPU-usage" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/CPU-usage.jpg" alt="CPU usage" width="252" height="223" />The CPU in embedded systems is an important resource. The CPU type, model and processing power is carefully selected during the project&#8217;s first steps. A system that its CPU is busy most of the time is not healthy, and a system that its CPU is idle most of the time is wasteful. Assuming we&#8217;re past the design stage and the system is running, you and your managers will probably want to know how busy is the CPU while performing various tasks. Operations that require extensive CPU processings include cryptographic calculations, handling and routing network traffic, graphic calculations and many other product specific calculations.</p>
<p style="text-align: justify;">This article covers 3 different utilities which will help you measure the CPU load in Linux environment. Their output defers from each other by the levels of report details.</p>
<p style="text-align: justify;"><span id="more-245"></span></p>
<h3 style="text-align: justify;">The VMSTAT utility</h3>
<p style="text-align: justify;">The  <a href="http://linux.die.net/man/8/vmstat" target="_blank">vmstat</a> utility reports summarized information about processes, memory, paging, block I/O, traps, and CPU load. It is distributed as part of the procps package which contains libraries and other utilities that provide information based on the information provided in the <a href="http://www.rt-embedded.com/blog/archives/process-information-in-the-proc-filesystem/" target="_blank">proc</a> filesystem. The following window provides an example screenshot of the vmstat utility, which was configured to display 12 readings in 1 second intervals. Each line provides a snapshot of the system in the current sampling time (use <em>vmstat 1 12</em>):</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># vmstat 1 12<br />
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------<br />
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st<br />
 3  0      0 257232  14876 309072    0    0    83     9   65   98  1  2 96  1  0<br />
 0  0      0 257000  14876 309072    0    0     0     0   78  106  2  2 96  0  0<br />
 0  0      0 257124  14884 309072    0    0     0    32   45   62  1  2 92  5  0<br />
 0  0      0 257232  14884 309072    0    0     0     0   74  107  1  2 97  0  0<br />
 0  0      0 257000  14884 309072    0    0     0     0   43   49  2  1 97  0  0<br />
 0  0      0 257124  14884 309072    0    0     0    16   86  124  1  2 97  0  0<br />
 0  0      0 257124  14884 309072    0    0     0     0  151  214  2  3 95  0  0<br />
 0  0      0 256984  14884 309072    0    0     0     0  156  234  2  4 94  0  0<br />
 0  0      0 257108  14884 309072    0    0     0     0  279  420  0  5 95  0  0<br />
 0  0      0 257000  14884 309084    0    0    12     0  201  285  3  5 92  0  0<br />
 0  0      0 257000  14884 309084    0    0     0    12  146  193  2  2 96  0  0<br />
 0  0      0 257000  14884 309084    0    0     0     0   63  110  0  3 97  0  0</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The following fields are displayed in the vmstat output:</p>
<table style="text-align: justify;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="768" valign="top"><strong>Procs</strong></td>
</tr>
<tr>
<td width="768" valign="top">r: The number of processes waiting for run time.</td>
</tr>
<tr>
<td width="768" valign="top">b: The number of processes in uninterruptible sleep.</td>
</tr>
<tr>
<td width="768" valign="top"> </td>
</tr>
<tr>
<td width="768" valign="top"><strong>Memory</strong></td>
</tr>
<tr>
<td width="768" valign="top">swpd: the amount of virtual memory used.</td>
</tr>
<tr>
<td width="768" valign="top">free: the amount of idle memory.</td>
</tr>
<tr>
<td width="768" valign="top">buff: the amount of memory used as buffers.</td>
</tr>
<tr>
<td width="768" valign="top">cache: the amount of memory used as cache.</td>
</tr>
<tr>
<td width="768" valign="top"> </td>
</tr>
<tr>
<td width="768" valign="top"><strong>Swap</strong></td>
</tr>
<tr>
<td width="768" valign="top">si: Amount of memory swapped in from disk (or disks).</td>
</tr>
<tr>
<td width="768" valign="top">so: Amount of memory swapped to disk (or disks).</td>
</tr>
<tr>
<td width="768" valign="top"> </td>
</tr>
<tr>
<td width="768" valign="top"><strong>IO</strong></td>
</tr>
<tr>
<td width="768" valign="top">bi: Blocks received from a block device (blocks/s).</td>
</tr>
<tr>
<td width="768" valign="top">bo: Blocks sent to a block device (blocks/s).</td>
</tr>
<tr>
<td width="768" valign="top"> </td>
</tr>
<tr>
<td width="768" valign="top"><strong>System</strong></td>
</tr>
<tr>
<td width="768" valign="top">in: The number of interrupts per second, including the clock.</td>
</tr>
<tr>
<td width="768" valign="top">cs: The number of context switches per second.</td>
</tr>
<tr>
<td width="768" valign="top"> </td>
</tr>
<tr>
<td width="768" valign="top"><strong>CPU</strong></td>
</tr>
<tr>
<td width="768" valign="top">us: %CPU spent by User space applications.</td>
</tr>
<tr>
<td width="768" valign="top">sy: %CPU spent by the System (kernel mode).</td>
</tr>
<tr>
<td width="768" valign="top">id: %CPU spent idle.</td>
</tr>
<tr>
<td width="768" valign="top">wa: %CPU spent waiting for I/O.</td>
</tr>
<tr>
<td width="768" valign="top">st: %CPU stolen from a virtual machine.</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The procps package source code can be downloaded from <a href="http://procps.sourceforge.net/procps-3.2.8.tar.gz" target="_blank">here</a>.</p>
<h3 style="text-align: justify;">The TOP utility</h3>
<p style="text-align: justify;">The TOP utility is another standard utility. It is distributed as part of the <a href="http://procps.sourceforge.net/" target="_blank">procps</a> package and a lighter implementation is provided in <a href="http://www.busybox.net/" target="_blank">Busybox</a>. It provides a summary about the CPU  and memory usage in the system, and it also provides detailed information about each task in the system. This information includes the process ID, the parent&#8217;s process ID, owner, state and memory consumption. It also shows the CPU usage breakdown per each process in the column before the process name. This detailed output is mainly used for debugging purposes, to detect tasks that consume too much CPU time. The output refresh rate (in seconds) and number of iteration can be configure in the command line. Here&#8217;s a snapshot of Busybox&#8217;s top output in an idle machine, which was configured to  to display 10 readings in 1 second intervals. The top output refreshes the whole screen in each iteration (use <em>top 2 10)</em>:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># top 2 10<br />
Mem: 9500K used, 44320K free, 0K shrd, 412K buff, 832K cached<br />
CPU:   0% usr   0% sys   0% nic 100% idle   0% io   0% irq   0% sirq<br />
Load average: 0.00 0.00 0.00 1/32 184<br />
  PID  PPID USER     STAT   VSZ %MEM %CPU COMMAND<br />
  171     1 root     S      568   1%   0% /bin/sh<br />
    1     0 root     S      564   1%   0% init<br />
  184   171 root     R      564   1%   0% top<br />
  170     1 root     S      420   1%   0% watchdog -t 15 /dev/watchdog<br />
  144    13 root     SW&lt;      0   0%   0% [IRQ 8]<br />
  101    13 root     SW&lt;      0   0%   0% [spi.0]<br />
   88     1 root     SW       0   0%   0% [mtdblockd]<br />
   12     1 root     SW&lt;      0   0%   0% [khelper]<br />
    7     1 root     SW       0   0%   0% [softirq-block/0]<br />
   10     1 root     SW&lt;      0   0%   0% [desched/0]<br />
   11     1 root     SW&lt;      0   0%   0% [events/0]<br />
    4     1 root     SW       0   0%   0% [softirq-timer/0]<br />
    5     1 root     SW       0   0%   0% [softirq-net-tx/]<br />
   17    13 root     SW&lt;      0   0%   0% [kblockd/0]<br />
    2     1 root     SW       0   0%   0% [posix_cpu_timer]<br />
    3     1 root     SW       0   0%   0% [softirq-high/0]<br />
   57    13 root     SW       0   0%   0% [pdflush]<br />
   58    13 root     SW&lt;      0   0%   0% [kswapd0]<br />
    6     1 root     SW       0   0%   0% [softirq-net-rx/]<br />
   84    13 root     SW&lt;      0   0%   0% [IRQ 41]</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The CPU summay line provides the follwoing information:</p>
<table style="text-align: justify;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="55" valign="top"><strong>Field</strong></td>
<td width="535" valign="top"><strong>Description</strong></td>
</tr>
<tr>
<td width="55" valign="top">usr</td>
<td width="535" valign="top">%CPU spent by User space applications</td>
</tr>
<tr>
<td width="55" valign="top">sys</td>
<td width="535" valign="top">%CPU spent by the System (kernel mode)</td>
</tr>
<tr>
<td width="55" valign="top">nic</td>
<td width="535" valign="top">%CPU spent by Low priority user mode (nice)</td>
</tr>
<tr>
<td width="55" valign="top">Idle</td>
<td width="535" valign="top">%CPU which is available (idle)</td>
</tr>
<tr>
<td width="55" valign="top">io</td>
<td width="535" valign="top">%CPU spent by I/O waiting</td>
</tr>
<tr>
<td width="55" valign="top">irq</td>
<td width="535" valign="top">%CPU spent servicing interrupt requests</td>
</tr>
<tr>
<td width="55" valign="top">sirq</td>
<td width="535" valign="top">%CPU spent servicing soft irqs</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Please note that this output may vary between different top versions or implementations. In Busybox implementation, the CPU measurement feature is disabled by default. In order to enable it, you must change the Busybox configuration to enable this feature (Process Utilities-&gt;top-&gt;Show CPU per-process usage percentage and  Show CPU global usage percentage).</p>
<h3 style="text-align: justify;">The IOSTAT utility</h3>
<p style="text-align: justify;">The <a href="http://linux.die.net/man/1/iostat" target="_blank">iostat</a> utility is used for monitoring system input/output device and can help you estimate the performance of block devices, but it also calculates the CPU load. It can be configured to display the CPU load periodically. Its output is a summary of the CPU load in the sampling time. Here&#8217;s the output of iostat, when it was configured to show the CPU usage in 1 second intervals (use <em>iostat c 1</em>):</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># iostat c 1<br />
 %user  %system %iowait %idle<br />
  0       0       0     100<br />
  0       50      0     50<br />
  2       50      0     48<br />
  4       50      0     46<br />
  48      10      0     42<br />
  12      0       0     88<br />
  7       0       93    0<br />
  7       6       87    0</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Each line provides the follwoing information:</p>
<table style="text-align: justify;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="59" valign="top"><strong>Field</strong></td>
<td width="532" valign="top"><strong>Description</strong></td>
</tr>
<tr>
<td width="59" valign="top">user</td>
<td width="532" valign="top">%CPU spent by User space applications</td>
</tr>
<tr>
<td width="59" valign="top">system</td>
<td width="532" valign="top">%CPU spent by System (kernel mode)</td>
</tr>
<tr>
<td width="59" valign="top">io</td>
<td width="532" valign="top">%CPU spent by I/O waiting</td>
</tr>
<tr>
<td width="59" valign="top">idle</td>
<td width="532" valign="top">%CPU which is available (idle)</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The iostat source code can be downloaded <a href="http://www.linuxinsight.com/iostat_utility.html" target="_blank">here</a>.</p>
<h3 style="text-align: justify;">How to interpret the numbers</h3>
<p style="text-align: justify;">In high level, all 3 tools provide a summary of the current CPU usage. The four imporant fields are; user, system, i/o and idle, where the sum of all four fields must be 100%. If you are interested to see the CPU load in general, you may run iostat for a summarized  information. If you want more details, you can run vmstat. If you need a per-process breakdown, use top.</p>
<p style="text-align: justify;">A system which shows high percentage in the <strong>user</strong> field, suggests that the CPU is busy mainly with one or more processes in the user space. Usually, a process should not consume too much CPU during its run time due to the scheduling policy. In case a process consumes constant high CPU processing power, it may suggest that it is performing either a heavy calculation taks or that something is wrong. If you know that the specific process should consume a lot of CPU time, then it is OK. Otherwise, you need to check the implementation of the process. It might be stuck in busy-wait state, it might be over prioritized (read <a href="http://www.rt-embedded.com/blog/archives/processes-background-and-priority/" target="_blank">here</a> for more details),  or perhaps its code implementation is not optimized (for example; performing many iterations of division/modulus calls, performing unneeded calculations, calling too many system calls, etc.).</p>
<p style="text-align: justify;">A system which shows high percentage in the <strong>system</strong> field, suggests that the CPU is busy mainly running kernel code and drivers. This is typically seen in high bandwidth networking drivers, where the system processes high networking traffic, or high interrupts load. The Linux kernel provides means to reduce the networking overhead with the NAPI networking API. Interrupt handling overhead can be reduced, if the hardware supports it, by interrupt pacing, which allows activation of the servicing function once per a number of interrupts, thus doing all the work once.</p>
<p style="text-align: justify;">A system which shows high percentage in the <strong>i/o</strong> field, suggests that the CPU is waiting for data to be read or written from or to a storage device (disk, flash, cd, etc). Note that it doesn&#8217;t mean that the CPU is busy; it could perform other tasks while the i/o operation completes. However, it might suggest that you need to tune your caches and file systems.</p>
<p style="text-align: justify;">A system which shows high percentage in the <strong>idle</strong> field, suggests that the CPU is idle most of the time (just sitting and waiting).  It is normal in idle state where the system is waiting (for example in the shell, or login screen). However, if this is the situation even in the highest work load possible in the product, then this CPU is too strong for the product, and it is a waste of money and power. You should consider adding additional work for this CPU to do, or put the CPU in low power mode while reducing its clock rate.</p>
<p style="text-align: justify;">Please note that the scenarios which were described are just examples. There could be other reasons which affect your system behavior in a different manner.</p>
<p style="text-align: justify;"><span style="text-decoration: underline;">Resources:<br />
</span><a href="http://procps.sourceforge.net/">http://procps.sourceforge.net/</a><br />
<a href="http://linux.die.net/man/8/vmstat">http://linux.die.net/man/8/vmstat</a><br />
<a href="http://linux.die.net/man/1/iostat">http://linux.die.net/man/1/iostat</a><br />
<a href="http://www.busybox.net/">http://www.busybox.net/</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/iVyRtc8Jbio" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/measuring-the-cpu-load/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/measuring-the-cpu-load/</feedburner:origLink></item>
		<item>
		<title>Embedded software 5 most destructive bugs</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/IdgCtMXPUJo/</link>
		<comments>http://www.rt-embedded.com/blog/archives/5-most-destructive-bugs/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 13:30:08 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Opinions]]></category>
		<category><![CDATA[alignment]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[crash]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[leaks]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[overflow]]></category>
		<category><![CDATA[real]]></category>
		<category><![CDATA[review]]></category>
		<category><![CDATA[rt]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=202</guid>
		<description><![CDATA[<p style="text-align: justify;">We all make mistakes, and it is only natural. However, if the product is already running in the field, mistakes we do in the code which are not detected on time, can cause:</p>

Damage to customers,
Massive recalls,
Massive Firmware update requirement,
Failure in field tests,
Loss of brand credibility,
Lot&#8217;s of wasted $$$,
Travel to customer&#8217;s/OEM site,
You loosing your position, or [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-232" title="bug" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/bug.jpg" alt="" width="280" height="198" />We all make mistakes, and it is only natural. However, if the product is already running in the field, mistakes we do in the code which are not detected on time, can cause:</p>
<ul style="text-align: justify;">
<li>Damage to customers,</li>
<li>Massive recalls,</li>
<li>Massive Firmware update requirement,</li>
<li>Failure in field tests,</li>
<li>Loss of brand credibility,</li>
<li>Lot&#8217;s of wasted $$$,</li>
<li>Travel to customer&#8217;s/OEM site,</li>
<li>You loosing your position, or job ? <img src='http://www.rt-embedded.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p style="text-align: justify;">The error types I am going to present are generic errors, which are not tied to specific architectures. A bug which belongs to these error types may be hidden, hard to find and hard to reproduce. The system may fail randomly or unexpectedly (Referred as the &#8220;Voodoo effect&#8221;).</p>
<p style="text-align: justify;">We can <em>never </em>guarantee that the software we provide is error free. What can we do in order to minimize the chance for these errors to pop up? Well, just continue to read.</p>
<p style="text-align: justify;"><span id="more-202"></span></p>
<h3 style="text-align: justify;">1. Buffer overflow</h3>
<p style="text-align: justify;">Buffer overflow is a large family of errors, and could be the result of various bugs:</p>
<ol style="text-align: justify;">
<li>Static buffer overflow: Writing, reading or copying data to or from a buffer in a larger amount than its maximum size. A buffer could be a string buffer, a structured variable, an array or a native type variable, either global or on the stack.</li>
<li>Dynamic buffer overflow: Similar to 1, with the difference of the buffer being allocated dynamically.</li>
<li>Array overflow: Misunderstanding or confusion about the last array index. An array in the size of X has the indexes of 0 to X-1.</li>
</ol>
<p style="text-align: justify;"><span style="text-decoration: underline;">How to avoid and detect:</span></p>
<ol style="text-align: justify;">
<li>When you write new code, pay attention to the <em>size </em>of each variable, or buffer you need to access.</li>
<li>Pay close attention to the memory read/write functions such as <em>memset, memcpy, memmove </em>and similar. Make sure that the destination buffer is large enough to accept the worst case scenario in terms of data size (maximum name length, maximum amount of bytes, largest data type).</li>
<li>When working with string functions such as <em>strcpy, strcat  </em>and similar, make sure that the buffer is terminated with a NULL character (&#8216;\0&#8242;), otherwise, the string function will continue to process your buffer until a NULL character will be found somewhere in the memory.</li>
<li>Similarly to 2 and 3, when using string format functions like <em>sprintf</em> and similar functions.</li>
<li>When working with strings, try to use the bounded versions of the string functions like: <em>strncpy, strncat, snprintf</em>. These functions accept another parameter in which you can specify your buffer length. They will limit the maximum bytes they write to the destination buffer. Note that in case these functions hit your number, they will stop their processing and will <em>not</em> place a NULL character. Therefore, the correct usage of these functions is; first, clear your buffer using <em>memset</em>, and then, let the string function process your buffer by specifying its length &#8211; 1.</li>
<li>When working with arrays, and the array is defined in the size of ARRAY_SIZE:
<ul>
<li>Never access the array in the index of ARRAY_SIZE. It&#8217;s one index outside of its boundary.</li>
<li>When using for loops, the end condition for the last entry must be smaller than ARRAY_SIZE and not smaller or equal.</li>
</ul>
</li>
</ol>
<p>Buffer overflow may result in <a href="http://www.rt-embedded.com/blog/archives/resolving-crashes-and-segmentation-faults/" target="_blank">Segmentation fault crash </a>(in user space) or a kernel panic (for code running in kernel space).</p>
<h3 style="text-align: left;">2. Memory (or resource) leaks</h3>
<p style="text-align: justify;"><a href="http://www.rt-embedded.com/blog/archives/memory-leaks/" target="_blank">Memory leaks</a> usually kill your system slowly and painfully. After a fresh boot, everything looks ok and the system works fine. Depending on the total amount of RAM and the extent of the memory leak, the system will continue to run and perform well for a certain amount of time. It could be hours, days, or weeks. During this time, the amount of free memory is constantly decreased. In Linux based systems, when memory is required and there is no free memory, the  kernel will start paging out programs and clearing the page cache. Further memory requests will cause performance impact due to paging out living tasks. Eventually, the kernel will trigger the Out-Of-Memory killer and start killing processes. In this stage, the system may be unusable already. The last stage will cause the kernel to panic and hang/reboot. Many consumer electronics products in the field remain active their whole life, therefore you can&#8217;t allow any memory leaks to happen, or else the customer will have to power cycle the unit from time to time. Memory leaks are also hard to notice during unit-tests or lab tests because in these scenarios, the unit is rebooted constantly, as part of the development or testing work.</p>
<p style="text-align: justify;">Memory leaks occur due to continuous resource allocation without freeing it. A system can process numerous amount of data, but if we&#8217;ll examine it in snapshots, at any given time its resource allocation is bounded. In order to keep the system functional, the resource must be freed when it is not required anymore, or when the work on it has been done.</p>
<p style="text-align: justify;">Memory leaks could be the results of the following:</p>
<ol style="text-align: justify;">
<li>Allocating a memory buffer using functions similar to <em>malloc (user) </em>or <em><a href="http://www.rt-embedded.com/blog/archives/memory-leaks-kernel/" target="_blank">kmalloc (kernel)</a></em> and not freeing it.</li>
<li>Opening files, sockets or pipes and not closing them.</li>
<li>A kernel network driver which processes a packet in an <em>skb</em> structure and doesn&#8217;t free it when it is finished.</li>
</ol>
<p style="text-align: justify;"><span style="text-decoration: underline;">How to avoid and detect:</span></p>
<ol style="text-align: justify;">
<li>When a resource is temporary allocated inside a function, go through all <em>flow</em> options and make sure the resource is freed before each <em>return</em> keyword. In some cases, usually error cases when the function exits prematurely, the resource free handling is forgotten. For example, assuming a resource was allocated, and then, an <em>if</em> statement that checks a condition may cause the function to return an error. Usually the <em>if</em> statement continues correctly and the resource is used and freed, but in case of an error, the function may return earlier.</li>
<li>Some functions may allocate memory for you and return a pointer to the allocated buffer. Make sure you free it once your processing is complete.</li>
<li>During long test trials, run &#8220;free&#8221; or  &#8221;top&#8221; in the background, and monitor any memory reduction (with free) or memory incrementation of a task (with top).</li>
</ol>
<h3 style="text-align: left;">3. Ignoring compiler warnings or fixing them without understanding</h3>
<p style="text-align: justify;">A common bad practice is to ignore compiler warnings. Indeed, there are cases that the warnings are not important, but there are cases where warnings may indicate a real problem. A few examples:</p>
<ol style="text-align: justify;">
<li>Missing return value.</li>
<li>Incompatible variable assignment.</li>
<li>Mismatch between pointers and variables.</li>
<li>Using an uninitialized varialbe.</li>
</ol>
<p style="text-align: justify;">Any many other examples.</p>
<p style="text-align: justify;">An even worse practice is to fix warnings without really understanding them, just to make the compiler quiet. A nice story about a developer who &#8220;fixed&#8221; a warning reported by a code inspection without really understanding what he did can be found <a href="http://www.technologyreview.com/Infotech/20801/?nlid=1085&amp;a=f" target="_blank">here</a>.</p>
<p style="text-align: justify;"><span style="text-decoration: underline;">How to avoid:</span></p>
<ol>
<li>
<div style="text-align: justify;">Write a simple well organized code. Writing &#8220;clever&#8221; code is not fashionable and makes it hard for everybody else to understand and fix.</div>
</li>
<li>
<div style="text-align: justify;">Enable the <a href="http://www.rt-embedded.com/blog/archives/using-gcc-part-2/" target="_blank"><em>-Wall</em> switch </a>in the gcc. It will enable all warnings report.</div>
</li>
<li>
<div style="text-align: justify;">Never finish a module with remaining warnings. If you won&#8217;t fix the first one as it appears, soon there will be more and more until there will be too many. If you coded this module, you know best, right now, how to fix it. Other people might not understand what was your intension, and even you may forget it after a while.</div>
</li>
<li>
<div style="text-align: justify;">Once the module is warning-free, it is recommended to configure the gcc to treat all warnings as errors using the <a href="http://www.rt-embedded.com/blog/archives/using-gcc-part-2/" target="_blank"><em>-Werror</em> switch</a>. This will keep the module warning free for good.</div>
</li>
<li>
<div style="text-align: justify;">Never fix a warning that you have doubts about. Many warnings are straight forward, but some may require you to ask the person who wrote the module, or to consult with other people.</div>
</li>
</ol>
<h3 style="text-align: justify;">4. Race conditions</h3>
<p style="text-align: justify;">Race condition is a scenario when two or more contexts are using the same resource in parallel without considering each other. Race condition could be a result of:</p>
<ol>
<li>
<div style="text-align: justify;">2 or more contexts are trying to access the same data, variable or memory in the same time.</div>
</li>
<li>
<div style="text-align: justify;">2 or more contexts are calling a non-reentrant function in the same time.</div>
</li>
</ol>
<p style="text-align: justify;">Contexts could be either kernel threads or user space threads inside a process. A single process is more protected, however, it could be exposed to the same issues in case 2 or more processes are using a shared memory region, or calling a non-reentrant function in a shared library.</p>
<p style="text-align: justify;">Race conditions could result in a randon an unpredictable behavior of the system due to mutual data corruption (one context may corrupt the data or flow other other one), and it may be hard to detect or reproduce such bugs.</p>
<p style="text-align: justify;"><span style="text-decoration: underline;">How to avoid:</span></p>
<ol>
<li>
<div style="text-align: justify;">Try to avoid creating and using global variables.</div>
</li>
<li>
<div style="text-align: justify;">Protect global variables and shared memory regions with semaphores or mutexes. Each context &#8220;locks&#8221; the resource until it finishes the processing (be careful from deadlocks).</div>
</li>
<li>
<div style="text-align: justify;">Write your functions to be reentrant. If the function accesses a shared resouce and can&#8217;t be reentrant, apply a mutex that will allow only single usage at a time. A second context calling the same function will go to sleep until it is freed.</div>
</li>
<li>
<div style="text-align: justify;">Try to minimize the use of external non-reentrant functions (such as strtok), use reentrant alternative (such as strtok_r).</div>
</li>
</ol>
<h3 style="text-align: justify;">5. Alignment traps</h3>
<p style="text-align: justify;"><a href="http://www.rt-embedded.com/blog/archives/resolving-alignment-traps/" target="_blank">Alignment traps</a> are crashes that occur due to misaligned memory access on 32-bit CPUs. Such scenarios could happen when porting code to a new architecture, and when using wrong methods to access raw network packets or data storage with varied length. This error condition occurs when the CPU is requested to read a 32-bit variable from an odd address (which is used by 8-bit variables) or an address that is used by 16-bit variables.  For example, suppose you receive an IPv4 packet and you want to read the first 32-bits of the IP header (contains information such as version and total length). The IP portion of the packet is concatenated after the Ethernet header which is composed of 14 bytes (6 source address, 6 destination address, 2 type). If you would try to access the IP packet directly with a 32-bit pointer, you will get an alignment trap because the IP packet data starts at offset 14, and the CPU can read a 32-bit variable from either address 12 or 16 (4 bytes alignment). Another example, suppose there is a function that retreives data from a database. The data size could be from 1 byte to 1024 bytes, and the function returns a void pointer. If you&#8217;ll cast this pointer to a 32-bit pointer (such as unsigned integer) in order to read the values, you will get an alignment trap in case the function returns a 1 byte (char) or 2 bytes (short) of data. Whilst the first example is easy to track and fix (happens always), the latter might be difficult, depending on the amount of times the function really returns a pointer to an 8-bit variable.</p>
<p style="text-align: justify;"><span style="text-decoration: underline;">How to avoid and detect:</span></p>
<ol>
<li>
<div style="text-align: justify;">Be careful with pointer casting. If there is a chance that the pointer points to an array of bytes or shorts, or comes from a packed structure, you must not cast this pointer to a 32-bit type. Instead, copy the pointer&#8217;s value to a local variable, and do the processing on it.</div>
</li>
<li>
<div style="text-align: justify;">Don&#8217;t ignore compiler warnings about pointer mismatch. Make sure that the right pointers are used.</div>
</li>
</ol>
<h3 style="text-align: justify;">Further actions to take</h3>
<p style="text-align: justify;">Understanding these errors and the ways to avoid them is the first step towards a better software.  The following actions will also help you avoid critical errors and increase the system stability and robustness:</p>
<ol>
<li>
<div style="text-align: justify;">Create Design Documents. They will help you with the implementation and later with debug.</div>
</li>
<li>
<div style="text-align: justify;">Hold code reviews. Sometimes, another pair of eyes will see things you missed.</div>
</li>
<li>
<div style="text-align: justify;">If you have a doubt, consult with other people.</div>
</li>
<li>
<div style="text-align: justify;">Keep the code simple and organized. &#8220;Clever&#8221; code is not appreciated.</div>
</li>
<li>
<div style="text-align: justify;">There are code inspection utilties which you can use, some are open sourced (such as <a href="http://valgrind.org/" target="_blank">Valgrind</a>), and some are commercial. These tools examine your code and provide a detailed report about potential bugs. Note that there are many false alarms, and some issues could be there on purpose.</div>
</li>
<li>
<div style="text-align: justify;">And last, but not least; Read my <a href="http://www.rt-embedded.com/" target="_blank">blog</a> :)</div>
</li>
</ol>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/IdgCtMXPUJo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/5-most-destructive-bugs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/5-most-destructive-bugs/</feedburner:origLink></item>
		<item>
		<title>Linux Memory Consumption</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/hWYNqRAOOxk/</link>
		<comments>http://www.rt-embedded.com/blog/archives/linux-memory-consumption/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 05:30:29 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[consumption]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[RAM]]></category>
		<category><![CDATA[real]]></category>
		<category><![CDATA[rt]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[top]]></category>
		<category><![CDATA[usage]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=189</guid>
		<description><![CDATA[<p style="text-align: justify;">You’ve probably heard the managers asking questions like; how much free memory does the system have? Why is the amount of free memory so low? Where did the entire RAM go?</p>
<p style="text-align: justify;">This article will provide you the right answers that will make the managers and customers smile, and understand the Linux memory [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-194" title="ddr" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/ddr.jpg" alt="" width="250" height="183" />You’ve probably heard the managers asking questions like; how much free memory does the system have? Why is the amount of free memory so low? Where did the entire RAM go?</p>
<p style="text-align: justify;">This article will provide you the right answers that will make the managers and customers smile, and understand the Linux memory consumption concept.</p>
<p style="text-align: justify;">The standard Linux utilities for memory consumption report are “<em>free</em>” and “<em>top</em>”. The “free” utility displays the amount of free and used RAM, and “top” utility also provides a per-process memory usage breakdown. These utilities may often report that the free amount of memory left in the system is rather lower than you would expect, especially after the system was up and running for a while. You may conclude that there are problems related to memory leaks, or that your available RAM is being used inefficiently or even that your system does not have enough RAM to run correctly. Whilst these issues could be real problems with your system, it doesn’t always have to be case.</p>
<p style="text-align: justify;"><span id="more-189"></span></p>
<h3 style="text-align: justify;">Linux memory consumption concept</h3>
<p style="text-align: justify;">Linux memory consumption concept is all about efficiency. The system’s RAM is a resource that is meant to be used; 100% of it (if possible), all the time (if possible).</p>
<p style="text-align: justify;">Linux utilizes unused RAM to cache data and filesystem meta-data from slower storage devices (Flash or disk) because fetching the information from the RAM is much quicker: There are no bottlenecks such as slow physical media, slow buses or device clocks, and not decompression is required.</p>
<p style="text-align: justify;">Assuming there are no memory leaks, the reason that memory report tools report low amount of free memory is because the RAM is considered to be <em>wasted</em> if it isn&#8217;t used. This concept may require some time to digest, because conventional thinking may lead to the conclusion that an efficient system is a system with a lot of free memory. This is not entirely correct. In Linux case, the kernel tries to utilize the most of the RAM to improve the system performance. Keeping the cache means that if the kernel or a task needs the same data again, there&#8217;s a good chance it will still be in the fast cache in memory.</p>
<h3 style="text-align: justify;">Getting the amount of free memory</h3>
<p style="text-align: justify;">As mentioned before, there are two standard utilities that report the amount of free memory. For RT Embedded projects, these utilities are provided by “Busybox”, and their output might be slightly different than standard distributions due to memory consumption restraints. Here’s an example for the output of “free” utility:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># free<br />
              total         used         free       shared      buffers<br />
  Mem:       119248        41524        77724            0         3392<br />
 Swap:            0            0            0<br />
Total:       119248        41524        77724</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The numbers shown are in Kilo-Bytes, meaning that this system has total memory of about 120MB, 41MB is used and 77MB is free. The buffers column displays the amount of memory that was used for filesystem meta-data (such as the filesystem tree information, file location information, etc.). System with hard disks or flash drives may also have swap memory. This snapshot was taken from a system with a single flash without a disk, and therefore, the total swap memory is 0.</p>
<p style="text-align: justify;">Here’s an example of the output from “top”, taken from Fedora distribution:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>top - 15:58:22 up 21 min,  2 users,  load average: 0.00, 0.03, 0.15<br />
Tasks: 127 total,   1 running, 125 sleeping,   0 stopped,   1 zombie<br />
Cpu(s):  0.3%us, 6.9%sy, 0.0%ni, 92.5%id, 0.0%wa, 0.3%hi, 0.0%si, 0.0%st<br />
Mem:    773720k total,   367820k used,   405900k free,    12720k buffers<br />
Swap:  1572856k total,        0k used,  1572856k free,   154052k cached<br />
  PID USER    PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND          <br />
 1899 root    20   0  9980 1124  776 S  4.3  0.1   0:15.41 kerneloops       <br />
 2706 hai     20   0  2428 1064  836 R  1.6  0.1   0:01.16 top              <br />
 2046 root    20   0 38120  18m 6720 S  0.7  2.4   0:22.82 Xorg             <br />
  130 root    15  -5     0    0    0 S  0.3  0.0   0:01.64 ata/0            <br />
  178 root    20   0     0    0    0 S  0.3  0.0   0:00.90 pdflush          <br />
 1683 root    20   0  3628 1032  916 S  0.3  0.1   0:03.05 hald-addon-stor  <br />
 2393 hai     20   0  7508 1424 1076 S  0.3  0.2   0:03.49 VBoxClient       <br />
 2658 hai     20   0  102m  20m  12m S  0.3  2.7   0:04.27 gnome-terminal   <br />
    1 root    20   0  2008  772  564 S  0.0  0.1   0:03.41 init             <br />
    2 root    15  -5     0    0    0 S  0.0  0.0   0:00.06 kthreadd         <br />
    3 root    RT  -5     0    0    0 S  0.0  0.0   0:00.00 migration/0      <br />
    4 root    15  -5     0    0    0 S  0.0  0.0   0:00.59 ksoftirqd/0      <br />
    5 root    RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0       <br />
    6 root    15  -5     0    0    0 S  0.0  0.0   0:00.27 events/0         <br />
    7 root    15  -5     0    0    0 S  0.0  0.0   0:00.21 khelper          <br />
   80 root    15  -5     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/0    <br />
   82 root    15  -5     0    0    0 S  0.0  0.0   0:00.89 kblockd/0</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The output shows similar memory reports fields as the “free” utility as well as per-proces detailed information. For the purpose of this article, the following columns are relevant:</p>
<ul style="text-align: justify;">
<li>VIRT: Virtual Image (KB). The total amount of virtual memory used by the task. It includes code, data and shared libraries as well as pages that have been swapped out.</li>
<li>RES: Resident size (KB). The non-swapped physical memory a task has used.</li>
<li>%MEM: Memory usage (RES). A task’s currently used share of available RAM.</li>
</ul>
<p style="text-align: justify;">The detailed output of “top” may interest you when you are debugging memory leaks. In this case, the leaky task Resident size (and %MEM) will rise over time. Otherwise, the output of “free” is sufficient.</p>
<h3 style="text-align: justify;">Getting more information from the proc filesystem</h3>
<p style="text-align: justify;">The kernel provides a special proc file called <em>meminfo</em>, which displays the memory consumption figures in details. The meminfo file may differ between distributions, especially between Embedded Linux and Desktop Linux. Here’s a typical output of this proc file, from the same system that was used for the “free” snapshot:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># cat /proc/meminfo<br />
MemTotal:       119248 kB<br />
MemFree:         77544 kB<br />
Buffers:          3392 kB<br />
Cached:          10260 kB<br />
SwapCached:          0 kB<br />
Active:          19612 kB<br />
Inactive:         7660 kB<br />
HighTotal:           0 kB<br />
HighFree:            0 kB<br />
LowTotal:       119248 kB<br />
LowFree:         77544 kB<br />
SwapTotal:           0 kB<br />
SwapFree:            0 kB<br />
Dirty:               0 kB<br />
Writeback:           0 kB<br />
AnonPages:       13640 kB<br />
Mapped:           5428 kB<br />
Slab:             4540 kB<br />
PageTables:       1616 kB<br />
NFS_Unstable:        0 kB<br />
Bounce:              0 kB<br />
CommitLimit:     59624 kB<br />
Committed_AS:    28588 kB<br />
VmallocTotal:   131072 kB<br />
VmallocUsed:      9296 kB<br />
VmallocChunk:   106492 kB</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;"><span style="text-decoration: underline;">The information fields which are available in meminfo proc file:</span></p>
<ul style="text-align: justify;">
<li><strong>MemTotal</strong> &#8211; Total amount of physical RAM, in kilobytes.</li>
<li><strong>MemFree</strong> &#8211; The amount of physical RAM, in kilobytes, left unused by the system.</li>
<li><strong>Buffers</strong> &#8211; The amount of physical RAM, in kilobytes, used for file buffers.</li>
<li><strong>Cached</strong> &#8211; The amount of physical RAM, in kilobytes, used as cache memory.</li>
<li><strong>Active</strong> &#8211; The total amount of buffer or page cache memory, in kilobytes, that is in active use. This is memory that has been recently used and is usually not reclaimed for other purposes.</li>
<li><strong>Inactive</strong> &#8211; The total amount of buffer or page cache memory, in kilobytes, that are free and available. This is memory that has not been recently used and can be reclaimed for other purposes.</li>
<li><strong>LowTotal</strong> and <strong>LowFree</strong> &#8211; The total and free amount of memory, in kilobytes, that is directly mapped into kernel space. The <strong>LowTotal</strong> value can vary based on the type of kernel used.</li>
<li><strong>Mapped</strong> &#8211; The total amount of memory, in kilobytes, which have been used to map devices, files, or libraries using the <strong>mmap</strong> command.</li>
<li><strong>Slab</strong> &#8211; The total amount of memory, in kilobytes, used by the kernel to cache data structures for its own use.</li>
<li><strong>Committed_AS</strong> &#8211; The total amount of memory, in kilobytes, estimated to complete the workload. This value represents the worst case scenario value, and also includes swap memory.</li>
<li><strong>PageTables</strong> &#8211; The total amount of memory, in kilobytes, dedicated to the lowest page table level.</li>
<li><strong>VMallocTotal</strong> &#8211; The total amount of memory, in kilobytes, of total allocated virtual address space.</li>
<li><strong>VMallocUsed</strong> &#8211; The total amount of memory, in kilobytes, of used virtual address space.</li>
<li><strong>VMallocChunk</strong> &#8211; The largest contiguous block of memory, in kilobytes, of available virtual address space.</li>
</ul>
<h3 style="text-align: justify;">Getting the absolute free memory number</h3>
<p style="text-align: justify;">Your manager wants to know how much free memory the system has. What he <em>really</em> wants to know actually is the <em>minimum</em> amount of memory that the <span style="text-decoration: underline;">system requires in order to function properly</span>. Let’s call this number “The absolute free memory number”. Given this number and the amount of actual installed memory, the manager also wants to know how much RAM is left for the customers’ customizations, or whether a smaller RAM device can be used in order to reduce the hardware cost (or even both…).</p>
<p style="text-align: justify;">The absolute free memory number is not only the value of <em>MemFree,</em> but the sum of the <em>MemFree</em>, <em>Buffers</em> and <em>Cached</em> fields. It means that the majority of the used memory by <em>Buffers</em> and <em>Cached</em> can be <em>reclaimed</em> in case a task will require it, while reducing the amount of <em>buffers</em> and <em>cached</em>. Using unused (clean) cache will not impact the system performance. However, degradation in the system performance will appear once starting to free <em>used</em> cache. In some point (usually in the last free 1MB, will be discussed soon) the degradation will be very noticeable and the system will become very slow. It happens because live tasks are swapped out of the RAM, and execution of commands requires flash/disk access and decompression. If even more RAM is required, the kernel will initiate the Out-Of-Memory Killer and start killing tasks that have the highest “bad” score (See the article about Linux processes for more details). In this stage, the system is not in a stable state and its functionality is not guaranteed. If your system reaches this state on a regular basis, you may either have a serious memory leak, or the actual physical RAM installed is insufficient.</p>
<h3 style="text-align: justify;">Flush and invalidate system caches</h3>
<p style="text-align: justify;">It is possible to artificially increase the value of <em>MemFree</em> (and make your manager happy) by flushing and invalidating the clean (unused) cache and filesystem meta-data. This operation is non-destructive and it does not free the currently used caches (like running processes). Prior to this operation, the “<em>sync</em>” command needs to be run first in order to make sure all cached objects are synchronized.</p>
<ul style="text-align: justify;">
<li>To free pagecache:<br />
–       <strong>echo 1 &gt; /proc/sys/vm/drop_caches</strong></li>
<li>To free dentries and inodes:<br />
–       <strong>echo 2 &gt; /proc/sys/vm/drop_caches</strong></li>
<li>To free pagecache, dentries and inodes (basically everything):<br />
–       <strong>echo 3 &gt; /proc/sys/vm/drop_caches</strong></li>
</ul>
<p style="text-align: justify;">Here is the output of the same <em>meminfo</em> file, before and after all the caches were freed:</p>
<table style="text-align: justify;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="295" valign="top">Memory status <strong>before</strong> dropping caches</td>
<td width="295" valign="top">Memory status <strong>after</strong> dropping caches</td>
</tr>
<tr>
<td width="295" valign="top"><code>MemTotal:       119248 kB<br />
MemFree:         77544 kB<br />
Buffers:          3392 kB<br />
Cached:          10260 kB<br />
SwapCached:          0 kB<br />
Active:          19612 kB<br />
Inactive:         7660 kB<br />
HighTotal:           0 kB<br />
HighFree:            0 kB<br />
LowTotal:       119248 kB<br />
LowFree:         77544 kB<br />
SwapTotal:           0 kB<br />
SwapFree:            0 kB<br />
Dirty:               0 kB<br />
Writeback:           0 kB<br />
AnonPages:       13640 kB<br />
Mapped:           5428 kB<br />
Slab:             4540 kB<br />
PageTables:       1616 kB<br />
NFS_Unstable:        0 kB<br />
Bounce:              0 kB<br />
CommitLimit:     59624 kB<br />
Committed_AS:    28588 kB<br />
VmallocTotal:   131072 kB<br />
VmallocUsed:      9296 kB<br />
VmallocChunk:   106492 kB</code></td>
<td width="295" valign="top"><code>MemTotal:       119248 kB<br />
MemFree:         85100 kB<br />
Buffers:           204 kB<br />
Cached:           6268 kB<br />
SwapCached:          0 kB<br />
Active:          16948 kB<br />
Inactive:         3144 kB<br />
HighTotal:           0 kB<br />
HighFree:            0 kB<br />
LowTotal:       119248 kB<br />
LowFree:         85100 kB<br />
SwapTotal:           0 kB<br />
SwapFree:            0 kB<br />
Dirty:               0 kB<br />
Writeback:           0 kB<br />
AnonPages:       13640 kB<br />
Mapped:           5428 kB<br />
Slab:             4164 kB<br />
PageTables:       1616 kB<br />
NFS_Unstable:        0 kB<br />
Bounce:              0 kB<br />
CommitLimit:     59624 kB<br />
Committed_AS:    28588 kB<br />
VmallocTotal:   131072 kB<br />
VmallocUsed:      9296 kB<br />
VmallocChunk:   106492 kB </code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">We can see that we gained almost 8MB in of RAM in <em>MemFree</em> field (and also in the “free” report) just by dropping the caches, and that the amount of <em>Buffers </em>and <em>Cached</em> has been reduced.</p>
<h3 style="text-align: justify;">Other kernel tunables regarding memory</h3>
<p style="text-align: justify;">The Kernel provides run-time tunables that can change its behavior in extreme cases:</p>
<ul style="text-align: justify;">
<li><strong>/proc/sys/vm/min_free_kbytes</strong>: Used to force the Linux VM to keep a minimum number of kilobytes free. This number’s value is around 1MB. The Kernel will start swapping out and killing processes in order to meet this requirement.</li>
<li><strong>/proc/sys/vm/overcommit_memory</strong>: Controls over-commit of system memory, possibly allowing processes to allocate (but not use) more memory than is actually available.</li>
</ul>
<p style="text-align: justify;"><span style="text-decoration: underline;">Resources:<br />
</span><a href="http://www.linuxinsight.com/proc_sys_vm_hierarchy.html" target="_parent">http://www.linuxinsight.com/proc_sys_vm_hierarchy.html</a><br />
<a href="https://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/en-US/Reference_Guide/s2-proc-meminfo.html" target="_parent">https://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/en-</a><a href="https://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/en-US/Reference_Guide/s2-proc-meminfo.html" target="_parent">US/Reference_Guide/s2-proc-meminfo.html</a><br />
<a href="http://wiki.linuxquestions.org/wiki/FAQ_-_Linux_problems" target="_parent">http://wiki.linuxquestions.org/wiki/FAQ_-_Linux_problems</a><br />
<a href="http://linux.die.net/man/1/top">http://linux.die.net/man/1/top</a><br />
<a href="http://linux.die.net/man/1/free">http://linux.die.net/man/1/free</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/hWYNqRAOOxk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/linux-memory-consumption/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/linux-memory-consumption/</feedburner:origLink></item>
		<item>
		<title>Linux Loadable Kernel Modules</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/a45MTEuPBK8/</link>
		<comments>http://www.rt-embedded.com/blog/archives/linux-loadable-kernel-modules/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 20:21:01 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[dlm]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[lkm]]></category>
		<category><![CDATA[loadable]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[real]]></category>
		<category><![CDATA[rt]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=179</guid>
		<description><![CDATA[<p style="text-align: justify;">Linux loadable Kernel Modules (also referred as LKMs, DLMs (dynamically loaded modules), or just Kernel modules) can be viewed as “kernel extensions”. They are programs that run in the Kernel’s execution environment and extend its basic functionality.</p>
<p style="text-align: justify;">Some more facts about LKMs:</p>

Most modules in the Kernel tree provide: device drivers, networking support, [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-183" title="loadble-module" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/loadble-module.jpg" alt="" width="250" height="188" />Linux loadable Kernel Modules (also referred as LKMs, DLMs (dynamically loaded modules), or just Kernel modules) can be viewed as “kernel extensions”. They are programs that run in the Kernel’s execution environment and extend its basic functionality.</p>
<p style="text-align: justify;">Some more facts about LKMs:</p>
<ul style="text-align: justify;">
<li>Most modules in the Kernel tree provide: device drivers, networking support, filesystem support, and more.</li>
<li>Modules can be loaded and removed in runtime, without needing to neither recompile nor reboot the system.</li>
<li>Modules are <em>not</em> user space programs. They can access the Kernel’s API directly, but they can cause a system crash as well (A kernel panic).</li>
<li>A module can overwrite kernel system calls (replace system calls with its own implementation).</li>
<li>A module can export API’s as well.</li>
<li>Some drivers in the Kernel can be compiled as an Integral mode or module mode.</li>
<li>A module source code does not have to reside in the Kernel tree. It could reside outside of it as well.</li>
<li>The Loadable Kernel module extension name is “<em>.ko</em>”.</li>
<li>The compiled modules reside in the filesystem under the /lib/modules directory.</li>
</ul>
<p style="text-align: justify;"><span id="more-179"></span></p>
<h3 style="text-align: justify;">Module utilities</h3>
<p style="text-align: justify;">Module utilities are a set of tools that provide module-related services. The utilities include:</p>
<ul style="text-align: justify;">
<li>insmod: Insert (load) a module in runtime. It can also pass run time configuration parameters to the module.</li>
<li>rmmod: Remove (unload) a module in runtime.</li>
<li>lsmod: List all active modules and their usage count.</li>
<li>modprobe: Handle the loading of modules and their dependencies.</li>
</ul>
<h3 style="text-align: justify;">Kernel built-in modules</h3>
<p style="text-align: justify;">Some drivers in the Kernel can be compiled as an Integral mode or module mode. These drivers are marked with 3-state mode variables: <em>Disabled</em>, <em>Modulized</em>, and <em>Enabled</em>. In order to configure the driver to be compiled in your desired mode, you need to activate the kernel’s configuration menu (read the kernel configuration article for more details how to do it) and find the appropriate driver configuration option.</p>
<p style="text-align: justify;">In the following example, you can see a screen shot of an I2C driver in each of the 3 states:</p>
<p style="text-align: justify;">Switching between the modes is done either by pressing &lt;SPACE&gt;, or &lt;Y&gt; to enable, &lt;M&gt; to modulize and &lt;N&gt; to disable.</p>
<p style="text-align: justify;">In case a driver was selected to be modulized, it will not be built when the kernel is built, and an explicit make call is required in order to build them. The first command is “make modules”, which builds all the modules, and the second command is “make modules_install INSTALL_MOD_PATH=/&lt;your filesystem path&gt;”, which installs the modules in the target’s filesystem.</p>
<h3 style="text-align: justify;">Module licensing</h3>
<p style="text-align: justify;">The linux/module.h header file defines the following licensing options:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><code>/*<br />
* The following license idents are currently accepted as indicating free<br />
* software modules<br />
*<br />
*  "GPL"               [GNU Public License v2 or later]<br />
*  "GPL v2"            [GNU Public License v2]<br />
*  "GPL and additional rights" [GNU Public License v2 rights and more]<br />
*  "Dual BSD/GPL"          [GNU Public License v2<br />
*                   or BSD license choice]<br />
*  "Dual MIT/GPL"          [GNU Public License v2<br />
*                   or MIT license choice]<br />
*  "Dual MPL/GPL"          [GNU Public License v2<br />
*                   or Mozilla license choice]<br />
*<br />
* The following other idents are available<br />
*<br />
*  "Proprietary"           [Non free products]<br />
*<br />
* There are dual licensed components, but when running with Linux it is the<br />
* GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL<br />
* is a GPL combined work.<br />
*<br />
* This exists for several reasons<br />
* 1.   So modinfo can show license info for users wanting to vet their setup is free<br />
* 2.   So the community can ignore bug reports including proprietary modules<br />
* 3.   So vendors can do likewise based on their own policies<br />
*/</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">According to GNU GPLv2, since the module runs in the same memory space as the kernel itself, and the kernel is licensed under GPLv2, then the module is considered to be a “combined work”, therefore, any non-GPL license is not a valid license. When running in the kernel space, the GPLv2 license applies even if the license is a dual license. Theoretically, you can ignore the module licensing or define it as “Proprietary”, but this will cause the following issues (other than the legal issues):</p>
<ul style="text-align: justify;">
<li>The kernel will display a warning that this module taints the kernel. The open source communities ignore error reports or support requests by companies who taint the kernel.</li>
<li>Non-GPL licensed modules are denied from calling some of the Kernel API which is export by <em>EXPORT_SYMBOL_GPL</em> macro). Therefore, when trying to load them, they will fail to link and load in case they are calling one of these functions.</li>
</ul>
<h3 style="text-align: justify;">Writing your own Loadable Kernel Module – Kernel configuration</h3>
<p style="text-align: justify;">A few configuration options must be enabled in order to support Loadable Kernel Modules in your Kernel:</p>
<ul style="text-align: justify;">
<li>CONFIG_MODULES: Enables the module handling ability in the kernel, therefore, when modules are used, this option must be enabled.</li>
<li>CONFIG_MODULE_UNLOAD: Enables the module unloading ability in the kernel. Theoretically, modules can be removed from the system, and their resources freed. However, some modules do not support it correctly, and others can’t be removed because they are used by other drivers or programs. It is recommended to enable this option anyway.</li>
<li>CONFIG_MODULE_FORCE_UNLOAD: Allows the kernel to forcibly remove modules upon request, even if the kernel believes it is not safe. This could result by other drivers keeping reference or using this module. This option is mainly used for debug and should be disabled.</li>
<li>CONFIG_MODVERSIONS: Usually, each module is compiled for the kernel you are using. In case a precompiled module is supposed to run in another kernel environment, this option adds extra kernel information inside the module which could make it possible to detect incompatibilities. If this scenario is not a part of your setup, you can disable this option.</li>
<li>CONFIG_MODULE_SRCVERSION_ALL: Calculates a checksum from all the module’s sources, and adds it in the module’s binary. This option is used to detect version variations even if the module’s version string is the same.</li>
<li>CONFIG_KMOD: Allows the kernel to load its modules automatically, upon request. If you manage your modules insertion, then this option could be disabled, otherwise, it is recommended to enable it.</li>
</ul>
<h3 style="text-align: justify;">Writing your own Loadable Kernel Module – Source code</h3>
<p style="text-align: justify;">Your module can be made of one or more source files which perform the task it was made for. However, one source file must be used for interfacing with the Kernel. Let’s call this file; the “main file”. The main file:</p>
<ul style="text-align: justify;">
<li>Must include linux/kernel.h, linux/module.h and linux/init.h header files.</li>
<li>Must contain an init and an exit functions.</li>
<li>Should declare the module licensing.</li>
<li>Can declare author’s name and module description.</li>
<li>Can declare initialization parameters.</li>
<li>Can export API to be used by other modules.</li>
</ul>
<p style="text-align: justify;">The <em>init</em> function initializes the module’s data, allocates resources and registers the module in the kernel (if necessary). It can also print a message in case it succeeds or fails. Returning 0 means that the init process was successful. Otherwise, a negative value means a failure which will cause the module to be unloaded and <em>insmod</em> command to fail.</p>
<p style="text-align: justify;">The <em>exit</em> function cleans up, frees the resources and unregisters the module from the kernel. The exit function does not return any value.</p>
<p style="text-align: justify;">It is possible to declare variables to be initialized by <em>insmod</em>. Parameter types are: byte, short, ushort, int, uint, long, ulong and charp. It is also possible to define an array of each type (except charp which is already an array).</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><code>/* Declare the variables */<br />
<strong>static</strong> <strong>char</strong> rte_byte = 128;<br />
<strong>static</strong> <strong>short</strong> <strong>int</strong> rte_short = 1000;<br />
<strong>static</strong> <strong>int</strong> rte_int = 128 * 1024;<br />
<strong>static</strong> <strong>long</strong> <strong>int</strong> rte_long = 128 * 1024 * 1024;<br />
<strong>static</strong> <strong>char</strong> *rte_string = "RT Embedded";<br />
<strong>static</strong> <strong>int</strong> rte_int_array[4] = { 0, 1, 2, 3 };<br />
<strong>static</strong> <strong>int</strong> rte_array_len = 0; </code><code>/* Setup modules parameters */<br />
<strong>module_param</strong>(rte_byte, byte, 0000);<br />
<strong>MODULE_PARM_DESC</strong>(rte_byte, "A byte");<br />
<strong>module_param</strong>(rte_short, <strong>short</strong>, 0000);<br />
<strong>MODULE_PARM_DESC</strong>(rte_short, "A short integer");<br />
<strong>module_param</strong>(rte_int, <strong>int</strong>, 0000);<br />
<strong>MODULE_PARM_DESC</strong>(rte_int, "An integer");<br />
<strong>module_param</strong>(rte_long, <strong>long</strong>, S_IRUSR);<br />
<strong>MODULE_PARM_DESC</strong>(rte_long, "A long integer");<br />
<strong>module_param</strong>(rte_string, charp, 0000);<br />
<strong>MODULE_PARM_DESC</strong>(rte_string, "A string");<br />
<strong>module_param_array</strong>(rte_int_array, <strong>int</strong>, &amp;rte_array_len, 0000);<br />
<strong>MODULE_PARM_DESC</strong>(rte_int_array, "An array of integers"); </code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Here’s an example for a basic module:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="code">/*
 *  rte_lkm.c - Linux Loadable Kernel Module example
 */

#include &lt;linux/module.h&gt;
#include &lt;linux/kernel.h&gt; 
#include &lt;linux/init.h&gt;

/* Macro definitions */
#define LKM_AUTHOR          "Hai Shalom, http://www.rt-embedded.com" 
#define LKM_DESCRIPTION     "An example LKM"

static int __init init_lkm(void)
{
	printk(KERN_INFO “Hello, world!\n”);
	return 0;
}

int rte_lkm_func( int i )
{
	printk( “%s: i=%d\n”, __FUNCTION__, i );
	return 0;
}

static void __exit exit_lkm(void)
{
	printk(KERN_INFO “Goodbye, world…\n”);
}

module_init(init_lkm);
module_exit(exit_lkm);

/*
* Define the module’s license. Important!
*/

MODULE_LICENSE(“GPL”);

/*
* Optional definitions
*/

MODULE_AUTHOR(LKM_AUTHOR);  /* The author’s name */
MODULE_DESCRIPTION(LKM_DESCRIPTION);    /* The module description */

/*
* API the module exports to other modules
*/

EXPORT_SYMBOL(rte_lkm_func);</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">This module will write “Hello, world!” after the <em>insmod</em> command, and “Goodbye, world&#8230;” after the <em>rmmod</em> command.<em> </em></p>
<h3 style="text-align: justify;">Writing your own Loadable Kernel Module – Makefile</h3>
<p style="text-align: justify;">There is no need to understand the Make syntax in order to build a kernel module. All you need is a 5 line Makefile and it works. However, there are some options you may want to explore, and later, I will provide a simple template which addresses them nicely.</p>
<p style="text-align: justify;">The make process is divided to two stages. In the first stage, the Makefile calls the Kernel build system in order to build the module. In the second stage, the build system uses the same Makefile in order to configure and build the required module. Therefore, there should be two parts in the Makefile, to handle each stage. The kernel provides a variable to detect if we are in the first or second stage. The variable name is KERNELRELEASE. Therefore, we shall use it to distinguish between the two parts. In the first part, we need to provide all the targets required in our build system, and call the kernel’s build system. In the second part, we define all the module’s parameters which are required in order to build the module. Please read the example Makefile. The remarks explain each line.</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><code>################################################################################<br />
#<br />
# Copyright(c) 2010, Hai Shalom, <a href="http://www.rt-embedded.com">http://www.rt-embedded.com</a><br />
#<br />
# This program is free software; you can redistribute it and/or modify it<br />
# under the terms of the GNU General Public License as published by the Free<br />
# Software Foundation; either version 2 of the License, or (at your option)<br />
# any later version.<br />
#<br />
################################################################################<br />
# Target module name<br />
TARGET := rtelkm<br />
</code><code><br />
# Kernel directory - This works only for the host.<br />
# The target kernel is different, and it can't be detected.<br />
ifndef KERNEL_DIR<br />
KERNEL_DIR := /lib/modules/$(shell uname -r)/build<br />
endif# Target development home directory<br />
ifndef TARGET_HOME<br />
TARGET_HOME := /usr/target<br />
endif# File system path, where to install the module<br />
ifndef FILESYSTEM_PATH<br />
FILESYSTEM_PATH := $(TARGET_HOME)/project/fs<br />
endif</code><code># Include export directory<br />
ifndef TARGET_INCLUDE_PATH<br />
TARGET_INCLUDE_PATH := $(TARGET_HOME)/$(TARGET_INCLUDE_PATH)<br />
endif</code><br />
<code><br />
# Include directories<br />
INCLUDES := -I$(CURDIR)/../include \<br />
-I$(KERNEL_DIR)/include/linux \<br />
-I$(KERNEL_DIR)/include/asm \<br />
-I$(TARGET_INCLUDE_PATH)</code><br />
<code><br />
export EXTRA_CFLAGS += $(INCLUDES)<br />
</code><code><br />
# Module extra compilation flags<br />
EXTRA_CFLAGS +=<br />
</code><code><br />
# Installation module directory: fill in the driver type<br />
export INSTALL_MOD_DIR := /drivers/&lt;type of driver: net, char, etc.&gt;<br />
</code><code><br />
# Filesystem path<br />
export INSTALL_MOD_PATH=$(FILESYSTEM_PATH)<br />
</code><code><br />
# Kernel module compilation – part 2<br />
ifneq ($(KERNELRELEASE),)<br />
obj-m += $(TARGET).o<br />
</code><code><br />
# Target objects - add as many as required<br />
$(TARGET)-objs := \<br />
rte_lkm.o<br />
else # Makefile targets – part 1<br />
</code><code><br />
all: build install<br />
</code><code><br />
build: #note that the MAKE commands must start with a &lt;TAB&gt;<br />
@$(MAKE) -C $(KERNEL_DIR) M=`pwd` modules<br />
</code><code><br />
install:<br />
@$(MAKE) -C $(KERNEL_DIR) M=`pwd` modules_install<br />
</code><code><br />
uninstall:<br />
@find $(FILESYSTEM_PATH)/lib/modules/ -name $(TARGET).ko | xargs rm -rf<br />
</code><code><br />
clean:<br />
@$(MAKE) -C $(KERNEL_DIR) M=`pwd` clean<br />
@rm -f Module.symvers<br />
</code><code><br />
endif</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Once your Makefile is complete, all you need to do in order to compile and install your module is to run “make” in the module’s directory.</p>
<p style="text-align: justify;"><span style="text-decoration: underline;">Resources:<br />
</span><a href="http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html">http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html</a><br />
<a href="http://en.wikipedia.org/wiki/Loadable_kernel_module">http://en.wikipedia.org/wiki/Loadable_kernel_module</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/a45MTEuPBK8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/linux-loadable-kernel-modules/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/linux-loadable-kernel-modules/</feedburner:origLink></item>
		<item>
		<title>Resolving/Debugging user space crashes and segmentation faults</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/f_4kF3lZwpY/</link>
		<comments>http://www.rt-embedded.com/blog/archives/resolving-crashes-and-segmentation-faults/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 19:09:52 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[crash]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[fault]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[pcd]]></category>
		<category><![CDATA[real]]></category>
		<category><![CDATA[resolve]]></category>
		<category><![CDATA[rt]]></category>
		<category><![CDATA[segmentation]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=156</guid>
		<description><![CDATA[What is a segmentation fault?
<p style="text-align: justify;">Segmentation fault is the most common error condition where your program tries to access either an invalid memory location, or a memory location it is not allowed to access.</p>
<p style="text-align: justify;">A few examples for this could be:</p>

Dereferencing a NULL pointer.
Dereferencing an uninitialized pointer.
Accessing memory with a wrong alignment.
Writing to [...]]]></description>
				<content:encoded><![CDATA[<h3 style="text-align: justify;"><img class="alignright size-full wp-image-161" title="crash" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/03/crash.jpg" alt="" width="250" height="188" />What is a segmentation fault?</h3>
<p style="text-align: justify;">Segmentation fault is the most common error condition where your program tries to access either an invalid memory location, or a memory location it is not allowed to access.</p>
<p style="text-align: justify;">A few examples for this could be:</p>
<ul style="text-align: justify;">
<li>Dereferencing a NULL pointer.</li>
<li>Dereferencing an uninitialized pointer.</li>
<li>Accessing memory with a <a href="http://www.rt-embedded.com/blog/archives/resolving-alignment-traps/" target="_blank">wrong alignment</a>.</li>
<li>Writing to a read only area.</li>
<li>Writing or reading beyond program allocated resources (buffer overflow).</li>
<li><a href="http://www.rt-embedded.com/blog/archives/resolving-memory-corruption/" target="_blank">Memory corruption</a>/overrun.</li>
</ul>
<p style="text-align: justify;"><span id="more-156"></span></p>
<p style="text-align: justify;">For our examples, let’s use the following function that simply writes the character ‘R’ to a given location provided by <em><span style="text-decoration: underline;">ptr</span></em><span style="text-decoration: underline;">:</span></p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre><strong>void</strong> <strong>rte_test_ptr</strong>( <strong>char</strong> *ptr )
{
    *ptr = 'R';
}</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">All the following code examples will cause the program to terminate and the message “<em>Segmentation Fault</em>” to appear on the console.</p>
<p style="text-align: justify;">Example 1: Dereference a NULL pointer:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre><strong>int</strong> <strong>main</strong>( <strong>int</strong> argc , <strong>char</strong> *argv[] )
{
    <strong>rte_test_ptr</strong>(<strong>NULL</strong>);
    <strong>return</strong> 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Example 2: Write to a read-only location:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre><strong>char</strong> *ro_ptr = "RT-Embedded";</pre>
<pre><strong>int</strong> <strong>main</strong>( <strong>int</strong> argc , <strong>char</strong> *argv[] )
{
    <strong>rte_test_ptr</strong>(ro_ptr);
    <strong>return</strong> 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Example 3: Dereference an uninitialized pointer:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre><strong>int</strong> <strong>main</strong>( <strong>int</strong> argc , <strong>char</strong> *argv[] )
{
    <strong>char</strong> *uninit_ptr;</pre>
<pre>    <strong>rte_test_ptr</strong>(uninit_ptr);
    <strong>return</strong> 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<h3 style="text-align: justify;">What other crash types we usually see?</h3>
<p style="text-align: justify;">Another common error is alignment trap (also referred as <em>bus error</em>). A bus error occurs when the CPU tries to access a 32-bit or 16-bit variable in an unaligned memory address.</p>
<p style="text-align: justify;">See the article about <a href="http://www.rt-embedded.com/blog/archives/resolving-alignment-traps/">alignment traps</a> for more details.</p>
<p style="text-align: justify;">Another type of crash is caused be an illegal instruction. Normally, this cannot happen because the compiler generates only legal instructions. However, in case your program uses callbacks, this scenario could happen in case the program is trying to jump to an uninitialized callback address.</p>
<p style="text-align: justify;">The last type of error which is covered in this article is the Floating point exception. If your program performed an illegal arithmetic operation (such as division by zero), the program will terminate and the message “<em>Floating point exception</em>” will appear on the console. Although the words “Floating point” are used, this type of error also refers to errors cause by arithmetic operations with integers.</p>
<h3 style="text-align: justify;">What happens when the program performs an illegal operation?</h3>
<p style="text-align: justify;">When one of the above happens, the kernel sends a <em>fault signal</em> (or an exception) to the program. A fault signal is a special signal that tells the program that something bad has happened, and it needs to be terminated. There are four fault signals in the system:</p>
<ul style="text-align: justify;">
<li><strong>SIGSEGV</strong>: In case of Segmentation fault</li>
<li><strong>SIGBUS</strong>: In case of Alignment trap</li>
<li><strong>SIGILL</strong>: In case of Illegal instruction</li>
<li><strong>SIGFPE</strong>: In case of Illegal arithmetic operation.</li>
</ul>
<p style="text-align: justify;">Each signal needs to be handled and acknowledged. There are also the signals SIGQUIT and SIGINT which are not caused by an error, but also must be handled because the program needs to be terminated. In case the program did not install such handlers, there are default handlers for the fault signals that just write a short error message; &#8220;<strong>Segmentation fault</strong>&#8220;, and then terminate the program. Unfortunately, this is insufficient information in order to find and fix the bug, and the system may become unstable or unusable once the program was terminated. Furthermore, this short error message might get lost within the many other messages which appear on the console while the system is running. Note that this behavior is usually unaccepted in a project which is meant for mass production (the user must manually power cycle the unit in this case).</p>
<h3 style="text-align: justify;">Enabling core dump</h3>
<p>During debug, it is possible to enable core dump, which can be parsed off line on a host machine using gdb. The core dump contains useful information about the last known whereabouts of the program. Further information is available in the <a href="http://www.rt-embedded.com/blog/archives/enabling-core-dumps-in-embedded-systems/" target="_blank">Enabling core dumps</a> post.</p>
<h3 style="text-align: justify;">Handling fault signals, and extracting information from them</h3>
<p style="text-align: justify;">In each system, it is crucial to install exception handlers for these fault signals, because you can never know when a program might crash (trust me; usually it happens in the customer’s premises or during some qualification tests). The <a href="http://www.rt-embedded.com/blog/archives/pcd/">PCD</a> provides an easy and convenient way for registering its exception handlers, which provide a lot of useful debug information (See the next paragraph for more details). In case you want to write your own exception handler, you must consider the following issues:</p>
<ol style="text-align: justify;">
<li>The exception can occur anytime, therefore, the handler must be written carefully.</li>
<li>Many ANSI-C functions are not signal-safe (including <em>printf( )</em>…), therefore, your signal handler must use only signal safe functions. A list of signal safe functions can be found <a href="http://linux.die.net/man/2/signal">here</a>.</li>
<li>Don’t try to call your <em>main( )</em> function from your signal handler. It may appear that you’ve revived your program, but it is unclear what will be the consequences, because your program’s stack or heap could have been corrupted.</li>
<li>Your exception handler must call <em>exit( )</em> once it has completed its work.</li>
</ol>
<p style="text-align: justify;">The next step is to write the exception handler. There are two types of exception handlers you can use; a standard exception handler which receives only the signal number, or an enhanced exception handler, which can also receive some more information, which may defer between architectures: a pointer to a <em>siginfo_t</em> and a pointer to a <em>ucontext_t</em> (casted to <em>void *</em>).  Let’s take a look at the <em>sigaction</em> structure, which is defined in signal.h:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>/* Structure describing the action to be taken when a signal arrives.  */</pre>
<pre><strong>struct</strong> sigaction
{
    /* Signal handler.  */</pre>
<pre>    <strong>union </strong>    {
         /* Used if SA_SIGINFO is not set.  */
         /* Type of a signal handler.  */
<strong>         </strong><strong>typedef</strong> <strong>void</strong> (*sa_handler) (<strong>int</strong>);</pre>
<pre>         /* Used if SA_SIGINFO is set.  */
         <strong>void</strong> (*sa_sigaction) (<strong>int</strong>, siginfo_t *, <strong>void</strong> *);</pre>
<pre>    }  __sigaction_handler;</pre>
<pre>    /* Additional set of signals to be blocked.  */
    __sigset_t sa_mask;</pre>
<pre>    /* Special flags.  */
    <strong>int</strong> sa_flags;</pre>
<pre>    /* Restore handler.  */
    <strong>void</strong> (*sa_restorer) (<strong>void</strong>);</pre>
<pre>  };</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">In case we want a simple handler, we can specify our handler in the <em>sa_handler</em> field, and in case we want the enhanced handler, we define SA_SIGINFO flag in <em>sa_flags</em> and specify our handler in <em>sa_sigaction</em>. The following macros can be used for registering exception handlers:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>#define <strong>SETSIG</strong>(sa, sig, func) \
    {    <strong>memset</strong>( &amp;sa, 0, <strong>sizeof</strong>( <strong>struct</strong> sigaction ) ); \
         sa.sa_handler = func; \
         sa.sa_flags = SA_RESTART; \
         <strong>sigaction</strong>(sig, &amp;sa, 0L); \
    }</pre>
<pre>#define <strong>SETSIGINFO</strong>(sa, sig, func) \
    {    <strong>memset</strong>( &amp;sa, 0, <strong>sizeof</strong>( <strong>struct</strong> sigaction ) ); \
         sa.sa_sigaction = func; \
         sa.sa_flags = SA_RESTART | SA_SIGINFO; \
         <strong>sigaction</strong>(sig, &amp;sa, 0L); \
    }</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The first macro is for the simple handler and the second macro is for the enhanced handler. The macros use the <em>sigaction( )</em> function.</p>
<p style="text-align: justify;">Once your handler has been activated, it means that an illegal operation has occurred. If you enabled the enhanced handler, it is possible to read the <em>siginfo_t</em> structure which contains information about the crash. This structure is large and contains a number of unions. I would like to mention only the most relevant members:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre><strong>typedef</strong> <strong>struct</strong> siginfo
{
    <strong>int</strong> si_signo;   /* Signal number.  */
    <strong>int</strong> si_errno;   /* If non-zero, an errno value associated with this signal,
                       as defined in &lt;errno.h&gt;.  */
    <strong>int</strong> si_code;    /* Signal code.  */</pre>
<pre>    /* SIGILL, SIGFPE, SIGSEGV, SIGBUS.  */
<strong> struct </strong>{
       <strong>void</strong> *si_addr;  /* Faulting insn/memory ref.  */</pre>
<pre>    } _sigfault;</pre>
<pre>} siginfo_t;</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">This structure provides information about the signal number and its code (See the <em>siginfo.h</em> header code for textual information about each signal code), the last known <em>errno</em> and the address which caused the error condition in the <em>si_addr</em> pointer. This address in this case is the address that the CPU was trying to access and not an address of the instruction (which is held by the Program Counter). The <em>ucontext_t </em>structure contains a list of the core register sets and their last known values (such as the Program Counter). It varies between architectures.</p>
<h3 style="text-align: justify;">What do we do with this information?</h3>
<p style="text-align: justify;">We can now understand the nature and root cause of the error. We can also understand what the fault address that caused the error is. From the <em>ucontext_t</em> structure, we can extract the Program Counter (for the last known execution address) and the Link register (for the return address in ARM architecture). Theoretically speaking, we could print this information to the console using <em>printf( )</em> function. However, if you remember, this function is not signal-safe, and therefore, it cannot be used, although I have seen some signal handlers that do use <em>printf</em> to print it. So how can we do it right? The solution is to have a crash daemon which listens on a socket. We can use our signal handler to send this information to the socket, and the daemon will print it for us. The socket API is signal safe and could be used without a problem. <em>There is </em>and easier way to do it, and I’ll present it next. Let’s say for now that we extracted the value of the Program Counter from the <em>ucontext_t </em>structure, and that the CPU was executing the instruction in address 0&#215;8548. Assuming we compiled our program with debug symbols, we can use the <em>objdump</em> utility and ask it to show the mixed assembly and C code (using the –S option) around this address. Here’s a snippet from the output:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre># armeb-linux-uclibceabi-objdump -S segv</pre>
<pre>...
00008544 &lt;rte_test_ptr&gt;:
#include &lt;stdio.h&gt;
#include &lt;pcdapi.h&gt;</pre>
<pre>void rte_test_ptr( char *ptr )
{
    *ptr = 'R';
    8544:       e3a03052        mov     r3, #82 ; 0x52
    <strong><span style="color: #ff0000;">8548:       e5c03000        strb    r3, [r0] </span></strong>}
    854c:       e12fff1e        bx      lr
...</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The line in red caused the Segmentation Fault crash. Object file with symbols will be presented in mixed-mode of C and assembly. In case the object was compiled without symbols, the only information we could extract near the address is the function name.</p>
<p style="text-align: justify;">It is also possible to extract the file name and line number from the address number using the <em>addr2line </em>utility. Here&#8217;s an example:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre># armeb-linux-uclibceabi-addr2line -e segv -f 8548
rte_test_ptr
/home/hai/rte/segv.c:7</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">There are cases where the fault address is not inside the program’s code, but inside a shared library’s code. A shared library code address will be mapped much higher than the area of the program code. We could use the <em>maps</em> file in the proc filesystem to determine where the last command came from. However, in order to print it, we’ll have to reboot the system because once the program has crashed, its proc entry is already gone. After we have rebooted and figured out the new PID, we need to print its map file by the command “cat /proc/&lt;PID&gt;/maps”. Here’s an example output:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre># cat /proc/204/maps
00008000-00009000 r-xp 00000000 1f:07 59         /usr/sbin/segv
00010000-00011000 rw-p 00000000 1f:07 59         /usr/sbin/segv
04000000-04005000 r-xp 00000000 1f:06 231        /lib/ld-uClibc-0.9.29.so
04005000-04007000 rw-p 04005000 00:00 0
0400c000-0400d000 r--p 00004000 1f:06 231        /lib/ld-uClibc-0.9.29.so
0400d000-0400e000 rw-p 00005000 1f:06 231        /lib/ld-uClibc-0.9.29.so
0400e000-04023000 r-xp 00000000 1f:06 175        /lib/libticc.so
04023000-0402a000 ---p 04023000 00:00 0
0402a000-0402c000 rw-p 00014000 1f:06 175        /lib/libticc.so
0402c000-04067000 r-xp 00000000 1f:06 200        /lib/libuClibc-0.9.29.so
04067000-0406e000 ---p 04067000 00:00 0
0406e000-0406f000 r--p 0003a000 1f:06 200        /lib/libuClibc-0.9.29.so
0406f000-04070000 rw-p 0003b000 1f:06 200        /lib/libuClibc-0.9.29.so
04070000-04075000 rw-p 04070000 00:00 0
04075000-0407f000 r-xp 00000000 1f:06 137        /lib/libgcc_s.so.1
0407f000-04086000 ---p 0407f000 00:00 0
04086000-04087000 rw-p 00009000 1f:06 137        /lib/libgcc_s.so.1
0ece0000-0ecf5000 rwxp 0ece0000 00:00 0          [stack]</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Look for the “<strong>x</strong>” symbol in the permission column for code sections. In this example, possible Program counter locations could theoretically reside in the ranges of 0&#215;8000-0&#215;9000, 0&#215;4000000-0&#215;4005000, 0x400e000-0&#215;4023000, 0x402c000-0&#215;4067000, and 0&#215;4075000-0x407f000. Matching the Program Counter and Link register to one of these ranges will result with the faulty code section. We’ll see an example later.</p>
<p style="text-align: justify;">In this example, we can see what the last executed command was, but not cause of the problem. The signal info structure also contains the signal code and the registers which could help us figure out what is the root cause. How can we extract this information easily? Well, just continue to read.<em> </em></p>
<h3 style="text-align: justify;">How can PCD help debugging, resolving and preventing crashes?</h3>
<p style="text-align: justify;">When a program crashes due to an exception, it will be terminated once the error message is displayed. This crash will not trigger any recovery action, and your system will probably because unstable or unusable. The <a href="http://www.rt-embedded.com/blog/archives/pcd/" target="_blank">PCD</a> can help here in two fields:</p>
<p style="text-align: justify;">Enhanced debugging capabilities and system recovery. Once registering to the PCD exception handlers, they will provide more information about this crash, including the Program counter, Link register (return address), all the other registers, last value of <em>errno</em> and the maps file of the process, right before it was terminated, without the need to reboot the system. The latter will help you analyze the location of the error just be looking at the PCD’s error report. It will also trigger a recovery action once the crash was detected, and return the system to functional mode. The crash information is also saved on a non-volatile storage for later/offline analysis. Let’s take the piece of code from example 2, and instrument it with the PCD exception handlers (See how easily it is done):</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>#include &lt;stdio.h&gt;
#include &lt;pcdapi.h&gt;</pre>
<pre><strong>void</strong> <strong>rte_test_ptr</strong>( <strong>char</strong> *ptr )
{
    *ptr = 'R';
}</pre>
<pre><strong>char</strong> *ro_ptr = "RT-Embedded";</pre>
<pre><strong>int</strong> <strong>main</strong>( <strong>int</strong> argc , <strong>char</strong> *argv[] )
{
    /* Register to PCD's exception handlers */
    <strong>PCD_API_REGISTER_EXCEPTION_HANDLERS</strong>();</pre>
<pre>    /* Crash test */
    <strong>rte_test_ptr</strong>(ro_ptr);</pre>
<pre>    <strong>printf</strong>(ro_ptr);</pre>
<pre>    <strong>return</strong> 0;
}</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Let’s configure a simple PCD rule to start an monitor a program:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre><strong>RULE</strong> = TEST_SIGSEGV
<strong>START_COND</strong> = <strong>NONE </strong><strong>COMMAND</strong> = /usr/sbin/segv
<strong>SCHED</strong> = <strong>NICE</strong>,0
<strong>DAEMON</strong> = <strong>YES </strong><strong>END_COND</strong> = <strong>NONE </strong><strong>END_COND_TIMEOUT</strong> = -1
<strong>FAILURE_ACTION</strong> = <strong>REBOOT </strong><strong>ACTIVE</strong> = <strong>YES</strong></pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Here is the output on the console once PCD has started this rule. Note that the selected recovery action here was “Reboot”, that’s why the system is rebooting right after the crash. Pay attention to the bolded red line:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>pcd: Starting process /usr/sbin/segv (Rule TEST_SIGSEGV).
pcd: Rule TEST_SIGSEGV: Success (Process /usr/sbin/segv (204)).</pre>
<pre>**************************************************************************
**************************** Exception Caught ****************************
**************************************************************************</pre>
<pre>Signal information:</pre>
<pre>Time: Thu Jan  1 00:00:12 1970
Process name: /usr/sbin/segv
PID: 204
Fault Address: 0x00008590
Signal: Segmentation fault
Signal Code: Invalid permissions for mapped object
Last error: Success (0)
Last error (by signal): 0</pre>
<pre>ARM registers:</pre>
<pre>trap_no=0x0000000e
error_code=0x0000081f
oldmask=0x00000000
r0=0x00008590
r1=0x0ecf4ba4
r2=0x00000000
r3=0x00000052
r4=0x00010690
r5=0x00000000
r6=0x0000846c
r7=0x00008418
r8=0x00000000
r9=0x00000000
r10=0x00000000
fp=0x00000000
ip=0x00000000
sp=0x0ecf4cf0
lr=0x0000856c
pc=0x00008548
cpsr=0x40000010
fault_address=0x00008590</pre>
<pre>Maps file:</pre>
<pre>00008000-00009000 r-xp 00000000 1f:07 59         /usr/sbin/segv
00010000-00011000 rw-p 00000000 1f:07 59         /usr/sbin/segv
04000000-04005000 r-xp 00000000 1f:06 231        /lib/ld-uClibc-0.9.29.so
04005000-04007000 rw-p 04005000 00:00 0
0400c000-0400d000 r--p 00004000 1f:06 231        /lib/ld-uClibc-0.9.29.so
0400d000-0400e000 rw-p 00005000 1f:06 231        /lib/ld-uClibc-0.9.29.so
0400e000-04023000 r-xp 00000000 1f:06 175        /lib/libticc.so
04023000-0402a000 ---p 04023000 00:00 0
0402a000-0402c000 rw-p 00014000 1f:06 175        /lib/libticc.so
0402c000-04067000 r-xp 00000000 1f:06 200        /lib/libuClibc-0.9.29.so
04067000-0406e000 ---p 04067000 00:00 0
0406e000-0406f000 r--p 0003a000 1f:06 200        /lib/libuClibc-0.9.29.so
0406f000-04070000 rw-p 0003b000 1f:06 200        /lib/libuClibc-0.9.29.so
04070000-04075000 rw-p 04070000 00:00 0
04075000-0407f000 r-xp 00000000 1f:06 137        /lib/libgcc_s.so.1
0407f000-04086000 ---p 0407f000 00:00 0
04086000-04087000 rw-p 00009000 1f:06 137        /lib/libgcc_s.so.1
0ece0000-0ecf5000 rwxp 0ece0000 00:00 0          [stack]</pre>
<pre>**************************************************************************
pcd: Error: Process /usr/sbin/segv (204) exited unexpectedly (Rule TEST_SIGSEGV).
pcd: Terminating PCD, rebooting system...
starting pid 205, tty '': '/bin/umount /var /sys'
The system is going down NOW!
Sent SIGTERM to all processes
Sent SIGKILL to all processes
Restarting system.</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">As we can see, the details of this crash provided by the PCD can help you find and resolve this issue easily and quickly. Let&#8217;s extract the file name and line number from the address number using the mentioned <em>addr2line </em>utility:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre># armeb-linux-uclibceabi-addr2line -e segv -f 8548
rte_test_ptr
/home/hai/rte/segv.c:7</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Now let’s get back to the <em>objdump</em>’s output for a more detailed output:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>00008544 &lt;rte_test_ptr&gt;:
#include &lt;stdio.h&gt;
#include &lt;pcdapi.h&gt;</pre>
<pre>void rte_test_ptr( char *ptr )
{
    *ptr = 'R';
    8544:       e3a03052        mov     r3, #82 ; 0x52
 <span style="color: #ff0000;"><strong>   8548:       e5c03000        strb    r3, [r0] </strong></span>}
    854c:       e12fff1e        bx      lr</pre>
<pre>00008550 &lt;main&gt;:</pre>
<pre>char *ro_ptr = "RT-Embedded";</pre>
<pre>int main( int argc , char *argv[] )
{
    8550:       e92d4010        push    {r4, lr}
    /* Register to PCD's exception handlers */
    PCD_API_REGISTER_EXCEPTION_HANDLERS();</pre>
<pre>    /* Crash test */
    rte_test_ptr(ro_ptr);
    8554:       e59f4020        ldr     r4, [pc, #32]   ; 857c &lt;main+0x2c&gt;
char *ro_ptr = "RT-Embedded";</pre>
<pre>int main( int argc , char *argv[] )
{
    /* Register to PCD's exception handlers */
    PCD_API_REGISTER_EXCEPTION_HANDLERS();
    8558:       e5910000        ldr     r0, [r1]
    855c:       e3a01000        mov     r1, #0  ; 0x0
    8560:       ebffffb8        bl      8448 &lt;_init+0x30&gt;</pre>
<pre>    /* Crash test */
    rte_test_ptr(ro_ptr);
    8564:       e5940000        ldr     r0, [r4]
    8568:       ebfffff5        bl      8544 &lt;rte_test_ptr&gt;
    printf(ro_ptr);
    <strong><span style="color: #ff9900;">856c:       e5940000        ldr     r0, [r4] </span></strong>    8570:       ebffffb1        bl      843c &lt;_init+0x24&gt;</pre>
<pre>    return 0;
}
    8574:       e3a00000        mov     r0, #0  ; 0x0
    8578:       e8bd8010        pop     {r4, pc}
    <strong><span style="color: #339966;">857c:       00010690        .word   0x00010690</span></strong></pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">We can see that the red line is the last instruction that the CPU performed before the crash, and the return address (by the Link Register) is marked by the orange line. In many cases, a function is called from various locations; the Link Register value tells us what the specific location is, and therefore, is also important. From the code, we can see that <em>r4</em> is loaded with address 0x857c and that the value in this address is sent to our <em>rte_test_ptr</em> function. We can use the objdump to lookup the variable’s name by grepping on the value of the word:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre># armeb-linux-uclibceabi-objdump -x segv | grep 10690
00010690 g     O .data  00000004 ro_ptr</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Now we also know the variable name, although it was possible because this variable was global and not on the stack.</p>
<p style="text-align: justify;">Suppose the faulty function is inside a shared library and not in our program. How can we debug this?<br />
Let’s repeat the example, and place the <em>rte_test_ptr</em> inside a shared library. After we compile and link, we run the program again. We’ll reexamine the crash log:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>**************************************************************************
**************************** Exception Caught ****************************
**************************************************************************</pre>
<pre>Signal information:</pre>
<pre>Time: Thu Jan  1 00:01:11 1970
Process name: /usr/sbin/segv
PID: 206
Fault Address: 0x000085f4
Signal: Segmentation fault
Signal Code: Invalid permissions for mapped object
Last error: Success (0)
Last error (by signal): 0</pre>
<pre>ARM registers:</pre>
<pre>trap_no=0x0000000e
error_code=0x0000081f
oldmask=0x00000000
r0=0x000085f4
r1=0x0eb72b74
r2=0x00000000
r3=0x00000052
r4=0x04078000
r5=0x00000000
r6=0x000084ac
r7=0x0000844c
r8=0x00000000
r9=0x00000000
r10=0x00000000
fp=0x0eb72cd4
ip=0x0402c4a0
sp=0x0eb72cc0
lr=0x000085c0
pc=0x0402c4a4
cpsr=0x00000010
fault_address=0x000085f4</pre>
<pre>Maps file:</pre>
<pre>00008000-00009000 r-xp 00000000 1f:06 315        /usr/sbin/segv
00010000-00011000 rw-p 00000000 1f:06 315        /usr/sbin/segv
04000000-04005000 r-xp 00000000 1f:06 232        /lib/ld-uClibc-0.9.29.so
04005000-04007000 rw-p 04005000 00:00 0
0400c000-0400d000 r--p 00004000 1f:06 232        /lib/ld-uClibc-0.9.29.so
0400d000-0400e000 rw-p 00005000 1f:06 232        /lib/ld-uClibc-0.9.29.so
0400e000-04023000 r-xp 00000000 1f:06 175        /lib/libticc.so
04023000-0402a000 ---p 04023000 00:00 0
0402a000-0402c000 rw-p 00014000 1f:06 175        /lib/libticc.so
0402c000-0402d000 r-xp 00000000 1f:06 212        /lib/libsegv.so
0402d000-04034000 ---p 0402d000 00:00 0
04034000-04035000 rw-p 00000000 1f:06 212        /lib/libsegv.so
04035000-04070000 r-xp 00000000 1f:06 200        /lib/libuClibc-0.9.29.so
04070000-04077000 ---p 04070000 00:00 0
04077000-04078000 r--p 0003a000 1f:06 200        /lib/libuClibc-0.9.29.so
04078000-04079000 rw-p 0003b000 1f:06 200        /lib/libuClibc-0.9.29.so
04079000-0407e000 rw-p 04079000 00:00 0
0407e000-04088000 r-xp 00000000 1f:06 137        /lib/libgcc_s.so.1
04088000-0408f000 ---p 04088000 00:00 0
0408f000-04090000 rw-p 00009000 1f:06 137        /lib/libgcc_s.so.1
0eb5e000-0eb73000 rwxp 0eb5e000 00:00 0          [stack]</pre>
<pre>**************************************************************************
pcd: Error: Process /usr/sbin/segv (206) exited unexpectedly (Rule TEST_SIGSEGV).
pcd: Terminating PCD, rebooting system...
starting pid 207, tty '': '/bin/umount -l /nvram /var /sys'
The system is going down NOW!
Sent SIGTERM to all processes
Requesting system reboot
Restarting system.</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Now we can see that unlike the previous example, the Program Counter is in a high address (0x0402c4a4 according to the log). We understand that it resides outside of the program’s code and it is somewhere in one of the linked shared libraries. As we already saw, we can determine which library executed the bad instruction by matching the PC value to the executable address ranges in the maps file. In this example, this address is in the execution range of <em>/lib/libsegv.so</em>, which is the library made for the purpose of this example. In order to understand where we can find the problem inside the library, we need to calculate the offset by reducing the library’s base address from the PC value. In this example, we do:  0x0402c4a4 &#8211; 0x402c000 = 0x4A4, and this is the address of the problematic code inside the library. Let’s use <em>objdump</em> utility again, but now we’ll specify the library name, and not the program’s name. We can truncate the output by using the <em>grep</em> utility, to match the address we calculated and a few lines before and after the match (use –A and –B options):</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre># armeb-linux-uclibceabi-objdump -S libsegv.so | grep 4a4 –B 4 –A 4</pre>
<pre>000004a0 &lt;rte_test_ptr&gt;:
void rte_test_ptr( char *ptr )
{
    *ptr = 'R';
 4a0:   e3a03052        mov     r3, #82 ; 0x52
<strong><span style="color: #ff0000;"> 4a4:   e5c03000        strb    r3, [r0]</span></strong>
}
 4a8:   e12fff1e        bx      lr</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">In red we see the instruction that caused the crash, as expected, inside the <em>rte_test_ptr</em> function we moved to a shared library.</p>
<p style="text-align: justify;">For conclusion, now we know how to:</p>
<ul style="text-align: justify;">
<li>Extract and understand the crash information provided in to a fault signal handler.</li>
<li>Find a bad instruction inside our program and inside a shared library.</li>
</ul>
<p style="text-align: justify;">Now we have all the required information to fix this crash. It can be done manually, and it can be done using the PCD.</p>
<h3 style="text-align: justify;">Memory corruption</h3>
<p style="text-align: justify;">Crashes and segmentation faults may be also a result of memory corruption. There could be a case where some code unintentionally changes a memory portion which it does not own thus causing a mess to the rightful owner.  Read <a href="http://www.rt-embedded.com/blog/archives/resolving-memory-corruption/" target="_blank">here</a> how to debug such errors.</p>
<h3 style="text-align: justify;">Resources:</h3>
<p style="text-align: justify;"><a href="http://linux.die.net/man/2/signal">http://linux.die.net/man/2/signal</a><br />
<a href="http://linux.die.net/man/2/sigaction">http://linux.die.net/man/2/sigaction</a><br />
<a href="http://sourceforge.net/projects/pcd/">http://sourceforge.net/projects/pcd/</a></p>
<div id="_mcePaste" class="mcePaste" style="position: absolute; left: -10000px; top: 893px; width: 1px; height: 1px; overflow: hidden;">http://www.rt-embedded.com/blog/archives/enabling-core-dumps-in-embedded-systems/</div>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/f_4kF3lZwpY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/resolving-crashes-and-segmentation-faults/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/resolving-crashes-and-segmentation-faults/</feedburner:origLink></item>
		<item>
		<title>Resolving Alignment Traps</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/FB1yO074foU/</link>
		<comments>http://www.rt-embedded.com/blog/archives/resolving-alignment-traps/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 18:46:04 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[alignment]]></category>
		<category><![CDATA[bus]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[pcd]]></category>
		<category><![CDATA[real]]></category>
		<category><![CDATA[resolve]]></category>
		<category><![CDATA[rt]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[trap]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=146</guid>
		<description><![CDATA[What is an Alignment Trap?
<p style="text-align: justify;">The memory of the system is used for two main purposes: Store code, and store data. Regarding code, each 32-bit opcode must be stored in a 32-bit aligned address, meaning, the address of each opcode must divide by 4. A 16-bit processor (or ARM in Thumb mode), can access [...]]]></description>
				<content:encoded><![CDATA[<h3 style="text-align: justify;"><img class="alignright size-full wp-image-147" title="alignment" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/alignment.jpg" alt="" width="252" height="189" />What is an Alignment Trap?</h3>
<p style="text-align: justify;">The memory of the system is used for two main purposes: Store code, and store data. Regarding code, each 32-bit opcode must be stored in a 32-bit aligned address, meaning, the address of each opcode must divide by 4. A 16-bit processor (or ARM in Thumb mode), can access opcodes only in 16-bit aligned addresses, meaning the address must divide by 2. Storing the code differently in the memory will cause an unpredictable behavior because the CPU will read the opcodes incorrectly.</p>
<p style="text-align: justify;">An alignment trap occurs when the CPU is required to access a 32-bit variable which is not 32-bit aligned (same for 16-bit).</p>
<h3 style="text-align: justify;">Some examples for alignment of variables</h3>
<p style="text-align: justify;">When you define a variable globally, on the stack, or allocate memory for it on the heap, it will be positioned in a memory location according to its size. A 32-bit variable address must divide by 4, a 16-bit variable address must divide by 2 and an 8-bit variable address could be anywhere. The CPU has opcodes to access words, half-words and bytes, accordingly. Data structures which contain variables with various sizes will be padded to make their variables aligned according to their sizes, unless forcing it to skip padding with <em>packed</em> attribute. In this case, the compiler generates code that reads the nearest aligned address and isolates the required value using <em>register shift,</em> <em>logical and</em> and multiple byte access functions. This of course makes the code larger, and therefore, it is always recommended to work with the native-size variables of the CPU (in this case; <em>int</em> or <em>unsigned int</em>) to get the optimal performance, even if we don’t need their full range, and avoid packed structures as much as possible (note that there are cases that packed structures must be used, for example, hardware address or network packets). For conclusion, it looks like we are ok, and all kinds of memory accesses should be covered. Alignment traps occur when the CPU tries to perform a memory access on an unaligned address. But if we are covered, why do we still get alignment traps?<em> </em></p>
<p style="text-align: justify;"><span id="more-146"></span></p>
<p style="text-align: justify;">Let’s take a look on the following code snippet which defines some types of variables, a structure and a packed structure:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code><strong>int</strong> global_i;<br />
<strong>char</strong> global_c1;<br />
<strong>short</strong> global_s;</code><br />
<code><strong>struct</strong> rte_unpacked_struct_t<br />
{<br />
<strong>   char</strong> c1;<br />
<strong>   int</strong> i;<br />
<strong>   char</strong> c2;<br />
<strong>   short</strong> s1;<br />
<strong> char</strong> c3;<br />
};</code><br />
<code><br />
<strong>struct</strong> rte_packed_struct_t<br />
{<br />
 <strong>   char</strong> c1;<br />
 <strong>   int</strong> i;<br />
 <strong>   char</strong> c2;<br />
 <strong>   short</strong> s1;<br />
 <strong>   char</strong> c3;<br />
} __<strong>attribute</strong>__((__<strong>packed</strong>__));<br />
</code><br />
<code><br />
<strong>struct</strong> rte_unpacked_struct_t unpacked;<br />
<strong>struct</strong> rte_packed_struct_t packed;</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Now let’s print each variable’s address, using a simple program:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>Address of unpacked = 0x109d8<br />
Address of unpacked.c1 = 0x109d8<br />
Address of unpacked.i = 0x109dc<br />
Address of unpacked.c2 = 0x109e0<br />
Address of unpacked.s1 = 0x109e2<br />
Address of unpacked.c3 = 0x109e4<br />
Address of packed = 0x1097a<br />
Address of packed.c1 = 0x1097a<br />
Address of packed.i = 0x1097b<br />
Address of packed.c2 = 0x1097f<br />
Address of packed.s1 = 0x10980<br />
Address of packed.c3 = 0x10982<br />
Address of global_i = 0x10994<br />
Address of global_c1 = 0x10979<br />
Address of global_s1 = 0x10978</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">As expected, the addresses of the unpacked structure, as well as the integers are 32-bit aligned, the addresses of the short integers are 16-bit aligned and the addresses of the 8-bit variables could be odd or even. Pay attention to the <em>packed</em> structure, where the addresses of its members are not aligned – but access to each one of them is expensive in terms of code size and performance.</p>
<h3 style="text-align: justify;">An example of bad code which causes an alignment trap</h3>
<p style="text-align: justify;">Let’s use the above data types, and write a function that returns the address of <em>s1</em> variable inside a <em>rte_unpacked_t</em> structure. From the main, we will use an integer variable (on purpose) which is incompatible with the size of the short integer (int is 32-bit in length, and short is 16-bit in length). From the output above, we know that the address of <em>s1</em> is aligned to 16-bit (0x109e2). The following code produces a warning from the compiler, about some incompatible pointer type. In many cases, these warnings are ignored, or even worse, a casting is added in the <em>main</em> function to make it shut up…</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code><strong>short</strong> *rte_get_s1( <strong>void</strong> )<br />
{<br />
return &amp;unpacked.s1;<br />
}</code></p>
<p><code><strong>int</strong> main( <strong>int</strong> argc, <strong>char</strong> *argv[] )<br />
{<br />
<strong>   int</strong> *val;  val = rte_get_s1();<br />
<strong>   printf</strong>( "val = %d\n", *val );</code><br />
<code><br />
<strong>   return</strong> 0;<br />
}</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">As a result, our program crashes immediately, and we get the following error:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>Alignment trap: alignment (268) PC=0x00008680 Instr=0xe5901000 Address=0x000109e2 FSR 0x011<br />
Bus error</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Well, this is easy because it’s a very simple and short program. It could be a major problem when it is hidden inside thousands of code lines, especially if someone used casting to avoid the warning. It could be even worse if the function returns a void pointer address to a database which contains various data types with different sizes.</p>
<h3 style="text-align: justify;">We have an alignment trap error. Now what?</h3>
<p style="text-align: justify;">The error message provides a lot of useful information we could use in order to isolate the bug. We can see the process name, process id, the current value of the Program Counter register, what was the instruction that caused the error, what is the address which was not aligned and what is the value of FSR (in ARM platforms).</p>
<p style="text-align: justify;">The process name and id gives us a general location of the problem. In a system with many programs, it’s a good first step towards the solution. As we suspected, the address in the error message matches the address of our <em>packed.s1</em> variable, but usually we can’t conclude any information from this field because we don’t know the address of each variable. Now, we’ll take a closer look at the <em>PC</em> and <em>Instr</em>. The value of the Program Counter points to the process’s virtual memory, so the physical address is not 0&#215;8680 but something else. We don’t really care about that. This value can help us to determine whether the instruction was executed from within the program or from a shared library.</p>
<p style="text-align: justify;">In the next step, will reboot (or restart the process) and look at its maps file:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># cat /proc/276/maps<br />
00008000-00009000 r-xp 00000000 00:0b 757 /var/alignment<br />
00010000-00011000 rw-p 00000000 00:0b 757 /var/alignment<br />
04000000-04005000 r-xp 00000000 1f:02 122 /lib/ld-uClibc-0.9.29.so<br />
04005000-04007000 rw-p 04005000 00:00 0<br />
0400c000-0400d000 r--p 00004000 1f:02 122 /lib/ld-uClibc-0.9.29.so<br />
0400d000-0400e000 rw-p 00005000 1f:02 122 /lib/ld-uClibc-0.9.29.so<br />
0400e000-04049000 r-xp 00000000 1f:02 117 /lib/libuClibc-0.9.29.so<br />
04049000-04050000 ---p 04049000 00:00 0<br />
04050000-04051000 r--p 0003a000 1f:02 117 /lib/libuClibc-0.9.29.so<br />
04051000-04052000 rw-p 0003b000 1f:02 117 /lib/libuClibc-0.9.29.so<br />
04052000-04057000 rw-p 04052000 00:00 0<br />
0e94a000-0e95f000 rwxp 0e94a000 00:00 0 [stack]</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Since it’s just an example, there are not many libraries. Let’s take a look at the address ranges in the first column, and compare against the reported value of the Program Counter. We can clearly see that the error comes from within the program’s code (See the first line, with the Read/Execute permissions). If there was a problem in a linked library (such as the libc library), the PC value would be mapped much higher, and for the libc library example, it would be something in the range of 0x400e000 and 0&#215;4049000. In order to calculate the instruction address inside the library, we reduce the corresponding library’s base address from the reported PC value.</p>
<p style="text-align: justify;">OK, so now we know where to look for. Assuming we compiled our program with debug symbols, we can use the <em>objdump</em> utility and ask it to show the mixed assembly and C code (using the –S option) around this address. Here’s a snippet from the output:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># armeb-linux-uclibceabi-objdump -S alignment<br />
...<br />
val = rte_get_s1();<br />
867c: ebffff6b bl 8430 &lt;rte_get_s1&gt;<br />
<span style="color: #ff0000;"><strong>8680: e5901000</strong> </span>ldr r1, [r0]<br />
8684: e59f0018 ldr r0, [pc, #24] ; 86a4 &lt;main+0xa0&gt;<br />
8688: ebffff26 bl 8328 &lt;_init+0x24&gt;<br />
printf( "val = %d\n", *val );<br />
return 0;<br />
}<br />
...</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The highlighted numbers in the <em>objdump</em> output show us both the reported address and instruction, right next to the problematic code! This trick works even for the most complicated programs. Assuming you can’t figure out the correct address, you can try to grep the objdump’s output with the reported instruction. In high probability, you’ll get a few matching results, one of them hold the key to your error.</p>
<p style="text-align: justify;">If the address belongs to a shared library, we use the <em>objdump</em> on the shared library’s binary code and not on the program’s code.</p>
<p style="text-align: justify;">In case your program was not compiled with symbols, you can recompile the program with symbols and try to reproduce again. It is recommended to always compile the program with symbols. Symbols are removed anyway in the final target’s filesystem.</p>
<p style="text-align: justify;">In order to find the exact assembly line when the problem comes from a library, we’ll subtract the correct base address from the reported PC value. This will be the offset of the problematic code inside the library.</p>
<h3 style="text-align: justify;">For conclusion</h3>
<ul style="text-align: justify;">
<li>Try to use 32-bit variables. Short and Char variables do not save memory or perform faster anyway because the CPU loads them into 32-bit registers in any case, and has even more work to deal with them correctly.</li>
<li>Use casting with caution. Beware of arrays of bytes; don’t try to access this array with a 32-bit pointer.</li>
<li>Clean the warnings. Don’t leave warnings in your code.</li>
<li>Don’t fix warnings that you are not sure how to fix. Consult with others.</li>
</ul>
<h3 style="text-align: justify;">How can PCD help debugging, resolving and preventing alignment traps?</h3>
<p style="text-align: justify;">When a program crashes due to an alignment trap, it will be terminated once the error message is displayed. This crash will not trigger any recovery action, and your system will probably because unstable or unusable. The <a href="http://www.rt-embedded.com/blog/archives/pcd/" target="_blank">PCD</a> can help here in two fields:</p>
<p style="text-align: justify;">Enhanced debugging capabilities and system recovery. Once registering to the PCD exception handlers, they will provide more information about this crash, including the Program counter, Link register (return address), all the other registers, last value of <em>errno</em> and the maps file of the process, right before it was terminated. The latter will help you analyze the location of the error just be looking at the PCD’s error report. It will also trigger a recovery action once the crash was detected, and return the system to functional mode. The crash information is also saved on a non-volatile storage for later/offline analysis. Let’s configure a simple PCD rule to start and monitor the alignment program:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code># PCD Rule for alignment program<br />
<strong>RULE</strong> = TEST_ALIGNMENT<br />
<strong>START_COND</strong> = <strong>NONE</strong><br />
<strong>COMMAND</strong> = /usr/sbin/alignment 315305 12 42<br />
<strong>SCHED</strong> = <strong>NICE</strong>,0<br />
<strong>DAEMON</strong> = <strong>YES</strong><br />
<strong>END_COND</strong> = <strong>NONE</strong><br />
<strong>END_COND_TIMEOUT</strong> = -1<br />
<strong>FAILURE_ACTION</strong> = <strong>REBOOT</strong><br />
<strong>ACTIVE</strong> = <strong>YES</strong></code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Here is the output on the console once PCD has started this rule. Note that the selected recovery action here was “Reboot”, that’s why the system is rebooting right after the crash:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td><code>pcd: Starting process /usr/sbin/alignment (Rule TEST_ALIGNMENT).<br />
Alignment trap: alignment (157) PC=0x000087cc Instr=0xe5901000 Address=0x00010b3a FSR 0x011<br />
pcd: Rule TEST_ALIGNMENT: Success (Process /usr/sbin/alignment (157)).</code></p>
<p><code>**************************************************************************<br />
**************************** Exception Caught ****************************<br />
**************************************************************************<br />
Signal information:<br />
Time: Thu Jan 1 00:03:10 1970<br />
Process name: /usr/sbin/alignment<br />
PID: 157<br />
Fault Address: 0x00000000<br />
Signal: Bus error<br />
Signal Code: Kernel<br />
Last error: Success (0)<br />
Last error (by signal): 0</code></p>
<p>ARM registers:<br />
trap_no=0&#215;00000000<br />
error_code=0&#215;00000000<br />
oldmask=0&#215;00000000<br />
r0=0x00010b3a<br />
r1=0x0ea5ab84<br />
r2=0&#215;00000000<br />
r3=0xfffff000<br />
r4=0x0000000c<br />
r5=0x0ea5aeb4<br />
r6=0x0004cfa9<br />
r7=0x00010b24<br />
r8=0&#215;00000000<br />
r9=0&#215;00000000<br />
r10=0&#215;00000000<br />
fp=0&#215;00000000<br />
ip=0&#215;00000000<br />
sp=0x0ea5acd0<br />
lr=0x000087cc<br />
pc=0x000087cc<br />
cpsr=0&#215;40000010<br />
fault_address=0&#215;00000000</p>
<p>Maps file:</p>
<p>00008000-00009000 r-xp 00000000 1f:07 57 /usr/sbin/alignment<br />
00010000-00011000 rw-p 00000000 1f:07 57 /usr/sbin/alignment<br />
04000000-04005000 r-xp 00000000 1f:05 282 /lib/ld-uClibc-0.9.29.so<br />
04005000-04007000 rw-p 04005000 00:00 0<br />
0400c000-0400d000 r&#8211;p 00004000 1f:05 282 /lib/ld-uClibc-0.9.29.so<br />
0400d000-0400e000 rw-p 00005000 1f:05 282 /lib/ld-uClibc-0.9.29.so<br />
0400e000-04023000 r-xp 00000000 1f:05 213 /lib/libpcdapi.so<br />
04023000-0402a000 &#8212;p 04023000 00:00 0<br />
0402a000-0402c000 rw-p 00014000 1f:05 213 /lib/libpcdapi.so<br />
0402c000-04067000 r-xp 00000000 1f:05 241 /lib/libuClibc-0.9.29.so<br />
04067000-0406e000 &#8212;p 04067000 00:00 0<br />
0406e000-0406f000 r&#8211;p 0003a000 1f:05 241 /lib/libuClibc-0.9.29.so<br />
0406f000-04070000 rw-p 0003b000 1f:05 241 /lib/libuClibc-0.9.29.so<br />
04070000-04075000 rw-p 04070000 00:00 0<br />
04075000-0407f000 r-xp 00000000 1f:05 171 /lib/libgcc_s.so.1<br />
0407f000-04086000 &#8212;p 0407f000 00:00 0<br />
04086000-04087000 rw-p 00009000 1f:05 171 /lib/libgcc_s.so.1<br />
0ea46000-0ea5b000 rwxp 0ea46000 00:00 0 [stack]<br />
**************************************************************************<br />
pcd: Error: Process /usr/sbin/alignment (157) exited unexpectedly (Rule TEST_ALIGNMENT).<br />
pcd: Terminating PCD, rebooting system&#8230;<br />
The system is going down NOW!<br />
Sent SIGTERM to all processes<br />
Sent SIGKILL to all processes<br />
Restarting system.<br />
.</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The details of this crash provided by the PCD can help you find and resolve this issue easily and quickly. The fact that it is also saved in the non-volatile storage of the target makes it even possible to fix it remotely or later during post-mortem analysis.</p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/FB1yO074foU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/resolving-alignment-traps/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/resolving-alignment-traps/</feedburner:origLink></item>
		<item>
		<title>Process information in the proc filesystem</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/R4wRJcb30RY/</link>
		<comments>http://www.rt-embedded.com/blog/archives/process-information-in-the-proc-filesystem/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 18:18:53 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[filesystem]]></category>
		<category><![CDATA[information]]></category>
		<category><![CDATA[proc]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[real]]></category>
		<category><![CDATA[rt]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=135</guid>
		<description><![CDATA[<p style="text-align: justify;">The proc filesystem is a pseudo/virtual filesystem which is used as a communication interface with the kernel.  It is commonly mounted at /proc and the files and directories which reside there are created in run-time by the kernel, drivers and loadable modules. The directories usually mark an area or an item, and the [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-140" title="proc" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/03/proc.jpg" alt="" width="280" height="218" />The <em>proc</em> filesystem is a pseudo/virtual filesystem which is used as a communication interface with the kernel.  It is commonly mounted at /proc and the files and directories which reside there are created in run-time by the kernel, drivers and loadable modules. The directories usually mark an area or an item, and the files, which are usually read-only, provide information about this area or item. Files that allow writing can change the behavior of the kernel or the driver which provided the file.</p>
<p style="text-align: justify;">In this article, I will present most of the files which you can find for each process in the proc filesystem. It might be slightly different in each and every Linux distribution, but most of it is the same. The following text was taken from the proc manual, and I added some more information when I felt necessary. Wherever you see a <em>[pid]</em>, you need to replace it with an actual process ID number. To see the information, use the cat command (e.g. cat /proc/225/cmdline).</p>
<p style="text-align: justify;">The list is long because there is a lot of information. Remember that this is only a partial list, which covers only the Process related files. There are more interesting files in the proc filesystem which will be covered in a different article.</p>
<p style="text-align: justify;"><span id="more-135"></span></p>
<p style="text-align: justify;"><span style="text-decoration: underline;">The following files are available in the proc filesystem per <em>each process</em> in the system:</span></p>
<h3 style="text-align: justify;">/proc/[pid]</h3>
<p style="text-align: justify;">There is a numerical subdirectory for each running process; the subdirectory is named by the process ID.  Each contains the following pseudo-files and directories.</p>
<h3 style="text-align: justify;">/proc/[pid]/cmdline</h3>
<p style="text-align: justify;">This holds the complete command line for the process, unless the whole process has been swapped out, or unless the process is a zombie.  In either of these later cases, there is nothing in this file: i.e. a read on this file will return 0 characters.  The command line arguments appear in this file as a set of null-separated strings, with a further null byte after the last string.</p>
<h3 style="text-align: justify;">/proc/[pid]/cwd</h3>
<p style="text-align: justify;">This is a link to the current working directory of the process.</p>
<h3 style="text-align: justify;">/proc/[pid]/environ</h3>
<p style="text-align: justify;">This file contains the environment for the process.  The entries are separated by null characters, and there may be a null character at the end.</p>
<h3 style="text-align: justify;">/proc/[pid]/exe</h3>
<p style="text-align: justify;">The exe file is a symbolic link containing the actual path name of  the executed  command. The exe symbolic link can be dereferenced normally &#8211; attempting to open exe will open the executable.  You can even type /proc/[pid]/exe to run  another copy of the same process as [pid].</p>
<h3 style="text-align: justify;">/proc/[pid]/fd</h3>
<p style="text-align: justify;">This is a subdirectory containing one entry for each file which the process has opened, named by its file descriptor, and which is a symbolic link to the actual file (as the exe entry does).  Thus, 0 is standard input, 1 standard output, 2 standard error, etc.</p>
<h3 style="text-align: justify;">/proc/[pid]/maps</h3>
<p style="text-align: justify;">A file containing the currently mapped memory regions and their access permissions.</p>
<p style="text-align: justify;">An example for the format:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: justify;"><span style="font-size: 8pt; font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: HE;">address        perms offset  dev   inode      pathname
00008000-0000a000 r-xp 00000000 1f:06 294        /usr/sbin/gptimer
00011000-00012000 rw-p 00001000 1f:06 294        /usr/sbin/gptimer
00012000-00013000 rwxp 00012000 00:00 0          [heap]
04000000-04005000 r-xp 00000000 1f:06 231        /lib/ld-uClibc-0.9.29.so
04005000-04007000 rw-p 04005000 00:00 0
04008000-04009000 rw-s 00000000 00:07 65538      /SYSV03030004 (deleted)
0400c000-0400d000 r--p 00004000 1f:06 231        /lib/ld-uClibc-0.9.29.so
0400d000-0400e000 rw-p 00005000 1f:06 231        /lib/ld-uClibc-0.9.29.so
0400e000-04023000 r-xp 00000000 1f:06 175        /lib/libticc.so
04023000-0402a000 ---p 04023000 00:00 0
0402a000-0402c000 rw-p 00014000 1f:06 175        /lib/libticc.so
0402c000-04036000 r-xp 00000000 1f:06 193        /lib/libpthread-0.9.29.so
04036000-0403d000 ---p 04036000 00:00 0
0403d000-0403e000 r--p 00009000 1f:06 193        /lib/libpthread-0.9.29.so
0403e000-0403f000 rw-p 0000a000 1f:06 193        /lib/libpthread-0.9.29.so
0403f000-04041000 rw-p 0403f000 00:00 0
04041000-0407c000 r-xp 00000000 1f:06 200        /lib/libuClibc-0.9.29.so
0407c000-04083000 ---p 0407c000 00:00 0
04083000-04084000 r--p 0003a000 1f:06 200        /lib/libuClibc-0.9.29.so
04084000-04085000 rw-p 0003b000 1f:06 200        /lib/libuClibc-0.9.29.so
04085000-0408a000 rw-p 04085000 00:00 0
0408a000-04094000 r-xp 00000000 1f:06 137        /lib/libgcc_s.so.1
04094000-0409b000 ---p 04094000 00:00 0
0409b000-0409c000 rw-p 00009000 1f:06 137        /lib/libgcc_s.so.1
0409c000-0409e000 r-xp 00000000 1f:06 199        /lib/libdl-0.9.29.so
0409e000-040a5000 ---p 0409e000 00:00 0
040a5000-040a6000 r--p 00001000 1f:06 199        /lib/libdl-0.9.29.so
040a6000-040a7000 rw-p 00002000 1f:06 199        /lib/libdl-0.9.29.so
040a8000-04125000 rw-s 00000000 00:07 0          /SYSVf00b0251 (deleted)
04125000-04126000 ---p 04125000 00:00 0
04126000-04925000 rw-p 04126000 00:00 0
0eace000-0eae3000 rwxp 0eace000 00:00 0          [stack]</span></pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Where; <em>address</em> is the address space in the process that it occupies, <em>perms</em> is a set of access permissions:</p>
<p style="text-align: justify;">     r = read<br />
     w = write<br />
     x = execute<br />
     s = shared<br />
     p = private (copy on write)</p>
<p style="text-align: justify;"><em>offset</em> is the offset into the file/whatever, <em>dev</em> is the device (major:minor), and <em>inode </em>is the index of the associated file structure in the filesystem, for that device. 0 indicates that no inode is associated with the memory region.</p>
<p style="text-align: justify;">The maps file also specify the address and range of the process <em>heap</em> (how much memory was dynamically allocated with <em>malloc( )</em>), and the address and range of the process <em>stack</em>.</p>
<h3 style="text-align: justify;">/proc/[pid]/smaps</h3>
<p style="text-align: justify;">Provides an even more detailed information about the process mapped memory regions.</p>
<p style="text-align: justify;">Here’s an example of the same process, which I had to truncate:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>00008000-0000a000 r-xp 00000000 1f:06 294        /usr/sbin/gptimer
Size:                 8 kB
Rss:                  8 kB
Shared_Clean:         0 kB
Shared_Dirty:         0 kB
Private_Clean:        8 kB
Private_Dirty:        0 kB
00011000-00012000 rw-p 00001000 1f:06 294        /usr/sbin/gptimer
Size:                 4 kB
Rss:                  4 kB
Shared_Clean:         0 kB
Shared_Dirty:         0 kB
Private_Clean:        0 kB
Private_Dirty:        4 kB
00012000-00013000 rwxp 00012000 00:00 0          [heap]
Size:                 4 kB
Rss:                  4 kB
Shared_Clean:         0 kB
Shared_Dirty:         0 kB
Private_Clean:        0 kB
Private_Dirty:        4 kB
04000000-04005000 r-xp 00000000 1f:06 231        /lib/ld-uClibc-0.9.29.so
Size:                20 kB
Rss:                 20 kB
Shared_Clean:        20 kB
Shared_Dirty:         0 kB
Private_Clean:        0 kB
Private_Dirty:        0 kB
04005000-04007000 rw-p 04005000 00:00 0
Size:                 8 kB
Rss:                  8 kB
Shared_Clean:         0 kB
Shared_Dirty:         0 kB
Private_Clean:        0 kB
Private_Dirty:        8 kB
04008000-04009000 rw-s 00000000 00:07 65538      /SYSV03030004 (deleted)
Size:                 4 kB
Rss:                  4 kB
Shared_Clean:         4 kB
Shared_Dirty:         0 kB
Private_Clean:        0 kB
Private_Dirty:        0 kB
0400c000-0400d000 r--p 00004000 1f:06 231        /lib/ld-uClibc-0.9.29.so
Size:                 4 kB
Rss:                  4 kB
Shared_Clean:         0 kB
Shared_Dirty:         0 kB
Private_Clean:        0 kB
Private_Dirty:        4 kB
0400d000-0400e000 rw-p 00005000 1f:06 231        /lib/ld-uClibc-0.9.29.so
Size:                 4 kB
Rss:                  4 kB
Shared_Clean:         0 kB
Shared_Dirty:         0 kB
Private_Clean:        0 kB
Private_Dirty:        4 kB</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">Where; <em>Size</em> is the total size of the region, <em>Rss</em> is the resident size of the region (the actual used memory size), <em>Shared Clean</em> is the amount of shared memory that was not changed by the process (like the code of a shared library), <em>Shared dirty</em> is the amount of shared memory that was changed by the process (like a code of a duplicated shared library – wrong), <em>Private clean¸</em> is the amount of memory (usually global data) that was not changed by the process (it is 0 most of the time) and <em>Private dirty</em>, which is the amount of memory that was modified by the process (like shared library’s global data which each process has its own copy of them). All the entries in this list are rounded to the system’s <em>Page Size</em>, in this example, it is 4KB.</p>
<h3 style="text-align: justify;">/proc/[pid]/mem</h3>
<p style="text-align: justify;">The mem file provides a means to access the process memory pages, using open, fseek and read commands.</p>
<h3 style="text-align: justify;">/proc/[pid]/root</h3>
<p style="text-align: justify;">This is a link to the root directory which is seen by the process. This root directory is usually “/”, but it can be changed by the <em>chroot </em>command.</p>
<h3 style="text-align: justify;">/proc/[pid]/stat</h3>
<p style="text-align: justify;">This file provides status information about the process. This is used by the <em>Process Show</em> utility. It is defined in <em>fs/proc/array.c</em> source file and may differ from one distribution to another.</p>
<p style="text-align: justify;">The fields, in order, with their proper <em>scanf</em> format specifiers, are:</p>
<ul style="text-align: justify;">
<li>pid %d      The process ID.</li>
<li>comm %s     The  filename  of  the  executable,  in parentheses. This is visible whether or not the executable is swapped out.</li>
<li>state %c    One  character  from  the string &#8220;RSDZTW&#8221; where R is running, S is sleeping in an interruptible  wait,  D is  waiting in uninterruptible disk sleep, Z is zombie, T is traced or stopped (on a signal), and W is paging.</li>
<li>ppid %d     The PID of the parent.</li>
<li>pgrp %d     The process group ID of the process.</li>
<li>session %d       The session ID of the process.</li>
<li>tty_nr %d         The controlling terminal of the process.  (The minor device number is contained in the combination of bits 31 to 20 and 7 to 0; the major device number is in bits 15 t0 8.)</li>
<li>tpgid %d    The ID of the foreground process group of  the  controlling terminal of the process.</li>
<li>flags %u           The kernel flags word of the process.  For bit meanings,  see  the  PF_*  defines  in  &lt;linux/sched.h&gt;. Details depend on the kernel version.</li>
<li>minflt %lu  The  number  of  minor  faults  the process has made which have not required loading a memory  page  from disk.</li>
<li>cminflt %lu The  number  of  minor  faults  that  the  process&#8217;s waited-for children have made.</li>
<li>majflt %lu  The number of major  faults  the  process  has  made which have required loading a memory page from disk.</li>
<li>cmajflt %lu The  number  of  major  faults  that  the  process&#8217;s waited-for children have made.</li>
<li>cutime %ld  Amount  of time that this process&#8217;s waited-for children have been scheduled in user mode,  measured  in clock  ticks.</li>
<li>cstime %ld  Amount of time that this process&#8217;s waited-for children have been scheduled in kernel mode, measured in clock ticks.</li>
<li>priority %ld      For processes running a real-time scheduling  policy, this is the negated scheduling  priority,  minus  one; that is, a number in the range -2 to -100, corresponding to real-time priorities  1  to  99.  For processes running under a non-real-time scheduling policy, this is the raw nice value as represented in the kernel (The kernel stores nice values as numbers in the range 0 (high) to 39 (low), corresponding to the user-visible nice range of -20 to 19.</li>
<li>nice %ld    The  nice value, a value in the range 19 (low priority) to -20 (high priority).</li>
<li>num_threads %ld          Number of threads in this process (since Linux 2.6).</li>
<li>itrealvalue %ld  The time in jiffies before the next SIGALRM is sent to the process due to an interval timer.  Since kernel 2.6.17, this field is no longer maintained, and is hard coded as 0.</li>
<li>starttime %llu                The time in jiffies the process started after system boot.</li>
<li>vsize %lu   Virtual memory size in bytes.</li>
<li>rss %ld     Resident Set Size: number of pages the process has in real memory. This is just the pages which count towards text, data, or stack space. This does not include pages which have not been demand-loaded in, or which are swapped out.</li>
<li>kstkesp %lu The  current  value of ESP (stack pointer), as found in the kernel stack page for the process.</li>
<li>kstkeip %lu The current EIP (instruction pointer).</li>
<li>signal %lu  The bitmap of pending signals, displayed as a  decimal  number.   Obsolete, because it does not provide information on real-time signals; use /proc/[pid]/status instead.</li>
<li>blocked %lu     The bitmap of blocked signals, displayed as a decimal number.  Obsolete, because it does not provide information on real-time signals; use /proc/[pid]/status instead.</li>
<li>sigignore %lu    The bitmap of ignored signals, displayed as a decimal number. Obsolete, because it does not provide information on real-time signals; use /proc/[pid]/status instead.</li>
<li>sigcatch %lu     The bitmap of caught signals, displayed as a decimal number. Obsolete, because it does not provide information on real-time signals; use /proc/[pid]/status instead.</li>
<li>wchan %lu   This is the &#8220;channel&#8221; in which the process is  wait ing.  It is the address of a system call, and can be looked up in a namelist if you need a textual name</li>
<li>nswap %lu   Number of pages swapped (not maintained).</li>
<li>cnswap %lu  Cumulative nswap  for  child  processes  (not  maintained).</li>
<li>exit_signal %d Signal to be sent to parent when we die.</li>
<li>processor %d CPU number last executed on.</li>
<li>rt_priority %u   Real-time scheduling priority, a number in the range 1 to 99 for processes scheduled under a real-time policy, or 0, for non-real-time processes.</li>
<li>policy %u         Scheduling   policy.</li>
</ul>
<h3 style="text-align: justify;">/proc/[pid]/statm</h3>
<p style="text-align: justify;">Provides information about memory status in pages.  The columns are:</p>
<table style="text-align: justify;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="307" valign="top">sizetotal</td>
<td width="307" valign="top">program size</td>
</tr>
<tr>
<td width="307" valign="top">resident</td>
<td width="307" valign="top">resident set size</td>
</tr>
<tr>
<td width="307" valign="top">share</td>
<td width="307" valign="top">shared pages</td>
</tr>
<tr>
<td width="307" valign="top">trs</td>
<td width="307" valign="top">text (code)</td>
</tr>
<tr>
<td width="307" valign="top">drs</td>
<td width="307" valign="top">data/stack</td>
</tr>
<tr>
<td width="307" valign="top">lrs</td>
<td width="307" valign="top">library</td>
</tr>
<tr>
<td width="307" valign="top">dt</td>
<td width="307" valign="top">dirty pages</td>
</tr>
</tbody>
</table>
<h3 style="text-align: justify;">/proc/[pid]/status</h3>
<p style="text-align: justify;">Provides much of the information <em>in /proc/[pid]/stat</em> and  <em>/proc/[pid]/statm</em> in a readable form.</p>
<p style="text-align: justify;">Here’s an example:</p>
<table style="text-align: justify;" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>Name:   pcd
State:  S (sleeping)
SleepAVG:       0%
Tgid:   219
Pid:    219
PPid:   1
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
FDSize: 32
Groups:
VmPeak:     1172 kB
VmSize:     1172 kB
VmLck:         0 kB
VmHWM:       356 kB
VmRSS:       356 kB
VmData:      148 kB
VmStk:        84 kB
VmExe:        28 kB
VmLib:       380 kB
VmPTE:         6 kB
Threads:        1
SigQ:   0/1024
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: fffffffffffabab1
SigCgt: 000000000001444e
CapInh: 0000000000000000
CapPrm: 00000000fffffeff
CapEff: 00000000fffffeff</pre>
</td>
</tr>
</tbody>
</table>
<h3 style="text-align: justify;">/proc/[pid]/oom_adj</h3>
<p style="text-align: justify;">This file can be used to adjust the score used to select which process should be killed in an out-of-memory (OOM) situation. The kernel uses this value for a bit-shift operation of the process&#8217;s <em>oom_score</em> value: valid values are in the range -16 to +15, plus the special value -17, which disables OOM-killing altogether for this process. A positive score increases the likelihood of this process being killed  by  the  OOM-killer;  a negative  score decreases the likelihood.  The default value for this file is 0; a new process inherits its parent&#8217;s oom_adj setting.  A process must be privileged (CAP_SYS_RESOURCE) to update this file.</p>
<h3 style="text-align: justify;">/proc/[pid]/oom_score</h3>
<p style="text-align: justify;">This file displays the current score that the kernel gives to this process for the purpose of selecting a process for the OOM-killer. A higher score means that the process is more likely to be selected by the OOM-killer. The basis for this score is the amount of memory used by the process, with increases (+) or decreases (-) for factors including:</p>
<ul style="text-align: justify;">
<li>whether  the  process  creates a lot of children using fork(2)  (+);</li>
<li>whether the process has been running a long time, or used a lot of CPU time (-);</li>
<li>whether the process has a low nice value (i.e., &gt; 0) (+);</li>
<li>whether the process is privileged (-); and</li>
<li>whether the process is making direct hardware access (-).</li>
</ul>
<p style="text-align: justify;">The  <em>oom_score</em>  also reflects the bit-shift adjustment specified by the <em>oom_adj</em> setting for the process.</p>
<p style="text-align: justify;"> </p>
<p style="text-align: justify;"><span style="text-decoration: underline;">Resources:<br />
</span><a href="http://linux.die.net/man/5/proc">http://linux.die.net/man/5/proc</a> </p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/R4wRJcb30RY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/process-information-in-the-proc-filesystem/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/process-information-in-the-proc-filesystem/</feedburner:origLink></item>
		<item>
		<title>Processes – Background and Priority</title>
		<link>http://feedproxy.google.com/~r/rt-embedded/~3/vvxKSOB2KlA/</link>
		<comments>http://www.rt-embedded.com/blog/archives/processes-background-and-priority/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 20:29:30 +0000</pubDate>
		<dc:creator>Hai Shalom</dc:creator>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[priority]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[real]]></category>
		<category><![CDATA[rt]]></category>
		<category><![CDATA[scheduling]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.rt-embedded.com/blog/?p=121</guid>
		<description><![CDATA[<p style="text-align: justify;">This article is the first amongst a series of articles about processes in Linux. The first article provides some background about processes, and covers process priority and scheduling policies.</p>
What is a process?
<p style="text-align: justify;">A process is a program, an independent entity, which performs a dedicated task in the system. A process is composed [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="alignright size-full wp-image-123" title="Priority" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/Priority.jpg" alt="" width="200" height="225" />This article is the first amongst a series of articles about processes in Linux. The first article provides some background about processes, and covers process priority and scheduling policies.</p>
<h3 style="text-align: justify;">What is a process?</h3>
<p style="text-align: justify;">A process is a program, an independent entity, which performs a dedicated task in the system. A process is composed of one or more binary objects which contain function implementations and an entry point function called <em>main( )</em>. Each process has a father, which created it, and can have many children processes which it can create. Each process can belong to a certain group (similar to a family).</p>
<p style="text-align: justify;"><span id="more-121"></span></p>
<h3 style="text-align: justify;">Context and Process ID</h3>
<p style="text-align: justify;">In Linux, each process has its own unique Process ID (or <em>pid</em>). Each process has a context of its own. It means that each process has its own copy of registers, data and instruction memory, which can be accessed by the process itself. The <em>process show</em> utility (<em>ps</em>) provides a list of all currently active processes in the system, as well as their owner and their virtual memory allocation:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre># ps -a
  PID USER       VSZ STAT COMMAND
    1 root       564 S    init
    2 root         0 SW   [posix_cpu_timer]
    3 root         0 SW   [softirq-high/0]
    4 root         0 SW   [softirq-timer/0]
    5 root         0 SW   [softirq-net-tx/]
    6 root         0 SW   [softirq-net-rx/]
    7 root         0 SW   [softirq-block/0]
    8 root         0 SW   [softirq-tasklet]
    9 root         0 SW   [softirq-rcu/0]
   10 root         0 SW&lt;  [desched/0]
   11 root         0 SW&lt;  [events/0]
   12 root         0 SW&lt;  [khelper]
   13 root         0 SW&lt;  [kthread]
   17 root         0 SW&lt;  [kblockd/0]
   21 root         0 SW&lt;  [khubd]
   56 root         0 SW   [pdflush]
   57 root         0 SW   [pdflush]
   58 root         0 SW&lt;  [kswapd0]
   59 root         0 SW&lt;  [aio/0]
   84 root         0 SW&lt;  [IRQ 41]
   88 root         0 SW   [mtdblockd]
  121 root         0 SW&lt;  [IRQ 29]
  122 root         0 SW&lt;  [IRQ 24]
  132 root         0 SW&lt;  [IRQ 8]
  151 root       420 S    watchdog_rt -t 15 /dev/watchdog
  152 root       572 S    /bin/sh
  154 root       564 R    ps -a</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The last item in the list is always the process show utility itself, and its state is Running because it was running during the time it was generating the list. Lines with square brackets mark kernel threads, which are also considered as processes, with the one difference that they run in Kernel space.</p>
<h3 style="text-align: justify;">Process memory space</h3>
<p style="text-align: justify;">In Linux, unlike other embedded operating systems, each process has its own virtual memory space, meaning it is living in its own world and no other process can read or write its data and code memory. This model provides enhanced security and robustness and avoids memory overflow or corruption which another task is creating. On the other hand, it makes the programmer’s life harder when debugging is required. It is also different in the scenario where one program wants to call another program’s function. To resolve this issue, a logical software module can export its API as a <em>shared library</em>, thus allowing other processes to link with it and perform certain functionalities it exports. There are also a few mechanisms of <em>Inter-Process-Communication</em> which will be discussed in the IPC section. The proc filesystem provides a lot of information regarding the memory map of the process, as well as area sizes, permissions, and library mappings. An example will be provided in the proc section.</p>
<h3 style="text-align: justify;">Process life cycle</h3>
<p style="text-align: justify;">The first process in the system is called <em>init</em>. We can see it in the <em>ps </em>output, and we can also see that its <em>pid</em> is 1. It is the father of all processes in the system and it is created by the kernel during the boot up process. A new child process is created by the <em>fork( )</em> function (or one of its permutations), followed by an <em>exec( )</em> function (or one of its permutations). Both functions are declared in <em>unistd.h </em>header file. The <em>fork(</em> <em>)</em> function creates a new identical process which has a different process id. The child inherits his father’s properties, including working directory, signal masks, environment and also duplicates the father’s resources. When the father calls the <em>fork( )</em> function, the function returns to two different contexts, which are executing the <span style="text-decoration: underline;">same code</span>. Therefore, in order to distinguish between the father and the child, the function returns different return value in each case. When it returns at the father side, the return value will be the new child’s process id (or an error value, in case of failures). When it returns at the child side, the return value will be 0. So we could write our code to handle this situation correctly. See the following code snippet:</p>
<table style="text-align: justify;" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre>pid_t pid;

pid = fork();

if( pid == 0 ) {
    /* Child context */
    /* Do the child's work; usually we call exec here */
} else if( pid &lt; 0 ) {
    /* This is an error */
    fprintf( stderr, "failed to fork\n" );
    exit(1);
} else {
    /* Father context */
    /* Do the father's work */
}</pre>
</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;"> In most cases, once a child process is created, we use the <em>exec( )</em> function to load a new program code and start its <em>main( )</em> function, instead of running two identical copies of the same program. The <em>exec( )</em> function has a few variations, which configure the parameters which are passed to the child process in various methods. See the manual page for more details. A process can be terminated by sending a termination signal. The father which created the child knows its process id, and can request it to terminate. In case a child terminates on its own, it must be picked up by its father, or else it will remain a <em>zombie</em>. Since zombies are dead already, you can’t kill them, and they won’t go away until their father picks them up. Picking up a child process is done by the <em>wait( ) </em>or <em>waitpid( )</em> functions. Once the father is notified that the child was terminated and used the above mentioned functions, the child will disappear from the system completely. The wait functions are declared in <em>sys/wait.h</em> header file.</p>
<h3 style="text-align: justify;">Scheduling policy and task priority</h3>
<p style="text-align: justify;">How can I guarantee that a high priority program will always get enough CPU time? What is the best scheduling policy and priority for my program? How do I change my program’s scheduling policy and priority?</p>
<p style="text-align: justify;">In most embedded Linux projects, there are two scheduling policies:</p>
<ul style="text-align: justify;">
<li>Standard time sharing policy: Scheduling policy which is meant for most tasks which share the CPU resources in fair turns, according to their given priority. This policy is not preemptive, and each process gets a CPU time slice in its turn. In case a low priority process does not get CPU time, its priority is temporarily raised until it is processed, then it gets its original priority back. This scheduling policy is also referenced as <em>nice</em>. The higher priority the program has, the <em>nicer</em> it is. This term was given because interactive processes must be responsive to their users, otherwise, the user will get suspicious that something is wrong in the system.</li>
<li>FIFO (First-in-First-out) Real-Time preemptive policy: Scheduling priority which is meant for real-time, high priority tasks. This policy is preemptive, meaning that if a higher priority FIFO task needs the CPU time, it will preempt any lower priority FIFO task, as well as <span style="text-decoration: underline;">all</span> other nice programs. Therefore, the work that these tasks do must be quick and short; otherwise, it will starve the rest of the system.</li>
</ul>
<p style="text-align: justify;">The total amount of “priorities” in the Linux OS is 120, as seen in the following diagram:</p>
<p style="text-align: justify;"><img title="priorities" src="http://www.rt-embedded.com/blog/wp-content/uploads/2010/04/priorities.jpg" alt="" width="748" height="172" /></p>
<p style="text-align: justify;">Some more facts about scheduling:</p>
<ul style="text-align: justify;">
<li>The default scheduling policy and priority is <em>nice</em> 0.</li>
<li>FIFO lowest priority is higher than nice highest priority.</li>
<li>The nice policy has a “reversed” numbering scheme. The lowest the priority value is, the highest its priority is.</li>
<li>FIFO 99 is the highest priority in the system.</li>
<li>Nice 19 is the lowest priority in the system.</li>
<li>Each process inherits its father’s scheduling policy and priority.</li>
<li>Each process can change its priority according to its needs.</li>
<li>A process controller program (such as PCD) can configure the appropriate priority per each program it controls.</li>
</ul>
<p style="text-align: justify;">Reading and changing a <em>nice-policy</em> priority is done using the <em>getpriority( )</em> and the <em>setpriority( )</em> functions accordingly. These functions are declared in <em>sys/resource.h </em>header file. Note that these functions have no effect for processes running in Real-Time-policy. Reading the scheduling policy is done using the <em>sched_getscheduler( )</em> function, and the <em>sched_setscheduler( )</em> function can be used to both switch to a different scheduling policy and to set a FIFO-policy priority. Both functions are declared in <em>linux/sched.h</em> header file. It is also possible to retrieve the process scheduling policy and priority in the system’s shell, by the proc filesystem. An example will be provided in the article about the proc filesystem.</p>
<h3 style="text-align: justify;">Assigning the correct scheduling and priority to your task</h3>
<p style="text-align: justify;">Here is a set of groups that can help you assign the right scheduling and priority to your task:</p>
<ul style="text-align: justify;">
<li>Low priority tasks (range 19-1): Tasks will be processed when the system is idle. Mainly used for logging and garbage collection daemons.</li>
<li>Normal priority tasks (0): The default middle balanced priority. The <em>init</em> process is created with priority 0, and all its children inherit it. This priority level is used by tasks which can compete on the CPU time and are not response critical.</li>
<li>Above average priority tasks (range (-1) – (-20)): Tasks will be processed more often than lower/normal priority tasks. This priority range is used by tasks which require higher responsiveness on one hand and lengthy amount of CPU time on the other hand (remember that the normal scheme is based on fare sharing). Examples for this kind of tasks are user space drivers and command line interfaces (like shells) which must react quickly. Otherwise (in case of shells), the user will think that the system is unstable due to lack of responsiveness.</li>
<li>Real-Time lower range (1-49): Tasks will be processed upon request while preempting lower priority tasks. Uses by tasks which require a fast response (usually less than a few milliseconds), and do not consume a lot of CPU time, otherwise, a system starvation will occur.</li>
<li>Real-Time average priority (50): In the Embedded Linux distro with RT patch enabled, this priority value is used by the kernel’s soft IRQ tasks and interrupt handlers. It is recommended not to use this level, but consider either lower or higher levels.</li>
<li>Real-Time high priority range (51-99): Similar idea as the RT lower range, but differs that it also preempts the kernel soft IRQ tasks and interrupt handlers. It should be used carefully and assigned only to super high time-critical tasks which seldom wake up and consume a very short amount of CPU time.</li>
</ul>
<h3 style="text-align: justify;">Process Stack</h3>
<p style="text-align: justify;">Process stack grows automatically upon request. The only limitation is the amount of available memory. Note that this doesn’t mean that you can allocate all your memory on the stack, because once it is allocated, it can’t be freed are reused by other process.</p>
<p style="text-align: justify;">
<p style="text-align: justify;"><span style="text-decoration: underline;">Resources:</span></p>
<p style="text-align: justify;"><a href="http://oreilly.com/catalog/linuxkernel/chapter/ch10.html">http://oreilly.com/catalog/linuxkernel/chapter/ch10.html</a><br />
<a href="http://www.opengroup.org/onlinepubs/007908799/xsh/fork.html">http://www.opengroup.org/onlinepubs/007908799/xsh/fork.html</a><br />
<a href="http://www.opengroup.org/onlinepubs/007908799/xsh/exec.html">http://www.opengroup.org/onlinepubs/007908799/xsh/exec.html</a><br />
<a href="http://www.opengroup.org/onlinepubs/007908799/xsh/getpriority.html">http://www.opengroup.org/onlinepubs/007908799/xsh/getpriority.html</a><br />
<a href="http://www.opengroup.org/onlinepubs/007908799/xsh/sched_getscheduler.html">http://www.opengroup.org/onlinepubs/007908799/xsh/sched_getscheduler.html</a></p>
<img src="http://feeds.feedburner.com/~r/rt-embedded/~4/vvxKSOB2KlA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.rt-embedded.com/blog/archives/processes-background-and-priority/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rt-embedded.com/blog/archives/processes-background-and-priority/</feedburner:origLink></item>
	</channel>
</rss>
