BOOL vktrace_process_spawn(vktrace_process_info* pInfo) { assert(pInfo != NULL); #if defined(WIN32) { unsigned long processCreateFlags = CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED; char fullExePath[_MAX_PATH]; PROCESS_INFORMATION processInformation; STARTUPINFO si = { 0 }; si.cb = sizeof(si); memset(&processInformation, 0, sizeof(PROCESS_INFORMATION)); memset(fullExePath, 0, sizeof(char)*_MAX_PATH); fullExePath[0] = 0; SetLastError(0); if (0==SearchPath(NULL, pInfo->exeName, ".exe", ARRAYSIZE(fullExePath), fullExePath, NULL)) { vktrace_LogVerbose("Failed to spawn '%s'.", pInfo->exeName); return FALSE; } if (!CreateProcess(fullExePath, pInfo->fullProcessCmdLine, NULL, NULL, TRUE, processCreateFlags, NULL, pInfo->workingDirectory, &si, &processInformation)) { vktrace_LogVerbose("Failed to spawn '%s'.", fullExePath); return FALSE; } pInfo->hProcess = processInformation.hProcess; pInfo->hThread = processInformation.hThread; pInfo->processId = processInformation.dwProcessId; // TODO : Do we need to do anything with processInformation.dwThreadId? } #elif defined(PLATFORM_LINUX) pInfo->processId = fork(); if (pInfo->processId == -1) { vktrace_LogError("Failed to spawn process."); return FALSE; } else if (pInfo->processId == 0) { // Inside new process char *args[128]; const char delim[] = " \t"; unsigned int idx; // Change process name so the the tracer DLLs will behave as expected when loaded. // NOTE: Must be 15 characters or less. const char * tmpProcName = "vktraceChildProcess"; prctl(PR_SET_NAME, (unsigned long)tmpProcName, 0, 0, 0); // Change working directory if (chdir(pInfo->workingDirectory) == -1) { vktrace_LogError("Failed to set working directory."); } args[0] = pInfo->exeName; args[127] = NULL; idx = 1; args[idx] = strtok(pInfo->processArgs, delim); while ( args[idx] != NULL && idx < 128) { idx++; args[idx] = strtok(NULL, delim); } vktrace_LogDebug("exec process=%s argc=%u\n", pInfo->exeName, idx); #if 0 //uncoment to print out list of env vars char *env = environ[0]; idx = 0; while (env && strlen(env)) { if (strstr(env, "VK") || strstr(env, "LD")) vktrace_LogDebug("env[%d] = %s", idx++, env); else idx++; env = environ[idx]; } #endif if (execv(pInfo->exeName, args) < 0) { vktrace_LogError("Failed to spawn process."); exit(1); } } #endif return TRUE; }
// ------------------------------------------------------------------------------------------------ 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; }