B4A Library Thread Debug Hack

I needed to debug a thread recently and it drove me crazy, so I came up with this hack.

I have written a small library SLDebug (attached) that just returns the Debug Line Number from the generated code.

It only runs in Legacy Debug mode, it saved me loads of time but it's a bit cumbersome. You need to copy debug lines of code to wherever you want the thread to stop:

B4X:
' Debug
        Dim Items() As Object = Array As Object("X = "&X)
        DebugFlag=True:ThreadTest.RunOnGuiThread("DebugSub",Array As Object(Debug.LineNo,Items)) : Do While DebugFlag : Loop

This sets a flag then calls a sub on the UI thread that allows a break point to be entered, once the execution is continued the flag is reset and the thread continues to run.

As this is running in a thread the empty Do While: Loop won't abort the app.

Debug Window

You can see global variables in the debug window, local variables from the thread that you are interested in need to be passed to the DebugSub in an array where they can be logged or just inspected.

You cannot Step through the code from the Debug, you must use the continue code execution (F5). It will then stop on the next Debug line you have entered.

It is a cumbersome hack, but it works.

Streaming Threads

Be careful if you are streaming data that is time critical i.e. AudioTrack or similar etc. as this will cause an error when the buffers empty. You'll need to disable the streaming in some manner.

I am sure that there are may configurations for which this won't work, but it did the trick for a straightforward thread from the main activity.

I hope you find it useful.

Files

SLDebug.zip is the library. (Copy the xml and jar to your additional libs folder
dc.zip is an example.

There are no other dependencies.
 

Attachments

  • SLDebug.zip
    1.6 KB · Views: 391
  • dc.zip
    6.3 KB · Views: 340
Last edited:
Top