int PrepareTracers(vktrace_process_capture_trace_thread_info** ppTracerInfo) { unsigned int num_tracers = 1; assert(ppTracerInfo != NULL && *ppTracerInfo == NULL); *ppTracerInfo = VKTRACE_NEW_ARRAY(vktrace_process_capture_trace_thread_info, num_tracers); memset(*ppTracerInfo, 0, sizeof(vktrace_process_capture_trace_thread_info) * num_tracers); // we only support Vulkan tracer (*ppTracerInfo)[0].tracerId = VKTRACE_TID_VULKAN; return num_tracers; }
// ------------------------------------------------------------------------------------------------ int main(int argc, char* argv[]) { memset(&g_settings, 0, sizeof(vktrace_settings)); vktrace_LogSetCallback(loggingCallback); vktrace_LogSetLevel(VKTRACE_LOG_ERROR); // setup defaults memset(&g_default_settings, 0, sizeof(vktrace_settings)); g_default_settings.output_trace = vktrace_allocate_and_copy("vktrace_out.vktrace"); g_default_settings.verbosity = "errors"; g_default_settings.screenshotList = NULL; if (vktrace_SettingGroup_init(&g_settingGroup, NULL, argc, argv, &g_settings.arguments) != 0) { // invalid cmd-line parameters vktrace_SettingGroup_delete(&g_settingGroup); vktrace_free(g_default_settings.output_trace); return -1; } else { // Validate vktrace inputs BOOL validArgs = TRUE; if (g_settings.output_trace == NULL || strlen (g_settings.output_trace) == 0) { validArgs = FALSE; } else { size_t len = strlen(g_settings.output_trace); if (strncmp(&g_settings.output_trace[len-8], ".vktrace", 8) != 0) { // output trace filename does not end in .vktrace vktrace_LogError("Output trace file specified with -o parameter must have a '.vktrace' extension."); validArgs = FALSE; } } if (strcmp(g_settings.verbosity, "quiet") == 0) vktrace_LogSetLevel(VKTRACE_LOG_NONE); else if (strcmp(g_settings.verbosity, "errors") == 0) vktrace_LogSetLevel(VKTRACE_LOG_ERROR); else if (strcmp(g_settings.verbosity, "warnings") == 0) vktrace_LogSetLevel(VKTRACE_LOG_WARNING); else if (strcmp(g_settings.verbosity, "full") == 0) vktrace_LogSetLevel(VKTRACE_LOG_VERBOSE); #if _DEBUG else if (strcmp(g_settings.verbosity, "debug") == 0) vktrace_LogSetLevel(VKTRACE_LOG_DEBUG); #endif else { vktrace_LogSetLevel(VKTRACE_LOG_ERROR); validArgs = FALSE; } vktrace_set_global_var("_VK_TRACE_VERBOSITY", g_settings.verbosity); if (validArgs == FALSE) { vktrace_SettingGroup_print(&g_settingGroup); return -1; } if (g_settings.program == NULL || strlen(g_settings.program) == 0) { vktrace_LogWarning("No program (-p) parameter found: Will run vktrace as server."); printf("Running vktrace as server...\n"); fflush(stdout); g_settings.arguments = NULL; } else { if (g_settings.working_dir == NULL || strlen(g_settings.working_dir) == 0) { CHAR* buf = VKTRACE_NEW_ARRAY(CHAR, 4096); vktrace_LogVerbose("No working directory (-w) parameter found: Assuming executable's path as working directory."); vktrace_platform_full_path(g_settings.program, 4096, buf); g_settings.working_dir = vktrace_platform_extract_path(buf); VKTRACE_DELETE(buf); } vktrace_LogVerbose("Running vktrace as parent process will spawn child process: %s", g_settings.program); if (g_settings.arguments != NULL && strlen(g_settings.arguments) > 0) { vktrace_LogVerbose("Args to be passed to child process: '%s'", g_settings.arguments); } } } if (g_settings.screenshotList) { // Export list to screenshot layer vktrace_set_global_var("_VK_SCREENSHOT", g_settings.screenshotList); } else { vktrace_set_global_var("_VK_SCREENSHOT",""); } unsigned int serverIndex = 0; do { // Create and start the process or run in server mode BOOL procStarted = TRUE; vktrace_process_info procInfo; memset(&procInfo, 0, sizeof(vktrace_process_info)); if (g_settings.program != NULL) { procInfo.exeName = vktrace_allocate_and_copy(g_settings.program); procInfo.processArgs = vktrace_allocate_and_copy(g_settings.arguments); procInfo.fullProcessCmdLine = vktrace_copy_and_append(g_settings.program, " ", g_settings.arguments); procInfo.workingDirectory = vktrace_allocate_and_copy(g_settings.working_dir); procInfo.traceFilename = vktrace_allocate_and_copy(g_settings.output_trace); } else { const char *pExtension = strrchr(g_settings.output_trace, '.'); char *basename = vktrace_allocate_and_copy_n(g_settings.output_trace, (int) ((pExtension == NULL) ? strlen(g_settings.output_trace) : pExtension - g_settings.output_trace)); char num[16]; #ifdef PLATFORM_LINUX snprintf(num, 16, "%u", serverIndex); #elif defined(WIN32) _snprintf_s(num, 16, _TRUNCATE, "%u", serverIndex); #endif procInfo.traceFilename = vktrace_copy_and_append(basename, num, pExtension); } procInfo.parentThreadId = vktrace_platform_get_thread_id(); // setup tracer, only Vulkan tracer suppported PrepareTracers(&procInfo.pCaptureThreads); if (g_settings.program != NULL) { char *instEnv = vktrace_get_global_var("VK_INSTANCE_LAYERS"); // Add ScreenShot layer if enabled if (g_settings.screenshotList && (!instEnv || !strstr(instEnv, "VK_LAYER_LUNARG_screenshot"))) { if (!instEnv || strlen(instEnv) == 0) vktrace_set_global_var("VK_INSTANCE_LAYERS", "VK_LAYER_LUNARG_screenshot"); else { char *newEnv = vktrace_copy_and_append(instEnv, VKTRACE_LIST_SEPARATOR, "VK_LAYER_LUNARG_screenshot"); vktrace_set_global_var("VK_INSTANCE_LAYERS", newEnv); } instEnv = vktrace_get_global_var("VK_INSTANCE_LAYERS"); } char *devEnv = vktrace_get_global_var("VK_DEVICE_LAYERS"); if (g_settings.screenshotList && (!devEnv || !strstr(devEnv, "VK_LAYER_LUNARG_screenshot"))) { if (!devEnv || strlen(devEnv) == 0) vktrace_set_global_var("VK_DEVICE_LAYERS", "VK_LAYER_LUNARG_screenshot"); else { char *newEnv = vktrace_copy_and_append(devEnv, VKTRACE_LIST_SEPARATOR, "VK_LAYER_LUNARG_screenshot"); vktrace_set_global_var("VK_DEVICE_LAYERS", newEnv); } devEnv = vktrace_get_global_var("VK_DEVICE_LAYERS"); } // Add vktrace_layer enable env var if needed if (!instEnv || strlen(instEnv) == 0) { vktrace_set_global_var("VK_INSTANCE_LAYERS", "VK_LAYER_LUNARG_vktrace"); } else if (instEnv != strstr(instEnv, "VK_LAYER_LUNARG_vktrace")) { char *newEnv = vktrace_copy_and_append("VK_LAYER_LUNARG_vktrace", VKTRACE_LIST_SEPARATOR, instEnv); vktrace_set_global_var("VK_INSTANCE_LAYERS", newEnv); } if (!devEnv || strlen(devEnv) == 0) { vktrace_set_global_var("VK_DEVICE_LAYERS", "VK_LAYER_LUNARG_vktrace"); } else if (devEnv != strstr(devEnv, "VK_LAYER_LUNARG_vktrace")) { char *newEnv = vktrace_copy_and_append("VK_LAYER_LUNARG_vktrace", VKTRACE_LIST_SEPARATOR, devEnv); vktrace_set_global_var("VK_DEVICE_LAYERS", newEnv); } // call CreateProcess to launch the application procStarted = vktrace_process_spawn(&procInfo); } if (procStarted == FALSE) { vktrace_LogError("Failed to setup remote process."); } else { if (InjectTracersIntoProcess(&procInfo) == FALSE) { vktrace_LogError("Failed to setup tracer communication threads."); return -1; } // create watchdog thread to monitor existence of remote process if (g_settings.program != NULL) procInfo.watchdogThread = vktrace_platform_create_thread(Process_RunWatchdogThread, &procInfo); #if defined(PLATFORM_LINUX) // Sync wait for local threads and remote process to complete. vktrace_platform_sync_wait_for_thread(&(procInfo.pCaptureThreads[0].recordingThread)); if (g_settings.program != NULL) vktrace_platform_sync_wait_for_thread(&procInfo.watchdogThread); #else vktrace_platform_resume_thread(&procInfo.hThread); // Now into the main message loop, listen for hotkeys to send over. MessageLoop(); #endif } vktrace_process_info_delete(&procInfo); serverIndex++; } while (g_settings.program == NULL); vktrace_SettingGroup_delete(&g_settingGroup); vktrace_free(g_default_settings.output_trace); return 0; }
//----------------------------------------------------------------------------- bool vktraceviewer_QTraceFileLoader::populate_trace_file_info(vktraceviewer_trace_file_info* pTraceFileInfo) { assert(pTraceFileInfo != NULL); assert(pTraceFileInfo->pFile != NULL); // read trace file header if (1 != fread(&(pTraceFileInfo->header), sizeof(vktrace_trace_file_header), 1, pTraceFileInfo->pFile)) { emit OutputMessage(VKTRACE_LOG_ERROR, "Unable to read header from file."); return false; } // Find out how many trace packets there are. // Seek to first packet long first_offset = pTraceFileInfo->header.first_packet_offset; int seekResult = fseek(pTraceFileInfo->pFile, first_offset, SEEK_SET); if (seekResult != 0) { emit OutputMessage(VKTRACE_LOG_WARNING, "Failed to seek to the first packet offset in the trace file."); } uint64_t fileOffset = pTraceFileInfo->header.first_packet_offset; uint64_t packetSize = 0; while(1 == fread(&packetSize, sizeof(uint64_t), 1, pTraceFileInfo->pFile)) { // success! pTraceFileInfo->packetCount++; fileOffset += packetSize; fseek(pTraceFileInfo->pFile, fileOffset, SEEK_SET); } if (pTraceFileInfo->packetCount == 0) { if (ferror(pTraceFileInfo->pFile) != 0) { perror("File Read error:"); emit OutputMessage(VKTRACE_LOG_ERROR, "There was an error reading the trace file."); return false; } else if (feof(pTraceFileInfo->pFile) != 0) { emit OutputMessage(VKTRACE_LOG_WARNING, "Reached the end of the file."); } emit OutputMessage(VKTRACE_LOG_WARNING, "There are no trace packets in this trace file."); pTraceFileInfo->pPacketOffsets = NULL; } else { pTraceFileInfo->pPacketOffsets = VKTRACE_NEW_ARRAY(vktraceviewer_trace_file_packet_offsets, pTraceFileInfo->packetCount); // rewind to first packet and this time, populate the packet offsets if (fseek(pTraceFileInfo->pFile, first_offset, SEEK_SET) != 0) { emit OutputMessage(VKTRACE_LOG_ERROR, "Unable to rewind trace file to gather packet offsets."); return false; } unsigned int packetIndex = 0; fileOffset = first_offset; while(1 == fread(&packetSize, sizeof(uint64_t), 1, pTraceFileInfo->pFile)) { // the fread confirms that this packet exists // NOTE: We do not actually read the entire packet into memory right now. pTraceFileInfo->pPacketOffsets[packetIndex].fileOffset = fileOffset; // rewind slightly fseek(pTraceFileInfo->pFile, -1*(long)sizeof(uint64_t), SEEK_CUR); // allocate space for the packet and read it in pTraceFileInfo->pPacketOffsets[packetIndex].pHeader = (vktrace_trace_packet_header*)vktrace_malloc(packetSize); if (1 != fread(pTraceFileInfo->pPacketOffsets[packetIndex].pHeader, packetSize, 1, pTraceFileInfo->pFile)) { emit OutputMessage(VKTRACE_LOG_ERROR, "Unable to read in a trace packet."); return false; } // adjust pointer to body of the packet pTraceFileInfo->pPacketOffsets[packetIndex].pHeader->pBody = (uintptr_t)pTraceFileInfo->pPacketOffsets[packetIndex].pHeader + sizeof(vktrace_trace_packet_header); // now seek to what should be the next packet fileOffset += packetSize; packetIndex++; } if (fseek(pTraceFileInfo->pFile, first_offset, SEEK_SET) != 0) { emit OutputMessage(VKTRACE_LOG_ERROR, "Unable to rewind trace file to restore position."); return false; } } return true; }