Lonely Cactus

A life of punk, code and apathy

Monday, February 16, 2009

Backtrace trees


I did a hack the other day I thought came out well. I had a large program, and I wanted to know the call tree for those function that used the low-level I/O routines.

The call trees generated by Doxygen were kind of useless, because they had too much detail and couldn't tell you which call paths are the normal paths. I really just wanted to know the call trees of those functions called the low-level I/O routines when the program was run on typical input data.

So, GDB will allow you to load a file of commands using the "-x" option. I wrote a command file that set up some breakpoints and requested backtraces for those breakpoints. I also added commands so that GDB ran non-interactively. I captured the backtraces in a typescript. The command file looked something like this.

##################
set pagination off
set breakpoint pending on

## Print call tree when "open" is called
break open
command
bt 10
cont
end

run
quit
##################

The first line keeps GDB from running its "more" command. The second line tells GDB not to worry if the functions I'm trying to break on aren't loaded yet. The middle section between "break" and "end" tells GDB to print a backtrace 10 levels deep when "open" is called. I end up with a file that contains a lot of GDB backtraces.

In the above I only showed the "open" command, but, I added similar backtrace requests for open, close, read, lseek, and write.

From there I coerced the output from the run into a graphviz-friendly file with a little bit of awk. Text that looked like this

#1 0x12345678 in foo (...
#2 0x12345678 in bar (...
#3 0x12345678 in baz (...

became this

bar -> for
baz -> bar

Using a simple awk script like this

###################
BEGIN { parent = ""; child = ""; }
{
 child = parent;
 if ($1 == "#0") {
  if ($3 == "in") parent = $4; else parent = $2;
 } else if (match($1, /\#[0-9]/) != 0) {
  if ($3 == "in") parent = $4; else parent = $2;
  print parent " -> " child;
 }
}
#############

Yeah I know, you could have done it in perl in 18 characters. So what.

I finished off making my graphviz file with a bit of shell. This generated the correct Graphviz file as "test.gv" and then created the plot as "test.png".

#############
echo "digraph call {" > test.gv
echo "node [rank=sink, shape=box]; open close read write lseek __libc_open64;" >> test.gv
echo "{rank=sink; open close read write lseek __libc_open64;}" >> test.gv
echo "node [shape=ellipse];" >> test.gv
cat test.out | awk -f split.awk | sort | uniq >> test.gv
echo "}" >> test.gv
dot -Tpng test.gv -o test.png
#############

And that got me my result.

Saturday, February 14, 2009

zzz

Thursday, December 25, 2008

16/365

Christmas day.

Merry Christmas to all!

Wednesday, December 24, 2008

15/365

Everyone is sick: I sit here celebrating Christmas in a swirling miasma of phlegm.

It was a relief when we headed out for a walk in the cold, snow-covered woods. It was the same conversation as always: plans, money, music.

I keep saying the same things over and over again. I'm bored. Life should be better. I need to change something. I don't know what.

And I keep not saying the important thing.

Tuesday, December 23, 2008

14/365

Fearing the worst from the Christmas rush, I pulled into LAX Parking Lot C a couple of hours before my flight, and hopped on the van to the airport. As we rounded the loop, I saw all the TV vans were parked by Tom Bradley terminal so they could do the holiday travel photo-op. Predictable. But there was no drama, and I was in the terminal in no time. I opted for a sit-down breakfast.

I see people sometimes and think that it might be fun to walk around in their shoes. On the plane, across the aisle from me was a shave-headed mid-thirties guy, scruffy, some tattoos. He wore a T-shirt and jeans on the plane and spent most of the trip working on reading (unsimplified) Chinese from a folder, and then writing Chinese on blue-lined paper. Homework? I wondered. Translation?

When he left the plane, he pulled out a coat that was nearly a parka, with a fur-lined hood.

Monday, December 22, 2008

13/365

After my brutal, one-day work week, I headed home to pack.

I'm superstitious. If I am going to fly, I like to leave with the house scrupulously clean. But I don't have the time or the energy.

At least I got the clothes in the wash.

My brain is buzzing, buzzing.

Sunday, December 21, 2008

12/365

When I was at the tea shop in West Hollywood, I was waited on by the same cheerful man that spoke in movie quotes. I was glad that he didn't remember me from the last time, when I told him something untrue that I was too proud to recant.