20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * Maintains a list of currently loaded DLLs (Dynamic Link Libraries) 28 * and their associated handles. Library names are case-insensitive. 29 */ 30 31 #include <windows.h> 32 #include <stdio.h> 33 #include <string.h> 34 #include <errno.h> 35 #include <io.h> 36 #include <stdlib.h> 37 38 #include "sys.h" 39 40 #include "path_md.h" 41 42 static void dll_build_name(char* buffer, size_t buflen, 43 const char* paths, const char* fname) { 44 char *path, *paths_copy, *next_token; 45 46 paths_copy = strdup(paths); 47 if (paths_copy == NULL) { 48 return; 49 } 50 51 next_token = NULL; 52 path = strtok_s(paths_copy, PATH_SEPARATOR, &next_token); 53 54 while (path != NULL) { 55 _snprintf(buffer, buflen, "%s\\%s.dll", path, fname); 56 if (_access(buffer, 0) == 0) { 57 break; 58 } 59 *buffer = '\0'; 60 path = strtok_s(NULL, PATH_SEPARATOR, &next_token); 61 } 62 63 free(paths_copy); 64 } 65 66 /* 67 * From system_md.c v1.54 68 */ 69 int 70 dbgsysGetLastErrorString(char *buf, int len) 71 { 72 long errval; 73 74 if ((errval = GetLastError()) != 0) { 75 /* DOS error */ 76 int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 90 /* C runtime error that has no corresponding DOS error code */ 91 const char *s = strerror(errno); 92 int n = (int)strlen(s); 93 if (n >= len) n = len - 1; 94 strncpy(buf, s, n); 95 buf[n] = '\0'; 96 return n; 97 } 98 99 return 0; 100 } 101 102 /* 103 * Build a machine dependent library name out of a path and file name. 104 */ 105 void 106 dbgsysBuildLibName(char *holder, int holderlen, const char *pname, const char *fname) 107 { 108 const int pnamelen = pname ? (int)strlen(pname) : 0; 109 110 *holder = '\0'; 111 /* Quietly truncates on buffer overflow. Should be an error. */ 112 if (pnamelen + (int)strlen(fname) + 10 > holderlen) { 113 return; 114 } 115 116 if (pnamelen == 0) { 117 sprintf(holder, "%s.dll", fname); 118 } else { 119 dll_build_name(holder, holderlen, pname, fname); 120 } 121 } 122 123 void * 124 dbgsysLoadLibrary(const char * name, char *err_buf, int err_buflen) 125 { 126 void *result = LoadLibrary(name); 127 if (result == NULL) { 128 /* Error message is pretty lame, try to make a better guess. */ 129 long errcode = GetLastError(); 130 if (errcode == ERROR_MOD_NOT_FOUND) { 131 strncpy(err_buf, "Can't find dependent libraries", err_buflen-2); 132 err_buf[err_buflen-1] = '\0'; 133 } else { 134 dbgsysGetLastErrorString(err_buf, err_buflen); 135 } 136 } | 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * Maintains a list of currently loaded DLLs (Dynamic Link Libraries) 28 * and their associated handles. Library names are case-insensitive. 29 */ 30 31 #include <windows.h> 32 #include <stdio.h> 33 #include <string.h> 34 #include <errno.h> 35 #include <io.h> 36 #include <stdlib.h> 37 38 #include "sys.h" 39 40 #include "util.h" 41 #include "path_md.h" 42 43 static void dll_build_name(char* buffer, size_t buflen, 44 const char* paths, const char* fname) { 45 char *path, *paths_copy, *next_token; 46 *buffer = '\0'; 47 48 paths_copy = strdup(paths); 49 if (paths_copy == NULL) { 50 return; 51 } 52 53 next_token = NULL; 54 path = strtok_s(paths_copy, PATH_SEPARATOR, &next_token); 55 56 while (path != NULL) { 57 size_t result_len = (size_t)_snprintf(buffer, buflen, "%s\\%s.dll", path, fname); 58 if (result_len >= buflen) { 59 EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, " 60 "likely by sun.boot.library.path, is too long."); 61 } else if (_access(buffer, 0) == 0) { 62 break; 63 } 64 *buffer = '\0'; 65 path = strtok_s(NULL, PATH_SEPARATOR, &next_token); 66 } 67 68 free(paths_copy); 69 } 70 71 /* 72 * From system_md.c v1.54 73 */ 74 int 75 dbgsysGetLastErrorString(char *buf, int len) 76 { 77 long errval; 78 79 if ((errval = GetLastError()) != 0) { 80 /* DOS error */ 81 int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 95 /* C runtime error that has no corresponding DOS error code */ 96 const char *s = strerror(errno); 97 int n = (int)strlen(s); 98 if (n >= len) n = len - 1; 99 strncpy(buf, s, n); 100 buf[n] = '\0'; 101 return n; 102 } 103 104 return 0; 105 } 106 107 /* 108 * Build a machine dependent library name out of a path and file name. 109 */ 110 void 111 dbgsysBuildLibName(char *holder, int holderlen, const char *pname, const char *fname) 112 { 113 const int pnamelen = pname ? (int)strlen(pname) : 0; 114 115 if (pnamelen == 0) { 116 if (pnamelen + (int)strlen(fname) + 10 > holderlen) { 117 EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, " 118 "likely by sun.boot.library.path, is too long."); 119 } 120 sprintf(holder, "%s.dll", fname); 121 } else { 122 dll_build_name(holder, holderlen, pname, fname); 123 } 124 } 125 126 void * 127 dbgsysLoadLibrary(const char * name, char *err_buf, int err_buflen) 128 { 129 void *result = LoadLibrary(name); 130 if (result == NULL) { 131 /* Error message is pretty lame, try to make a better guess. */ 132 long errcode = GetLastError(); 133 if (errcode == ERROR_MOD_NOT_FOUND) { 134 strncpy(err_buf, "Can't find dependent libraries", err_buflen-2); 135 err_buf[err_buflen-1] = '\0'; 136 } else { 137 dbgsysGetLastErrorString(err_buf, err_buflen); 138 } 139 } |