Delphi Tip – When the debugger fails to attach to an Android app.

Android

Sometimes, if your application fails during start-up, the RAD Studio debugger can fail to attach making it difficult to debug the problem. So what should you do?

The debugger usually has plenty of time to attach to the android process because the FMX start-up code takes sufficient time to load. There are occasions when this can fail. For example, if you are loading a library (.so) as a dependency for your application and it fails to initialize.┬áIn my case, it was working on a boot-strap application which does not depend on FMX, and therefore doesn’t have the start-up time.

I tried turning on the Android developer option “Wait for debugger to attach” on the device it’s self, but sadly, I fear this feature may be specifically for use with Android Developer Studio as it fails to ever attach under Rad Studio / Delphi.

The solution that I stumbled on is this.
Right click on your project file and select “View Source”

viewsource

You’ll be taken to the entry point of the application, which looks like this…

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Alter this code to read as follows…

var
  temp: boolean;

begin
  temp := true;
  while temp do continue;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

What we’ve done here is to introduce an endless loop, which will ensure the application takes sufficient time to load for the debugger to attach. So you may now be wondering “what good does that do for me? How do I reach my application break points?” Well…

Place a break point on the line beginning “while temp do” and run the application in debug mode. Because the loop is infinite, there should be sufficient time for the debugger to attach and break at the while loop. When it does, highlight the word “temp” in that line and press [CTRL]+[F7] and you’ll be presented with this dialog window…

evaluate1

In the drop-down box labeled “expression” type the word “temp” (if it isn’t already there) and press evaluate..

evaluate2

Now, in the box at the bottom labeled “New value” alter the value from “true” to “false” and click the modify button.

evaluate3

You can now close this box and return to your break-point. When you do, press [F9] to continue debugging, you’ll find that you’ve broken out of the endless loop, and can proceed to your own break points.

What?!

For anyone that doesn’t understand what happened here, the dialog box is a debugging tool which allows you to inspect the value of variables at run-time. This dialog can also be used to alter variables while the application is running in debug mode. By setting the value of the “temp” variable to false, we’ve altered the conditions of the test “while temp do…” and therefore ended the loop.

Don’t forget!

It should go without saying, that once you’ve been able to debug the application successfully you should remove the code we added here. You don’t need an infinite loop at the beginning of your application, and you don’t need a temporary global variable hanging around in there either.

Oh, one more thing. This technique was a bit of hackery that I used to get me out of a tricky situation. If you know of a better method, please feel free to add a comment below or on reddit!

Thanks for reading!

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Leave a Reply