<?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/" version="2.0">

<channel>
	<title>Truth, Computing and Fail</title>
	
	<link>http://anomit.com</link>
	<description />
	<lastBuildDate>Sun, 26 Dec 2010 19:39:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/anomit" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="anomit" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Apologies</title>
		<link>http://anomit.com/2010/12/27/apologies/</link>
		<comments>http://anomit.com/2010/12/27/apologies/#comments</comments>
		<pubDate>Sun, 26 Dec 2010 18:42:36 +0000</pubDate>
		<dc:creator>anomit</dc:creator>
				<category><![CDATA[My Life]]></category>

		<guid isPermaLink="false">http://anomit.com/?p=207</guid>
		<description><![CDATA[Warning: Long post This year is almost at its end. It&#8217;s been quite an eventful one for me. I graduated in May and in the meanwhile got a chance to work on Planga in my last semester thanks to Swaroop. Anyway, I&#8217;ll put off the details of my experience in those 5 months for a [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Warning: Long post</strong><br/><br />
This year is almost at its end. It&#8217;s been quite an eventful one for me. I graduated in May and in the meanwhile got a chance to work on <a href="http://planga.com">Planga</a> in my last semester thanks to <a href="http://swaroophegde.com">Swaroop</a>. Anyway, I&#8217;ll put off the details of my experience in those 5 months for a later post. Back to the actual topic.</p>
<p>I got a job around July in Bangalore. Being passionate about martial arts, it was kind of a plan to be based in Bangalore so I could get a chance to train at <a href="http://shootfighter.in">Independent Shootfighters Inc</a>. It has been 5 months since I joined the classes. I have been training quite regularly and there have been remarkable improvements in all the physiological aspects. I&#8217;m stronger, fitter and faster than ever before. But these are just the changes on the surface. I believe the bigger changes have come internally, which is what this post is about. I have been been able to reduce my ego by a huge extent. I can&#8217;t claim it to be gone completely. I&#8217;m not that good yet. With the diminishing ego, I gained the ability to reflect on the opinions, biases I held and how I interacted with different people. Earlier the same effort would be thwarted by the ego that grows like cancer unless kept in check. In plain words, I realized I have been an asshole to a lot of people just because of my ego; and those were just the incidents I could recall. I have no idea about the occasions I might have done so without even having a clue about it.<br />
<a href="http://anomit.com/wordpress/wp-content/uploads/2010/12/2034.jpg"><img src="http://anomit.com/wordpress/wp-content/uploads/2010/12/2034-300x217.jpg" alt="" title="argh" width="300" height="217" class="alignright size-medium wp-image-208" /></a><br />
While in college, I was part of the LUG and with a close group of other guys there, formed a well knit group that&#8217;d occasionally geek out. Individually the only things I&#8217;d care about were: coding, working out and insane partying (not in any order). It&#8217;s the first one that was at the root of all problems. Unknowingly, it developed a false sense of superiority, an air of smugness about the less technically accomplished people and also those not conforming to the ideology of hardcore geekdom. The aim of this post is to publicly apologize to whomever it concerns regarding such retarded indiscretions of mine when it came to getting my point across. Some examples follow.</p>
<ul>
<li>I published a rant of epic proportions against the generic &#8220;tech blogs&#8221;. Whether they serve any purpose or not is a different question. It&#8217;s certainly none of my concern if they are useless. If that&#8217;s really the case, people will stop visiting them and they&#8217;ll die out. Regarding this incident, I&#8217;m sorry about the insane douchebaggery I unleashed upon them and the persons in charge.
  </li>
<p><br/></p>
<li>I used to be full of contempt towards those who didn&#8217;t have a clue about open source or were willfully ignorant about it. I&#8217;d pitch in with particularly caustic and scathing comments right on their face. Behind their back, I&#8217;d bitch and make fun of them. I now see it was misplaced. It&#8217;s certainly none of my business if they don&#8217;t care. People are free to choose what they want, believe what they believe is the truth. I&#8217;d like to apologize to all such persons who have come across such behavior from me. It&#8217;s totally uncalled for.
  </li>
<p><br/></p>
<li>..and a lot more I don&#8217;t recall. </li>
</ul>
<p>Apart from these specific instances, I&#8217;m genuinely sorry for all the hateful speech and contempt directed towards individuals and groups just because their line of thought didn&#8217;t align with mine. (<em>This does not cover the posts against the university administration though. That was some serious nonsense going on there. I&#8217;m glad to have got out of it all.</em>) I also plan to personally apologize to the people I can recall having done the same to. There&#8217;s nothing better to start the new year than to shed the bad blood.</p>
<p>PS: This is for all the geeks/hackers/whatever-you-want-to-call-yourself and not quite related to this post. You might be really good with computers and be able to make them bend at your will. But please don&#8217;t treat that as a license to treat others like shit or get your nose up in the air. Realize that you might be completely useless for something that the other person might be a wizard in. Having a strong analytic bent of mind that is capable of following logic and reason is an enviable trait but don&#8217;t give yourself too much credit for it. When you do that, you start to feed your ego. You start to believe you are eligible to comment on anything and everything just because you are a &#8220;hacker&#8221;. Case in point, Hacker News. It&#8217;s beyond doubt that a lot of the users who hang around there are awesome hackers who&#8217;d rock the socks off most of the novices like me. But when you look at the comments on the submissions completely unrelated to programming or technology, one thing becomes amply clear. There&#8217;s a lot of nonsense hidden in verbiage from people who think they are awesome in the concerned field and under the implicit assumption that since such stuff is of interest to them, it automatically makes them experts. &#8220;Field of interest&#8221; and &#8220;expertise&#8221; are two very different things. The only thing it leads to is even more verbiage in ensuing threads of conversations which go round and round without contributing anything of real value. Except for feeding the ego of said persons. The internet makes it convenient to have a debate stretch endlessly on nothing but semantic bullshittery without either party conceding anything. So, in the end nothing of value is created. I know this might touch a few raw nerves and I might get flamed too. Still, think about it. The mental energy and space you get to play with when you let go of the ego is immense <img src='http://anomit.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .</p>
<img src="http://feeds.feedburner.com/~r/anomit/~4/Ai8cpngI6s4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://anomit.com/2010/12/27/apologies/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Examining the Linux VDSO</title>
		<link>http://anomit.com/2010/04/18/examining-the-linux-vdso/</link>
		<comments>http://anomit.com/2010/04/18/examining-the-linux-vdso/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 21:06:52 +0000</pubDate>
		<dc:creator>anomit</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[syscalls]]></category>
		<category><![CDATA[system calls]]></category>

		<guid isPermaLink="false">http://anomit.com/?p=200</guid>
		<description><![CDATA[I have been recently looking into the sysenter/sysexit way of implementing system calls on Linux. It&#8217;s then that I came to know about the concept of VDSO (Virtual Dynamic Shared Object). It may look hacky to some but IMO, it&#8217;s quite an elegant and practical solution to overcome the incompatibilities that might be introduced if [...]]]></description>
			<content:encoded><![CDATA[<p>I have been recently looking into the sysenter/sysexit way of implementing system calls on Linux. It&#8217;s then that I came to know about the concept of VDSO (Virtual Dynamic Shared Object). It may look hacky to some but IMO, it&#8217;s quite an elegant and practical solution to overcome the incompatibilities that might be introduced if it was left to the userland libraries like libc to use the software interrupt or sysenter/sysexit mechanism. You will get more information about the VDSO <a href="http://www.trilithium.com/johan/2005/08/linux-gate/">here</a> than I could ever dig into.</p>
<p>Even though the post linked above is very informative, it suffers from the same problems that plague most of the resources on linux kernel on the web. A couple of things are outdated there which could seriously put you off if you like to get your hands dirty along with reading such stuff.</p>
<ul>
<li>
It states that the VDSO is</p>
<blockquote><p>a shared object exposed by the kernel at a fixed address in every process&#8217; memory</p></blockquote>
<p>Unfortunately, this isn&#8217;t the case anymore. It might have been true for the <2.6.15 kernels but it certainly isn't that way on my 2.6.32 kernel. To get an idea, try this command a few times:<br />
<code>cat /proc/self/maps | fgrep vdso</code><br />
This will give you the memory map of the process running `cat` itself. You will see the memory address to which the vdso is mapped is different each time rather than the fixed mapping to 0xffffe000 as the post claims, which brings us to our second problem.
</li>
<li>
Assuming the fixed mapping at 0xffffe000, the post tells you to use dd to extract the relevant information by accessing the process' pages through /proc/self/mem.<br />
<code>dd if=/proc/self/mem of=linux-gate.dso bs=4096 skip=1048574 count=1</code><br />
But things aren't the same now. You will never know the pages to skip over because the VDSO is always mapped at a different location everytime you run the `dd` command. If you don't believe me, try it out yourself.
</li>
</ul>
<p>To overcome this problem, I created this small script in python which will extracts the VDSO from its own mapping into a file and then you can use `objdump` to examine it.</p>
<pre class="brush: python; title: ;">
&quot;&quot;&quot;
This script writes the VDSO to the file linux-gate.dso.1 .
Use `objdump -d linux-gate.dso.1` to examine it.
You might also want to play around more with the other objdump options and
the readelf tool <img src='http://anomit.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 

LICENSE: MIT License ( http://www.opensource.org/licenses/mit-license.php )
&quot;&quot;&quot;
from __future__ import with_statement
import os
import re

## regex pattern for finding out the memory address range from the output line
pattern = re.compile(r'[\w\d]+-[\w\d]+')
with open('/proc/self/maps', 'r') as file:
    for line in file:
        line = line.rstrip()
        if '[vdso]' in line:
            addr_range = pattern.findall(line)[0]
            start_addr, end_addr = [int(addr, 16)
                                    for addr in addr_range.split('-')]

fd = os.open('/proc/self/mem', os.O_RDONLY)
os.lseek(fd, start_addr, os.SEEK_SET)
buf = os.read(fd, (end_addr-start_addr))

with open('linux-gate.dso.1', 'w') as file:
    file.write(buf)
    file.close()
os.close(fd)
</pre>
<p>I also created a <a href="http://gist.github.com/369785">github gist</a> in case you need to track any further corrections to problems that might arise later on or maybe fork it.</p>
<img src="http://feeds.feedburner.com/~r/anomit/~4/tAt9FDBkLMM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://anomit.com/2010/04/18/examining-the-linux-vdso/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Symlinks in a libfs virtual file system: The Pains</title>
		<link>http://anomit.com/2010/01/07/symlinks-in-a-libfs-virtual-file-system-the-pains/</link>
		<comments>http://anomit.com/2010/01/07/symlinks-in-a-libfs-virtual-file-system-the-pains/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 18:44:22 +0000</pubDate>
		<dc:creator>anomit</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[file systems]]></category>
		<category><![CDATA[inode]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[libfs]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[syscalls]]></category>
		<category><![CDATA[vfs]]></category>

		<guid isPermaLink="false">http://anomit.com/?p=192</guid>
		<description><![CDATA[The only documentation you have got for libfs is the code itself, which says a lot about the hoops I had to jump through to at least get this thing working I still don&#8217;t claim that I know all the innards of libfs regarding this functionality. It took me around 3 days, poking around the [...]]]></description>
			<content:encoded><![CDATA[<p>The only documentation you have got for libfs is the code itself, which says a lot about the hoops I had to jump through to at least get this thing working <img src='http://anomit.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I still don&#8217;t claim that I know all the innards of libfs regarding this functionality. It took me around 3 days, poking around the code and quite a few kernel freezes and oops to figure this thing out. Also to be noticed is the fact is that this is for a non disk-based file system like /proc. I guess things will be different when it&#8217;s done on file systems like ext3.<strong></p>
<p>WARNING:</strong> This is quite a long drawn post as I haven&#8217;t presented the solution in a straight forward manner. I have rather chosen to ramble on about my personal experience. For a no-nonsense, tldr-free answer, <a href="http://stackoverflow.com/questions/1966856/implementing-symlinks-in-a-virtual-file-system/1992387#1992387">visit this</a></p>
<p>To begin with, I was absolutely clueless. All this time I had been using `ln -s` or symlink() without worrying about what really goes on under the hood. Imagine my frustration when ln -s didn&#8217;t work on the file system. I had completely neglected this part and thought that it&#8217;d be somehow taken care of automagically.<br />
<a href="http://anomit.com/wordpress/wp-content/uploads/2010/01/625.jpg"><img src="http://anomit.com/wordpress/wp-content/uploads/2010/01/625-282x300.jpg" alt="" title="only-so-many-tears-you-can-cry" width="282" height="300" class="alignright size-medium wp-image-194" /></a><br />
Going through a few online resources, I managed to deduce that the symlink operation takes place on inode level (whatever you make of that). Looking through the fields of <a href="http://lxr.linux.no/#linux+v2.6.32/include/linux/fs.h#L719">struct inode </a>, I found <b>struct i_op</b> for inode operations. Referring to both the book Understanding Linux Kernel and the source code for the fields in <b>i_op</b>, I found out a few function pointers that had something to do with links, namely <b>symlink()</b>, <b>readlink()</b>, <b>follow_link()</b>. </p>
<p>Initially, I thought of implementing <b>getattr()</b> so that it&#8217;d return a S_IFLNK for the symlink but the idea of having to handle attribute generation for the rest of the dentry objects was too much for my puny brain and this plan was discarded.</p>
<p>Reading through <b>man 2 symlink</b>, I came across this and instantly everything was clear:</p>
<blockquote><p>Symbolic links are interpreted at run time as if the contents of the link had been substituted into the path being followed to find a file or directory.</p></blockquote>
<p>First, I changed the file creation function a bit for the symlink so that the proper st_mode is set and put the target location string in the <b>i_private</b> field of the inode structure.</p>
<p>Then I created a <b>inode_operations</b> structure and put in the following function definitions:</p>
<pre class="brush: cpp; title: ;">
static void *sample_follow_link (struct dentry *dentry, struct nameidata *nd)
{
    nd-&gt;depth = 0;
    nd_set_link(nd, (char *)dentry-&gt;d_inode-&gt;i_private);
    return NULL;
}

static struct inode_operations sample_inode_ops = {
    .readlink = generic_readlink,
    .follow_link = sample_follow_link,
};

....
//in the function for the dentry and inode creation
inode-&gt;i_op = sample_inode_ops
</pre>
<p>But it was the second step that actually took up the most of my time. I read the man page of <b>readlink</b> and naively put in my own readlink implementation.</p>
<pre class="brush: cpp; title: ;">
int sample_readlink(struct dentry *dentry, char __user *buffer, int buflen)
{
    unsigned long ret = copy_to_user(buffer, dentry-&gt;d_inode-&gt;i_private, buflen);
    if (ret == 0)
        return buflen;
    else
        return -EFAULT;
}
</pre>
<p>Kernel freezes happened and wailing of wolves were heard in the distance. Putting in just generic_readlink didn&#8217;t solve the problem either with kernel oops happening every time ls was run. Looking into the<a href="http://lxr.linux.no/#linux+v2.6.32/fs/namei.c#L2810"> function definition</a> of <strong>generic_readlink</strong> in the kernel source, I found it&#8217;s mentioned in the corresponding comment block that <b>follow_link</b> needs to be implemented for it to work. Looking into the code for ext2 gave me some idea about dealing with <b>struct nameidata</b>. Problem solved <img src='http://anomit.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/anomit/~4/8vQmOtTG-H8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://anomit.com/2010/01/07/symlinks-in-a-libfs-virtual-file-system-the-pains/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Small rant on the FUSE API reference</title>
		<link>http://anomit.com/2009/12/15/small-rant-on-the-fuse-api-reference/</link>
		<comments>http://anomit.com/2009/12/15/small-rant-on-the-fuse-api-reference/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 13:02:48 +0000</pubDate>
		<dc:creator>anomit</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[file systems]]></category>
		<category><![CDATA[fuse]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://anomit.com/?p=190</guid>
		<description><![CDATA[I generally don&#8217;t rant on such well established open source projects simply because I&#8217;m not even remotely qualified to do so. But I&#8217;ll be making an exception this time. The bad cold I got more than a week back simply refuses to go away and poorly thought out documentation rules really raised my hackles this [...]]]></description>
			<content:encoded><![CDATA[<p>I generally don&#8217;t rant on such well established open source projects simply because I&#8217;m not even remotely qualified to do so. But I&#8217;ll be making an exception this time. The bad cold I got more than a week back simply refuses to go away and poorly thought out documentation rules really raised my hackles this time.</p>
<p>Sample this from the <a href="http://fuse.sourceforge.net/doxygen/structfuse__operations.html#897d1ece4b8b04c92d97b97b2dbf9768">struct fuse_operations documentation page</a>:</p>
<pre class="brush: cpp; title: ;">int(* fuse_operations::write)(const char *, const char *, size_t, off_t, struct fuse_file_info *) </pre>
<p>Read the accompanying description. How is someone who is just starting off with FUSE supposed to know what the two char *&#8217;s are for? So off you go looking into source code provided by some tutorial and thereby waste at least 10 minutes in the process. </p>
<p>As it can be seen the docs have been generated by Doxygen. I know it&#8217;s &#8220;experimental&#8221; and all but is it really that difficult to write a bit more detailed comment on the function?</p>
<img src="http://feeds.feedburner.com/~r/anomit/~4/9b5n7Fgdv3Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://anomit.com/2009/12/15/small-rant-on-the-fuse-api-reference/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Kernel module debugging: a simple technique</title>
		<link>http://anomit.com/2009/11/04/kernel-module-debugging-a-simple-technique/</link>
		<comments>http://anomit.com/2009/11/04/kernel-module-debugging-a-simple-technique/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 17:59:05 +0000</pubDate>
		<dc:creator>anomit</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[module]]></category>

		<guid isPermaLink="false">http://anomit.com/?p=186</guid>
		<description><![CDATA[Disclaimer: I have only started out with developing kernel modules and even novice would be an overstatement to describe my current skills. What follows is stuff I gathered from different sources while trying to debug a kernel oops due to a module: some googled, some from the LDD3 book which finally put together gives more [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: I have only started out with developing kernel modules and even novice would be an overstatement to describe my current skills. What follows is stuff I gathered from different sources while trying to debug a kernel oops due to a module: some googled, some from the <a href="http://lwn.net/Kernel/LDD3/">LDD3 book</a> which finally put together gives more or less a basic strategy to start with debugging a kernel module. </p>
<p>As I figured out from reading LDD3, you can use one of these tools to debug a module</p>
<ul>
<li>plain ol&#8217; gdb</li>
<li>kgdb</li>
<li>kdb</li>
</ul>
<p><strong>kgdb</strong> doesn&#8217;t really strike me as something I will be needing in the near future. I&#8217;m quite sure I won&#8217;t be taking the trouble to find another system to set up a debug session. But for all I know it might be invaluable to those involved in serious work.</p>
<p><strong>kdb</strong> requires you to patch the kernel. I&#8217;ll admit I didn&#8217;t try this out of sheer laziness.</p>
<p><strong>gdb</strong> should be a part of the arsenal of even a half-serious programmer and in my case, it was. There are just a few things that need to be in place before you start using it. First, you need the uncompressed kernel image, <strong>vmlinux</strong> (not <strong>vmlinuz</strong>). Second, you need to compile the kernel with some extra options to help you with debugging. This one is again from the LDD3 book, Chapter 4. </p>
<pre>
CONFIG_DEBUG_KERNEL* <br/>
CONFIG_DEBUG_SLAB<br/>
CONFIG_DEBUG_PAGEALLOC<br/>
CONFIG_DEBUG_SPINLOCK<br/>
CONFIG_DEBUG_SPINLOCK_SLEEP<br/>
CONFIG_INIT_DEBUG*<br/>
CONFIG_DEBUG_INFO*<br/>
CONFIG_MAGIC_SYSRQ<br/>
CONFIG_DEBUG_STACKOVERFLOW<br/>
CONFIG_DEBUG_STACK_USAGE<br/>
CONFIG_KALLSYMS*<br/>
CONFIG_IKCONFIG*<br/>
CONFIG_IKCONFIG_PROC*<br/>
CONFIG_ACPI_DEBUG<br/>
CONFIG_DEBUG_DRIVER<br/>
CONFIG_SCSI_CONSTANTS<br/>
CONFIG_INPUT_EVBUG<br/>
CONFIG_PROFILING*<br/>
</pre>
<p>It&#8217;s not that all of these are absolutely necessary to get any kind of debugging work done but you never know what kind of oops/kernel panic you might be facing. Still I have starred the ones that I feel *must* be enabled. But don&#8217;t go by my words, compile and recompile to find out the truth <img src='http://anomit.com/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>With all the yak mowing out of the way, you can finally start debugging the module with your freshly recompiled kernel.</p>
<p>Start the debugger with<br />
<code>#gdb /usr/src/linux/vmlinux /proc/kcore</code></p>
<p>But gdb doesn&#8217;t yet know where to find the module&#8217;s code and data sections. You can either do it manually by going into /sys/module/module_name/sections, cat-ing the values of .text, .data and .bss and then this command at the gdb prompt</p>
<pre>
(gdb)add-symbol-file /path/to/module 0xd081d000 \  # .text<br/>
 		-s .data 0xd08232c0 \<br/>
		-s .bss  0xd0823e20<br/>
</pre>
<p>or this shell script will output the whole command for you:</p>
<pre class="brush: bash; title: ;">
#!/bin/bash
#
# gdbline module image
#
# Outputs an add-symbol-file line suitable for pasting into gdb to examine
# a loaded module.
#
cd /sys/module/$1/sections
echo -n add-symbol-file $2 `/bin/cat .text`

for section in .[a-z]* *; do
    if [ $section != &quot;.text&quot; ]; then
	echo  &quot; \\&quot;
	echo -n &quot;	-s&quot; $section `/bin/cat $section`
    fi
done
echo
</pre>
<p>This information is again thanks to the LDD3 author Corbet, from <a href="http://lwn.net/Articles/90913/">this article</a>. What would I have done without his book and articles?!</p>
<p>The module I was trying to debug was causing an oops due to null pointer dereferencing, which actually has been the source of quite a few vulnerabilities in the mainline kernel source. The following is what it looked like (got it from dmesg)</p>
<pre class="brush: plain; light: true; title: ;">
[27570.020736] BUG: unable to handle kernel NULL pointer dereference at 00000018
[27570.020747] IP: [&lt;e07b3c31&gt;] :plan9_net:socknet_connect+0xd1/0x110
[27570.020760] *pde = 00000000
[27570.020767] Oops: 0000 [#1] SMP
[snip]
[27570.020939]
[27570.020945] Pid: 8622, comm: bash Tainted: P          (2.6.27-14-generic #1)
[27570.020951] EIP: 0060:[&lt;e07b3c31&gt;] EFLAGS: 00010296 CPU: 0
[27570.020960] EIP is at socknet_connect+0xd1/0x110 [plan9_net]
[27570.020966] EAX: 00000000 EBX: 00000000 ECX: 00000000 EDX: 00f60000
[27570.020971] ESI: de4182a8 EDI: 00000002 EBP: dddedf20 ESP: dddedef4
[27570.020977]  DS: 007b ES: 007b FS: 00d8 GS: 0033 SS: 0068
[27570.020983] Process bash (pid: 8622, ti=dddec000 task=c198f110 task.ti=dddec000)
[27570.020988] Stack: 00000000 de4189f0 de418308 17000002 0101a8c0 d0f85494 d0f85493 de4ed200
[27570.021004]        dda077c0 d0f85494 d0f85493 dddedf54 e07b37d5 00000000 dd4fd340 dda077c0
[27570.021019]        0000000e dd4fd340 de4182a8 d0f85493 dd4fd340 d0f85480 09582808 caaa4540
[27570.021035] Call Trace:
[27570.021041]  [&lt;e07b37d5&gt;] ? tcp_n_ctl_process+0x145/0x170 [plan9_net]
[27570.021053]  [&lt;e07b3505&gt;] ? slashnet_write_file+0x185/0x190 [plan9_net]
[27570.021070]  [&lt;c01b2c70&gt;] ? vfs_write+0xa0/0x110
[27570.021081]  [&lt;e07b3380&gt;] ? slashnet_write_file+0x0/0x190 [plan9_net]
[27570.021092]  [&lt;c01b2db2&gt;] ? sys_write+0x42/0x70
[27570.021101]  [&lt;c0103f7b&gt;] ? sysenter_do_call+0x12/0x2f
[27570.021110]  [&lt;c0380000&gt;] ? __down_killable+0x60/0xd0
[27570.021121]  =======================
[27570.021124] Code: 7b e0 bb ff ff ff ff e8 cc ab bc df eb 8f 8b 58 0c 8d 55 e0 b9 10 00 00 00 c7 04 24 00 00 00 00 ff 53 10 89 c3 8b 45 f0 8b 40 14 &lt;8b&gt; 40 18 c7 04 24 5c 3e 7b e0 89 44 24 04 e8 9a ab bc df 85 db
[27570.021211] EIP: [&lt;e07b3c31&gt;] socknet_connect+0xd1/0x110 [plan9_net] SS:ESP 0068:dddedef4
[27570.021235] ---[ end trace 1d54537d6fc8b3bc ]---
</pre>
<p>Phew that&#8217;s a lot of information! You get a dump of all the register values, the stacktrace, codetrace etc in an oops message. I&#8217;ve given a couple of links at the end that deal with all the information present. Refer to them for more details.</p>
<p>For now, we can see that something was executed in the socknet_connect section at an offset of 0xd1 which caused the null pointer dereference. We&#8217;re very close to finding out the errant piece of code now. Just do the following in the gdb prompt to home in right on the culprit statement  :<br />
<code>(gdb)list *socknet_connect+0xd1</code></p>
<p>..and we are done! Pretty simple and basic, wasn&#8217;t it?</p>
<p>These two links are really good for pointers on how to look for the necessary information in an oops message</p>
<ul>
<li><a href="http://lkml.indiana.edu/hypermail/linux/kernel/0303.1/0009.html">Re: what&#8217;s an OOPS</a> by John Bradford from LKML</li>
<li>A very detailed <a href="http://article.gmane.org/gmane.linux.kernel/624292">oops report analysis</a> that&#8217;ll really help you with &#8216;how to get from bug report to the source of bug&#8217;</li>
</ul>
<p>I&#8217;ve been also trying to use the offset information with the disassembled module to figure out which part of the source code it might actually correspond to. I haven&#8217;t met with much success though.</p>
<img src="http://feeds.feedburner.com/~r/anomit/~4/seBZxQkOQq0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://anomit.com/2009/11/04/kernel-module-debugging-a-simple-technique/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vim/Cscope quickie</title>
		<link>http://anomit.com/2009/10/10/vimcscope-quickie/</link>
		<comments>http://anomit.com/2009/10/10/vimcscope-quickie/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 19:51:26 +0000</pubDate>
		<dc:creator>anomit</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[cscope]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://anomit.com/2009/10/10/vimcscope-quickie/</guid>
		<description><![CDATA[You can find a detailed tutorial here. This will only help you get started real soon with cscope. First of all, you need to have cscope support compiled with your vim installation. Packages for vim offered by Ubuntu and Fedora already have it. In Gentoo, I had to add app-editors/vim cscope in /etc/portage/package.use for the [...]]]></description>
			<content:encoded><![CDATA[<p>You can find a detailed tutorial <b><a href="http://cscope.sourceforge.net/cscope_vim_tutorial.html">here</a></b>. This will only help you get started real soon with cscope.</p>
<p>First of all, you need to have cscope support compiled with your vim installation. Packages for vim offered by Ubuntu and Fedora already have it. In Gentoo, I had to add <code>app-editors/vim cscope</code> in /etc/portage/package.use for the cscope USE flag to be enabled.</p>
<p>With all the yak shaving out of the way, get down straight to business. Move over the cursor to any symbol or identifier in your source code and try out these:</p>
<ul>
<li><b>Ctrl-\-s</b>: Search results are loaded in the same window</li>
<p></p>
<li><b>Ctrl-Space-s</b>: Search results are loaded in a horizontally split window</li>
<p></p>
<li><b>Ctrl-Space(twice)-s</b>: Search results are loaded in a vertically split window</li>
</ul>
<p>You should also check out a link within that tutorial that will help you to use cscope efficiently with a large codebase such as the Linux kernel <img src='http://anomit.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><b>PS:</b> I forgot to mention that you need to install the plugin for these keymappings from the sourceforge project whose tutorial link I have already posted. </p>
<img src="http://feeds.feedburner.com/~r/anomit/~4/7dB6cmvYnnw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://anomit.com/2009/10/10/vimcscope-quickie/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PyCon India or Code Jam?</title>
		<link>http://anomit.com/2009/09/15/pycon-india-or-code-jam/</link>
		<comments>http://anomit.com/2009/09/15/pycon-india-or-code-jam/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 17:35:32 +0000</pubDate>
		<dc:creator>anomit</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[My Life]]></category>
		<category><![CDATA[code jam]]></category>
		<category><![CDATA[PyCon]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://anomit.com/?p=175</guid>
		<description><![CDATA[That is something which has been playing at the back of my mind for the past couple of days. Google Code Jam Round 2 is on 26th Sept, 21:30 IST, right on the day PyCon India starts. In theory I can attend the 1st day of PyCon and compete in GCJ as well at night. [...]]]></description>
			<content:encoded><![CDATA[<p>That is something which has been playing at the back of my mind for the past couple of days. <a href="http://anomit.com/wordpress/wp-content/uploads/2009/09/not-this-shit-again.jpg"><img src="http://anomit.com/wordpress/wp-content/uploads/2009/09/not-this-shit-again-230x300.jpg" alt="not this shit again" title="not this shit again" width="230" height="300" class="alignright size-medium wp-image-176" /></a></p>
<p>Google Code Jam Round 2 is on 26th Sept, 21:30 IST, right on the day PyCon India starts. In theory I can attend the 1st day of PyCon and compete in GCJ as well at night. But there are a few small things that have been bugging me like the possibility of being dead tired at the end of the day, lack of a decent internet connection etc. </p>
<p>Advancing to round 3 of GCJ would require me to be placed within the top 500 of the 3000 competitors. Effectively I have about 5-6 days in total to prepare for it, excluding the useless exams in between and a ~500 rank isn&#8217;t asking for too much. This is one of the reasons that I&#8217;m inclined to stay back instead of taking on a 10 hour long overnight bus journey coupled with running around the city for a whole day.</p>
<p>If you are reading this, put in a few words of wisdom (considering you have them at your disposal).</p>
<img src="http://feeds.feedburner.com/~r/anomit/~4/XzgIZa5N-ZY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://anomit.com/2009/09/15/pycon-india-or-code-jam/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Param and TechTatva 2009</title>
		<link>http://anomit.com/2009/09/04/param-and-techtatva-2009/</link>
		<comments>http://anomit.com/2009/09/04/param-and-techtatva-2009/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 05:43:27 +0000</pubDate>
		<dc:creator>anomit</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[My Life]]></category>
		<category><![CDATA[college]]></category>

		<guid isPermaLink="false">http://anomit.com/?p=169</guid>
		<description><![CDATA[TechTatva is the technical fest held at my college and this year it starts from 8th September, that is a few days from now. No use of rambling on about its greatness and the value it brings to an otherwise morbid campus life. Param is the category under TechTatva broadly relating to the computer science [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://techtatva.in">TechTatva</a> is the technical fest held at my college and this year it starts from 8th September, that is a few days from now. No use of rambling on about its greatness and the value it brings to an otherwise morbid campus life. </p>
<p><strong>Param</strong> is the category under TechTatva broadly relating to the computer science and IT oriented events. I&#8217;m in charge of handling it this time. Since last year, we have been trying to infuse some freshness into this category with the help of new events. Last year it was Mobivision which was about application development on two mobile platforms: Android and Symbian S60. The event was a huge hit in the college with 200+ people turning up for the workshops on PyS60 and Android development. </p>
<p>This time we have introduced an algorithm intensive event, <strong>Algosm</strong> which will be hosted and evaluated online by <a href="http://codechef.com">Codechef</a>. This is the first time such an event will be a part of Param like Mobivision was last year. We hope this will help a lot of students in our college get acquainted with the overall concept of online programming contests.</p>
<p>Outstation participants are also welcome (obviously because it&#8217;s an online event <img src='http://anomit.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  . Do check out the <a href="http://techtatva.in/09/event.php?event=algosm">Algosm page</a> on the TechTatva website for more details.</p>
<img src="http://feeds.feedburner.com/~r/anomit/~4/oEspaAg_xgY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://anomit.com/2009/09/04/param-and-techtatva-2009/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Proof of suckage, early 2008</title>
		<link>http://anomit.com/2009/09/02/proof-of-suckage-early-2008/</link>
		<comments>http://anomit.com/2009/09/02/proof-of-suckage-early-2008/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 19:54:51 +0000</pubDate>
		<dc:creator>anomit</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[etherboot]]></category>
		<category><![CDATA[pointers]]></category>

		<guid isPermaLink="false">http://anomit.com/2009/09/02/proof-of-suckage-early-2008/</guid>
		<description />
			<content:encoded><![CDATA[<p?<br />
I applied for the <a href="http://etherboot.org">Etherboot</a> project in the 2008 edition of GSoC. Looking back it doesn&#8217;t look like a wise decision at all since my C skills sucked hairy camel balls back then.
</p>
<p>
Today I was reading through some shellcode and buffer overflow attack basics and just about an hour ago from now I happened to remember this certain question that a couple of developers from the Etherboot project asked me during the IRC screening of potential candidates. Right now it took me less than 5 minutes to come up with a hopefully correct solution. Back then I was absolutely at a loss how to even begin coding the problem. The logic of the problem is really simple but I had no idea how to put it down in concrete code. Take a look at the problem and the solution below and laugh at me.
</p>
<pre class="brush: cpp; title: ;">
/**
* Search memory for a 32-bit pattern match on a 32-bit boundary
*
* @v start             Start address of region to search
* @v len      Length of region, in bytes
* @v pattern           Pattern to search for
* @v mask              Mask of which bits in the pattern we care about
* @ret found           First address at which pattern is found
*
*
* The mask is used to indicate that we care about only part of the
* pattern matching.  For example, suppose we wanted to search the
* region for words of the form
*
*   0xabcdXXXX
*
* where X indicates that we don't care about that digit (i.e. that we
* would want to match on 0xabcd0000, or 0xabcd1234, or 0xabcdffff,
* etc.).  We would then call memsearch() as
*
*   memsearch ( start, len, 0xabcd0000, 0xffff0000 );
*/
#include &lt;stdint.h&gt;
#include &lt;stdio.h&gt;

uint32_t *memsearch ( uint32_t *start, size_t len, uint32_t pattern, uint32_t mask )
{
    uint32_t s = (uint32_t)start;
    while(len--)
    {
        s++;
        if ( (s &amp; mask) == (pattern &amp; mask) )
            return (uint32_t *)s;
    }
    return NULL;
}

int main()
{
    uint32_t result;
    printf(&quot;Result is: %x&quot;, (result=(uint32_t)memsearch ( 0x00000000, 4294967295u,\
0x000000df, 0x000000ff ))?result:0);
    return 0;
}
</pre>
<img src="http://feeds.feedburner.com/~r/anomit/~4/hbpMDuDOO5k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://anomit.com/2009/09/02/proof-of-suckage-early-2008/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Loki: my attempt at creating an online judge</title>
		<link>http://anomit.com/2009/07/05/loki/</link>
		<comments>http://anomit.com/2009/07/05/loki/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 06:35:51 +0000</pubDate>
		<dc:creator>anomit</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[loki]]></category>
		<category><![CDATA[signals]]></category>
		<category><![CDATA[system calls]]></category>

		<guid isPermaLink="false">http://anomit.com/2009/07/05/loki/</guid>
		<description><![CDATA[This would be my first major open source contribution out in the wild. If you don&#8217;t know what an online judge means here, consider paying a visit to SPOJ or UVa Online Judge to get an idea. I wrote a mammoth README file that covers all the issues and features of this system and won&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>
This would be my first major open source contribution out in the wild. If you don&#8217;t know what an online judge means here, consider paying a visit to <a href="http://www.spoj.com">SPOJ</a> or <a href="http://uva.onlinejudge.org/">UVa Online Judge</a> to get an idea.
</p>
<p>
I wrote a mammoth README file that covers all the issues and features of this system and won&#8217;t waste much space here copying the same thing. It&#8217;s hosted at <a href="http://github.com">github</a> (Rohit, happy now? <img src='http://anomit.com/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ). Head over to the <strong><a href="http://github.com/anomit/loki">main tree of the project.</a></strong>. Right now the source needs ugly hackery to get it running on another computer but I promise to correct that in the next few releases. Till then I believe the README you will help you out.
</p>
<p>
Oh, the code is licensed GPLv2 by the way. But you already knew it, didn&#8217;t you? <img src='http://anomit.com/wordpress/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /></p>
<img src="http://feeds.feedburner.com/~r/anomit/~4/ZXTE-jdgAaI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://anomit.com/2009/07/05/loki/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

