annotate agent/src/os/win32/procList.cpp @ 2700:d06cff53b77e

More cleanup.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 18 May 2011 16:09: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) 2000, 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 #include "procList.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
26 #include "nt4internals.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
27 #include "isNT4.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
28 #include "toolHelp.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
29 #include <assert.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
30
a61af66fc99e Initial load
duke
parents:
diff changeset
31 using namespace std;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 using namespace NT4;
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 typedef void ProcListImplFunc(ProcEntryList& processes);
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 void procListImplNT4(ProcEntryList& processes);
a61af66fc99e Initial load
duke
parents:
diff changeset
37 void procListImplToolHelp(ProcEntryList& processes);
a61af66fc99e Initial load
duke
parents:
diff changeset
38
a61af66fc99e Initial load
duke
parents:
diff changeset
39 ProcEntry::ProcEntry(ULONG pid, USHORT nameLength, WCHAR* name) {
a61af66fc99e Initial load
duke
parents:
diff changeset
40 this->pid = pid;
a61af66fc99e Initial load
duke
parents:
diff changeset
41 this->nameLength = nameLength;
a61af66fc99e Initial load
duke
parents:
diff changeset
42 this->name = new WCHAR[nameLength];
a61af66fc99e Initial load
duke
parents:
diff changeset
43 memcpy(this->name, name, nameLength * sizeof(WCHAR));
a61af66fc99e Initial load
duke
parents:
diff changeset
44 }
a61af66fc99e Initial load
duke
parents:
diff changeset
45
a61af66fc99e Initial load
duke
parents:
diff changeset
46 ProcEntry::ProcEntry(ULONG pid, USHORT nameLength, char* name) {
a61af66fc99e Initial load
duke
parents:
diff changeset
47 this->pid = pid;
a61af66fc99e Initial load
duke
parents:
diff changeset
48 this->nameLength = nameLength;
a61af66fc99e Initial load
duke
parents:
diff changeset
49 this->name = new WCHAR[nameLength];
a61af66fc99e Initial load
duke
parents:
diff changeset
50 int j = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
51 for (int i = 0; i < nameLength; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
52 // FIXME: what is the proper promotion from ASCII to UNICODE?
a61af66fc99e Initial load
duke
parents:
diff changeset
53 this->name[i] = name[i] & 0xFF;
a61af66fc99e Initial load
duke
parents:
diff changeset
54 }
a61af66fc99e Initial load
duke
parents:
diff changeset
55 }
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57 ProcEntry::ProcEntry(const ProcEntry& arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
58 name = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
59 copyFrom(arg);
a61af66fc99e Initial load
duke
parents:
diff changeset
60 }
a61af66fc99e Initial load
duke
parents:
diff changeset
61
a61af66fc99e Initial load
duke
parents:
diff changeset
62 ProcEntry&
a61af66fc99e Initial load
duke
parents:
diff changeset
63 ProcEntry::operator=(const ProcEntry& arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
64 copyFrom(arg);
a61af66fc99e Initial load
duke
parents:
diff changeset
65 return *this;
a61af66fc99e Initial load
duke
parents:
diff changeset
66 }
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 ProcEntry::~ProcEntry() {
a61af66fc99e Initial load
duke
parents:
diff changeset
69 delete[] name;
a61af66fc99e Initial load
duke
parents:
diff changeset
70 }
a61af66fc99e Initial load
duke
parents:
diff changeset
71
a61af66fc99e Initial load
duke
parents:
diff changeset
72 void
a61af66fc99e Initial load
duke
parents:
diff changeset
73 ProcEntry::copyFrom(const ProcEntry& arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 if (name != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
75 delete[] name;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 }
a61af66fc99e Initial load
duke
parents:
diff changeset
77 pid = arg.pid;
a61af66fc99e Initial load
duke
parents:
diff changeset
78 nameLength = arg.nameLength;
a61af66fc99e Initial load
duke
parents:
diff changeset
79 name = new WCHAR[nameLength];
a61af66fc99e Initial load
duke
parents:
diff changeset
80 memcpy(name, arg.name, nameLength * sizeof(WCHAR));
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 ULONG
a61af66fc99e Initial load
duke
parents:
diff changeset
84 ProcEntry::getPid() {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 return pid;
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 USHORT
a61af66fc99e Initial load
duke
parents:
diff changeset
89 ProcEntry::getNameLength() {
a61af66fc99e Initial load
duke
parents:
diff changeset
90 return nameLength;
a61af66fc99e Initial load
duke
parents:
diff changeset
91 }
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 WCHAR*
a61af66fc99e Initial load
duke
parents:
diff changeset
94 ProcEntry::getName() {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 return name;
a61af66fc99e Initial load
duke
parents:
diff changeset
96 }
a61af66fc99e Initial load
duke
parents:
diff changeset
97
a61af66fc99e Initial load
duke
parents:
diff changeset
98 void
a61af66fc99e Initial load
duke
parents:
diff changeset
99 procList(ProcEntryList& processes) {
a61af66fc99e Initial load
duke
parents:
diff changeset
100 static ProcListImplFunc* impl = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 if (impl == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 // See which operating system we're on
a61af66fc99e Initial load
duke
parents:
diff changeset
104 impl = (isNT4() ? &procListImplNT4 : &procListImplToolHelp);
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106
a61af66fc99e Initial load
duke
parents:
diff changeset
107 assert(impl != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 (*impl)(processes);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 }
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 void
a61af66fc99e Initial load
duke
parents:
diff changeset
113 procListImplNT4(ProcEntryList& processes) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 using namespace NT4;
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 static ZwQuerySystemInformationFunc* query = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 if (query == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 HMODULE ntDLL = loadNTDLL();
a61af66fc99e Initial load
duke
parents:
diff changeset
120 query =
a61af66fc99e Initial load
duke
parents:
diff changeset
121 (ZwQuerySystemInformationFunc*) GetProcAddress(ntDLL,
a61af66fc99e Initial load
duke
parents:
diff changeset
122 "ZwQuerySystemInformation");
a61af66fc99e Initial load
duke
parents:
diff changeset
123 assert(query != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
124 }
a61af66fc99e Initial load
duke
parents:
diff changeset
125
a61af66fc99e Initial load
duke
parents:
diff changeset
126 ULONG n = 0x100;
a61af66fc99e Initial load
duke
parents:
diff changeset
127 PSYSTEM_PROCESSES sp = new SYSTEM_PROCESSES[n];
a61af66fc99e Initial load
duke
parents:
diff changeset
128 while ((*query)(SystemProcessesAndThreadsInformation,
a61af66fc99e Initial load
duke
parents:
diff changeset
129 sp, n * sizeof(SYSTEM_PROCESSES), 0) == STATUS_INFO_LENGTH_MISMATCH) {
a61af66fc99e Initial load
duke
parents:
diff changeset
130 delete[] sp;
a61af66fc99e Initial load
duke
parents:
diff changeset
131 n *= 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 sp = new SYSTEM_PROCESSES[n];
a61af66fc99e Initial load
duke
parents:
diff changeset
133 }
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135 bool done = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
136 for (PSYSTEM_PROCESSES p = sp; !done;
a61af66fc99e Initial load
duke
parents:
diff changeset
137 p = PSYSTEM_PROCESSES(PCHAR(p) + p->NextEntryDelta)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
138 processes.push_back(ProcEntry(p->ProcessId,
a61af66fc99e Initial load
duke
parents:
diff changeset
139 p->ProcessName.Length / 2,
a61af66fc99e Initial load
duke
parents:
diff changeset
140 p->ProcessName.Buffer));
a61af66fc99e Initial load
duke
parents:
diff changeset
141 done = p->NextEntryDelta == 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
142 }
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 procListImplToolHelp(ProcEntryList& processes) {
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 Process32FirstFunc* firstFunc = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
151 static Process32NextFunc* 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 = (Process32FirstFunc*) GetProcAddress(dll,
a61af66fc99e Initial load
duke
parents:
diff changeset
161 "Process32First");
a61af66fc99e Initial load
duke
parents:
diff changeset
162
a61af66fc99e Initial load
duke
parents:
diff changeset
163 nextFunc = (Process32NextFunc*) GetProcAddress(dll,
a61af66fc99e Initial load
duke
parents:
diff changeset
164 "Process32Next");
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_SNAPPROCESS, 0 /* ignored */);
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 PROCESSENTRY32 proc;
a61af66fc99e Initial load
duke
parents:
diff changeset
179 if ((*firstFunc)(snapshot, &proc)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
180 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
181 // FIXME: could make this uniform to the NT version by cutting
a61af66fc99e Initial load
duke
parents:
diff changeset
182 // off the path name just before the executable name
a61af66fc99e Initial load
duke
parents:
diff changeset
183 processes.push_back(ProcEntry(proc.th32ProcessID,
a61af66fc99e Initial load
duke
parents:
diff changeset
184 strlen(proc.szExeFile),
a61af66fc99e Initial load
duke
parents:
diff changeset
185 proc.szExeFile));
a61af66fc99e Initial load
duke
parents:
diff changeset
186 } while ((*nextFunc)(snapshot, &proc));
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 CloseHandle(snapshot);
a61af66fc99e Initial load
duke
parents:
diff changeset
190 }