Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why wait for debugger and then attach debugger to process?

What is the significance of

android.os.Debug.waitForDebugger();

I am aware that we [sometimes] need to use this function in order to debug a Service, but my question is:

  • WHY do we have to do this?
  • Why does this method exist? What other purpose does it serve?
  • What does it mean to attach a debugger to a process, and why do we have to do this? Why does a Service have to be debugged in this way, and not an Activity or BroadcastReceiver?

One would think that debugging any kind of code in a project would be straightforward. Except it isn't. Not for a Service anyway.

What is the reason for the existence of this special procedure? And what else is it used for?

enter image description here

Wait until a debugger attaches. As soon as the debugger attaches, this returns, so you will need to place a breakpoint after the waitForDebugger() call if you want to start tracing immediately.

The WHY remains unanswered.

UPDATE:

I found one use case for this function: when an Android app is auto-restarted after the app process is killed. The process is killed when a runtime permission is toggled by the user in the app settings. More generally, an app process is killed & restarted whenever ...

android.os.Process.killProcess(android.os.Process.myPid());

is called.

To debug the app after process restart, you write android.os.Debug.waitForDebugger() in your code and then attach the debugger to the current process.

Android: how to debug app's start-up process.

This is one use case for this function.

References:

Debugging a service.

A proper way to Debug a Service in Android Studio?.

Breakpoint in service not working.

How to debug an Android Background Service?.

How to debug a Service?.

How to Attach debbuger to process from the first line without compile code every time.

you can attach a debugger after the app is already open.

Possibly related:

Accessibility service disabled upon each debug run

When using the Java debugger in Intellij what does “Drop Frame” mean?

Configure on-device developer options.

Test your service.

Force application to restart from first Activity.

like image 571
Y.S Avatar asked Sep 24 '19 09:09

Y.S


People also ask

What does wait for debugger mean?

This setting allows the execution of an app to be paused until a debugger is attached to it, upon the launch of the app. The app to debug must first be selected using the Debug app option.

How do debuggers attach to processes?

You can attach the Visual Studio debugger to a running process on a local or remote computer. After the process is running, select Debug > Attach to Process or press Ctrl+Alt+p in Visual Studio, and use the Attach to Process dialog to attach the debugger to the process.

How do I wait for debugger?

Go to Settings > Developer options > Select debug app and choose your app from the list, then click Wait for debugger. Wait for the app to load and a dialog to appear telling you the app is waiting for a debugger.

What is attach debugger in VS code?

Once you have your launch configuration set, start your debug session with F5. Alternatively, you can run your configuration through the Command Palette (Ctrl+Shift+P) by filtering on Debug: Select and Start Debugging or typing 'debug ' and selecting the configuration you want to debug.


1 Answers

I don't sure my answer would be right, because you might expect complicated insights inside. In reality, there is pretty straight forward process, to connect and intercept process responses.

enter image description here

  • Why does this method exist? What other purpose does it serve?

As you can see, answering this question is pretty simple. Because of race condition during process interception, you will not be able to stop on your early break point, and should call waitForDebugger(). In most frequent places, like Service debugging and Application.

  • What does it mean to "attach a debugger to a process", and why do we have to do this? Why does a Service have to be debugged in this way, and not an Activity or BroadcastReceiver?

It's another question and I don't sure about context here, but. We need to call the method, and attach to process manually for cases, where our Service using another process from a Manifest. Obviously debug bridge will set on the current active process with application, but since we can run Service from another process, we need to manually add it to debug bridge and wait for debug connection.

like image 104
GensaGames Avatar answered Oct 10 '22 01:10

GensaGames