A recursive gdb script for Binary Trees

If we have a binary tree, how do we determine if a key is in the tree? Or another way, how do we know it was inserted correctly? We need to visit every node.

This is a recursive problem and hard to do in gdb. The convenience variables are very simple and we can not implement a stack with them. Since they are effectively global, we can not even hide them in stack frames. What we can do is use concatenation and a node-id to generate uniqe names.

Note that you have to do all variable name declarations before you do any recursion. That statement will make sense when you violate it and wonder why your output looks strange.

define indentby
        printf "%*c", $arg0+1, 0
end

define walk_btree
        set $node_$arg0 = ($arg1)
        set $fnum_$arg0 = ($arg2)
        set $branch_$arg0 = ($arg3)
        set $space_$arg0 = ($arg4)
        set $leftid_$arg0 = $arg0 + 1
        set $rightid_$arg0 = $arg0 + 2

        indentby $space_$arg0
        printf "%d %u (btree *)%p\n", $branch_$arg0, $node_$arg0->e_type[$fnum_$arg0].key, $node_$arg0

        set $space_$arg0 = $space_$arg0 + 1
        if $node_$arg0->e_type[$fnum_$arg0].left
                walk_btree $leftid_$arg0 $node_$arg0->e_type[$fnum_$arg0].left $fnum_$arg0 0 $space_$arg0
        end
        if $node_$arg0->e_type[$fnum_$arg0].right
                walk_btree $rightid_$arg0 $node_$arg0->e_type[$fnum_$arg0].right $fnum_$arg0 1 $space_$arg0
        end
end
document walk_btree
        NODE-ID NODE FNUM BRANCH INDENTBY
        Recursively walk the binary tree printing out the key for the fnum'ed entry
end

define start_walk_btree
        walk_btree 0 $arg0 $arg1 3 1
end
document start_walk_btree
        NODE FNUM
        Recursively walk the binary tree printing out the key for the fnum'ed entry
end

Contrast that against a very simple algorithm to find if the key is in the tree (with the assumption the tree is valid).

Recursion is not needed as we lop of a branch with each iteration.

define find_btree
        set $node = $arg0
        set $fnum = $arg1
        set $key = $arg2
        set $process = 1

        while $node && $process
                if $key == $node->e_type[$fnum].key
                        set $process = 0
                else

                        if $key < $node->e_type[$fnum].key
                                $node = $node->e_type[$fnum].left
                        else
                                $node = $node->e_type[$fnum].right
                        end
                end
        end

        if $node
                printf "Found match on (btree *)%p\n", $node
        else
                printf "No matching node in the binary tree\n"
        end
end

Using xargs to apply tshark to many capture files

pktt on a filer can capture for all network interfaces on a head. In order to quickly determine which have relevant data, we can use xargs:

KinMage:csiqa-6080-15 thomas$ ls -1 *021* | xargs -I {} -t tshark -R nfs -r {}
tshark -R nfs -r e0a_20101021_090354.trc
tshark -R nfs -r e0b_20101021_090354.trc
tshark -R nfs -r e0c_20101021_090354.trc
tshark -R nfs -r e0d_20101021_090354.trc
tshark -R nfs -r e0e_20101021_090354.trc
122   0.265844 10.227.74.89 -> 10.227.67.203 NFS V4 COMPOUND Call <EMPTY> PUTFH;CLOSE;GETATTR
123   0.267673 10.227.67.203 -> 10.227.74.89 NFS V4 COMPOUND Reply (Call In 122) <EMPTY> PUTFH;CLOSE
tshark -R nfs -r e0f_20101021_090354.trc
tshark -R nfs -r e4a_20101021_090354.trc
tshark -R nfs -r lo_20101021_090354.trc
tshark -R nfs -r losk_20101021_090354.trc
KinMage:csiqa-6080-15 thomas$ cd ../csiqa-6080-16/
KinMage:csiqa-6080-16 thomas$ ls -1 *021* | xargs -I {} -t tshark -R nfs -r {}
tshark -R nfs -r e0a_20101021_090347.trc
tshark -R nfs -r e0b_20101021_090347.trc
tshark -R nfs -r e0c_20101021_090347.trc
tshark -R nfs -r e0d_20101021_090347.trc
tshark -R nfs -r e4a_20101021_090347.trc
tshark -R nfs -r lo_20101021_090347.trc
tshark -R nfs -r losk_20101021_090347.trc

So we see the first head saw NFS traffic on e0e, but the second saw none.

tshark, my new favorite command line tool

[thomas@godwit ~]> tshark -R nfs -r e4a_20101020_223437.trc
9   1.020806 10.225.212.66 -> 10.225.212.116 NFS V4 NULL Call
10   1.020972 10.225.212.116 -> 10.225.212.66 NFS V4 NULL Reply (Call In 9)

Going KVM

I’ve been cleaning up the clutter in my office and as much as I like having a keyboard for each computer, it isn’t like I can type on both at the same time. So I dug out my iogear GC634U 4-port USB KVM switch. Now I like the form factor and such, but it only does VGA, despite being shown on the box working with Mac Minis.

So I’ve just hooked up the KM and left the V on the roadside. I do use multiple displays at the same time, so this is a plus.

Anyway, the hotkeys are [scroll-lock][scroll-lock] and the Macs do not have that key. You can reprogram that with [clear][-] and once the [caps-lock] starts blinking, [t]. At that point [control][control] will be your hotkey initiator. And to cycle through the attached machines, [control][control][return]. I.e., I don’t care to remember which ports I have hooked up.

I haven’t made the jump to attaching my Win7 box yet.

Oh yeah, on the two interior cable sets, the ones not always attached, the white capped connector goes to the KVM and the blue capped one (which has the USB and audio cables) goes to your computer.

How to mount nfsv4 from Snow Leopard

KinMage:~ tdh$ sudo mount -o vers=4 192.168.12.138:/photos /mnt
mount_nfs: sorry, you must specify vers=4.0alpha to use the alpha-quality NFSv4 support
KinMage:~ tdh$ sudo mount -o vers=4.0alpha 192.168.12.138:/photos /mnt
KinMage:~ tdh$ ls -la /mnt
total 23952
drwxr-xr-x   2 root  220776704     4096 Oct  7 17:31 .
drwxrwxr-t  32 root  admin         1156 Oct  4 14:01 ..
-rwxr-xr-x   1 root  daemon     1455680 Oct  7 17:31 img_0132.jpg
-rwxr-xr-x   1 root  daemon     1698991 Oct  7 17:31 img_0135.jpg
-rwxr-xr-x   1 root  daemon     1539525 Oct  7 17:31 img_0136.jpg
-rwxr-xr-x   1 root  daemon     1522669 Oct  7 17:31 img_0137.jpg
-rwxr-xr-x   1 root  daemon     1524863 Oct  7 17:31 img_0138.jpg
-rwxr-xr-x   1 root  daemon     1459397 Oct  7 17:31 img_0140.jpg
-rwxr-xr-x   1 root  220776704  1505265 Oct  7 17:31 img_0141.jpg
-rwxr-xr-x   1 root  daemon     1470002 Oct  7 17:31 img_0143.jpg

Mac Book Air struggles with external monitor

My first generation MBA is having a hard time driving my Dell 2005FPW at 1680 x 1050 @60Hz. For general purpose use, i.e., web browsing, it would be sufficient. But for extended use and coding, I’m not seeing as crisp an output as I would like.

I do have to admit, a green on black iTerm is crisp, but black on white, like in Thunderbird or a web entry form, causes me eye strain.

Still struggling with wanting a new computer

So, I want a Mac Mini, but I basically really want a bare bones system that I can then flesh out. I want to have the ability to put in two drives, i.e., I really don’t care about having an optical drive.

I’d like to be able to add in a pair of SSDs. I don’t even really care about paying for the 2 500G drives, they can go in an external case. Having a TB of firewire attached drives is appealing as well.

But I guess there is a price point where I don’t want to pay for Apple’s quality versus a Frankenstein’s price. And yes, I also know that the Mac will probably be quieter.

Finally doing some mail filtering

Okay, started off with some simple procmail filtering on my main account. It has been a while since I’ve used it. But I got tired of my desktop being off while I was traveling and not having Thuderbird do the filtering for me.

The one incompatibility I have is that I want to keep unfiltered email in ‘/var/spool/mail/<username>’ such that it then shows up in my Inbox for both Thunderbird and the iPhone.

I guess I’ll add a dummy account and see if I can get it to do that with procmail.

Server reinstall

I tried to update my Shuttle XPC from Fedora 11 to 13 via yum. I’m pretty sure my system got horked with respect to yum right before this effort when I did my last yum update. It stopped being able to connect to the servers.

Once I determined it was in a bad state, I went ahead and copied off most of the data I knew was important. Yes, I missed something. :->

The data I tend to copy off is:

  1. Home directories
  2. /etc
  3. /usr/local
  4. /var/<domainname> – web site(s)
  5. /var/named

What I failed to get this time was /var/spool/mail. I guess I either thought /var was too big or too deeply nested. This is despite the fact I had over 100G more free space on my backup drive than the server’s hard drive.

So I lost a couple thousand email messages that remained in inboxes rather than being saved. Since I tend to hoard the stuff, no biggie.

The install over didn’t work as well as I would have liked, so I did a fresh install. I wasn’t that impressed with the installer – it seemed to take too long discovering the disk and skipped over some of the screens (e.g., it would consistently skip over the installation method {cd} and try to find the image on a drive slice).

After the install was done, I had to configure the following services from my backup:

  1. /etc/passwd
  2. DNS
  3. mail
  4. ssh
  5. sudo
  6. dovecot
    1. Don’t forget to generate your own certificate to enable SSL
  7. httpd
  8. noip

I hate that it makes you create an user before you can do anything and it goes ahead to create a group at the same time.

The other complication here is that since this is my only DNS server, I have to do tricks on my other machines to get outside to documentation. (Hence my desire to have a backup server ready to go on a moment’s notice).