Emacs source code navigation

Recently I have been spending a lot of time with a large amount of unfamiliar C code. Navigating a code base that is unfamiliar can be quite a challenge so I went on a search for useful Emacs features to make it easier.

The most useful tool I have found is etags. With etags you can quickly jump to the source file where a function is defined. If the source file is not already open in a buffer Emacs will open it for you. First you need to generate a TAGS file. To create a TAGS file run

find . -name '*.[ch]' | xargs etags

in the top directory of the source tree. Now start Emacs. M-x visit-tags-table will prompt you for the location of the tags file. Emacs now knows the name and location of all function definitions in the source tree. To jump to a function use M-. (that’s Meta-Period) and type the name of the function. If you simply press enter Emacs will jump to the function declaration that matches the word under the cursor. Very nice. As useful as this feature is it would still be very annoying if you couldn’t easily move back to where you jumped from. To jump backwards use M-*.

The etags system also gives you the ability to do auto-completion on function names that are in the TAGS file. Here is the necessary .emacs LISP code to bind this functionality to CTRL-Tab.

(add-hook 'c-mode-common-hook
        (lambda ()
                (define-key c-mode-map [(ctrl tab)] 'complete-tag)))

If you are a fan of long descriptive function names this is a very nice feature.

Finally, if you want to use etags with your own source tree you can add a Makefile target like this:

etags:
        find . -name '*.[ch]' | xargs etags

Dogma

Earlier tonight I stumbled upon Dogma on TV. One of my all time favorite movies. I’m sure traditionally religious people are appalled by this movie but the image of God presented at the end is the most powerful and positive I have ever seen.

Why are new laws our first reaction?

For those who are new to the story last year there was a horrible abduction and murder of a young girl in Toronto named Holly Jones. See CBC — Holly Jones for a time line and some background information.

On June 16th the trial of Michael Briere, the accused murderer began. He pleaded guilty. The twist to all of this is that in he plea he explained how he was looking at child pornography on the Internet the same day he abducted and killed Holly. CTV News has an article covering this. As expected this admission has resulted in calls for new laws to punish people who possess child pornography.

Canada already has laws that cover child pornography. If Briere would have been caught with these materials on his computer or viewing them on the Internet he would have been punished by the justice system. How would stricter laws have saved Holly when the enforcement of existing laws failed to find and punish Briere?

Especially troubling are the people who believe that ISPs should be filtering all ‘bad’ content. Obviously child pornography is bad but where is the line drawn? Coming from the technical side of things it’s also pretty much impossible. Good old fashioned police work is whats needed. We need police forces capable of working with new technology and not new laws that are unenforcible.

DRM

Wow. Everyone interested in copyright and how it applies to new technologies should read this. Absolutely brilliant.

C lesson

Given:

struct mystruct myinstance;

Which is better?

sizeof(struct mystruct)
sizeof(myinstance)

Normally this doesn’t matter all that much. However, I believe the first is the better way. The reason for this is that if you use the instance variable in the sizeof you can later introduce a nasty bug should you decide that the instance should no longer exist on the stack. That is changing the first line to:

struct mystruct *myinstance;

The second form of sizeof() given above will now return the size of the pointer not the size of the structure. Boom. Nasty bug.

Subversion

My Linux QoS work has come to the point where I really need a revision control system. I normally use CVS but decided it was time to setup a Subversion repository. So far subversion is everything I expected, it’s just like CVS. When it comes to reorganizing the repository I’ll find out just how much better Subversion actually is.

The big bummer is that the current stable release of Emacs does not include Subversion support in VC-Mode. I love VC-Mode. I am not sure I can go back to coding without it. VC-Mode is so easy it encourages frequent commits. Having to run the svn command from the shell encourages the opposite.

Lightening and DSL

Reading Bob’s blog entry about lightening over here made me think about lightening and DSL modems. Bob’s quite right about computers most often getting lightening damage through the phone lines not the power lines. The fact that most DSL services use external modems actually provides a extra layer of protection for your computer. For lightening to travel the phone line and damage any of my computers it would have to go through the modem and my Ethernet switch. I’m not an electrical engineer but I suspect this isn’t very likely. Of course it’s still a good idea to unplug your DSL modem during a storm.

Fedora Core 2

I have had Fedora Core 2 installed on my three computers for a couple of weeks now. Traditionally I have used a stock install for my gateway and laptop but have customized the desktop components of my main work station. Typically this meant building the latest GNOME from source via Garnome. Fedora Core 2 is the first Linux installation I have had in a long while that I don’t have the urge to customize. Sure, I have installed some new stuff that is not part of the distribution but the core desktop components are stock. Kudos to Fedora for putting together such a good distribution. Someday I’ll take the time to get familiar with Debian but at the moment I don’t have much incentive to.

Non-open software

I finally broke down and installed some non-open software on my desktop. This is something I try hard not to do. I don’t even have the Macromedia Flash plugin installed. I installed Real player so I could listen to CBC for the election coverage. Hopefully someday CBC will use Icecast so I don’t need this installed anymore.

2^x or 10^x?

When working on my Linux QoS project I ran into the confusion over what k,M,G mean in computing again.

Here is the problem. In normal usage k = 1000. Obvious example, kilometer == 1000 meters. However in computing the factor most used is 1024. This means a kilobyte in your computer RAM is 1024 bytes == 2^10 bytes. A megabyte in RAM is 1024 * 1024 == 1048576 bytes == 2^20 bytes. The nice network and hard drive people have decided to use powers of 10 instead of powers of two. This means your 100Mbit network is 100,000,000 bits/sec not 100 * 1024 * 1024 == 104857600 bits/sec. Hard drives are also a fun example of this. A hard drive sold with a capacity of 40 GB can store 40,000,000,000 bits but your computer calculates file sizes with powers of two so this is really 40,000,000,000 / 1024 / 1024 / 1024 == 37.25 GB to your computer.

The networking example is where the problem with Linux QoS came up. The Linux traffic control utilities use a multiplier of 1024 for kbit. My upload data rate is 640kbit. When I specified 640kbit/sec via the Linux QoS utilities I was actually specifying 640 * 1024 == 655360 bits/sec not the actual line rate which is 640,000 bits/sec. Even more fun is calculating download rates from network interface speeds. The network is rated with 10^x but the KB number you see on the screen is 2^x.

Wikipedia has a great article on this whole mess. This article explains the new prefixes that have been introduced to remove this abiguity. So from now on my computer has 512Mib of RAM, the hard drive has 40Gb of capacity and the network interface is 100Mbit/sec.