annotate agent/src/os/win32/libInfo.cpp @ 2977:f01de7799e05

Updated hgignore.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 15 Jun 2011 17:54:31 +0200
parents c18cbe5936b8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
2 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 // Disable too-long symbol warnings
a61af66fc99e Initial load
duke
parents:
diff changeset
26 #pragma warning ( disable : 4786 )
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28 #include "libInfo.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
29 #include "nt4internals.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
30 #include "isNT4.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
31 #include "toolHelp.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
32 #include <assert.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 using namespace std;
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 typedef void LibInfoImplFunc(DWORD pid, vector<LibInfo>& info);
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38 static void libInfoImplNT4(DWORD pid, vector<LibInfo>& info);
a61af66fc99e Initial load
duke
parents:
diff changeset
39 static void libInfoImplToolHelp(DWORD pid, vector<LibInfo>& info);
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41 void
a61af66fc99e Initial load
duke
parents:
diff changeset
42 libInfo(DWORD pid, vector<LibInfo>& info) {
a61af66fc99e Initial load
duke
parents:
diff changeset
43 static LibInfoImplFunc* impl = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 if (impl == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
46 // See which operating system we're on
a61af66fc99e Initial load
duke
parents:
diff changeset
47 impl = (isNT4() ? &libInfoImplNT4 : &libInfoImplToolHelp);
a61af66fc99e Initial load
duke
parents:
diff changeset
48 }
a61af66fc99e Initial load
duke
parents:
diff changeset
49
a61af66fc99e Initial load
duke
parents:
diff changeset
50 assert(impl != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
51
a61af66fc99e Initial load
duke
parents:
diff changeset
52 (*impl)(pid, info);
a61af66fc99e Initial load
duke
parents:
diff changeset
53 }
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 static ULONG
a61af66fc99e Initial load
duke
parents:
diff changeset
56 ModuleCount(NT4::PDEBUG_BUFFER db) {
a61af66fc99e Initial load
duke
parents:
diff changeset
57 return db->ModuleInformation ? *PULONG(db->ModuleInformation) : 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
58 }
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 #define MAX2(a, b) (((a) < (b)) ? (b) : (a))
a61af66fc99e Initial load
duke
parents:
diff changeset
61
a61af66fc99e Initial load
duke
parents:
diff changeset
62 static void
a61af66fc99e Initial load
duke
parents:
diff changeset
63 libInfoImplNT4(DWORD pid, vector<LibInfo>& info) {
a61af66fc99e Initial load
duke
parents:
diff changeset
64 static EnumProcessModulesFunc* enumFunc = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
65 static GetModuleFileNameExFunc* fnFunc = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
66 static GetModuleInformationFunc* infoFunc = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 if (enumFunc == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
69 HMODULE dll = loadPSAPIDLL();
a61af66fc99e Initial load
duke
parents:
diff changeset
70
a61af66fc99e Initial load
duke
parents:
diff changeset
71 enumFunc = (EnumProcessModulesFunc*) GetProcAddress(dll, "EnumProcessModules");
a61af66fc99e Initial load
duke
parents:
diff changeset
72 fnFunc = (GetModuleFileNameExFunc*) GetProcAddress(dll, "GetModuleFileNameExA");
a61af66fc99e Initial load
duke
parents:
diff changeset
73 infoFunc = (GetModuleInformationFunc*) GetProcAddress(dll, "GetModuleInformation");
a61af66fc99e Initial load
duke
parents:
diff changeset
74
a61af66fc99e Initial load
duke
parents:
diff changeset
75 assert(enumFunc != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
76 assert(fnFunc != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
77 assert(infoFunc != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 static HMODULE* mods = new HMODULE[256];
a61af66fc99e Initial load
duke
parents:
diff changeset
81 static int numMods = 256;
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 if (mods == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
84 mods = new HMODULE[numMods];
a61af66fc99e Initial load
duke
parents:
diff changeset
85 if (mods == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
86 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
87 }
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 bool done = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
91
a61af66fc99e Initial load
duke
parents:
diff changeset
92 HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
93 if (proc == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
94 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
95 }
a61af66fc99e Initial load
duke
parents:
diff changeset
96
a61af66fc99e Initial load
duke
parents:
diff changeset
97 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 DWORD bufSize = numMods * sizeof(HMODULE);
a61af66fc99e Initial load
duke
parents:
diff changeset
99 DWORD neededSize;
a61af66fc99e Initial load
duke
parents:
diff changeset
100
a61af66fc99e Initial load
duke
parents:
diff changeset
101 if (!(*enumFunc)(proc, mods, bufSize, &neededSize)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
102 // Enum failed
a61af66fc99e Initial load
duke
parents:
diff changeset
103 CloseHandle(proc);
a61af66fc99e Initial load
duke
parents:
diff changeset
104 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106
a61af66fc99e Initial load
duke
parents:
diff changeset
107 int numFetched = neededSize / sizeof(HMODULE);
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 if (numMods < numFetched) {
a61af66fc99e Initial load
duke
parents:
diff changeset
110 // Grow buffer
a61af66fc99e Initial load
duke
parents:
diff changeset
111 numMods = MAX2(numFetched, 2 * numMods);
a61af66fc99e Initial load
duke
parents:
diff changeset
112 delete[] mods;
a61af66fc99e Initial load
duke
parents:
diff changeset
113 mods = new HMODULE[numMods];
a61af66fc99e Initial load
duke
parents:
diff changeset
114 if (mods == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 CloseHandle(proc);
a61af66fc99e Initial load
duke
parents:
diff changeset
116 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
117 }
a61af66fc99e Initial load
duke
parents:
diff changeset
118 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 char filename[MAX_PATH];
a61af66fc99e Initial load
duke
parents:
diff changeset
120 MODULEINFO modInfo;
a61af66fc99e Initial load
duke
parents:
diff changeset
121
a61af66fc99e Initial load
duke
parents:
diff changeset
122 // Iterate through and fetch each one's info
a61af66fc99e Initial load
duke
parents:
diff changeset
123 for (int i = 0; i < numFetched; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
124 if (!(*fnFunc)(proc, mods[i], filename, MAX_PATH)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
125 CloseHandle(proc);
a61af66fc99e Initial load
duke
parents:
diff changeset
126 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
127 }
a61af66fc99e Initial load
duke
parents:
diff changeset
128
a61af66fc99e Initial load
duke
parents:
diff changeset
129 if (!(*infoFunc)(proc, mods[i], &modInfo, sizeof(MODULEINFO))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
130 CloseHandle(proc);
a61af66fc99e Initial load
duke
parents:
diff changeset
131 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 }
a61af66fc99e Initial load
duke
parents:
diff changeset
133
a61af66fc99e Initial load
duke
parents:
diff changeset
134 info.push_back(LibInfo(string(filename), (void*) modInfo.lpBaseOfDll));
a61af66fc99e Initial load
duke
parents:
diff changeset
135 }
a61af66fc99e Initial load
duke
parents:
diff changeset
136
a61af66fc99e Initial load
duke
parents:
diff changeset
137 done = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }
a61af66fc99e Initial load
duke
parents:
diff changeset
139 } while (!done);
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 CloseHandle(proc);
a61af66fc99e Initial load
duke
parents:
diff changeset
142 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
143 }
a61af66fc99e Initial load
duke
parents:
diff changeset
144
a61af66fc99e Initial load
duke
parents:
diff changeset
145 void
a61af66fc99e Initial load
duke
parents:
diff changeset
146 libInfoImplToolHelp(DWORD pid, vector<LibInfo>& info) {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 using namespace ToolHelp;
a61af66fc99e Initial load
duke
parents:
diff changeset
148
a61af66fc99e Initial load
duke
parents:
diff changeset
149 static CreateToolhelp32SnapshotFunc* snapshotFunc = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
150 static Module32FirstFunc* firstFunc = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
151 static Module32NextFunc* nextFunc = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
152
a61af66fc99e Initial load
duke
parents:
diff changeset
153 if (snapshotFunc == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
154 HMODULE dll = loadDLL();
a61af66fc99e Initial load
duke
parents:
diff changeset
155
a61af66fc99e Initial load
duke
parents:
diff changeset
156 snapshotFunc =
a61af66fc99e Initial load
duke
parents:
diff changeset
157 (CreateToolhelp32SnapshotFunc*) GetProcAddress(dll,
a61af66fc99e Initial load
duke
parents:
diff changeset
158 "CreateToolhelp32Snapshot");
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 firstFunc = (Module32FirstFunc*) GetProcAddress(dll,
a61af66fc99e Initial load
duke
parents:
diff changeset
161 "Module32First");
a61af66fc99e Initial load
duke
parents:
diff changeset
162
a61af66fc99e Initial load
duke
parents:
diff changeset
163 nextFunc = (Module32NextFunc*) GetProcAddress(dll,
a61af66fc99e Initial load
duke
parents:
diff changeset
164 "Module32Next");
a61af66fc99e Initial load
duke
parents:
diff changeset
165
a61af66fc99e Initial load
duke
parents:
diff changeset
166 assert(snapshotFunc != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
167 assert(firstFunc != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
168 assert(nextFunc != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
169 }
a61af66fc99e Initial load
duke
parents:
diff changeset
170
a61af66fc99e Initial load
duke
parents:
diff changeset
171 HANDLE snapshot = (*snapshotFunc)(TH32CS_SNAPMODULE, pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
172 if (snapshot == (HANDLE) -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 // Error occurred during snapshot
a61af66fc99e Initial load
duke
parents:
diff changeset
174 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
175 }
a61af66fc99e Initial load
duke
parents:
diff changeset
176
a61af66fc99e Initial load
duke
parents:
diff changeset
177 // Iterate
a61af66fc99e Initial load
duke
parents:
diff changeset
178 MODULEENTRY32 module;
a61af66fc99e Initial load
duke
parents:
diff changeset
179 if ((*firstFunc)(snapshot, &module)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
180 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
181 info.push_back(LibInfo(string(module.szExePath), (void*) module.modBaseAddr));
a61af66fc99e Initial load
duke
parents:
diff changeset
182 } while ((*nextFunc)(snapshot, &module));
a61af66fc99e Initial load
duke
parents:
diff changeset
183 }
a61af66fc99e Initial load
duke
parents:
diff changeset
184
a61af66fc99e Initial load
duke
parents:
diff changeset
185 CloseHandle(snapshot);
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }