VOID ServiceStart (DWORD dwArgc, LPTSTR *Argv) { #ifdef _WIN32 setlogfile("\\%s.newagent.txt"); #else setlogfile("./%s.newagent.txt"); #endif logfile("Start NewAgent server\r\n"); #define tempdir "C:\\NewAgentTemp" CreateDirectory(tempdir,0); SetCurrentDirectory(tempdir); if (dwArgc>1) is_debug_session = (stristr(Argv[1],"-debug") != NULL); unsigned port = NEWAGENT_PORT; if (dwArgc>2) port = atoi(Argv[2]); try { RunAgentServer(port); } catch (...) { } }
void CheckHook() { ShimData *data = NULL; HANDLE datahandle = OpenFileMappingA(FILE_MAP_READ, FALSE, GLOBAL_HOOK_DATA_NAME); if(datahandle == NULL) { LOGPRINT(L"renderdocshim: can't open global data\n"); return; } data = (ShimData *)MapViewOfFile(datahandle, FILE_MAP_READ, 0, 0, sizeof(ShimData)); if(data == NULL) { CloseHandle(datahandle); LOGPRINT(L"renderdocshim: can't map global data\n"); return; } if(data->pathmatchstring[0] == 0 || data->pathmatchstring[1] == 0 || data->pathmatchstring[2] == 0 || data->pathmatchstring[3] == 0) { LOGPRINT(L"renderdocshim: invalid pathmatchstring: '"); LOGPRINT(data->pathmatchstring); LOGPRINT(L"'\n"); UnmapViewOfFile(data); CloseHandle(datahandle); return; } // no new[], need to use VirtualAlloc const int exepathLen = 1024; wchar_t *exepath = (wchar_t *)VirtualAlloc(NULL, exepathLen * sizeof(wchar_t), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); if(exepath) { // no memset :). for(int i = 0; i < exepathLen; i++) exepath[i] = 0; GetModuleFileNameW(NULL, exepath, exepathLen - 1); // no str*cmp functions int find = FindStringOrdinal(FIND_FROMSTART, exepath, -1, data->pathmatchstring, -1, TRUE); if(find >= 0) { LOGPRINT(L"renderdocshim: Hooking into '"); LOGPRINT(exepath); LOGPRINT(L"', based on '"); LOGPRINT(data->pathmatchstring); LOGPRINT(L"'\n"); HMODULE mod = LoadLibraryW(data->rdocpath); if(mod) { pINTERNAL_SetCaptureOptions setopts = (pINTERNAL_SetCaptureOptions)GetProcAddress(mod, "INTERNAL_SetCaptureOptions"); pINTERNAL_SetLogFile setlogfile = (pINTERNAL_SetLogFile)GetProcAddress(mod, "INTERNAL_SetLogFile"); pRENDERDOC_SetDebugLogFile setdebuglog = (pRENDERDOC_SetDebugLogFile)GetProcAddress(mod, "RENDERDOC_SetDebugLogFile"); if(setopts) setopts((const CaptureOptions *)data->opts); if(setlogfile && data->logfile[0]) setlogfile(data->logfile); if(setdebuglog && data->debuglog[0]) setdebuglog(data->debuglog); } } else { LOGPRINT(L"renderdocshim: NOT Hooking into '"); LOGPRINT(exepath); LOGPRINT(L"', based on '"); LOGPRINT(data->pathmatchstring); LOGPRINT(L"'\n"); } VirtualFree(exepath, 0, MEM_RELEASE); } else { LOGPRINT(L"renderdocshim: Failed to allocate exepath\n"); } UnmapViewOfFile(data); CloseHandle(datahandle); }
int main (int argc, char **argv, char **envp) { int narg; fd_set set; char commandLine[BIG_LINE_MAX]; // data from cdt char consoleLine[LINE_MAX]; // data from eclipse's console long chars; struct timeval timeout; int isVersion=0, isInterpreter=0; const char *testCommand=NULL; int isLog=0; int logmask=LOG_ALL; state.ptyfd = EOF; state.gdbPrompt = "GNU gdb (GDB) 7.7.1\n"; sprintf (state.lldbmi2Prompt, "lldbmi2 version %s\n", LLDBMI2_VERSION); state.cdtbufferB.grow(BIG_LINE_MAX); limits.frames_max = FRAMES_MAX; limits.children_max = CHILDREN_MAX; limits.walk_depth_max = WALK_DEPTH_MAX; limits.change_depth_max = CHANGE_DEPTH_MAX; // get args for (narg=0; narg<argc; narg++) { logarg (argv[narg]); if (strcmp (argv[narg],"--version") == 0) isVersion = 1; else if (strcmp (argv[narg],"--interpreter") == 0 ) { isInterpreter = 1; if (++narg<argc) logarg(argv[narg]); } else if (strcmp (argv[narg],"--test") == 0 ) { limits.istest = true; if (++narg<argc) sscanf (logarg(argv[narg]), "%d", &state.test_sequence); if (state.test_sequence) setTestSequence (state.test_sequence); } else if (strcmp (argv[narg],"--script") == 0 ) { limits.istest = true; if (++narg<argc) strcpy (state.test_script, logarg(argv[narg])); // no spaces allowed in the name if (state.test_script[0]) setTestScript (state.test_script); } else if (strcmp (argv[narg],"--log") == 0 ) isLog = 1; else if (strcmp (argv[narg],"--logmask") == 0 ) { isLog = 1; if (++narg<argc) sscanf (logarg(argv[narg]), "%x", &logmask); } else if (strcmp (argv[narg],"--frames") == 0 ) { if (++narg<argc) sscanf (logarg(argv[narg]), "%d", &limits.frames_max); } else if (strcmp (argv[narg],"--children") == 0 ) { if (++narg<argc) sscanf (logarg(argv[narg]), "%d", &limits.children_max); } else if (strcmp (argv[narg],"--walkdepth") == 0 ) { if (++narg<argc) sscanf (logarg(argv[narg]), "%d", &limits.walk_depth_max); } else if (strcmp (argv[narg],"--changedepth") == 0 ) { if (++narg<argc) sscanf (logarg(argv[narg]), "%d", &limits.change_depth_max); } } // create a log filename from program name and open log file if (isLog) { if (limits.istest) setlogfile (state.logfilename, sizeof(state.logfilename), argv[0], "lldbmi2t.log"); else setlogfile (state.logfilename, sizeof(state.logfilename), argv[0], "lldbmi2.log"); openlog (state.logfilename); setlogmask (logmask); } // log program args addlog("\n"); logprintf (LOG_ARGS, NULL); state.envp[0] = NULL; state.envpentries = 0; state.envspointer = state.envs; const char *wl = "PWD="; // want to get eclipse project_loc if any int wll = strlen(wl); // copy environment for tested program for (int ienv=0; envp[ienv]; ienv++) { addEnvironment (&state, envp[ienv]); if (strncmp(envp[ienv], wl, wll)==0) strcpy (state.project_loc, envp[ienv]+wll); } // return gdb version if --version if (isVersion) { writetocdt (state.gdbPrompt); writetocdt (state.lldbmi2Prompt); return EXIT_SUCCESS; } // check if --interpreter mi2 else if (!isInterpreter) { help (&state); return EXIT_FAILURE; } initializeSB (&state); signal (SIGINT, signalHandler); signal (SIGSTOP, signalHandler); cdtprintf ("(gdb)\n"); // main loop FD_ZERO (&set); while (!state.eof) { if (limits.istest) logprintf (LOG_NONE, "main loop\n"); // get inputs timeout.tv_sec = 0; timeout.tv_usec = 200000; // check command from CDT FD_SET (STDIN_FILENO, &set); if (state.ptyfd != EOF) { // check data from Eclipse's console FD_SET (state.ptyfd, &set); select(state.ptyfd+1, &set, NULL, NULL, &timeout); } else select(STDIN_FILENO+1, &set, NULL, NULL, &timeout); if (FD_ISSET(STDIN_FILENO, &set) && !state.eof && !limits.istest) { logprintf (LOG_NONE, "read in\n"); chars = read (STDIN_FILENO, commandLine, sizeof(commandLine)-1); logprintf (LOG_NONE, "read out %d chars\n", chars); if (chars>0) { commandLine[chars] = '\0'; while (fromCDT (&state,commandLine,sizeof(commandLine)) == MORE_DATA) commandLine[0] = '\0'; } else state.eof = true; } if (state.ptyfd!=EOF && state.isrunning) { // input from user to program if (FD_ISSET(state.ptyfd, &set) && !state.eof && !limits.istest) { logprintf (LOG_NONE, "pty read in\n"); chars = read (state.ptyfd, consoleLine, sizeof(consoleLine)-1); logprintf (LOG_NONE, "pty read out %d chars\n", chars); if (chars>0) { logprintf (LOG_PROG_OUT, "pty read %d chars\n", chars); consoleLine[chars] = '\0'; SBProcess process = state.process; if (process.IsValid()) process.PutSTDIN (consoleLine, chars); } } } // execute test command if test mode if (!state.eof && limits.istest && !state.isrunning) { if ((testCommand=getTestCommand ())!=NULL) { snprintf (commandLine, sizeof(commandLine), "%s\n", testCommand); fromCDT (&state, commandLine, sizeof(commandLine)); } } // execute stacked commands if many command arrived once if (!state.eof && state.cdtbufferB.size()>0) { commandLine[0] = '\0'; while (fromCDT (&state, commandLine, sizeof(commandLine)) == MORE_DATA) ; } } if (state.ptyfd != EOF) close (state.ptyfd); terminateSB (); logprintf (LOG_INFO, "main exit\n"); closelog (); return EXIT_SUCCESS; }