Monday, May 19, 2008

LuaGrind: Easier Dynamic Binary Instrumentation Using Valgrind

For those unfamiliar with Valgrind, it's a heavy-weight dynamic binary instrumentation (DBI) framework which offers an API for writing plugins (called Valgrind tools) that leverage its DBI capabilities to do several dynamic analysis operations. The most widely used feature, if I'm not mistaken, is memory debugging. Valgrind is a real time saver when it comes to detecting the sources of memory leakage in your code, and it has helped me countless times in the past. Valgrind also has tools for cache profiling, heap profiling, thread debugging, etc. (full list here).

Why am I babbling about it? Well, in order to answer that I have to introduce you to yet another programmer's best friend: DTrace (too lazy to write more, go read about it yourself!). In my opinion, if there's one particular attribute of DTrace that makes it so appealing, it has to be its simplicity. I mean, look at what it achieves; in a completely unobtrusive manner, you're suddenly in the know of the inner workings of the deepest layers of your code. All you need is knowledge of the D language and a shell. Such information about the behavior of your code has never been so easily accessible. Valgrind, similarly, gives you a wealth of information about your code -- at a much lower level, though -- but the process you have to go through to arrive at such information is very complicated in comparison, unless a tool that extracts the exact information you need is already available. So, I've been thinking, what if DTrace's ease and accessibility could be brought to Valgrind, so that anyone could write a Valgrind tool in a scripting language? Although the complexity of writing a Valgrind tool would not be an as big an obstacle for its intended audience in comparison to DTrace's, I believe such possibility would still be appreciated.

It's been a while since I started playing with this idea in my head, but for a few weekends now, I've been actually trying to come up with a prototype. The idea was to write a Valgrind tool that's composed of a scripting language interpreter and a set of interface functions/objects that act as a bridge between the script and the Valgrind tool API. How the code is instrumented is completely dictated by the script -- the tool is simply an environment in which the script can be executed.

Initially, I thought of writing my own interpreter for a language that I had intended to make as similar as possible to D. Fortunately, not much time had passed till I realized this was not the wisest thing to do. It made much more sense to try to embed an interpreter for any existing language such as Ruby or Python. I settled for Ruby, since I found it easy to write C extensions for, and Ruby 1.9 was moving ahead nicely in the performance race. Filled with excitement, I ran immediately into the first roadblock. Valgrind has this incredibly tight restriction of not allowing tools to use any libc calls. Instead, it offers its own implementation of a subset of libc functions. Good luck trying to modify a modern, full-fledged language interpreter to use those!

However, I didn't give up. I downloaded the Ruby interpreter source and tried to switch it over to Valgrind's own libc substitutes. It was tough. I first had to get rid of the dependency on any library, since this basically means code that also relies on libc and which I can't change easily. During that process, I had to disable some language modules, but then I came across code that looked like trouble; code using traps & setjmps. My inner guts felt this won't play nicely with Valgrind, and it was difficult to replace anyway, so I started looking for a less demanding interpreter.

This time around I did my homework better, and spent more time comparing scripting languages. I finally chose Lua: it's light, fast, compilable, it's interpreter is smaller and simpler than Ruby's, and it too can be extended easily in C. In less than day, I was able to get it to compile inside a Valgrind tool, and actually run simple code snippets at certain events, so basically, phase 1 of the project is done. I called the tool luagrind, and for starters, here's a sample script that simply dumps the command line options passed to the tool:


-- Script: script.lua
print("Valgrind command line options:")
print("--begin")
for i, v in pairs(vg.clos) do
print(i, "=", v)
end
print("--end")


When luagrind starts, it creates a "Lua state" where a single global class called "vg" lives. The script simply dumps the "clos" table which is also created by luagrind using the C API, and contains all command line options. To invoke luagrind using this script, the following command is used:

valgrind --tool=luagrind --luascript=script.lua --arg1=v1 --arg2=v2 date

It doesn't matter which binary we're running here as we're not instrumenting anything yet. In the example above, I chose "date". The output looks like this:


...
==28839==
Valgrind command line options:
--begin
luascript = script.lua
arg2 = v2
arg1 = v1
--end
Sat Jun 7 03:17:38 EEST 2008
==28839==


A lot of work still has to be done. The "vg" class has to grow to support all important operations, such as defining the needs of the tool, exposing the VEX instruction set (used internally by Valgrind as an intermediate representation of instructions), and most importantly, directing the instrumentation process. More on this when more is done!

Sunday, May 11, 2008

A Google Maps View for The iPhone

If you've been following the development of location based applications under Android and the iPhone you must have seen a lot of comparison charts showing the strengths and weaknesses of each SDK and mentioning the differences. Well, now you can cross one difference off that list! As this article mentions, the iPhone SDK lacks a programmable map component similar to the MapView on Android, but you don't need to worry about that anymore; at eSpace, we've written a similar one for the iPhone! This one simply loads http://maps.google.com in a UIWebView which is displayed under a custom transparent UIView that receives touch events and translates them to Javascript Maps API calls. The source is available under the BSD license. If have questions, or if you want to merge in any cool updates, the project has a Google group at iphone-map-view@googlegroups.com. Here's a video demo of the thing in action:



Monday, March 10, 2008

Gaza

وَمَا لَكُمْ لا تُقَاتِلُونَ فِي سَبِيلِ اللَّهِ وَالْمُسْتَضْعَفِينَ مِنَ الرِّجَالِ وَالنِّسَاءِ وَالْوِلْدَانِ الَّذِينَ يَقُولُونَ رَبَّنَا أَخْرِجْنَا مِنْ هَذِهِ الْقَرْيَةِ الظَّالِمِ أَهْلُهَا وَاجْعَل لَنَا مِنْ لَدُنْكَ وَلِيًّا وَاجْعَل لَنَا مِنْ لَدُنْكَ نَصِيرًا - النساء:75

"And why should ye not fight in the cause of God and of those who, being weak, are ill-treated (and oppressed)? - Men, women, and children, whose cry is: "Our Lord! Rescue us from this town, whose people are oppressors; and raise for us from thee one who will protect; and raise for us from thee one who will help!"
Holy Quran 4:75




http://www.thepeoplesvoice.org/cgi-bin/blogs/voices.php/2008/03/03/brazilian_cartoonist_carlos_latuff_creat

Saturday, December 22, 2007

Hack: Getting Audio to Work in VMware

Ubuntu is my Linux distro of choice, and due to the 6-month release schedule they're following, I reinstall everything on my machine -- at least -- twice every year. I know it's painful, but I just don't believe in upgrades. This is in part due to an incident where upgrading left my system in limbo, and in part due to that false feeling that everything is going to work just right if you opt for a fresh install....Ahh!

Usually, I have to do a LOT of tweaks after installation, and since I don't keep a list of these modifications (or even how to apply the complicated ones), I tend to forget many of them, which leads to a lot of wasted time digging up information which I had already known but that has gone into oblivion. That's why I decided to document them here, for all the selfish reasons mentioned, and in case they're of any use to other miserable souls...

Ever started a VMware virtual machine hosted on Ubuntu just to be greeted by a dialog box telling you that sound won't be available because /dev/dsp is busy? This is because VMware uses OSS for audio output, which isn't going to work while ALSA (Ubuntu's default) is controlling the audio device. The solution is to use a wrapper (or adapter) that plugs VMware's output into ALSA instead of directly into the audio device. Here are the steps to do that:

1. Install alsa-oss:

apt-get install alsa-oss

2. Wrap the vmware-vmx binary in a script that preloads libaoss:

mv /usr/lib/vmware-server/bin/vmware-vmx /usr/lib/vmware-server/bin/vmware-vmx.original

echo -e '#!/bin/sh\n\nLD_PRELOAD=libaoss.so exec /usr/lib/vmware-server/bin/vmware-vmx.original "$@"' > /usr/lib/vmware-server/bin/vmware-vmx

chmod u=rwxs,g=rx,o=rx /usr/lib/vmware-server/bin/vmware-vmx


This was tested on the following configurations:
  • Ubuntu Feisty 7.04 + VMware Server 1.0.2/3
  • Ubuntu Gutsy 7.10 + VMware Server 1.0.4


Source: HOWTO: Solve VMWare sound (mixing?) problem once and for all

Sunday, December 16, 2007

A Picture Worth a Thousand Words

Appendectomy

Ehab, my squash buddy & sensei (or so he wishes :P) gave me this after my appendectomy operation...


Ehab, thanks buddy, and get ready for your next round of defeats real soon :)

Saturday, October 20, 2007

Google Maps not Showing in Firefox?

Is anyone having this problem too? Google Maps comes up completely blank in my Firefox. This blog post is discussing the same issue and refers to a page in Maps Help saying that the problem is in one of the configuration parameters. I changed it to the suggested value...No luck. The page also says that one of the installed plugins might be the cause. Makes sense. I started a fresh Firefox instance as a different user, and lo and behold, Maps works just as advertised. It has to be one of the plugins then. I installed all the active plugins I use on the fresh Firefox instance, anticipating that Maps screws up once the guilty plugin is installed, but it just didn't happen :( These are the plugins I have installed, if someone has an idea then please post a comment:
  • Theme: macfoxII
  • DOM Inspector
  • Download Manager Tweak
  • DownThemAll!
  • Firebug
  • Gmail Manager
  • Google Browser Sync
  • Google Gears
  • Greasemonkey
  • JavaScript Debugger
  • Tamper Data
  • User Agent Switcher
  • VideoDownloader
UPDATE: Mystery Solved!

There was a flaw in my testing methodology after installing each plugin: it's not enough to see images appearing in Maps. Those that I used to see were cached ones, but when I zoom or pan, the same problem occurs. By disabling plugins one by one, I found that the guilty one is ... DOM Inspector! Which is strange, because it's a very old (and supposedly mature) plugin developed by mozilla.og themselves. However, to be fair & not hurt the reputation of a plugin that I personally like & have relied on a lot, the problem might be more convoluted, or might only occur when certain plugins are mixed, or whatever. Anyway, since I don't have time to investigate more into this, and since a better alternative exists, I declare victory and stop at this point :)