DR_EXPORT void dr_init(client_id_t id) { dr_set_client_name("DynamoRIO Sample Client 'instrcalls'", "http://dynamorio.org/issues"); my_id = id; /* make it easy to tell, by looking at log file, which client executed */ dr_log(NULL, LOG_ALL, 1, "Client 'instrcalls' initializing\n"); /* also give notification to stderr */ #ifdef SHOW_RESULTS if (dr_is_notify_on()) { # ifdef WINDOWS /* ask for best-effort printing to cmd window. must be called in dr_init(). */ dr_enable_console_printing(); # endif dr_fprintf(STDERR, "Client instrcalls is running\n"); } #endif dr_register_exit_event(event_exit); dr_register_bb_event(event_basic_block); dr_register_thread_init_event(event_thread_init); dr_register_thread_exit_event(event_thread_exit); #ifdef SHOW_SYMBOLS if (drsym_init(0) != DRSYM_SUCCESS) { dr_log(NULL, LOG_ALL, 1, "WARNING: unable to initialize symbol translation\n"); } #endif }
static void process_symbols(void *dcontext, char *dllname, LOADED_IMAGE *img) { /* We have to specify the module via "modname!symname". * We must use the same modname as in full_path. */ # define MAX_SYM_WITH_MOD_LEN 256 char sym_with_mod[MAX_SYM_WITH_MOD_LEN]; size_t modoffs; drsym_error_t symres; char *fname = NULL, *c; search_data_t sd; if (drsym_init(NULL) != DRSYM_SUCCESS) { print("WARNING: unable to initialize symbol engine\n"); return; } if (dllname == NULL) return; for (c = dllname; *c != '\0'; c++) { if (*c == '/' || *c == '\\') fname = c + 1; } assert(fname != NULL && "unable to get fname for module"); if (fname == NULL) return; /* now get rid of extension */ for (; c > fname && *c != '.'; c--) ; /* nothing */ assert(c - fname < BUFFER_SIZE_ELEMENTS(sym_with_mod) && "sizes way off"); modoffs = dr_snprintf(sym_with_mod, c - fname, "%s", fname); assert(modoffs > 0 && "error printing modname!symname"); modoffs = dr_snprintf(sym_with_mod + modoffs, BUFFER_SIZE_ELEMENTS(sym_with_mod) - modoffs, "!%s", SYM_PATTERN); assert(modoffs > 0 && "error printing modname!symname"); sd.dcontext = dcontext; sd.img = img; verbose_print("Searching \"%s\" for \"%s\"\n", dllname, sym_with_mod); symres = drsym_search_symbols(dllname, sym_with_mod, true, search_syms_cb, &sd); if (symres != DRSYM_SUCCESS) print("Error %d searching \"%s\" for \"%s\"\n", dllname, sym_with_mod); drsym_exit(); }
/* * Main entry point that sets up all the facilities we need. */ DR_EXPORT void dr_client_main(client_id_t id, int argc, const char **argv) { dr_set_client_name( "Time-sensitive activity logger for PuTTY crypto testing", "https://www.chiark.greenend.org.uk/~sgtatham/putty/"); outfile = INVALID_FILE; bool ok = drmgr_init(); DR_ASSERT(ok); /* * Run our main instrumentation pass with lower priority than * drwrap, so that we don't start logging the inside of a function * whose drwrap pre-wrapper would have wanted to disable logging. */ drmgr_priority_t pri = {sizeof(pri), "sclog", NULL, NULL, DRMGR_PRIORITY_INSERT_DRWRAP+1}; ok = drmgr_register_bb_instrumentation_event( NULL, instrument_instr, &pri); DR_ASSERT(ok); ok = drutil_init(); DR_ASSERT(ok); ok = drwrap_init(); DR_ASSERT(ok); drsym_error_t symstatus = drsym_init(0); DR_ASSERT(symstatus == DRSYM_SUCCESS); dr_register_exit_event(exit_event); drreg_options_t ops = { sizeof(ops), 3, false }; drreg_status_t regstatus = drreg_init(&ops); DR_ASSERT(regstatus == DRREG_SUCCESS); drmgr_register_module_load_event(load_module); ok = drmgr_register_bb_app2app_event(expand_rep_movsb, NULL); DR_ASSERT(ok); }
DR_EXPORT void dr_init(client_id_t id) { uint i = 0; uint const_arrays_num; drsys_options_t ops = { sizeof(ops), 0, }; dr_set_client_name("Dr. STrace", "http://drmemory.org/issues"); #ifdef WINDOWS dr_enable_console_printing(); #endif options_init(id); drsym_init(0); drmgr_init(); drx_init(); if (drsys_init(id, &ops) != DRMF_SUCCESS) ASSERT(false, "drsys failed to init"); dr_register_exit_event(exit_event); dr_register_filter_syscall_event(event_filter_syscall); drmgr_register_pre_syscall_event(event_pre_syscall); drmgr_register_post_syscall_event(event_post_syscall); if (drsys_filter_all_syscalls() != DRMF_SUCCESS) ASSERT(false, "drsys_filter_all_syscalls should never fail"); open_log_file(); const_arrays_num = get_const_arrays_num(); hashtable_init(&nconsts_table, HASHTABLE_BITSIZE, HASH_STRING, false); while (i < const_arrays_num) { const_values_t *named_consts = const_struct_array[i]; bool res = hashtable_add(&nconsts_table, (void *) named_consts[0].const_name, (void *) named_consts); if (!res) ASSERT(false, "drstrace failed to add to hashtable"); i++; } }
DR_EXPORT void dr_init(client_id_t id) { printf("Started dr_init\n"); dr_register_exit_event(exit_event); dr_register_bb_event(bb_event); dr_register_thread_init_event(writeLog); count_mutex = dr_mutex_create(); client_id = id; #ifdef SHOW_SYMBOLS if (drsym_init(0) != DRSYM_SUCCESS) { dr_log(NULL, LOG_ALL, 1, "WARNING: unable to initialize symbol translation\n"); } #endif htinit(); }
/*----------------------------------------------------------------------------*/ static void probe_def_init(void) { probes[0].name = "chrome probe"; probes[0].insert_loc.type = DR_PROBE_ADDR_LIB_OFFS; probes[0].insert_loc.lib_offs.library = "../mutatee/chrome"; drsym_init(0); { size_t exe_export_offs; drsym_error_t r = drsym_lookup_symbol("../mutatee/chrome", "doubler",&exe_export_offs, DRSYM_DEMANGLE); if (r!=DRSYM_SUCCESS) { dr_fprintf(STDERR, "<FAILED to find gpu::gles2::GLES2Implementation::Viewport\n"); } else { dr_printf("<Found Original symbol>\n"); probes[0].insert_loc.lib_offs.offset = exe_export_offs; } } //probes[0].insert_loc.lib_offs.offset = 0x50530; probes[0].callback_func.type = DR_PROBE_ADDR_LIB_OFFS; probes[0].callback_func.lib_offs.library = "libhooks.so"; { size_t exe_export_offs; drsym_error_t r = drsym_lookup_symbol("libhooks.so", "preHook",&exe_export_offs, DRSYM_DEMANGLE); if (r!=DRSYM_SUCCESS) { dr_fprintf(STDERR, "<FAILED to find gpu::gles2::GLES2Implementation::Viewport>\n"); } else { dr_printf("<Found Hook symbol>\n"); probes[0].callback_func.lib_offs.offset = exe_export_offs; } } drsym_exit(); /* probes[0].callback_func.lib_offs.offset = 0xe30; */ }
DR_EXPORT void dr_init( client_id_t id ) { dr_printf( "In dr_init()\n" ); // Initialize extensions. drsym_error_t rc = drsym_init( 0 ); if( DRSYM_SUCCESS != rc ) { dr_printf( "drsym_init() failed: %i\n", rc ); exit( 1 ); } bool wrapInit = drwrap_init(); if( !wrapInit ) { dr_printf( "drwrap_init() failed\n" ); exit( 1 ); } // Set up output. char fileName[256]; unsigned int pid = (unsigned int)dr_get_process_id(); dr_snprintf( fileName, sizeof( fileName ), "objcount-%u.out", pid ); fileName[sizeof( fileName ) - 1] = 0; outFile = dr_open_file( fileName, DR_FILE_WRITE_OVERWRITE ); outMutex = dr_mutex_create(); // Set up hashtable. hashtable_init_ex( &wraps, // table 16, // num_bits HASH_INTPTR, // hashtype false, // str_dup false, // synch &free_wrap, // free_payload_func NULL, // hash_key_func NULL ); // cmp_key_func // Register for events. dr_register_module_load_event( onLoad ); dr_register_exit_event( onExit ); }
bool drstrace_unit_test_syscall_init() { uint const_arrays_num; uint i = 0; dr_standalone_init(); if (drsym_init(0) != DRSYM_SUCCESS) return false; const_arrays_num = get_const_arrays_num(); hashtable_init(&nconsts_table, HASHTABLE_BITSIZE, HASH_STRING, false); while (i < const_arrays_num) { const_values_t *named_consts = const_struct_array[i]; bool res = hashtable_add(&nconsts_table, (void *) named_consts[0].const_name, (void *) named_consts); if (!res) return false; i++; } return true; }
int main(int argc, char *argv[]) { char *dll = NULL; int i; /* module + address per line */ char line[MAXIMUM_PATH*2]; size_t modoffs; /* options that can be local vars */ bool addr2sym = false; bool addr2sym_multi = false; bool sym2addr = false; bool enumerate = false; bool enumerate_all = false; bool search = false; bool searchall = false; for (i = 1; i < argc; i++) { if (_stricmp(argv[i], "-e") == 0) { if (i+1 >= argc) { PRINT_USAGE(argv[0]); return 1; } i++; dll = argv[i]; if ( #ifdef WINDOWS _access(dll, 4/*read*/) == -1 #else !dr_file_exists(dll) #endif ) { printf("ERROR: invalid path %s\n", dll); return 1; } } else if (_stricmp(argv[i], "-f") == 0) { show_func = true; } else if (_stricmp(argv[i], "-v") == 0) { verbose = true; } else if (_stricmp(argv[i], "-a") == 0 || _stricmp(argv[i], "-s") == 0) { if (i+1 >= argc) { PRINT_USAGE(argv[0]); return 1; } if (_stricmp(argv[i], "-a") == 0) addr2sym = true; else sym2addr = true; i++; /* rest of args read below */ break; } else if (_stricmp(argv[i], "-q") == 0) { addr2sym_multi = true; } else if (_stricmp(argv[i], "--enum") == 0) { enumerate = true; } else if (_stricmp(argv[i], "--list") == 0) { enumerate_all = true; } else if (_stricmp(argv[i], "--search") == 0) { search = true; } else if (_stricmp(argv[i], "--searchall") == 0) { search = true; searchall = true; } else { PRINT_USAGE(argv[0]); return 1; } } if (((sym2addr || addr2sym) && dll == NULL) || (addr2sym_multi && dll != NULL) || (!sym2addr && !addr2sym && !addr2sym_multi && !enumerate_all)) { PRINT_USAGE(argv[0]); return 1; } dr_standalone_init(); if (drsym_init(IF_WINDOWS_ELSE(NULL, 0)) != DRSYM_SUCCESS) { printf("ERROR: unable to initialize symbol library\n"); return 1; } if (!addr2sym_multi) { if (enumerate_all) enumerate_symbols(dll, NULL, search, searchall); else { /* kind of a hack: assumes i hasn't changed and that -s/-a is last option */ for (; i < argc; i++) { if (addr2sym) { if (sscanf(argv[i], "%x", (uint *)&modoffs) == 1) lookup_address(dll, modoffs); else printf("ERROR: unknown input %s\n", argv[i]); } else if (enumerate || search) enumerate_symbols(dll, argv[i], search, searchall); else lookup_symbol(dll, argv[i]); } } } else { while (!feof(stdin)) { char modpath[MAXIMUM_PATH]; if (fgets(line, sizeof(line), stdin) == NULL || /* when postprocess.pl closes the pipe, fgets is not * returning, so using an alternative eof code */ strcmp(line, ";exit\n") == 0) break; /* Ensure we support spaces in paths by using ; to split. * Since ; separates PATH, no Windows dll will have ; in its name. */ if (sscanf(line, "%"MAX_PATH_STR"[^;];%x", (char *)&modpath, (uint *)&modoffs) == 2) { lookup_address(modpath, modoffs); fflush(stdout); /* ensure flush in case piped */ } else if (verbose) printf("Error: unknown input %s\n", line); } } if (drsym_exit() != DRSYM_SUCCESS) printf("WARNING: error cleaning up symbol library\n"); return 0; }
int _tmain(int argc, TCHAR *targv[]) { int res = 1; char **argv; char dll[MAXIMUM_PATH]; int i; /* module + address per line */ char line[MAXIMUM_PATH*2]; size_t modoffs; /* options that can be local vars */ bool addr2sym = false; bool addr2sym_multi = false; bool sym2addr = false; bool enumerate = false; bool enumerate_all = false; bool search = false; bool searchall = false; bool enum_lines = false; #if defined(WINDOWS) && !defined(_UNICODE) # error _UNICODE must be defined #else /* Convert to UTF-8 if necessary */ if (drfront_convert_args((const TCHAR **)targv, &argv, argc) != DRFRONT_SUCCESS) { printf("ERROR: failed to process args\n"); return 1; } #endif for (i = 1; i < argc; i++) { if (_stricmp(argv[i], "-e") == 0) { bool is_readable; if (i+1 >= argc) { PRINT_USAGE(argv[0]); goto cleanup; } i++; if (drfront_get_absolute_path(argv[i], dll, BUFFER_SIZE_ELEMENTS(dll)) != DRFRONT_SUCCESS) { printf("ERROR: invalid path %s\n", argv[i]); goto cleanup; } if (drfront_access(dll, DRFRONT_READ, &is_readable) != DRFRONT_SUCCESS || !is_readable) { printf("ERROR: invalid path %s\n", argv[i]); goto cleanup; } } else if (_stricmp(argv[i], "-f") == 0) { show_func = true; } else if (_stricmp(argv[i], "-v") == 0) { verbose = true; } else if (_stricmp(argv[i], "-a") == 0 || _stricmp(argv[i], "-s") == 0) { if (i+1 >= argc) { PRINT_USAGE(argv[0]); goto cleanup; } if (_stricmp(argv[i], "-a") == 0) addr2sym = true; else sym2addr = true; i++; /* rest of args read below */ break; } else if (_stricmp(argv[i], "--lines") == 0) { enum_lines = true; } else if (_stricmp(argv[i], "-q") == 0) { addr2sym_multi = true; } else if (_stricmp(argv[i], "--enum") == 0) { enumerate = true; } else if (_stricmp(argv[i], "--list") == 0) { enumerate_all = true; } else if (_stricmp(argv[i], "--search") == 0) { search = true; } else if (_stricmp(argv[i], "--searchall") == 0) { search = true; searchall = true; } else { PRINT_USAGE(argv[0]); goto cleanup; } } if ((!addr2sym_multi && dll == NULL) || (addr2sym_multi && dll != NULL) || (!sym2addr && !addr2sym && !addr2sym_multi && !enumerate_all && !enum_lines)) { PRINT_USAGE(argv[0]); goto cleanup; } dr_standalone_init(); if (dll != NULL) { if (!check_architecture(dll, argv)) goto cleanup; } if (drsym_init(IF_WINDOWS_ELSE(NULL, 0)) != DRSYM_SUCCESS) { printf("ERROR: unable to initialize symbol library\n"); goto cleanup; } if (!addr2sym_multi) { if (enum_lines) enumerate_lines(dll); else if (enumerate_all) enumerate_symbols(dll, NULL, search, searchall); else { /* kind of a hack: assumes i hasn't changed and that -s/-a is last option */ for (; i < argc; i++) { if (addr2sym) { if (sscanf(argv[i], SIZE_FMT, &modoffs) == 1) symquery_lookup_address(dll, modoffs); else printf("ERROR: unknown input %s\n", argv[i]); } else if (enumerate || search) enumerate_symbols(dll, argv[i], search, searchall); else symquery_lookup_symbol(dll, argv[i]); } } } else { while (!feof(stdin)) { char modpath[MAXIMUM_PATH]; if (fgets(line, sizeof(line), stdin) == NULL || /* when postprocess.pl closes the pipe, fgets is not * returning, so using an alternative eof code */ strcmp(line, ";exit\n") == 0) break; /* Ensure we support spaces in paths by using ; to split. * Since ; separates PATH, no Windows dll will have ; in its name. */ if (sscanf(line, "%"MAX_PATH_STR"[^;];"SIZE_FMT, (char *)&modpath, &modoffs) == 2) { symquery_lookup_address(modpath, modoffs); fflush(stdout); /* ensure flush in case piped */ } else if (verbose) printf("Error: unknown input %s\n", line); } } if (drsym_exit() != DRSYM_SUCCESS) printf("WARNING: error cleaning up symbol library\n"); res = 0; cleanup: if (drfront_cleanup_args(argv, argc) != DRFRONT_SUCCESS) printf("WARNING: drfront_cleanup_args failed\n"); return res; }
static void process_symbols(void *dcontext, char *dllname, LOADED_IMAGE *img) { /* We have to specify the module via "modname!symname". * We must use the same modname as in full_path. */ char fullpath[MAX_PATH]; # define MAX_SYM_WITH_MOD_LEN 256 char sym_with_mod[MAX_SYM_WITH_MOD_LEN]; int len; drsym_error_t symres; char *fname = NULL, *c; search_data_t sd; if (drsym_init(NULL) != DRSYM_SUCCESS) { print("WARNING: unable to initialize symbol engine\n"); return; } if (dllname == NULL) return; fname = dllname; for (c = dllname; *c != '\0'; c++) { if (*c == '/' || *c == '\\') fname = c + 1; } assert(fname != NULL && "unable to get fname for module"); if (fname == NULL) return; /* now get rid of extension */ for (; c > fname && *c != '.'; c--) ; /* nothing */ assert(c > fname && "file has no extension"); assert(c - fname < BUFFER_SIZE_ELEMENTS(sym_with_mod) && "sizes way off"); len = dr_snprintf(sym_with_mod, BUFFER_SIZE_ELEMENTS(sym_with_mod), "%.*s!%s", c - fname, fname, SYM_PATTERN); assert(len > 0 && "error printing modname!symname"); NULL_TERMINATE_BUFFER(sym_with_mod); len = GetFullPathName(dllname, BUFFER_SIZE_ELEMENTS(fullpath), fullpath, NULL); assert(len > 0); NULL_TERMINATE_BUFFER(dllname); if (list_usercalls) { int i; for (i = 0; i < NUM_USERCALL; i++) { size_t offs; symres = drsym_lookup_symbol(fullpath, usercall_names[i], &offs, 0); if (symres == DRSYM_SUCCESS) { usercall_addr[i] = ImageRvaToVa(img->FileHeader, img->MappedAddress, (ULONG)offs, NULL); verbose_print("%s = %d +0x%x == "PFX"\n", usercall_names[i], symres, offs, usercall_addr[i]); } else { dr_printf("Error locating usercall %s: aborting\n", usercall_names[i]); return; } } } sd.dcontext = dcontext; sd.img = img; sd.modpath = fullpath; verbose_print("Searching \"%s\" for \"%s\"\n", fullpath, sym_with_mod); symres = drsym_search_symbols(fullpath, sym_with_mod, true, search_syms_cb, &sd); if (symres != DRSYM_SUCCESS) print("Error %d searching \"%s\" for \"%s\"\n", symres, fullpath, sym_with_mod); drsym_exit(); }