/** * Prints the footer and closes the file being logged to. */ void MM_VerboseWriterFileLoggingSynchronous::closeFile(MM_EnvironmentBase *env) { OMRPORT_ACCESS_FROM_OMRPORT(env->getPortLibrary()); if(-1 != _logFileDescriptor) { omrfile_write_text(_logFileDescriptor, getFooter(env), strlen(getFooter(env))); omrfile_write_text(_logFileDescriptor, "\n", strlen("\n")); omrfile_close(_logFileDescriptor); _logFileDescriptor = -1; } }
void printFile(const char *name) { OMRPORT_ACCESS_FROM_OMRPORT(gcTestEnv->portLib); intptr_t fileDescriptor = omrfile_open(name, EsOpenRead, 0444); char readBuf[2048]; while (readBuf == omrfile_read_text(fileDescriptor, readBuf, 2048)) { omrtty_printf("%s", readBuf); } omrfile_close(fileDescriptor); omrtty_printf("\n"); }
/* * Test scenario for CorruptedAgent: * - create an empty agent file * - call omr_agent_create() and OMR_Agent::createAgent by providing the empty agent file * - expect openLibrary() to fail */ TEST_F(RASAgentNegativeTest, CorruptedAgent) { intptr_t fileDescriptor; char fileName[256]; char agentName[256]; char hostname[128]; OMRPORT_ACCESS_FROM_OMRVM(&testVM.omrVM); ASSERT_EQ(0, gethostname(hostname, sizeof(hostname))); /* generate machine specific file name to prevent conflict between multiple tests running on shared drive */ omrstr_printf(agentName, sizeof(agentName), "corruptedAgent_%s", hostname); /* create fileName with platform-dependent shared library prefix & suffix. * for Windows, fileName = corruptedAgent_<hostname>.dll * for Unix, fileName = libcorruptedAgent_<hostname>.so * for OSX, fileName = libcorruptedAgent_<hostname>.dylib */ #if defined(WIN32) || defined(WIN64) omrstr_printf(fileName, sizeof(fileName), "%s.dll", agentName); #elif defined(OSX) omrstr_printf(fileName, sizeof(fileName), "lib%s.dylib", agentName); #else /* defined(OSX) */ omrstr_printf(fileName, sizeof(fileName), "lib%s.so", agentName); #endif /* defined(WIN32) || defined(WIN64) */ /* create the empty agent file with permission 751*/ fileDescriptor = omrfile_open(fileName, EsOpenCreate | EsOpenWrite, 0751); ASSERT_NE(-1, fileDescriptor) << "omrfile_open \"" << fileName << "\" failed"; omrfile_close(fileDescriptor); /* call omr_agent_create() by providing the empty agent file */ struct OMR_Agent *agentC = omr_agent_create(&testVM.omrVM, agentName); ASSERT_FALSE(NULL == agentC) << "testAgent: createAgent() " << agentName << " failed"; OMRTEST_ASSERT_ERROR(OMR_ERROR_ILLEGAL_ARGUMENT, omr_agent_openLibrary(agentC)); /* call OMR_Agent::createAgent() by providing the empty agent file */ OMR_Agent *agentCPP = OMR_Agent::createAgent(&testVM.omrVM, agentName); ASSERT_FALSE(NULL == agentCPP) << "testAgent: createAgent() failed"; OMRTEST_ASSERT_ERROR(OMR_ERROR_ILLEGAL_ARGUMENT, agentCPP->openLibrary()); omrfile_unlink(fileName); }
static omr_error_t loadFormatStringsForComponent(UtComponentData *componentData) { omr_error_t rc = OMR_ERROR_NONE; intptr_t formatFileFD = -1; int numFormats = componentData->tracepointCount; char **formatStringsComponentArray = NULL; int formatStringsComponentArraySize = 0; char *tempPtr, *tempPtr2; int currenttp = 0; char compName[1024]; char *fileContents = NULL; int64_t fileSize; float datFileVersion; const unsigned int componentNameLength = (const unsigned int)strlen(componentData->componentName); OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary)); UT_DBGOUT(2, ("<UT> loadFormatStringsForComponent %s\n", componentData->componentName)); if (componentData->alreadyfailedtoloaddetails != 0) { UT_DBGOUT(2, ("<UT> loadFormatStringsForComponent %s returning due to previous load failure\n", componentData->componentName)); return OMR_ERROR_INTERNAL; } UT_DBGOUT(2, ("<UT> loadFormatStringsForComponent %s parsing filename = %s\n", componentData->componentName, componentData->formatStringsFileName)); /* buffer format files into memory at some point */ if (fileContents == NULL) { /* look in jre/lib directory first */ UT_DBGOUT(1, ("<UT> loadFormatStringsForComponent trying to load = %s\n", componentData->formatStringsFileName)); formatFileFD = openFileFromDirectorySearchList(OMR_TRACEGLOBAL(traceFormatSpec), componentData->formatStringsFileName, EsOpenText | EsOpenRead, 0); if (formatFileFD == -1) { UT_DBGOUT(1, ("<UT> loadFormatStringsForComponent can't load = %s, from current directory either - marking it unfindeable\n", componentData->formatStringsFileName)); rc = OMR_ERROR_INTERNAL; goto epilogue; } fileSize = omrfile_flength(formatFileFD); if (fileSize < 0) { UT_DBGOUT(1, ("<UT> error getting file size for file %s\n", componentData->formatStringsFileName)); rc = OMR_ERROR_INTERNAL; goto epilogue; } fileContents = (char *)omrmem_allocate_memory((uintptr_t)fileSize + 1, OMRMEM_CATEGORY_TRACE); if (fileContents == NULL) { UT_DBGOUT(1, ("<UT> can't allocate buffer\n")); rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY; goto epilogue; } if (omrfile_read(formatFileFD, fileContents, (int32_t)fileSize) != (int32_t) fileSize) { UT_DBGOUT(1, ("<UT> can't read the file into the buffer\n")); rc = OMR_ERROR_INTERNAL; goto epilogue; } omrfile_close(formatFileFD); formatFileFD = -1; /* if running on z/os convert to ascii */ fileContents[fileSize] = '\0'; twE2A(fileContents); } datFileVersion = getDatFileVersion(fileContents); if (datFileVersion == 0.0F) { UT_DBGOUT(1, ("<UT> dat file version error.\n")); rc = OMR_ERROR_INTERNAL; goto epilogue; } formatStringsComponentArraySize = numFormats * sizeof(char *); formatStringsComponentArray = (char **)omrmem_allocate_memory(formatStringsComponentArraySize, OMRMEM_CATEGORY_TRACE); if (formatStringsComponentArray == NULL) { UT_DBGOUT(1, ("<UT> can't allocate formatStrings array\n")); rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY; goto epilogue; } /* parse the file */ currenttp = 0; tempPtr = fileContents; while (currenttp < numFormats) { if (*tempPtr == '\n') { unsigned int tempLen; tempPtr2 = ++tempPtr; /* points to start of next line */ /* read to the next space */ while (*tempPtr2 != ' ') { tempPtr2++; } tempLen = (unsigned int)(tempPtr2 - tempPtr); strncpy(compName, tempPtr, tempLen); compName[tempLen] = '\0'; if (datFileVersion >= 5.1F) { /* then the first word in each dat file line is of the form * componentName.integerID, which is all currently stored in comp name */ char *period = strchr(compName, '.'); if (period == NULL) { UT_DBGOUT(1, ("<UT> error parsing 5.1 dat file component name: [%.*s].\n", tempLen, tempPtr)); } else { *period = '\0'; /* replace the '.' with a '\0' to make compname coherent */ tempLen = (unsigned int)(period - compName); ++period; } } tempPtr = tempPtr2; if ((tempLen == componentNameLength) && (0 == strncmp(compName, componentData->componentName, tempLen))) { /* skip the next four fields: type number, overhead, level and explicit. e.g. " 0 1 1 N " */ int field; for (field = 0; field < 4; field++) { while (*++tempPtr != ' '); } tempPtr++; tempPtr2 = tempPtr; /* find the end of the trace point name */ while (*tempPtr2 != ' ') { tempPtr2++; } tempLen = (unsigned int)(tempPtr2 - tempPtr); /* Skip the name as we don't use them. */ tempPtr2 += 2; /* skip space and open quote */ tempPtr = tempPtr2; /* find the end of the tracepoint's format string */ while (*tempPtr2 != '\"') { tempPtr2++; } tempLen = (unsigned int)(tempPtr2 - tempPtr); formatStringsComponentArray[currenttp] = (char *)omrmem_allocate_memory(tempLen + 1, OMRMEM_CATEGORY_TRACE); if (formatStringsComponentArray[currenttp] == NULL) { UT_DBGOUT(1, ("<UT> can't allocate a format string\n")); rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY; goto epilogue; } else { strncpy(formatStringsComponentArray[currenttp], tempPtr, tempLen); formatStringsComponentArray[currenttp][tempLen] = '\0'; } tempPtr = tempPtr2; /* up to the end of line */ currenttp++; } } tempPtr++; if (tempPtr >= (fileContents + fileSize - 1)) { /* fill any remaining positions in the arrays so they can be dereferenced */ for (; currenttp < numFormats; currenttp++) { formatStringsComponentArray[currenttp] = (char *)UT_MISSING_TRACE_FORMAT; } break; } } componentData->numFormats = numFormats; componentData->tracepointFormattingStrings = formatStringsComponentArray; epilogue: if (OMR_ERROR_NONE != rc) { int i = 0; componentData->alreadyfailedtoloaddetails = 1; /* don't try and load them again next time */ if (formatStringsComponentArray != NULL) { for (i = 0; i < formatStringsComponentArraySize; i++) { if (formatStringsComponentArray[i] != NULL) { omrmem_free_memory(formatStringsComponentArray[i]); } else { break; } } omrmem_free_memory(formatStringsComponentArray); } if (formatFileFD != -1) { omrfile_close(formatFileFD); } } if (fileContents != NULL) { omrmem_free_memory(fileContents); } return rc; }