qt android9无法调试,c++ - How to debug Android/GDB/Qt problems? - Stack Overflow

I had the same issue XD:

Ensure both sides (this App and IDE) are NOT blocked by any

firewall.

Ensure your app will not exit until IDE gets connected to GNU debug server

Below is stand-alone version of Qt debug server source (xd/DebugServer.java):

package xd;

//beaware: before call to "xd.DebugServer.start();" Gdb Server:

// Ensure all native libraries you want debug break in are loaded

// just add to class that calls "start" static initalizer sample:

// static { System.loadLibrary("tun2http"); } //this may load "libs/armeabi-v7a/libtun2http.so"

// Ensure both sides (this App and IDE) are NOT blocked by any firewall

//

//you can ignore signals using below (but may need to change SIG33):

//handle SIG33 nostop noprint noignore pass

import android.util.Log;

import android.content.pm.PackageManager;

import android.content.pm.ApplicationInfo;

import android.net.LocalServerSocket;

import java.io.File;

import java.io.IOException;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.DataOutputStream;

public class DebugServer {

public static final String TAG = "XD_GDB"; // string used for Log.x (the colon helps cut out false positives)

private static Process m_debuggerProcess = null; // debugger process

private static void debugLog(String msg) {

Log.i(TAG, msg);

}

//do not use this or IDE will fail to connect to debug server

public static String flushLog() {

try {

String command = "logcat -d";

Process process = Runtime.getRuntime().exec(command);

BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(process.getInputStream()));

StringBuilder log=new StringBuilder();

String line = "";

while ((line = bufferedReader.readLine()) != null) {

log.append(line + System.lineSeparator());

}

//process.waitFor();

bufferedReader.close();

return log.toString();

} catch (IOException e) {

// Handle Exception

}

return "";

}

private static class DebugWaitRunnable implements Runnable {

public DebugWaitRunnable(String pingPongSocket) throws IOException {

socket = new LocalServerSocket(pingPongSocket);

}

public boolean wasFailure;

private LocalServerSocket socket;

public void run() {

final int napTime = 200; // milliseconds between file accesses

final int timeOut = 5000; // ms until we give up on ping and pong

final int maxAttempts = timeOut / napTime;

try {

android.net.LocalSocket connectionFromClient = socket.accept();

debugLog("Debug socket accepted");

BufferedReader inFromClient =

new BufferedReader(new InputStreamReader(connectionFromClient.getInputStream()));

DataOutputStream outToClient = new DataOutputStream(connectionFromClient.getOutputStream());

outToClient.writeBytes("" + android.os.Process.myPid());

for (int i = 0; i < maxAttempts; i++) {

String clientData = inFromClient.readLine();

debugLog("Incoming socket " + clientData);

if (!clientData.isEmpty())

break;

if (connectionFromClient.isClosed()) {

wasFailure = true;

break;

}

Thread.sleep(napTime);

}

} catch (IOException ioEx) {

ioEx.printStackTrace();

wasFailure = true;

Log.e(TAG,"Can't start debugger" + ioEx.getMessage());

} catch (InterruptedException interruptEx) {

wasFailure = true;

Log.e(TAG,"Can't start debugger" + interruptEx.getMessage());

}

}

public void shutdown() throws IOException

{

wasFailure = true;

try {

socket.close();

} catch (IOException ignored) { }

}

};

//beaware: before call to "start" Gdb Server:

// Ensure all native libraries you want debug break in are loaded

// just add to class that calls "xd.DebugServer.start();" static initalizer sample:

// static { System.loadLibrary("tun2http"); } //this may load "libs/armeabi-v7a/libtun2http.so"

// Ensure both sides (this App and IDE) are NOT blocked by any firewall

public static boolean start(android.app.Activity activity) {

try {

//System.loadLibrary("gnustl_shared");

final int napTime = 200; // milliseconds between file accesses

final int timeOut = 30000; // ms until we give up on ping and pong

final int maxAttempts = timeOut / napTime;

PackageManager pm = activity.getPackageManager();

ApplicationInfo appInfo = pm.getApplicationInfo(

activity.getPackageName(), android.content.pm.PackageManager.GET_CONFIGURATIONS

);

final String packagePath = appInfo.dataDir + "/";

//using constant '"/data/data/"' instate of 'appInfo.dataDir+"/"' since that does not work old code was "extras.getString("gdbserver_socket");"

final String gdbserverSocket = "/data/data/" + appInfo.packageName + "/debug-socket";

final String gdbserverCommand = packagePath + "lib/libgdbserver.so --multi +" + gdbserverSocket;

debugLog("removing gdb socket " + gdbserverSocket);

new File(gdbserverSocket).delete();

debugLog("starting " + gdbserverCommand);

m_debuggerProcess = Runtime.getRuntime().exec(gdbserverCommand);

debugLog("gdbserver started");

int i;

for (i = 0; i < maxAttempts; ++i) {

debugLog("waiting for socket at " + gdbserverSocket + ", attempt " + i);

File file = new File(gdbserverSocket);

if (file.exists()) {

file.setReadable(true, false);

file.setWritable(true, false);

file.setExecutable(true, false);

break;

}

Thread.sleep(napTime);

}

if (i == maxAttempts) {

debugLog("time out when waiting for debug socket");

return false;

}

debugLog("socket ok");

final String pingSocket = appInfo.packageName + ".ping_pong_socket";

debugLog("pingSocket: " + pingSocket);

DebugWaitRunnable runnable = new DebugWaitRunnable(pingSocket);

Thread waitThread = new Thread(runnable);

waitThread.start();

debugLog("Waiting for debug socket connect");

for (i = 0; i < maxAttempts && waitThread.isAlive(); ++i) {

if(i % 10 == 0)

debugLog("Waiting attempt: " + i);

Thread.sleep(napTime);

}

if (i == maxAttempts) {

debugLog("time out when waiting for ping socket");

runnable.shutdown();

return false;

}

if (runnable.wasFailure) {

debugLog("Could not connect to debug client");

return false;

} else {

debugLog("Got pid acknowledgment");

}

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

public static void stop(android.app.Activity activity) {

if (m_debuggerProcess != null)

m_debuggerProcess.destroy();

System.exit(0);// FIXME remove it or find a better way

}

} //class GdbServer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值