1 /*
2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
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
dll_build_name(char * buffer,size_t buflen,const char * paths,const char * fname)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 = jvmtiAllocate((int)strlen(paths) + 1);
49 strcpy(paths_copy, paths);
50 if (paths_copy == NULL) {
51 return;
52 }
53
54 next_token = NULL;
55 path = strtok_s(paths_copy, PATH_SEPARATOR, &next_token);
56
57 while (path != NULL) {
58 size_t result_len = (size_t)_snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
59 if (result_len >= buflen) {
60 EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
61 "likely by sun.boot.library.path, is too long.");
62 } else if (_access(buffer, 0) == 0) {
63 break;
64 }
65 *buffer = '\0';
66 path = strtok_s(NULL, PATH_SEPARATOR, &next_token);
67 }
68
69 jvmtiDeallocate(paths_copy);
70 }
71
72 /*
73 * From system_md.c v1.54
74 */
75 int
dbgsysGetLastErrorString(char * buf,int len)76 dbgsysGetLastErrorString(char *buf, int len)
77 {
78 long errval;
79
80 if ((errval = GetLastError()) != 0) {
81 /* DOS error */
82 int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
83 NULL, errval,
84 0, buf, len, NULL);
85 if (n > 3) {
86 /* Drop final '.', CR, LF */
87 if (buf[n - 1] == '\n') n--;
88 if (buf[n - 1] == '\r') n--;
89 if (buf[n - 1] == '.') n--;
90 buf[n] = '\0';
91 }
92 return n;
93 }
94
95 if (errno != 0) {
96 /* C runtime error that has no corresponding DOS error code */
97 const char *s = strerror(errno);
98 int n = (int)strlen(s);
99 if (n >= len) n = len - 1;
100 strncpy(buf, s, n);
101 buf[n] = '\0';
102 return n;
103 }
104
105 return 0;
106 }
107
108 /*
109 * Build a machine dependent library name out of a path and file name.
110 */
111 void
dbgsysBuildLibName(char * holder,int holderlen,const char * pname,const char * fname)112 dbgsysBuildLibName(char *holder, int holderlen, const char *pname, const char *fname)
113 {
114 const int pnamelen = pname ? (int)strlen(pname) : 0;
115
116 if (pnamelen == 0) {
117 if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
118 EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
119 "likely by sun.boot.library.path, is too long.");
120 }
121 sprintf(holder, "%s.dll", fname);
122 } else {
123 dll_build_name(holder, holderlen, pname, fname);
124 }
125 }
126
127 void *
dbgsysLoadLibrary(const char * name,char * err_buf,int err_buflen)128 dbgsysLoadLibrary(const char * name, char *err_buf, int err_buflen)
129 {
130 void *result = LoadLibrary(name);
131 if (result == NULL) {
132 /* Error message is pretty lame, try to make a better guess. */
133 long errcode = GetLastError();
134 if (errcode == ERROR_MOD_NOT_FOUND) {
135 strncpy(err_buf, "Can't find dependent libraries", err_buflen-2);
136 err_buf[err_buflen-1] = '\0';
137 } else {
138 dbgsysGetLastErrorString(err_buf, err_buflen);
139 }
140 }
141 return result;
142 }
143
dbgsysUnloadLibrary(void * handle)144 void dbgsysUnloadLibrary(void *handle)
145 {
146 FreeLibrary(handle);
147 }
148
dbgsysFindLibraryEntry(void * handle,const char * name)149 void * dbgsysFindLibraryEntry(void *handle, const char *name)
150 {
151 return GetProcAddress(handle, name);
152 }
153