transport.c
来自「This is a resource based on j2me embedde」· C语言 代码 · 共 641 行 · 第 1/2 页
C
641 行
EXIT_ERROR(JVMTI_ERROR_NONE, "could not connect, timeout or fatal error"); } else { (*t)->StopListening(t); connectionInitiated(t); } LOG_MISC(("End accept thread"));}static void JNICALLattachThread(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg){ LOG_MISC(("Begin attach thread")); connectionInitiated((jdwpTransportEnv *)(void*)arg); LOG_MISC(("End attach thread"));}void transport_initialize(void){ transport = NULL; listenerLock = debugMonitorCreate("JDWP Transport Listener Monitor"); sendLock = debugMonitorCreate("JDWP Transport Send Monitor");}void transport_reset(void){ /* * Reset the transport by closing any listener (will silently fail * with JDWPTRANSPORT_ERROR_ILLEGAL_STATE if not listening), and * closing any connection (will also fail silently if not * connected). * * Note: There's an assumption here that we don't yet support * multiple transports. When we do then we need a clear transition * from the current transport to the new transport. */ if (transport != NULL) { setTransportProperty(getEnv(), NULL); (*transport)->StopListening(transport); (*transport)->Close(transport); }}static jdwpErrorlaunch(char *command, char *name, char *address) { jint rc; char *buf; char *commandLine; int len; /* Construct complete command line (all in UTF-8) */ commandLine = jvmtiAllocate((int)strlen(command) + (int)strlen(name) + (int)strlen(address) + 3); if (commandLine == NULL) { return JDWP_ERROR(OUT_OF_MEMORY); } (void)strcpy(commandLine, command); (void)strcat(commandLine, " "); (void)strcat(commandLine, name); (void)strcat(commandLine, " "); (void)strcat(commandLine, address); /* Convert commandLine from UTF-8 to platform encoding */ len = (int)strlen(commandLine); buf = jvmtiAllocate(len*3+3); (void)(gdata->npt->utf8ToPlatform)(gdata->npt->utf, (jbyte*)commandLine, len, buf, len*3+3); /* Exec commandLine */ rc = dbgsysExec(buf); /* Free up buffers */ jvmtiDeallocate(buf); jvmtiDeallocate(commandLine); /* And non-zero exit status means we had an error */ if (rc != SYS_OK) { return JDWP_ERROR(TRANSPORT_INIT); } return JDWP_ERROR(NONE);}jdwpError transport_startTransport(jboolean isServer, char *name, char *address, long timeout){ jvmtiStartFunction func; jdwpTransportEnv *trans = NULL; char threadName[MAXPATHLEN + 100]; jint err; jdwpError serror; /* * If the transport is already loaded then use it * Note: We're assuming here that we don't support multiple * transports - when we do then we need to handle the case * where the transport library only supports a single environment. * That probably means we have a bag a transport environments * to correspond to the transports bag. */ if (transport != NULL) { trans = transport; } else { serror = loadTransport(name, &trans); if (serror != JDWP_ERROR(NONE)) { return serror; } } if (isServer) { char *retAddress; char *launchCommand; TransportInfo *info; jvmtiError error; int len; char* prop_value; info = jvmtiAllocate(sizeof(*info)); if (info == NULL) { return JDWP_ERROR(OUT_OF_MEMORY); } info->name = jvmtiAllocate((int)strlen(name)+1); (void)strcpy(info->name, name); info->address = NULL; info->timeout = timeout; if (info->name == NULL) { serror = JDWP_ERROR(OUT_OF_MEMORY); goto handleError; } if (address != NULL) { info->address = jvmtiAllocate((int)strlen(address)+1); (void)strcpy(info->address, address); if (info->address == NULL) { serror = JDWP_ERROR(OUT_OF_MEMORY); goto handleError; } } info->transport = trans; err = (*trans)->StartListening(trans, address, &retAddress); if (err != JDWPTRANSPORT_ERROR_NONE) { printLastError(trans, err); serror = JDWP_ERROR(TRANSPORT_INIT); goto handleError; } /* * Record listener address in a system property */ len = strlen(name) + strlen(retAddress) + 2; /* ':' and '\0' */ prop_value = (char*)jvmtiAllocate(len); strcpy(prop_value, name); strcat(prop_value, ":"); strcat(prop_value, retAddress); setTransportProperty(getEnv(), prop_value); jvmtiDeallocate(prop_value); (void)strcpy(threadName, "JDWP Transport Listener: "); (void)strcat(threadName, name); func = &acceptThread; error = spawnNewThread(func, (void*)info, threadName); if (error != JVMTI_ERROR_NONE) { serror = map2jdwpError(error); goto handleError; } launchCommand = debugInit_launchOnInit(); if (launchCommand != NULL) { serror = launch(launchCommand, name, retAddress); if (serror != JDWP_ERROR(NONE)) { goto handleError; } } else { if ( ! gdata->quiet ) { TTY_MESSAGE(("Listening for transport %s at address: %s", name, retAddress)); } } return JDWP_ERROR(NONE); handleError: jvmtiDeallocate(info->name); jvmtiDeallocate(info->address); jvmtiDeallocate(info); } else { /* * Note that we don't attempt to do a launch here. Launching * is currently supported only in server mode. */ /* * If we're connecting to another process, there shouldn't be * any concurrent listens, so its ok if we block here in this * thread, waiting for the attach to finish. */ err = (*trans)->Attach(trans, address, timeout, 0); if (err != JDWPTRANSPORT_ERROR_NONE) { printLastError(trans, err); serror = JDWP_ERROR(TRANSPORT_INIT); return serror; } /* * Start the transport loop in a separate thread */ (void)strcpy(threadName, "JDWP Transport Listener: "); (void)strcat(threadName, name); func = &attachThread; err = spawnNewThread(func, (void*)trans, threadName); serror = map2jdwpError(err); } return serror;}void transport_stopTransport(){ /* * Note: We're assuming here that we don't support multiple * transports. */ if (transport != NULL) { (*transport)->StopListening(transport); (*transport)->Close(transport); }}void transport_unloadTransport(){ if (transport != NULL) { dbgsysUnloadLibrary((*transport)->_handle); }}void transport_close(void){ if ( transport != NULL ) { (*transport)->Close(transport); }}jboolean transport_is_open(void){ jboolean is_open = JNI_FALSE; if ( transport != NULL ) { is_open = (*transport)->IsOpen(transport); } return is_open;}jint transport_sendPacket(jdwpPacket *packet){ jdwpTransportError err = JDWPTRANSPORT_ERROR_NONE; jint rc = 0; if (transport != NULL) { if ( (*transport)->IsOpen(transport) ) { debugMonitorEnter(sendLock); err = (*transport)->WritePacket(transport, packet); debugMonitorExit(sendLock); } if (err != JDWPTRANSPORT_ERROR_NONE) { if ((*transport)->IsOpen(transport)) { printLastError(transport, err); } /* * The users of transport_sendPacket except 0 for * success; non-0 otherwise. */ rc = (jint)-1; } } /* else, bit bucket */ return rc;}jint transport_receivePacket(jdwpPacket *packet) { jdwpTransportError err; err = (*transport)->ReadPacket(transport, packet); if (err != JDWPTRANSPORT_ERROR_NONE) { /* * If transport has been closed return EOF */ if (!(*transport)->IsOpen(transport)) { packet->type.cmd.len = 0; return 0; } printLastError(transport, err); /* * Users of transport_receivePacket expect 0 for success, * non-0 otherwise. */ return (jint)-1; } return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?