Tuesday, November 6, 2012

About Choice

I watched a DVD tonight. It started with the usual previews, but then - I don't even remember why I did this - I somehow saw myself pressing that 'menu' button on my remote. Well, usually this would simply produce the usual "operation prohibited by disk" message, forcing you to actually watch (or go to the kitchen and make yourself a sandwich) the previews and commercials. Not this time! There was also no annoying "FBI warning" or anything like that, pressing the menu key on my remote got me right into the DVD's main menu. But there was this menu items called "sneak peeks" and I got very curios what this could be - and I clicked on it before even watching the main feature. Well, it were these preview videos that I just skipped by pressing that 'menu' key. I really liked this :-) Dear DVD vendors: if you force people to watch your commercials before the main feature - well, then I'll simply put the DVD into the player, press Play and the main feature will be ready to be viewed when I come back from McDonald's. But if you give me the choice of watching your commercials whenever I want to - well, maybe I like your main feature so much that I'm wondering whether there's something else that I could purchase from you ...

Saturday, April 9, 2011

Limiting page faults on the HTC Desire HD

After several happy weeks with my HTC Desire HD, I made a rather shocking discovery this week:

HTC put some hack into the kernel to kill a process after encountering more than 10 page faults !

It was really hard to believe until I discovered this in arch/arm/mm/fault.c:

void
__do_user_fault(struct task_struct *tsk, unsigned long addr,
                unsigned int fsr, unsigned int sig, int code,
                struct pt_regs *regs)
{
        struct siginfo si;
        struct task_struct *g, *p, *selected = NULL;

#ifdef CONFIG_DEBUG_USER
        if (user_debug & UDBG_SEGV) {
                printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
                       tsk->comm, sig, addr, fsr);
                show_pte(tsk->mm, addr);
                show_regs(regs);
        }
#endif
        if (sig == SIGSEGV)
                tsk->segfault_count++;

        if (tsk->segfault_count > 10) {
                tsk->segfault_count = 0;
                printk(KERN_ERR "unhandled page fault at 0x%08lx, code 0x%03x\n",
                        addr, fsr);
                show_pte(tsk->mm, addr);
                show_regs(regs);

                do_each_thread(g, p) {
                        task_lock(p);
                        if (p == tsk)
                                selected = g;
                        task_unlock(p);
                } while_each_thread(g, p);

                if (selected) {
                        printk(KERN_ERR "%s: triggered too many segfaults, force killing parent: %s\n",
                                tsk->comm, selected->comm);
                        force_sig(SIGKILL, selected);
                        return;
                }
        }

        tsk->thread.address = addr;
        tsk->thread.error_code = fsr;
        tsk->thread.trap_no = 14;
        si.si_signo = sig;
        si.si_errno = 0;
        si.si_code = code;
        si.si_addr = (void __user *)addr;
        force_sig_info(sig, &si, tsk);
}

However, looking at the kernel sources wasn't that easy as it may look like - first, you have to actually get them. One of my coworkers pointed me at this interesting article:
http://www.freedom-to-tinker.com/blog/sjs/htc-willfully-violates-gpl-t-mobiles-new-g2-android-phone - and there's a link in it to a download location.

My kernel is 2.6.32.21-g1e30168, but for some reason, this doesn't work in Germany.

The entire story started about a month ago, where I discovered some very weird crashes while debugging on my device that nobody else in the team had. It was extremely frustrating for me and I kept thinking why me, what am I doing wrong here ?

After some investigation, it pretty much looked like something during variable evaluation was killing the app. We could insert a breakpoint, run to it, but it stopped at the breakpoint and I tried to evaluate some variables, the app crashes ... silently, without anything in adb logcat.

I wrote a simple test app and a soft debugger client application (both can be found in the martins-playground module on github) and soon discovered that we were only crashing during single-threaded invokes.

So what's the difference here ? Well, there's this bug in SDB - single-stepping isn't always disabled during single-threaded invokes. However, this should only impact performance and never cause the app to actually crash. But it gave me the idea ...

As a next step, I wrote a small native C application called NativeTest which installs a SIGSEGV handler and then creates 100 page faults in a loop.

The same thing can also be accomplished with something like this:


    for (int i = 0; i < 1000; i++) {
        try {
            object o = null;
            o.GetType ();
        } catch {
        }
    }

Compile something like that with Mono for Android and execute it on the device - it should not crash.

Luckily, Mono's JIT already has a feature called "explicit null checks" and I also have a patch for the Soft Debugger to check some variable instead of using page faults for single-step and breakpoint events.

However, I'm not entirely sure whether this really covers all possible scenarios where a page fault would normally be handled gracefully. And seeing something like this in their kernel also makes me a bit worried that there might be other surprises.

Monday, March 7, 2011

Connecting to the device

Today, I tried to find a good way of establishing a TCP connection to the SDB debugging session on the device.

Unfortunately, I didn't get my HTC Desire HD's Internet pass-through working. Tried on Windows 7, Windows Vista and Linux without any success. There's something very weird going on - whenever I try to activate Settings / Wireless & networks / Internet Pass-through, it says "Turning on ..." for about 1-2 minutes, then a message pops up that it can't find HTC Sync on the computer. However, internet actually does work during this time.

I'm very disappointed that this won't work, connecting the phone to the computer's network over USB would be very useful.

To make things worse, adb doesn't work while this option is activated.

Using USB tethering also isn't an option for me since this'd require the phone network be activated all the time.

Luckily, there's still the good old 'adb forward' and I just tested this with a small app.

Tuesday, March 1, 2011

Fixing the boot loader mess

After the last dist-upgrade from OpenSuSE 11.3 to 11.4, my boot loader and MBR was updated, causing me quite some headaches.

Now, I think I found the problem - my /etc/grub.conf was broken, it had a setup (hd0) line in it. I replaced that with

setup --stage2=/boot/grub/stage2 --force-lba (hd0,6) (hd0,6)
quit

I also created a script to set things up inside VMware:

root (hd1,6)
install (hd1,6)/boot/grub/stage1-isengard d (hd0) (hd1,6)/boot/grub/stage2-isengard (hd1,6)/boot/grub/menu-isengard.lst

Using a custom stage1-isengard should also make this more robust against future GRUB updates.

This should hopefully fix it for the next dist-upgrade, so we can close the bug. The remaining instructions from my old blog post should still be up-to-date.

Thursday, February 17, 2011

Fight These Evil Clones !

One thing I learned from past mistakes is that if you don't want to have any forks, any private versions of some Open Source Software, then you have to fight these from the beginning.

As soon as you create a local copy of something "for convenience", you set yourself up having a fork with plenty of local changes in a couple of months. These convenience copies tend to age pretty quickly.

By creating a local copy of something, you make it more difficult to use the upstream version than your local one. Each time you make some trivial change to one of these files, it becomes so easy to just commit your change to the local copy and persuade yourself that you'll submit it upstream later.

Having a local copy also means you can't quickly pull and test a new version of their code, but instead need to manually copy some files. Even if you use some automated script to do that, these files will appear as modified in your source revisioning system. The same goes for any edits that you may be made in your IDE. Since most source revisioning systems don't allow certain actions such as committing or pulling new upstream versions on dirty stage, you end up committing these changes locally.

The more your local copy has aged, the more difficult it will become to make modifications and submit these modifications to the upstream maintainers.

Let's say one our your customers files a bug in six months from now. When you start debugging, you'll most likely just use the latest version of your module - without updating all these upstream sources first. So you start tracking down this bad bug, edit a few files ... and when you're done, you realize that you also modified some files in one of these these upstream libraries.

Now, you often have the problem that your patch won't apply cleanly anymore against the latest upstream version.

With a local copy of some files, you don't have all these features anymore that a source revisioning system such as git provides to you. You can't easily make patches, get newer upstream versions, etc.

* * *

When I started to work on this new debugging engine, I made a promise to the MonoDevelop team, not to use any local forks of their software anymore, but instead submit all my changes upstream and have them integrated there.

Therefor, I created a system for MonoTools where you need a checked out copy of mono and monodevelop to build; I simply created a .sln that includes all the upstream .csproj's as project references. There are also binary copies of all these .dll's in a private module on svn.

There's nothing wrong with using binary copies of some upstream library in your project - provided that this binary is always build from the upstream sources. A binary .dll can easily be replaced with a newer versions - and as soon as you want to make any modifications to it, you're forced to checkout the upstream version from github.

Friday, February 11, 2011

A new Debugging Engine for Mono Tools and Mono for Android

Today, I finally finished the massive refactoring of our debugging engine that's used in Mono Tools and Mono for Android.

The biggest news is that this new engine is now using the unmodified, original debugging libraries from MonoDevelop and the managed pdb2mdb from Cecil.

As mentioned in an earlier post, Mono Tools was previously using a locally forked copy of these libraries with plenty of local changes. Cleaning these up was a huge and complicated process, and I'd especially like to thank Michael Hutchinson and Lluis Sanchez for their great help.

This was not only about getting a few patches into the upstream version, Michael and Lluis also showed me alternative, better ways of doing certain things, helped me eliminate local hacks and replace them with good and clean solutions.

As a result, I was also able to cleanup and redesign my code in the debugging engine a lot.

In the current version, we only use the public Mono.Debugging.Client APIs in Mono Tools and Mono for Android (with the only exception of Mono.Debugging.Soft.SoftDebuggerStartInfo). The Mono.Debugging.Client.DebuggerSession is now also responsible for creating the connection to SDB - by using either launch, connect or listen mode, depending on the Mono.Debugging.Soft.SoftDebuggerStartArgs that you use.

I also turned things upside-down by making the debugger session create the debugger engine (these COM objects that are used to communicate with Visual Studio) and not the other way around. This has the great benefit that we can re-use MonoDevelop connection and error handling code.

With my new build system, it's also very easy to pull a new version from MonoDevelop and integrate it in Mono Tools / Mono for Android, it's just a matter of checking out MonoDevelop, running some script to .zip up all the required sources, unzip and build them in Visual Studio and done. In theory, it's also possible to build on Linux, building in Visual Studio just makes it a lot easier to debug things.

There are still a few tiny issues that need to be figured out, but these shouldn't take too long.

My next step is doing a bug-fixing week or two, to finally process and fix this long list of outstanding bugs.

Saturday, January 29, 2011

MonoDevelop.MonoTools

This week, I cleaned up all the local patches that we did to the Mono.Debugging libraries, split them into smaller pieces and got them integrated in the upstream versions. Many thanks to Lluis and Michael for helping me out with this.

Things are looking really good now, I did some extensive refactoring in our debugging engine, and I'm now only using the official Mono.Debugging.Client API and not Mono.Debugging.Soft anymore. This also made many of the previously added protected virtual overloads unnecessary.

The soft-debugger specific code in MonoTools is now really tiny, almost everything now talks directly to the Mono.Debugging library, we don't even have a reference on Mono.Debugger.Soft.dll anymore.

There are still a few minor issues to sort out before I can start testing, but I'm really happy with this project. We can now finally use a MonoDevelop.MonoTools.dll that's compiled entirely from the original, unmodified sources which is a great step forward in terms of long-term maintainability.