void Irony::diagnostics() const { if (activeTu_ == nullptr) { std::clog << "W: diagnostics - parse wasn't called\n"; std::cout << "nil\n"; return; } dumpDiagnostics(activeTu_); }
static void abortHandler(int signo, siginfo_t *info, void *ctx) { AbortHandlerState state; state.pid = getpid(); state.signo = signo; state.info = info; pid_t child; time_t t = time(NULL); char crashLogFile[256]; abortHandlerCalled++; if (abortHandlerCalled > 1) { // The abort handler itself crashed! char *end = state.messageBuf; end = appendText(end, "[ origpid="); end = appendULL(end, (unsigned long long) state.pid); end = appendText(end, ", pid="); end = appendULL(end, (unsigned long long) getpid()); end = appendText(end, ", timestamp="); end = appendULL(end, (unsigned long long) t); if (abortHandlerCalled == 2) { // This is the first time it crashed. end = appendText(end, " ] Abort handler crashed! signo="); end = appendSignalName(end, state.signo); end = appendText(end, ", reason="); end = appendSignalReason(end, state.info); end = appendText(end, "\n"); write_nowarn(STDERR_FILENO, state.messageBuf, end - state.messageBuf); // Run default signal handler. raise(signo); } else { // This is the second time it crashed, meaning it failed to // invoke the default signal handler to abort the process! end = appendText(end, " ] Abort handler crashed again! Force exiting this time. signo="); end = appendSignalName(end, state.signo); end = appendText(end, ", reason="); end = appendSignalReason(end, state.info); end = appendText(end, "\n"); write_nowarn(STDERR_FILENO, state.messageBuf, end - state.messageBuf); _exit(1); } return; } if (emergencyPipe1[0] != -1) { close(emergencyPipe1[0]); } if (emergencyPipe1[1] != -1) { close(emergencyPipe1[1]); } if (emergencyPipe2[0] != -1) { close(emergencyPipe2[0]); } if (emergencyPipe2[1] != -1) { close(emergencyPipe2[1]); } emergencyPipe1[0] = emergencyPipe1[1] = -1; emergencyPipe2[0] = emergencyPipe2[1] = -1; /* We want to dump the entire crash log to both stderr and a log file. * We use 'tee' for this. */ if (createCrashLogFile(crashLogFile, t)) { forkAndRedirectToTee(crashLogFile); } char *end = state.messagePrefix; end = appendText(end, "[ pid="); end = appendULL(end, (unsigned long long) state.pid); *end = '\0'; end = state.messageBuf; end = appendText(end, state.messagePrefix); end = appendText(end, ", timestamp="); end = appendULL(end, (unsigned long long) t); end = appendText(end, " ] Process aborted! signo="); end = appendSignalName(end, state.signo); end = appendText(end, ", reason="); end = appendSignalReason(end, state.info); end = appendText(end, ", randomSeed="); end = appendULL(end, (unsigned long long) randomSeed); end = appendText(end, "\n"); write_nowarn(STDERR_FILENO, state.messageBuf, end - state.messageBuf); end = state.messageBuf; if (*crashLogFile != '\0') { end = appendText(end, state.messagePrefix); end = appendText(end, " ] Crash log dumped to "); end = appendText(end, crashLogFile); end = appendText(end, "\n"); } else { end = appendText(end, state.messagePrefix); end = appendText(end, " ] Could not create crash log file, so dumping to stderr only.\n"); } write_nowarn(STDERR_FILENO, state.messageBuf, end - state.messageBuf); if (beepOnAbort) { end = state.messageBuf; end = appendText(end, state.messagePrefix); end = appendText(end, " ] PASSENGER_BEEP_ON_ABORT on, executing beep...\n"); write_nowarn(STDERR_FILENO, state.messageBuf, end - state.messageBuf); child = asyncFork(); if (child == 0) { closeAllFileDescriptors(2, true); #ifdef __APPLE__ execlp("osascript", "osascript", "-e", "beep 2", (const char * const) 0); safePrintErr("Cannot execute 'osascript' command\n"); #else execlp("beep", "beep", (const char * const) 0); safePrintErr("Cannot execute 'beep' command\n"); #endif _exit(1); } else if (child == -1) { int e = errno; end = state.messageBuf; end = appendText(end, state.messagePrefix); end = appendText(end, " ] Could fork a child process for invoking a beep: fork() failed with errno="); end = appendULL(end, e); end = appendText(end, "\n"); write_nowarn(STDERR_FILENO, state.messageBuf, end - state.messageBuf); } } if (stopOnAbort) { end = state.messageBuf; end = appendText(end, state.messagePrefix); end = appendText(end, " ] PASSENGER_STOP_ON_ABORT on, so process stopped. Send SIGCONT when you want to continue.\n"); write_nowarn(STDERR_FILENO, state.messageBuf, end - state.messageBuf); raise(SIGSTOP); } // It isn't safe to call any waiting functions in this signal handler, // not even read() and waitpid() even though they're async signal safe. // So we fork a child process and let it dump as much diagnostics as possible // instead of doing it in this process. child = asyncFork(); if (child == 0) { // Sleep for a short while to allow the parent process to raise SIGSTOP. // usleep() and nanosleep() aren't async signal safe so we use select() // instead. struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 100000; select(0, NULL, NULL, NULL, &tv); resetSignalHandlersAndMask(); child = asyncFork(); if (child == 0) { // OS X: for some reason the SIGPIPE handler may be reset to default after forking. // Later in this program we're going to pipe backtrace_symbols_fd() into the backtrace // sanitizer, which may fail, and we don't want the diagnostics process to crash // with SIGPIPE as a result, so we ignore SIGPIPE again. ignoreSigpipe(); dumpDiagnostics(state); // The child process may or may or may not resume the original process. // We do it ourselves just to be sure. kill(state.pid, SIGCONT); _exit(0); } else if (child == -1) { int e = errno; end = state.messageBuf; end = appendText(end, state.messagePrefix); end = appendText(end, "] Could fork a child process for dumping diagnostics: fork() failed with errno="); end = appendULL(end, e); end = appendText(end, "\n"); write_nowarn(STDERR_FILENO, state.messageBuf, end - state.messageBuf); _exit(1); } else { // Exit immediately so that child process is adopted by init process. _exit(0); } } else if (child == -1) { int e = errno; end = state.messageBuf; end = appendText(end, state.messagePrefix); end = appendText(end, " ] Could fork a child process for dumping diagnostics: fork() failed with errno="); end = appendULL(end, e); end = appendText(end, "\n"); write_nowarn(STDERR_FILENO, state.messageBuf, end - state.messageBuf); } else { raise(SIGSTOP); // Will continue after the child process has done its job. } // Run default signal handler. raise(signo); }
void processALoadMessage(UdrGlobals *UdrGlob, UdrServerReplyStream &msgStream, UdrLoadMsg &request, IpcEnvironment &env) { const char *moduleName = "processALoadMessage"; char errorText[MAXERRTEXT]; ComDiagsArea *diags = ComDiagsArea::allocate(UdrGlob->getIpcHeap()); doMessageBox(UdrGlob, TRACE_SHOW_DIALOGS, UdrGlob->showLoad_, moduleName); NABoolean showLoadLogic = (UdrGlob->verbose_ && UdrGlob->traceLevel_ >= TRACE_IPMS && UdrGlob->showLoad_); if (showLoadLogic) { ServerDebug("[UdrServ (%s)] Processing load request", moduleName); } // UDR_LOAD message always comes with transaction and they are out // side Enter Tx and Exit Tx pair. Make sure we are under correct // transaction. msgStream.activateCurrentMsgTransaction(); // // Check to see if the incoming UDR handle has already been seen // NABoolean handleAlreadyExists = FALSE; SPInfo *sp = UdrGlob->getSPList()->spFind(request.getHandle()); if (sp) { handleAlreadyExists = TRUE; if (showLoadLogic) { ServerDebug(" Duplicate handle arrived"); ServerDebug(" SPInfoState is %s", sp->getSPInfoStateString()); } if (sp->getSPInfoState() != SPInfo::UNLOADING) { // // An SPInfo exists but it is not one of the token instances to // represent an out-of-sequence LOAD/UNLOAD. This is an internal // error. Something has been botched in the message protocol. // char buf[100]; convertInt64ToAscii(request.getHandle(), buf); *diags << DgSqlCode(-UDR_ERR_DUPLICATE_LOADS); *diags << DgString0(buf); } else { // The LOAD/UNLOAD requests for this handle arrived // out-of-sequence. Nothing to do at this point. An empty reply // will be generated later in this function. } } if (!handleAlreadyExists) { if (!UdrHandleIsValid(request.getHandle())) { *diags << DgSqlCode(-UDR_ERR_MISSING_UDRHANDLE); *diags << DgString0("Load Message"); } else { // // First process the metadata in the LOAD requests and then // contact Language Manager to load the SP. // sp = processLoadParameters(UdrGlob, request, *diags); if (showLoadLogic) { ServerDebug("[UdrServ (%s)] About to call LM::getRoutine", moduleName); } if (sp == NULL) { *diags << DgSqlCode(-UDR_ERR_UNABLE_TO_ALLOCATE_MEMORY); *diags << DgString0("SPInfo"); } else { UdrGlob->setCurrSP(sp); LmRoutine *lmRoutine; LmResult lmResult; LmLanguageManager *lm = UdrGlob->getOrCreateLM(lmResult, sp->getLanguage(), diags); LmHandle emitRowFuncPtr; if (sp->getParamStyle() == COM_STYLE_CPP_OBJ) emitRowFuncPtr = (LmHandle)&SpInfoEmitRowCpp; else emitRowFuncPtr = (LmHandle)&SpInfoEmitRow; if (lm) { if (sp->getParamStyle() == COM_STYLE_JAVA_OBJ || sp->getParamStyle() == COM_STYLE_CPP_OBJ) { lmResult = lm->getObjRoutine( request.getUDRSerInvocationInfo(), request.getUDRSerInvocationInfoLen(), request.getUDRSerPlanInfo(), request.getUDRSerPlanInfoLen(), sp->getLanguage(), sp->getParamStyle(), sp->getExternalName(), sp->getContainerName(), sp->getExternalPathName(), sp->getLibrarySqlName(), &lmRoutine, diags); if (lmRoutine) { LmRoutineCppObj *objRoutine = static_cast<LmRoutineCppObj *>(lmRoutine); if (sp->getParamStyle() == COM_STYLE_CPP_OBJ) // set function pointers for functions provided // by tdm_udrserv objRoutine->setFunctionPtrs(SpInfoGetNextRow, SpInfoEmitRowCpp); // add items to the UDRInvocationInfo that are not // known at compile time (total # of instances is // kind of known, but we want to give the executor a // chance to change it) lmRoutine->setRuntimeInfo(request.getParentQid(), request.getNumInstances(), request.getInstanceNum()); #ifndef NDEBUG int debugLoop = 2; if (objRoutine->getInvocationInfo()->getDebugFlags() & tmudr::UDRInvocationInfo::DEBUG_LOAD_MSG_LOOP) debugLoop = 1; // go into a loop to allow the user to attach a debugger, // if requested, set debugLoop = 2 in the debugger to get out while (debugLoop < 2) debugLoop = 1-debugLoop; #endif } } else lmResult = lm->getRoutine(sp->getNumParameters(), sp->getLmParameters(), sp->getNumTables(), sp->getLmTables(), sp->getReturnValue(), sp->getParamStyle(), sp->getTransactionAttrs(), sp->getSQLAccessMode(), sp->getParentQid(), sp->getRequestRowSize(), sp->getReplyRowSize(), sp->getSqlName(), sp->getExternalName(), sp->getRoutineSig(), sp->getContainerName(), sp->getExternalPathName(), sp->getLibrarySqlName(), UdrGlob->getCurrentUserName(), UdrGlob->getSessionUserName(), sp->getExternalSecurity(), sp->getRoutineOwnerId(), &lmRoutine, (LmHandle)&SpInfoGetNextRow, emitRowFuncPtr, sp->getMaxNumResultSets(), diags); } if (lmResult == LM_OK) { if (lmRoutine == NULL) { *diags << DgSqlCode(-UDR_ERR_MISSING_LMROUTINE); *diags << DgString0("error: returned a null LM handle"); *diags << DgInt1((Int32)0); } else { sp->setLMHandle(lmRoutine); // Retrieve any optional data from UdrLoadMsg. copyRoutineOptionalData(request, sp); reportLoadResults(UdrGlob, sp, lmRoutine); sp->setSPInfoState(SPInfo::LOADED); } } // lmResult == LM_OK if (showLoadLogic) { if (lmResult == LM_OK) { sprintf(errorText, "[UdrServ (%.30s)] LM::getRoutine was successful.", moduleName); } else { sprintf(errorText, "[UdrServ (%.30s)] LM::getRoutine resulted in error.", moduleName); } ServerDebug(errorText); doMessageBox(UdrGlob, TRACE_SHOW_DIALOGS, UdrGlob->showMain_, errorText); } if (sp && !(sp->isLoaded())) { sp->setSPInfoState(SPInfo::LOAD_FAILED); } } // if (sp == NULL) else ... } // if (handle is not valid) else ... } // !handleAlreadyExists // build a reply and send it msgStream.clearAllObjects(); UdrLoadReply *reply = new (UdrGlob->getIpcHeap()) UdrLoadReply(UdrGlob->getIpcHeap()); if (reply == NULL) { // no reply buffer build... controlErrorReply(UdrGlob, msgStream, UDR_ERR_MESSAGE_PROCESSING, INVOKE_ERR_NO_REPLY_BUFFER, NULL); return; } // Only return a valid UDR Handle if diagnostics are not present and // no LM errors occurred. We also return a valid handle if this LOAD // arrived out-of-sequence after the UNLOAD and no diagnostics have // been generated yet. if (diags && diags->getNumber() > 0) { reply->setHandle(INVALID_UDR_HANDLE); } else if (sp) { if (sp->isLoaded() || handleAlreadyExists) { reply->setHandle(sp->getUdrHandle()); } else { reply->setHandle(INVALID_UDR_HANDLE); } } msgStream << *reply; if (diags && diags->getNumber() > 0) { msgStream << *diags; UdrGlob->numErrUDR_++; UdrGlob->numErrSP_++; UdrGlob->numErrLoadSP_++; if (showLoadLogic) dumpDiagnostics(diags, 2); } if (showLoadLogic) { ServerDebug("[UdrServ (%s)] About to send LOAD reply", moduleName); } #ifdef NA_DEBUG_C_RUNTIME if (UdrGlob && UdrGlob->getJavaLM()) { sleepIfPropertySet(*(UdrGlob->getJavaLM()), "MXUDR_LOAD_DELAY", diags); } #endif // NA_DEBUG_C_RUNTIME sendControlReply(UdrGlob, msgStream, sp); if (diags) { diags->decrRefCount(); } reply->decrRefCount(); } // processALoadMessage