Android: Add Thread.sleep before loading main in Debug mode

This helps with the jdb debugging settling, and the native debugger
can hit breakpoints at the beginning of main() function or in case
of unittests at initTestCase() function.

By default in debug mode the delay is 1000ms. This value can be changed
with the environment variable: QT_ANDROID_DEBUGGER_MAIN_THREAD_SLEEP_MS

Fixes: QTCREATORBUG-30425
Change-Id: Ica0c6080c55468579a28eecf8f45cff68d99c3a8
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit 269187bfa272f9456aad6a6233100d846915f175)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 76c6b71db41962005854fac57b09ca0907e2ed96)
This commit is contained in:
Cristian Adam 2024-03-06 16:51:24 +01:00 committed by Qt Cherry-pick Bot
parent b444ae3996
commit f32abfdc78
3 changed files with 22 additions and 0 deletions

View File

@ -14,6 +14,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.system.Os;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.util.Log;
@ -134,6 +135,14 @@ public class QtActivityLoader extends QtLoader {
String extraAppParams = extras.getString("extraappparams");
appendApplicationParameters(getDecodedUtfString(extraAppParams));
}
m_debuggerSleepMs = 1000;
if (Os.getenv("QT_ANDROID_DEBUGGER_MAIN_THREAD_SLEEP_MS") != null) {
try {
m_debuggerSleepMs = Integer.parseInt(Os.getenv("QT_ANDROID_DEBUGGER_MAIN_THREAD_SLEEP_MS"));
} catch (NumberFormatException ignored) {
}
}
} else {
Log.d(QtNative.QtTAG, "Not in debug mode! It is not allowed to use extra arguments " +
"in non-debug mode.");

View File

@ -46,6 +46,8 @@ public abstract class QtLoader {
protected String m_applicationParameters = "";
protected HashMap<String, String> m_environmentVariables = new HashMap<>();
protected int m_debuggerSleepMs = 0;
/**
* Sets and initialize the basic pieces.
* Initializes the class loader since it doesn't rely on anything
@ -509,6 +511,9 @@ public abstract class QtLoader {
String mainLibPath = getLibrariesFullPaths(oneEntryArray).get(0);
final boolean[] success = {true};
QtNative.getQtThread().run(() -> {
if (m_debuggerSleepMs > 0)
QtNative.getQtThread().sleep(m_debuggerSleepMs);
m_mainLibPath = loadLibraryHelper(mainLibPath);
if (m_mainLibPath == null)
success[0] = false;

View File

@ -42,6 +42,14 @@ public class QtThread {
}
}
public void sleep(int milliseconds) {
try {
m_qtThread.sleep(milliseconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(final Runnable runnable) {
final Semaphore sem = new Semaphore(0);
synchronized (m_qtThread) {