tips


11
Dec 09

Hacking with IDE plugins – fun art of binary patching

Today’s software is so much about integration. You can have a cool Java utility but if you don’t have an Eclipse plugin for that, a large % of Eclipse users won’t adopt (IDEA & NetBeans users require plugins just as much). In the consumer market the same goes for browser extensions and iPhone apps that support the main service with better accessibility.

There are different ways for integration. Usually it is done via an API. The host platform offers some hooks into the system and you can use them to add some custom functionality on top of the host (or maybe just integrate your own product with the host). The result of this in the IDE world is a plugin. JRebel (the product our team is spending the most development time on) has plugins for application servers, frameworks and even IDEs.

If you’re in luck you can achieve everything with the API, if not you need other tricks. If the API does not offer certain public methods or access to some internal fields you can go in with reflection and still use them (of course there are no guarantees if the internals stay the same on version changes).

One step further to the darkside is binary patching. By binary patching I mean adding/changing/removing methods/fields/classes in runtime. This is the approach that we had to take when writing the JRebel NetBeans plugin.

On NetBeans startup we lookup a debugger class and on success try to patch it. In 2009 and using the JVM (not native apps) it is quite an easy task, at least for the most part. No assembler involved (of course you could be using a bytecode generation library), no need to do jumps to correct offsets or even read assembler (or bytecode).

In our case we use the Javassist library. It is a matter of inserting Java code embedded into a String to methods looked up via reflection. For example the following code adds some new code into the beginning of a method.

ctc.getDeclaredMethod("setRequests").insertAfter(
  "org.netbeans.api.debugger.jpda.LineBreakpoint _breakpoint = getBreakpoint();" +
  "java.lang.String _className = _breakpoint.getPreferredClassName();" +
  "if (_className == null) {" +
  "  _className = reader.findCachedClassName(_breakpoint);" +
  "  if (_className == null) {" +
  "    _className = org.netbeans.modules.debugger.jpda.EditorContextBridge.getContext().getClassName (" +
  "      _breakpoint.getURL ()," +
  "      lineNumber);" +
  "    if (_className != null && _className.length() > 0) {" +
  "      reader.storeCachedClassName(_breakpoint, _className);" +
  "    }" +
  "  }" +
  "}"

For the complete source code of the patch see the src folder of the nb-plugin.

In conclusion, the best way to integrate with any system is via the API it provides. If it does not cut it then look into reflection and start using internals. You will lose on maintainability of course. Still not happy? Look into binary patching and have your fingers crossed whenever the target platform releases a new version.

Reblog this post [with Zemanta]

21
Nov 08

Waiting for GMail themes? Log out and log back in

I was waiting for the Gmail themes tab to appear on the settings page but it never did. I run Gmail under Prism and I hardly ever log out. Once I did do that and logged back in I finally got the themes tabs. Running terminal right now, nooooot.


6
Nov 08

How to waste 3GB of traffic on 404 pages

The previous post on dow.ngra.de generated tons of traffic. That tons of traffic generated 120 000 requests to a style sheet file that did not exist. WordPress will display a semi fancy page for 404 files that weighs about 25 KB. 25 KB times 120 000 is 3 000 000 KB :).

Actually the same happened to favicon.ico and a few others. So either have your referred files and favicon.ico on your server or rework WordPress mod_rewrite configuration not to handle css/js/ico/png/gif/jpg files. This will save you money!


22
Oct 08

Linux, Windows and exit codes – the debug story

While running a test framework I was very pleased to see all the tests pass. The problem was that I was the only one that lucky.

The logic of the framework is quite simple, ANT build.xml starts up, runs a test as a separate java task, gathers the result code and tests for equality of -1. The test was failing for no reason.

Checking the source code of the java task it was obvious that it was returning -1. Quick grep and a debugger session showed that ANT was not mangling the return code. So System.exit(-1) was returning 255. System.exit(42) then again returned 42.

Quick google shows that this is how it is supposed to work. Quoting from steve-parker.org

Exit codes are a number between 0 and 256, which is returned by any Unix command when it returns control to its parent process. Other numbers can be used, but these are treated modulo 256, so exit -10 is equivalent to exit 246, and exit 257 is equivalent to exit 1.

And then we have Windows where -1 is a valid exit code (well, at least it works). Something to remember when have to exit on multiple platforms.


1
Aug 08

PHP and microbenchmarks

Microbenchmarks always have to be taken with a grain of salt. I do know that but I happened to stumble upon a PHP optimization tip Avoid overusing function calls.

The test is fairly simple. 4 million iterations of $j = 1 and 4 million iterations of some_func(), a function wrapping the same $j = 1.

The results? Function calls are so expensive that inlined version takes 2 seconds to execute and invoking the function takes 16. 8 time difference, whoah!

The test environment: PHP 5.2.6-2+b1 with Suhosin-Patch 0.9.6.2 (cli) (built: Jul 22 2008 21:03:43) running on a P4@3ghz