Android jdwp 原理
jdwp 工作原理
![](https://images2015.cnblogs.com/blog/440021/201511/440021-20151106104332664-708226350.png)
在setCommandLineDefaults 里面设置了开关,默认开启。
在dvmJdwpStartup 里面会设置sock或者adb 方式打开监听
JNI_CreateJavaVM 创建虚拟机
实际是使用dvmStartup 去创建核心类,基础线程等工作。
/* Initialize VM. */
true;
gDvm.initializing = std::string status =
dvmStartup(argc, argv.get(), args->ignoreUnrecognized, (JNIEnv*)pEnv);
false;
gDvm.initializing = 调用setCommandLineDefault,设置默认启动参数
std::string dvmStartup(int argc, const char* const argv[],
bool ignoreUnrecognized, JNIEnv* pEnv)
"VM init args (%d):", argc);
LOGV(for (int i = 0; i < argc; i++) {
" %d: '%s'", i, argv[i]);
LOGV(//这里会设置一些默认参数。
setCommandLineDefaults();
if (gDvm.zygote) {
if (!initZygote()) {
return "initZygote failed";
else {
} if (!dvmInitAfterZygote()) {
return "dvmInitAfterZygote failed";
需要注意的是CLASSPATH路径填充,和jdwp启动开关
static void setCommandLineDefaults()
const char* envStr = getenv("CLASSPATH");
if (envStr != NULL) {
else {
} ".");
gDvm.classPathStr = strdup("BOOTCLASSPATH");
envStr = getenv(if (envStr != NULL) {
else {
} ".");
gDvm.bootClassPathStr = strdup(true;
gDvm.concurrentMarkSweep = /* gDvm.jdwpSuspend = true; */
//这里设置是否开启jdwp
/* allowed unless zygote config doesn't allow it */
true;
gDvm.jdwpAllowed = 具体的配置参数,由启动参数传进
static void showJdwpHelp()
"Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y
");
"Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n
");
static bool handleJdwpOption(const char* name, const char* value)
if (strcmp(name, "transport") == 0) {
//这里决定使用adb方式,还是socket方式。
if (strcmp(value, "dt_socket") == 0) {
else if (strcmp(value, "dt_android_adb") == 0) {
} else {
} "JDWP transport '%s' not supported", value);
LOGE(return false;
//是否启动服务
else if (strcmp(name, "server") == 0) {
} if (*value == 'n')
false;
gDvm.jdwpServer = else if (*value == 'y')
true;
gDvm.jdwpServer = else {
"JDWP option 'server' must be 'y' or 'n'");
LOGE(return false;
//启动后是否挂起
else if (strcmp(name, "suspend") == 0) {
} if (*value == 'n')
false;
gDvm.jdwpSuspend = else if (*value == 'y')
true;
gDvm.jdwpSuspend = else {
"JDWP option 'suspend' must be 'y' or 'n'");
LOGE(return false;
//绑定地址
else if (strcmp(name, "address") == 0) {
} dvmInitAfterZygote 里面会调用initJdwp去初始化jdwp。
static bool initJdwp()
if (gDvm.jdwpAllowed && gDvm.jdwpConfigured) {
if (gDvm.jdwpHost != NULL) {
if (strlen(gDvm.jdwpHost) >= sizeof(params.host)-1) {
"ERROR: hostname too long: '%s'", gDvm.jdwpHost);
LOGE(return false;
strcpy(params.host, gDvm.jdwpHost);
else {
} 0] = ' ';
params.host[dvmJdwpStartup(¶ms);
gDvm.jdwpState = 启动端口监听
adb会使用dvmJdwpAndroidAdbTransport绑定jdwp调用接口,socket会使用dvmJdwpSocketTransport,dvmJdwpNetStartup是实际调用的地方。
const JdwpStartupParams* pParams)
JdwpState* dvmJdwpStartup(/* comment this out when debugging JDWP itself */
calloc(1, sizeof(JdwpState));
state = (JdwpState*) switch (pParams->transport) {
case kJdwpTransportSocket:
// LOGD("prepping for JDWP over TCP");
dvmJdwpSocketTransport();
state->transport = break;
case kJdwpTransportAndroidAdb:
// LOGD("prepping for JDWP over ADB");
dvmJdwpAndroidAdbTransport();
state->transport = /* TODO */
break;
default:
"Unknown transport %d", pParams->transport);
LOGE(false);
assert(goto fail;
if (!dvmJdwpNetStartup(state, pParams))