annotate agent/src/os/solaris/dbx/svc_agent_dbx.cpp @ 210:05712c37c828

6676016: ParallelOldGC leaks memory Summary: ensure that GCTask threads release resource and handle memory Reviewed-by: jmasa, chrisphi
author jcoomes
date Wed, 18 Jun 2008 18:36:47 -0700
parents a61af66fc99e
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2000-2002 Sun Microsystems, Inc. All Rights Reserved.
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 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
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 // This is the implementation of a very simple dbx import module which
a61af66fc99e Initial load
duke
parents:
diff changeset
26 // handles requests from the VM which come in over a socket. The
a61af66fc99e Initial load
duke
parents:
diff changeset
27 // higher-level Java wrapper for dbx starts the debugger, attaches to
a61af66fc99e Initial load
duke
parents:
diff changeset
28 // the process, imports this command, and runs it. After that, the SA
a61af66fc99e Initial load
duke
parents:
diff changeset
29 // writes commands to this agent via its own private communications
a61af66fc99e Initial load
duke
parents:
diff changeset
30 // channel. The intent is to move away from the text-based front-end
a61af66fc99e Initial load
duke
parents:
diff changeset
31 // completely in the near future (no more calling "debug" by printing
a61af66fc99e Initial load
duke
parents:
diff changeset
32 // text to dbx's stdin).
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 #include <stdio.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
35 #include <errno.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
36 #include <ctype.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
37 #include <sys/types.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
38 #include <sys/socket.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
39 #include <unistd.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
40 #include <string.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
41 #include <stropts.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
42 #include <netinet/in.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
43 #include <netinet/tcp.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 #include <proc_service.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
46 #include <sys/procfs_isa.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
47 #include <rtld_db.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
48 #include "proc_service_2.h"
a61af66fc99e Initial load
duke
parents:
diff changeset
49 #include "svc_agent_dbx.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
50
a61af66fc99e Initial load
duke
parents:
diff changeset
51 static ServiceabilityAgentDbxModule* module = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 #define NEEDS_CLEANUP
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54 // Useful for debugging
a61af66fc99e Initial load
duke
parents:
diff changeset
55 #define VERBOSE_DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57 #ifdef VERBOSE_DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
58 # define debug_only(x) x
a61af66fc99e Initial load
duke
parents:
diff changeset
59 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
60 # define debug_only(x)
a61af66fc99e Initial load
duke
parents:
diff changeset
61 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
62
a61af66fc99e Initial load
duke
parents:
diff changeset
63 // For profiling
a61af66fc99e Initial load
duke
parents:
diff changeset
64 //#define PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
65
a61af66fc99e Initial load
duke
parents:
diff changeset
66 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
67 #define PROFILE_COUNT 200
a61af66fc99e Initial load
duke
parents:
diff changeset
68 static Timer scanTimer;
a61af66fc99e Initial load
duke
parents:
diff changeset
69 static Timer workTimer;
a61af66fc99e Initial load
duke
parents:
diff changeset
70 static Timer writeTimer;
a61af66fc99e Initial load
duke
parents:
diff changeset
71 static int numRequests = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
73
a61af66fc99e Initial load
duke
parents:
diff changeset
74 const char* ServiceabilityAgentDbxModule::CMD_ADDRESS_SIZE = "address_size";
a61af66fc99e Initial load
duke
parents:
diff changeset
75 const char* ServiceabilityAgentDbxModule::CMD_PEEK_FAIL_FAST = "peek_fail_fast";
a61af66fc99e Initial load
duke
parents:
diff changeset
76 const char* ServiceabilityAgentDbxModule::CMD_PEEK = "peek";
a61af66fc99e Initial load
duke
parents:
diff changeset
77 const char* ServiceabilityAgentDbxModule::CMD_POKE = "poke";
a61af66fc99e Initial load
duke
parents:
diff changeset
78 const char* ServiceabilityAgentDbxModule::CMD_MAPPED = "mapped";
a61af66fc99e Initial load
duke
parents:
diff changeset
79 const char* ServiceabilityAgentDbxModule::CMD_LOOKUP = "lookup";
a61af66fc99e Initial load
duke
parents:
diff changeset
80 const char* ServiceabilityAgentDbxModule::CMD_THR_GREGS = "thr_gregs";
a61af66fc99e Initial load
duke
parents:
diff changeset
81 const char* ServiceabilityAgentDbxModule::CMD_EXIT = "exit";
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 // The initialization routines must not have C++ name mangling
a61af66fc99e Initial load
duke
parents:
diff changeset
84 extern "C" {
a61af66fc99e Initial load
duke
parents:
diff changeset
85
a61af66fc99e Initial load
duke
parents:
diff changeset
86 /** This is the initialization routine called by dbx upon importing of
a61af66fc99e Initial load
duke
parents:
diff changeset
87 this module. Returns 0 upon successful initialization, -1 upon
a61af66fc99e Initial load
duke
parents:
diff changeset
88 failure. */
a61af66fc99e Initial load
duke
parents:
diff changeset
89 int shell_imp_init(int major, int minor,
a61af66fc99e Initial load
duke
parents:
diff changeset
90 shell_imp_interp_t interp, int argc, char *argv[])
a61af66fc99e Initial load
duke
parents:
diff changeset
91 {
a61af66fc99e Initial load
duke
parents:
diff changeset
92 // Ensure shell interpreter data structure is laid out the way we
a61af66fc99e Initial load
duke
parents:
diff changeset
93 // expect
a61af66fc99e Initial load
duke
parents:
diff changeset
94 if (major != SHELL_IMP_MAJOR) {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 debug_only(fprintf(stderr, "Serviceability agent: unexpected value for SHELL_IMP_MAJOR (got %d, expected %d)\n", major, SHELL_IMP_MAJOR);)
a61af66fc99e Initial load
duke
parents:
diff changeset
96 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 }
a61af66fc99e Initial load
duke
parents:
diff changeset
98 if (minor < SHELL_IMP_MINOR) {
a61af66fc99e Initial load
duke
parents:
diff changeset
99 debug_only(fprintf(stderr, "Serviceability agent: unexpected value for SHELL_IMP_MINOR (got %d, expected >= %d)\n", minor, SHELL_IMP_MINOR);)
a61af66fc99e Initial load
duke
parents:
diff changeset
100 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 if (module != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
104 debug_only(fprintf(stderr, "Serviceability agent: module appears to already be initialized (should not happen)\n");)
a61af66fc99e Initial load
duke
parents:
diff changeset
105 // Already initialized. Should not happen.
a61af66fc99e Initial load
duke
parents:
diff changeset
106 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 module = new ServiceabilityAgentDbxModule(major, minor, interp, argc, argv);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 if (!module->install()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 debug_only(fprintf(stderr, "Serviceability agent: error installing import module\n");)
a61af66fc99e Initial load
duke
parents:
diff changeset
112 delete module;
a61af66fc99e Initial load
duke
parents:
diff changeset
113 module = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
114 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
115 }
a61af66fc99e Initial load
duke
parents:
diff changeset
116
a61af66fc99e Initial load
duke
parents:
diff changeset
117 // Installation was successful. Next step will be for the user to
a61af66fc99e Initial load
duke
parents:
diff changeset
118 // enter the appropriate command on the command line, which will
a61af66fc99e Initial load
duke
parents:
diff changeset
119 // make the SA's dbx module wait for commands to come in over the
a61af66fc99e Initial load
duke
parents:
diff changeset
120 // socket.
a61af66fc99e Initial load
duke
parents:
diff changeset
121 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
a61af66fc99e Initial load
duke
parents:
diff changeset
123
a61af66fc99e Initial load
duke
parents:
diff changeset
124 /** This is the routine called by dbx upon unloading of this module.
a61af66fc99e Initial load
duke
parents:
diff changeset
125 Returns 0 upon success, -1 upon failure. */
a61af66fc99e Initial load
duke
parents:
diff changeset
126 int
a61af66fc99e Initial load
duke
parents:
diff changeset
127 shell_imp_fini(shell_imp_interp_t)
a61af66fc99e Initial load
duke
parents:
diff changeset
128 {
a61af66fc99e Initial load
duke
parents:
diff changeset
129 if (module == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
130 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
131 }
a61af66fc99e Initial load
duke
parents:
diff changeset
132
a61af66fc99e Initial load
duke
parents:
diff changeset
133 bool res = module->uninstall();
a61af66fc99e Initial load
duke
parents:
diff changeset
134 delete module;
a61af66fc99e Initial load
duke
parents:
diff changeset
135 module = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
136 if (!res) {
a61af66fc99e Initial load
duke
parents:
diff changeset
137 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }
a61af66fc99e Initial load
duke
parents:
diff changeset
139 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
140 }
a61af66fc99e Initial load
duke
parents:
diff changeset
141
a61af66fc99e Initial load
duke
parents:
diff changeset
142 } // extern "C"
a61af66fc99e Initial load
duke
parents:
diff changeset
143
a61af66fc99e Initial load
duke
parents:
diff changeset
144 /** This is the routine which is called by the dbx shell when the user
a61af66fc99e Initial load
duke
parents:
diff changeset
145 requests the serviceability agent module to run. This delegates to
a61af66fc99e Initial load
duke
parents:
diff changeset
146 ServiceabilityAgentDbxModule::run. This routine's signature must
a61af66fc99e Initial load
duke
parents:
diff changeset
147 match that of shell_imp_fun_t. */
a61af66fc99e Initial load
duke
parents:
diff changeset
148 extern "C" {
a61af66fc99e Initial load
duke
parents:
diff changeset
149 static int
a61af66fc99e Initial load
duke
parents:
diff changeset
150 svc_agent_run(shell_imp_interp_t, int, char **, void *) {
a61af66fc99e Initial load
duke
parents:
diff changeset
151 if (module == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
152 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
153 }
a61af66fc99e Initial load
duke
parents:
diff changeset
154
a61af66fc99e Initial load
duke
parents:
diff changeset
155 module->run();
a61af66fc99e Initial load
duke
parents:
diff changeset
156 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
157 }
a61af66fc99e Initial load
duke
parents:
diff changeset
158 }
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
161 * Implementation of ServiceabilityAgentDbxModule class
a61af66fc99e Initial load
duke
parents:
diff changeset
162 */
a61af66fc99e Initial load
duke
parents:
diff changeset
163
a61af66fc99e Initial load
duke
parents:
diff changeset
164 // NOTE: we need to forward declare the special "ps_get_prochandle2"
a61af66fc99e Initial load
duke
parents:
diff changeset
165 // function which allows examination of core files as well. It isn't
a61af66fc99e Initial load
duke
parents:
diff changeset
166 // currently in proc_service_2.h. Note also that it has name mangling
a61af66fc99e Initial load
duke
parents:
diff changeset
167 // because it isn't declared extern "C".
a61af66fc99e Initial load
duke
parents:
diff changeset
168 //const struct ps_prochandle *ps_get_prochandle2(int cores_too);
a61af66fc99e Initial load
duke
parents:
diff changeset
169
a61af66fc99e Initial load
duke
parents:
diff changeset
170 ServiceabilityAgentDbxModule::ServiceabilityAgentDbxModule(int, int, shell_imp_interp_t interp,
a61af66fc99e Initial load
duke
parents:
diff changeset
171 int argc, char *argv[])
a61af66fc99e Initial load
duke
parents:
diff changeset
172 :myComm(32768, 131072)
a61af66fc99e Initial load
duke
parents:
diff changeset
173 {
a61af66fc99e Initial load
duke
parents:
diff changeset
174 _interp = interp;
a61af66fc99e Initial load
duke
parents:
diff changeset
175 _argc = argc;
a61af66fc99e Initial load
duke
parents:
diff changeset
176 _argv = argv;
a61af66fc99e Initial load
duke
parents:
diff changeset
177 _tdb_agent = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
178 peek_fail_fast = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
179 libThreadName = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181
a61af66fc99e Initial load
duke
parents:
diff changeset
182 ServiceabilityAgentDbxModule::~ServiceabilityAgentDbxModule() {
a61af66fc99e Initial load
duke
parents:
diff changeset
183 if (_command != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
184 uninstall();
a61af66fc99e Initial load
duke
parents:
diff changeset
185 }
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }
a61af66fc99e Initial load
duke
parents:
diff changeset
187
a61af66fc99e Initial load
duke
parents:
diff changeset
188 char*
a61af66fc99e Initial load
duke
parents:
diff changeset
189 readCStringFromProcess(psaddr_t addr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
190 char c;
a61af66fc99e Initial load
duke
parents:
diff changeset
191 int num = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
192 ps_prochandle* cur_proc = (ps_prochandle*) ps_get_prochandle2(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
193
a61af66fc99e Initial load
duke
parents:
diff changeset
194 // Search for null terminator
a61af66fc99e Initial load
duke
parents:
diff changeset
195 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
196 if (ps_pread(cur_proc, addr + num, &c, 1) != PS_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
197 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
198 }
a61af66fc99e Initial load
duke
parents:
diff changeset
199 ++num;
a61af66fc99e Initial load
duke
parents:
diff changeset
200 } while (c != 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
201
a61af66fc99e Initial load
duke
parents:
diff changeset
202 // Allocate string
a61af66fc99e Initial load
duke
parents:
diff changeset
203 char* res = new char[num];
a61af66fc99e Initial load
duke
parents:
diff changeset
204 if (ps_pread(cur_proc, addr, res, num) != PS_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 delete[] res;
a61af66fc99e Initial load
duke
parents:
diff changeset
206 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
207 }
a61af66fc99e Initial load
duke
parents:
diff changeset
208 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
209 }
a61af66fc99e Initial load
duke
parents:
diff changeset
210
a61af66fc99e Initial load
duke
parents:
diff changeset
211 int
a61af66fc99e Initial load
duke
parents:
diff changeset
212 findLibThreadCB(const rd_loadobj_t* lo, void* data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
213 ServiceabilityAgentDbxModule* module = (ServiceabilityAgentDbxModule*) data;
a61af66fc99e Initial load
duke
parents:
diff changeset
214 char* name = readCStringFromProcess(lo->rl_nameaddr);
a61af66fc99e Initial load
duke
parents:
diff changeset
215 if (strstr(name, "libthread.so") != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
216 module->libThreadName = name;
a61af66fc99e Initial load
duke
parents:
diff changeset
217 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
218 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
219 delete[] name;
a61af66fc99e Initial load
duke
parents:
diff changeset
220 return 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
221 }
a61af66fc99e Initial load
duke
parents:
diff changeset
222 }
a61af66fc99e Initial load
duke
parents:
diff changeset
223
a61af66fc99e Initial load
duke
parents:
diff changeset
224 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
225 ServiceabilityAgentDbxModule::install() {
a61af66fc99e Initial load
duke
parents:
diff changeset
226 // NOTE interdependency between here and Java side wrapper
a61af66fc99e Initial load
duke
parents:
diff changeset
227 // FIXME: casts of string literal to char * to match prototype
a61af66fc99e Initial load
duke
parents:
diff changeset
228 _command = shell_imp_define_command((char *) "svc_agent_run",
a61af66fc99e Initial load
duke
parents:
diff changeset
229 &svc_agent_run,
a61af66fc99e Initial load
duke
parents:
diff changeset
230 0,
a61af66fc99e Initial load
duke
parents:
diff changeset
231 NULL,
a61af66fc99e Initial load
duke
parents:
diff changeset
232 (char *) "Run the serviceability agent's dbx module.\n"
a61af66fc99e Initial load
duke
parents:
diff changeset
233 "This routine causes the module to listen on a socket for requests.\n"
a61af66fc99e Initial load
duke
parents:
diff changeset
234 "It does not return until the Java-side code tells it to exit, at\n"
a61af66fc99e Initial load
duke
parents:
diff changeset
235 "which point control is returned to the dbx shell.");
a61af66fc99e Initial load
duke
parents:
diff changeset
236 if (_command == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
237 debug_only(fprintf(stderr, "Serviceability agent: Failed to install svc_agent_run command\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
238 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
239 }
a61af66fc99e Initial load
duke
parents:
diff changeset
240
a61af66fc99e Initial load
duke
parents:
diff changeset
241 // This is fairly painful. Since dbx doesn't currently load
a61af66fc99e Initial load
duke
parents:
diff changeset
242 // libthread_db with RTLD_GLOBAL, we can't just use RTLD_DEFAULT for
a61af66fc99e Initial load
duke
parents:
diff changeset
243 // the argument to dlsym. Instead, we have to use rtld_db to search
a61af66fc99e Initial load
duke
parents:
diff changeset
244 // through the loaded objects in the target process for libthread.so and
a61af66fc99e Initial load
duke
parents:
diff changeset
245
a61af66fc99e Initial load
duke
parents:
diff changeset
246 // Try rtld_db
a61af66fc99e Initial load
duke
parents:
diff changeset
247 if (rd_init(RD_VERSION) != RD_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
248 debug_only(fprintf(stderr, "Serviceability agent: Unable to init rtld_db\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
249 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
250 }
a61af66fc99e Initial load
duke
parents:
diff changeset
251
a61af66fc99e Initial load
duke
parents:
diff changeset
252 rd_agent_t* rda = rd_new((struct ps_prochandle*) ps_get_prochandle2(1));
a61af66fc99e Initial load
duke
parents:
diff changeset
253 if (rda == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
254 debug_only(fprintf(stderr, "Serviceability agent: Unable to allocate rtld_db agent\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
255 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
256 }
a61af66fc99e Initial load
duke
parents:
diff changeset
257
a61af66fc99e Initial load
duke
parents:
diff changeset
258 if (rd_loadobj_iter(rda, (rl_iter_f*) findLibThreadCB, this) != RD_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
259 debug_only(fprintf(stderr, "Serviceability agent: Loadobject iteration failed\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
260 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
261 }
a61af66fc99e Initial load
duke
parents:
diff changeset
262
a61af66fc99e Initial load
duke
parents:
diff changeset
263 if (libThreadName == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
264 debug_only(fprintf(stderr, "Serviceability agent: Failed to find pathname to libthread.so in target process\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
265 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
266 }
a61af66fc99e Initial load
duke
parents:
diff changeset
267
a61af66fc99e Initial load
duke
parents:
diff changeset
268 // Find and open libthread_db.so
a61af66fc99e Initial load
duke
parents:
diff changeset
269 char* slash = strrchr(libThreadName, '/');
a61af66fc99e Initial load
duke
parents:
diff changeset
270 if (slash == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
271 debug_only(fprintf(stderr, "Serviceability agent: can't parse path to libthread.so \"%s\"\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
272 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
273 }
a61af66fc99e Initial load
duke
parents:
diff changeset
274
a61af66fc99e Initial load
duke
parents:
diff changeset
275 int slashPos = slash - libThreadName;
a61af66fc99e Initial load
duke
parents:
diff changeset
276 char* buf = new char[slashPos + strlen("libthread_db.so") + 20]; // slop
a61af66fc99e Initial load
duke
parents:
diff changeset
277 if (buf == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
278 debug_only(fprintf(stderr, "Serviceability agent: error allocating libthread_db.so pathname\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
279 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
280 }
a61af66fc99e Initial load
duke
parents:
diff changeset
281 strncpy(buf, libThreadName, slashPos + 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
282
a61af66fc99e Initial load
duke
parents:
diff changeset
283 // Check dbx's data model; use sparcv9/ subdirectory if 64-bit and
a61af66fc99e Initial load
duke
parents:
diff changeset
284 // if target process is 32-bit
a61af66fc99e Initial load
duke
parents:
diff changeset
285 if ((sizeof(void*) == 8) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
286 (strstr(libThreadName, "sparcv9") == NULL)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
287 strcpy(buf + slashPos + 1, "sparcv9/");
a61af66fc99e Initial load
duke
parents:
diff changeset
288 slashPos += strlen("sparcv9/");
a61af66fc99e Initial load
duke
parents:
diff changeset
289 }
a61af66fc99e Initial load
duke
parents:
diff changeset
290
a61af66fc99e Initial load
duke
parents:
diff changeset
291 strcpy(buf + slashPos + 1, "libthread_db.so");
a61af66fc99e Initial load
duke
parents:
diff changeset
292
a61af66fc99e Initial load
duke
parents:
diff changeset
293 libThreadDB = dlopen(buf, RTLD_LAZY);
a61af66fc99e Initial load
duke
parents:
diff changeset
294 void* tmpDB = libThreadDB;
a61af66fc99e Initial load
duke
parents:
diff changeset
295 if (libThreadDB == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
296 debug_only(fprintf(stderr, "Serviceability agent: Warning: unable to find libthread_db.so at \"%s\"\n", buf));
a61af66fc99e Initial load
duke
parents:
diff changeset
297 // Would like to handle this case as well. Maybe dbx has a better
a61af66fc99e Initial load
duke
parents:
diff changeset
298 // idea of where libthread_db.so lies. If the problem with dbx
a61af66fc99e Initial load
duke
parents:
diff changeset
299 // loading libthread_db without RTLD_GLOBAL specified ever gets
a61af66fc99e Initial load
duke
parents:
diff changeset
300 // fixed, we could run this code all the time.
a61af66fc99e Initial load
duke
parents:
diff changeset
301 tmpDB = RTLD_DEFAULT;
a61af66fc99e Initial load
duke
parents:
diff changeset
302 }
a61af66fc99e Initial load
duke
parents:
diff changeset
303
a61af66fc99e Initial load
duke
parents:
diff changeset
304 delete[] buf;
a61af66fc99e Initial load
duke
parents:
diff changeset
305
a61af66fc99e Initial load
duke
parents:
diff changeset
306 // Initialize access to libthread_db
a61af66fc99e Initial load
duke
parents:
diff changeset
307 td_init_fn = (td_init_fn_t*) dlsym(tmpDB, "td_init");
a61af66fc99e Initial load
duke
parents:
diff changeset
308 td_ta_new_fn = (td_ta_new_fn_t*) dlsym(tmpDB, "td_ta_new");
a61af66fc99e Initial load
duke
parents:
diff changeset
309 td_ta_delete_fn = (td_ta_delete_fn_t*) dlsym(tmpDB, "td_ta_delete");
a61af66fc99e Initial load
duke
parents:
diff changeset
310 td_ta_map_id2thr_fn = (td_ta_map_id2thr_fn_t*) dlsym(tmpDB, "td_ta_map_id2thr");
a61af66fc99e Initial load
duke
parents:
diff changeset
311 td_thr_getgregs_fn = (td_thr_getgregs_fn_t*) dlsym(tmpDB, "td_thr_getgregs");
a61af66fc99e Initial load
duke
parents:
diff changeset
312
a61af66fc99e Initial load
duke
parents:
diff changeset
313 if (td_init_fn == NULL ||
a61af66fc99e Initial load
duke
parents:
diff changeset
314 td_ta_new_fn == NULL ||
a61af66fc99e Initial load
duke
parents:
diff changeset
315 td_ta_delete_fn == NULL ||
a61af66fc99e Initial load
duke
parents:
diff changeset
316 td_ta_map_id2thr_fn == NULL ||
a61af66fc99e Initial load
duke
parents:
diff changeset
317 td_thr_getgregs_fn == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
318 debug_only(fprintf(stderr, "Serviceability agent: Failed to find one or more libthread_db symbols:\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
319 debug_only(if (td_init_fn == NULL) fprintf(stderr, " td_init\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
320 debug_only(if (td_ta_new_fn == NULL) fprintf(stderr, " td_ta_new\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
321 debug_only(if (td_ta_delete_fn == NULL) fprintf(stderr, " td_ta_delete\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
322 debug_only(if (td_ta_map_id2thr_fn == NULL) fprintf(stderr, " td_ta_map_id2thr\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
323 debug_only(if (td_thr_getgregs_fn == NULL) fprintf(stderr, " td_thr_getgregs\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
324 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
325 }
a61af66fc99e Initial load
duke
parents:
diff changeset
326
a61af66fc99e Initial load
duke
parents:
diff changeset
327 if ((*td_init_fn)() != TD_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
328 debug_only(fprintf(stderr, "Serviceability agent: Failed to initialize libthread_db\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
329 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
330 }
a61af66fc99e Initial load
duke
parents:
diff changeset
331
a61af66fc99e Initial load
duke
parents:
diff changeset
332 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
333 }
a61af66fc99e Initial load
duke
parents:
diff changeset
334
a61af66fc99e Initial load
duke
parents:
diff changeset
335 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
336 ServiceabilityAgentDbxModule::uninstall() {
a61af66fc99e Initial load
duke
parents:
diff changeset
337 if (_command == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
338 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
339 }
a61af66fc99e Initial load
duke
parents:
diff changeset
340
a61af66fc99e Initial load
duke
parents:
diff changeset
341 if (libThreadDB != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
342 dlclose(libThreadDB);
a61af66fc99e Initial load
duke
parents:
diff changeset
343 libThreadDB = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
344 }
a61af66fc99e Initial load
duke
parents:
diff changeset
345
a61af66fc99e Initial load
duke
parents:
diff changeset
346 int res = shell_imp_undefine_command(_command);
a61af66fc99e Initial load
duke
parents:
diff changeset
347
a61af66fc99e Initial load
duke
parents:
diff changeset
348 if (res != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
349 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
350 }
a61af66fc99e Initial load
duke
parents:
diff changeset
351
a61af66fc99e Initial load
duke
parents:
diff changeset
352 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
353 }
a61af66fc99e Initial load
duke
parents:
diff changeset
354
a61af66fc99e Initial load
duke
parents:
diff changeset
355 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
356 ServiceabilityAgentDbxModule::run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
357 // This is where most of the work gets done.
a61af66fc99e Initial load
duke
parents:
diff changeset
358 // The command processor loop looks like the following:
a61af66fc99e Initial load
duke
parents:
diff changeset
359 // - create listening socket
a61af66fc99e Initial load
duke
parents:
diff changeset
360 // - accept a connection (only one for now)
a61af66fc99e Initial load
duke
parents:
diff changeset
361 // - while that connection is open and the "exit" command has not
a61af66fc99e Initial load
duke
parents:
diff changeset
362 // been received:
a61af66fc99e Initial load
duke
parents:
diff changeset
363 // - read command
a61af66fc99e Initial load
duke
parents:
diff changeset
364 // - if it's the exit command, cleanup and return
a61af66fc99e Initial load
duke
parents:
diff changeset
365 // - otherwise, process command and write result
a61af66fc99e Initial load
duke
parents:
diff changeset
366
a61af66fc99e Initial load
duke
parents:
diff changeset
367 int listening_socket = socket(AF_INET, SOCK_STREAM, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
368 if (listening_socket < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
369 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
370 }
a61af66fc99e Initial load
duke
parents:
diff changeset
371
a61af66fc99e Initial load
duke
parents:
diff changeset
372 // Set the SO_REUSEADDR property on the listening socket. This
a61af66fc99e Initial load
duke
parents:
diff changeset
373 // prevents problems with calls to bind() to the same port failing
a61af66fc99e Initial load
duke
parents:
diff changeset
374 // after this process exits. This seems to work on all platforms.
a61af66fc99e Initial load
duke
parents:
diff changeset
375 int reuse_address = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
376 if (setsockopt(listening_socket, SOL_SOCKET, SO_REUSEADDR,
a61af66fc99e Initial load
duke
parents:
diff changeset
377 (char *)&reuse_address, sizeof(reuse_address)) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
378 close(listening_socket);
a61af66fc99e Initial load
duke
parents:
diff changeset
379 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
380 }
a61af66fc99e Initial load
duke
parents:
diff changeset
381
a61af66fc99e Initial load
duke
parents:
diff changeset
382 sockaddr_in server_address;
a61af66fc99e Initial load
duke
parents:
diff changeset
383 // Build the server address. We can bind the listening socket to the
a61af66fc99e Initial load
duke
parents:
diff changeset
384 // INADDR_ANY internet address.
a61af66fc99e Initial load
duke
parents:
diff changeset
385 memset((char*)&server_address, 0, sizeof(server_address));
a61af66fc99e Initial load
duke
parents:
diff changeset
386 server_address.sin_family = AF_INET;
a61af66fc99e Initial load
duke
parents:
diff changeset
387 server_address.sin_addr.s_addr = (unsigned long)htonl(INADDR_ANY);
a61af66fc99e Initial load
duke
parents:
diff changeset
388 server_address.sin_port = htons((short)PORT);
a61af66fc99e Initial load
duke
parents:
diff changeset
389
a61af66fc99e Initial load
duke
parents:
diff changeset
390 // Bind socket to port
a61af66fc99e Initial load
duke
parents:
diff changeset
391 if (bind(listening_socket, (sockaddr*) &server_address,
a61af66fc99e Initial load
duke
parents:
diff changeset
392 sizeof(server_address)) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
393 close(listening_socket);
a61af66fc99e Initial load
duke
parents:
diff changeset
394 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
395 }
a61af66fc99e Initial load
duke
parents:
diff changeset
396
a61af66fc99e Initial load
duke
parents:
diff changeset
397 // Arbitrarily chosen backlog of 5 (shouldn't matter since we expect
a61af66fc99e Initial load
duke
parents:
diff changeset
398 // at most one connection)
a61af66fc99e Initial load
duke
parents:
diff changeset
399 if (listen(listening_socket, 5) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
400 close(listening_socket);
a61af66fc99e Initial load
duke
parents:
diff changeset
401 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
402 }
a61af66fc99e Initial load
duke
parents:
diff changeset
403
a61af66fc99e Initial load
duke
parents:
diff changeset
404 // OK, now ready to wait for a data connection. This call to
a61af66fc99e Initial load
duke
parents:
diff changeset
405 // accept() will block.
a61af66fc99e Initial load
duke
parents:
diff changeset
406 struct sockaddr_in client_address;
a61af66fc99e Initial load
duke
parents:
diff changeset
407 int address_len = sizeof(client_address);
a61af66fc99e Initial load
duke
parents:
diff changeset
408 int client_socket = accept(listening_socket, (sockaddr*) &client_address,
a61af66fc99e Initial load
duke
parents:
diff changeset
409 &address_len);
a61af66fc99e Initial load
duke
parents:
diff changeset
410 // Close listening socket regardless of whether accept() succeeded.
a61af66fc99e Initial load
duke
parents:
diff changeset
411 // (FIXME: this may be annoying, especially during debugging, but I
a61af66fc99e Initial load
duke
parents:
diff changeset
412 // really feel that robustness and multiple connections should be
a61af66fc99e Initial load
duke
parents:
diff changeset
413 // handled higher up, e.g., at the Java level -- multiple clients
a61af66fc99e Initial load
duke
parents:
diff changeset
414 // could conceivably connect to the SA via RMI, and that would be a
a61af66fc99e Initial load
duke
parents:
diff changeset
415 // more robust solution than implementing multiple connections at
a61af66fc99e Initial load
duke
parents:
diff changeset
416 // this level)
a61af66fc99e Initial load
duke
parents:
diff changeset
417 NEEDS_CLEANUP;
a61af66fc99e Initial load
duke
parents:
diff changeset
418
a61af66fc99e Initial load
duke
parents:
diff changeset
419 // NOTE: the call to shutdown() usually fails, so don't panic if this happens
a61af66fc99e Initial load
duke
parents:
diff changeset
420 shutdown(listening_socket, 2);
a61af66fc99e Initial load
duke
parents:
diff changeset
421
a61af66fc99e Initial load
duke
parents:
diff changeset
422 if (close(listening_socket) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
423 debug_only(fprintf(stderr, "Serviceability agent: Error closing listening socket\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
424 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
425 }
a61af66fc99e Initial load
duke
parents:
diff changeset
426
a61af66fc99e Initial load
duke
parents:
diff changeset
427 if (client_socket < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
428 debug_only(fprintf(stderr, "Serviceability agent: Failed to open client socket\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
429 // No more cleanup necessary
a61af66fc99e Initial load
duke
parents:
diff changeset
430 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
431 }
a61af66fc99e Initial load
duke
parents:
diff changeset
432
a61af66fc99e Initial load
duke
parents:
diff changeset
433 // Attempt to disable TCP buffering on this socket. We send small
a61af66fc99e Initial load
duke
parents:
diff changeset
434 // amounts of data back and forth and don't want buffering.
a61af66fc99e Initial load
duke
parents:
diff changeset
435 int buffer_val = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
436 if (setsockopt(client_socket, IPPROTO_IP, TCP_NODELAY, (char *) &buffer_val, sizeof(buffer_val)) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
437 debug_only(fprintf(stderr, "Serviceability agent: Failed to set TCP_NODELAY option on client socket\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
438 cleanup(client_socket);
a61af66fc99e Initial load
duke
parents:
diff changeset
439 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
440 }
a61af66fc99e Initial load
duke
parents:
diff changeset
441
a61af66fc99e Initial load
duke
parents:
diff changeset
442 // OK, we have the data socket through which we will communicate
a61af66fc99e Initial load
duke
parents:
diff changeset
443 // with the Java side. Wait for commands or until reading or writing
a61af66fc99e Initial load
duke
parents:
diff changeset
444 // caused an error.
a61af66fc99e Initial load
duke
parents:
diff changeset
445
a61af66fc99e Initial load
duke
parents:
diff changeset
446 bool should_continue = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
447
a61af66fc99e Initial load
duke
parents:
diff changeset
448 myComm.setSocket(client_socket);
a61af66fc99e Initial load
duke
parents:
diff changeset
449
a61af66fc99e Initial load
duke
parents:
diff changeset
450 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
451 scanTimer.reset();
a61af66fc99e Initial load
duke
parents:
diff changeset
452 workTimer.reset();
a61af66fc99e Initial load
duke
parents:
diff changeset
453 writeTimer.reset();
a61af66fc99e Initial load
duke
parents:
diff changeset
454 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
455
a61af66fc99e Initial load
duke
parents:
diff changeset
456 // Allocate a new thread agent for libthread_db
a61af66fc99e Initial load
duke
parents:
diff changeset
457 if ((*td_ta_new_fn)((ps_prochandle*) ps_get_prochandle2(1), &_tdb_agent) !=
a61af66fc99e Initial load
duke
parents:
diff changeset
458 TD_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
459 debug_only(fprintf(stderr, "Serviceability agent: Failed to allocate thread agent\n"));
a61af66fc99e Initial load
duke
parents:
diff changeset
460 cleanup(client_socket);
a61af66fc99e Initial load
duke
parents:
diff changeset
461 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
462 }
a61af66fc99e Initial load
duke
parents:
diff changeset
463
a61af66fc99e Initial load
duke
parents:
diff changeset
464 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
465 // Decided to use text to communicate between these processes.
a61af66fc99e Initial load
duke
parents:
diff changeset
466 // Probably will make debugging easier -- could telnet in if
a61af66fc99e Initial load
duke
parents:
diff changeset
467 // necessary. Will make scanning harder, but probably doesn't
a61af66fc99e Initial load
duke
parents:
diff changeset
468 // matter.
a61af66fc99e Initial load
duke
parents:
diff changeset
469
a61af66fc99e Initial load
duke
parents:
diff changeset
470 // Why not just do what workshop does and parse dbx's console?
a61af66fc99e Initial load
duke
parents:
diff changeset
471 // Probably could do that, but at least this way we are in control
a61af66fc99e Initial load
duke
parents:
diff changeset
472 // of the text format on both ends.
a61af66fc99e Initial load
duke
parents:
diff changeset
473
a61af66fc99e Initial load
duke
parents:
diff changeset
474 // FIXME: should have some way of synchronizing these commands
a61af66fc99e Initial load
duke
parents:
diff changeset
475 // between the C and Java sources.
a61af66fc99e Initial load
duke
parents:
diff changeset
476
a61af66fc99e Initial load
duke
parents:
diff changeset
477 NEEDS_CLEANUP;
a61af66fc99e Initial load
duke
parents:
diff changeset
478
a61af66fc99e Initial load
duke
parents:
diff changeset
479 // Do a blocking read of a line from the socket.
a61af66fc99e Initial load
duke
parents:
diff changeset
480 char *input_buffer = myComm.readLine();
a61af66fc99e Initial load
duke
parents:
diff changeset
481 if (input_buffer == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
482 debug_only(fprintf(stderr, "Serviceability agent: error during read: errno = %d\n", errno));
a61af66fc99e Initial load
duke
parents:
diff changeset
483 debug_only(perror("Serviceability agent"));
a61af66fc99e Initial load
duke
parents:
diff changeset
484 // Error occurred during read.
a61af66fc99e Initial load
duke
parents:
diff changeset
485 // FIXME: should guard against SIGPIPE
a61af66fc99e Initial load
duke
parents:
diff changeset
486 cleanup(client_socket);
a61af66fc99e Initial load
duke
parents:
diff changeset
487 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
488 }
a61af66fc99e Initial load
duke
parents:
diff changeset
489
a61af66fc99e Initial load
duke
parents:
diff changeset
490 // OK, now ready to scan. See README-commands.txt for syntax
a61af66fc99e Initial load
duke
parents:
diff changeset
491 // descriptions.
a61af66fc99e Initial load
duke
parents:
diff changeset
492
a61af66fc99e Initial load
duke
parents:
diff changeset
493 bool res = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
494 if (!strncmp(input_buffer, CMD_ADDRESS_SIZE, strlen(CMD_ADDRESS_SIZE))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
495 res = handleAddressSize(input_buffer + strlen(CMD_ADDRESS_SIZE));
a61af66fc99e Initial load
duke
parents:
diff changeset
496 } else if (!strncmp(input_buffer, CMD_PEEK_FAIL_FAST, strlen(CMD_PEEK_FAIL_FAST))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
497 res = handlePeekFailFast(input_buffer + strlen(CMD_PEEK_FAIL_FAST));
a61af66fc99e Initial load
duke
parents:
diff changeset
498 } else if (!strncmp(input_buffer, CMD_PEEK, strlen(CMD_PEEK))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
499 res = handlePeek(input_buffer + strlen(CMD_PEEK));
a61af66fc99e Initial load
duke
parents:
diff changeset
500 } else if (!strncmp(input_buffer, CMD_POKE, strlen(CMD_POKE))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
501 res = handlePoke(input_buffer + strlen(CMD_POKE));
a61af66fc99e Initial load
duke
parents:
diff changeset
502 } else if (!strncmp(input_buffer, CMD_MAPPED, strlen(CMD_MAPPED))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
503 res = handleMapped(input_buffer + strlen(CMD_MAPPED));
a61af66fc99e Initial load
duke
parents:
diff changeset
504 } else if (!strncmp(input_buffer, CMD_LOOKUP, strlen(CMD_LOOKUP))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
505 res = handleLookup(input_buffer + strlen(CMD_LOOKUP));
a61af66fc99e Initial load
duke
parents:
diff changeset
506 } else if (!strncmp(input_buffer, CMD_THR_GREGS, strlen(CMD_THR_GREGS))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
507 res = handleThrGRegs(input_buffer + strlen(CMD_THR_GREGS));
a61af66fc99e Initial load
duke
parents:
diff changeset
508 } else if (!strncmp(input_buffer, CMD_EXIT, strlen(CMD_EXIT))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
509 should_continue = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
510 }
a61af66fc99e Initial load
duke
parents:
diff changeset
511
a61af66fc99e Initial load
duke
parents:
diff changeset
512 if (should_continue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
513 if (!res) {
a61af66fc99e Initial load
duke
parents:
diff changeset
514 cleanup(client_socket);
a61af66fc99e Initial load
duke
parents:
diff changeset
515 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
516 }
a61af66fc99e Initial load
duke
parents:
diff changeset
517 }
a61af66fc99e Initial load
duke
parents:
diff changeset
518
a61af66fc99e Initial load
duke
parents:
diff changeset
519 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
520 if (++numRequests == PROFILE_COUNT) {
a61af66fc99e Initial load
duke
parents:
diff changeset
521 fprintf(stderr, "%d requests: %d ms scanning, %d ms work, %d ms writing\n",
a61af66fc99e Initial load
duke
parents:
diff changeset
522 PROFILE_COUNT, scanTimer.total(), workTimer.total(), writeTimer.total());
a61af66fc99e Initial load
duke
parents:
diff changeset
523 fflush(stderr);
a61af66fc99e Initial load
duke
parents:
diff changeset
524 scanTimer.reset();
a61af66fc99e Initial load
duke
parents:
diff changeset
525 workTimer.reset();
a61af66fc99e Initial load
duke
parents:
diff changeset
526 writeTimer.reset();
a61af66fc99e Initial load
duke
parents:
diff changeset
527 numRequests = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
528 }
a61af66fc99e Initial load
duke
parents:
diff changeset
529 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
530
a61af66fc99e Initial load
duke
parents:
diff changeset
531 } while (should_continue);
a61af66fc99e Initial load
duke
parents:
diff changeset
532
a61af66fc99e Initial load
duke
parents:
diff changeset
533 // Successful exit
a61af66fc99e Initial load
duke
parents:
diff changeset
534 cleanup(client_socket);
a61af66fc99e Initial load
duke
parents:
diff changeset
535 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
536 }
a61af66fc99e Initial load
duke
parents:
diff changeset
537
a61af66fc99e Initial load
duke
parents:
diff changeset
538 void
a61af66fc99e Initial load
duke
parents:
diff changeset
539 ServiceabilityAgentDbxModule::cleanup(int client_socket) {
a61af66fc99e Initial load
duke
parents:
diff changeset
540 shutdown(client_socket, 2);
a61af66fc99e Initial load
duke
parents:
diff changeset
541 close(client_socket);
a61af66fc99e Initial load
duke
parents:
diff changeset
542 if (_tdb_agent != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
543 (*td_ta_delete_fn)(_tdb_agent);
a61af66fc99e Initial load
duke
parents:
diff changeset
544 }
a61af66fc99e Initial load
duke
parents:
diff changeset
545 }
a61af66fc99e Initial load
duke
parents:
diff changeset
546
a61af66fc99e Initial load
duke
parents:
diff changeset
547 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
548 ServiceabilityAgentDbxModule::handleAddressSize(char* data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
549 int data_model;
a61af66fc99e Initial load
duke
parents:
diff changeset
550 ps_err_e result = ps_pdmodel((ps_prochandle*) ps_get_prochandle2(1),
a61af66fc99e Initial load
duke
parents:
diff changeset
551 &data_model);
a61af66fc99e Initial load
duke
parents:
diff changeset
552 if (result != PS_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
553 myComm.writeString("0");
a61af66fc99e Initial load
duke
parents:
diff changeset
554 myComm.flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
555 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
556 }
a61af66fc99e Initial load
duke
parents:
diff changeset
557
a61af66fc99e Initial load
duke
parents:
diff changeset
558 int val;
a61af66fc99e Initial load
duke
parents:
diff changeset
559 switch (data_model) {
a61af66fc99e Initial load
duke
parents:
diff changeset
560 case PR_MODEL_ILP32:
a61af66fc99e Initial load
duke
parents:
diff changeset
561 val = 32;
a61af66fc99e Initial load
duke
parents:
diff changeset
562 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
563 case PR_MODEL_LP64:
a61af66fc99e Initial load
duke
parents:
diff changeset
564 val = 64;
a61af66fc99e Initial load
duke
parents:
diff changeset
565 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
566 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
567 val = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
568 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
569 }
a61af66fc99e Initial load
duke
parents:
diff changeset
570
a61af66fc99e Initial load
duke
parents:
diff changeset
571 if (!myComm.writeInt(val)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
572 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
573 }
a61af66fc99e Initial load
duke
parents:
diff changeset
574 if (!myComm.writeEOL()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
575 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
576 }
a61af66fc99e Initial load
duke
parents:
diff changeset
577 return myComm.flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
578 }
a61af66fc99e Initial load
duke
parents:
diff changeset
579
a61af66fc99e Initial load
duke
parents:
diff changeset
580 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
581 ServiceabilityAgentDbxModule::handlePeekFailFast(char* data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
582 unsigned int val;
a61af66fc99e Initial load
duke
parents:
diff changeset
583 if (!scanUnsignedInt(&data, &val)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
584 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
585 }
a61af66fc99e Initial load
duke
parents:
diff changeset
586 peek_fail_fast = (val ? true : false);
a61af66fc99e Initial load
duke
parents:
diff changeset
587 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
588 }
a61af66fc99e Initial load
duke
parents:
diff changeset
589
a61af66fc99e Initial load
duke
parents:
diff changeset
590 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
591 ServiceabilityAgentDbxModule::handlePeek(char* data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
592 // Scan hex address, return false if failed
a61af66fc99e Initial load
duke
parents:
diff changeset
593 psaddr_t addr;
a61af66fc99e Initial load
duke
parents:
diff changeset
594 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
595 scanTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
596 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
597 if (!scanAddress(&data, &addr)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
598 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
599 }
a61af66fc99e Initial load
duke
parents:
diff changeset
600 unsigned int num;
a61af66fc99e Initial load
duke
parents:
diff changeset
601 if (!scanUnsignedInt(&data, &num)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
602 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
603 }
a61af66fc99e Initial load
duke
parents:
diff changeset
604 if (num == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
605 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
606 writeTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
607 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
608 myComm.writeBinChar('B');
a61af66fc99e Initial load
duke
parents:
diff changeset
609 myComm.writeBinChar(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
610 myComm.writeBinUnsignedInt(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
611 myComm.writeBinChar(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
612 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
613 writeTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
614 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
615 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
616 }
a61af66fc99e Initial load
duke
parents:
diff changeset
617 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
618 scanTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
619 workTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
620 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
621 char* buf = new char[num];
a61af66fc99e Initial load
duke
parents:
diff changeset
622 ps_prochandle* cur_proc = (ps_prochandle*) ps_get_prochandle2(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
623 ps_err_e result = ps_pread(cur_proc, addr, buf, num);
a61af66fc99e Initial load
duke
parents:
diff changeset
624 if (result == PS_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
625 // Fast case; entire read succeeded.
a61af66fc99e Initial load
duke
parents:
diff changeset
626 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
627 workTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
628 writeTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
629 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
630 myComm.writeBinChar('B');
a61af66fc99e Initial load
duke
parents:
diff changeset
631 myComm.writeBinChar(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
632 myComm.writeBinUnsignedInt(num);
a61af66fc99e Initial load
duke
parents:
diff changeset
633 myComm.writeBinChar(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
634 myComm.writeBinBuf(buf, num);
a61af66fc99e Initial load
duke
parents:
diff changeset
635 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
636 writeTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
637 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
638 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
639 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
640 workTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
641 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
642
a61af66fc99e Initial load
duke
parents:
diff changeset
643 if (peek_fail_fast) {
a61af66fc99e Initial load
duke
parents:
diff changeset
644 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
645 writeTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
646 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
647 // Fail fast
a61af66fc99e Initial load
duke
parents:
diff changeset
648 myComm.writeBinChar('B');
a61af66fc99e Initial load
duke
parents:
diff changeset
649 myComm.writeBinChar(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
650 myComm.writeBinUnsignedInt(num);
a61af66fc99e Initial load
duke
parents:
diff changeset
651 myComm.writeBinChar(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
652 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
653 writeTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
654 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
655 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
656 // Slow case: try to read one byte at a time
a61af66fc99e Initial load
duke
parents:
diff changeset
657 // FIXME: need better way of handling this, a la VirtualQuery
a61af66fc99e Initial load
duke
parents:
diff changeset
658
a61af66fc99e Initial load
duke
parents:
diff changeset
659 unsigned int strideLen = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
660 int bufIdx = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
661 bool lastByteMapped = (ps_pread(cur_proc, addr, buf, 1) == PS_OK ? true : false);
a61af66fc99e Initial load
duke
parents:
diff changeset
662
a61af66fc99e Initial load
duke
parents:
diff changeset
663 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
664 writeTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
665 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
666 myComm.writeBinChar('B');
a61af66fc99e Initial load
duke
parents:
diff changeset
667 myComm.writeBinChar(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
668 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
669 writeTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
670 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
671
a61af66fc99e Initial load
duke
parents:
diff changeset
672 for (int i = 0; i < num; ++i, ++addr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
673 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
674 workTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
675 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
676 result = ps_pread(cur_proc, addr, &buf[bufIdx], 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
677 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
678 workTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
679 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
680 bool tmpMapped = (result == PS_OK ? true : false);
a61af66fc99e Initial load
duke
parents:
diff changeset
681 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
682 writeTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
683 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
684 if (tmpMapped != lastByteMapped) {
a61af66fc99e Initial load
duke
parents:
diff changeset
685 // State change. Write the length of the last stride.
a61af66fc99e Initial load
duke
parents:
diff changeset
686 myComm.writeBinUnsignedInt(strideLen);
a61af66fc99e Initial load
duke
parents:
diff changeset
687 if (lastByteMapped) {
a61af66fc99e Initial load
duke
parents:
diff changeset
688 // Stop gathering data. Write the data of the last stride.
a61af66fc99e Initial load
duke
parents:
diff changeset
689 myComm.writeBinChar(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
690 myComm.writeBinBuf(buf, strideLen);
a61af66fc99e Initial load
duke
parents:
diff changeset
691 bufIdx = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
692 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
693 // Start gathering data to write.
a61af66fc99e Initial load
duke
parents:
diff changeset
694 myComm.writeBinChar(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
695 }
a61af66fc99e Initial load
duke
parents:
diff changeset
696 strideLen = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
697 lastByteMapped = tmpMapped;
a61af66fc99e Initial load
duke
parents:
diff changeset
698 }
a61af66fc99e Initial load
duke
parents:
diff changeset
699 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
700 writeTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
701 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
702 if (lastByteMapped) {
a61af66fc99e Initial load
duke
parents:
diff changeset
703 ++bufIdx;
a61af66fc99e Initial load
duke
parents:
diff changeset
704 }
a61af66fc99e Initial load
duke
parents:
diff changeset
705 ++strideLen;
a61af66fc99e Initial load
duke
parents:
diff changeset
706 }
a61af66fc99e Initial load
duke
parents:
diff changeset
707
a61af66fc99e Initial load
duke
parents:
diff changeset
708 // Write last stride (must be at least one byte long by definition)
a61af66fc99e Initial load
duke
parents:
diff changeset
709 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
710 writeTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
711 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
712 myComm.writeBinUnsignedInt(strideLen);
a61af66fc99e Initial load
duke
parents:
diff changeset
713 if (lastByteMapped) {
a61af66fc99e Initial load
duke
parents:
diff changeset
714 myComm.writeBinChar(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
715 myComm.writeBinBuf(buf, strideLen);
a61af66fc99e Initial load
duke
parents:
diff changeset
716 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
717 myComm.writeBinChar(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
718 }
a61af66fc99e Initial load
duke
parents:
diff changeset
719 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
720 writeTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
721 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
722 }
a61af66fc99e Initial load
duke
parents:
diff changeset
723 }
a61af66fc99e Initial load
duke
parents:
diff changeset
724 delete[] buf;
a61af66fc99e Initial load
duke
parents:
diff changeset
725 myComm.flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
726 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
727 }
a61af66fc99e Initial load
duke
parents:
diff changeset
728
a61af66fc99e Initial load
duke
parents:
diff changeset
729 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
730 ServiceabilityAgentDbxModule::handlePoke(char* data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
731 // FIXME: not yet implemented
a61af66fc99e Initial load
duke
parents:
diff changeset
732 NEEDS_CLEANUP;
a61af66fc99e Initial load
duke
parents:
diff changeset
733 bool res = myComm.writeBoolAsInt(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
734 myComm.flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
735 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
736 }
a61af66fc99e Initial load
duke
parents:
diff changeset
737
a61af66fc99e Initial load
duke
parents:
diff changeset
738 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
739 ServiceabilityAgentDbxModule::handleMapped(char* data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
740 // Scan address
a61af66fc99e Initial load
duke
parents:
diff changeset
741 psaddr_t addr;
a61af66fc99e Initial load
duke
parents:
diff changeset
742 if (!scanAddress(&data, &addr)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
743 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
744 }
a61af66fc99e Initial load
duke
parents:
diff changeset
745 unsigned int num;
a61af66fc99e Initial load
duke
parents:
diff changeset
746 if (!scanUnsignedInt(&data, &num)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
747 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
748 }
a61af66fc99e Initial load
duke
parents:
diff changeset
749 unsigned char val;
a61af66fc99e Initial load
duke
parents:
diff changeset
750 ps_prochandle* cur_proc = (ps_prochandle*) ps_get_prochandle2(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
751 char* buf = new char[num];
a61af66fc99e Initial load
duke
parents:
diff changeset
752 if (ps_pread(cur_proc, addr, buf, num) == PS_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
753 myComm.writeBoolAsInt(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
754 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
755 myComm.writeBoolAsInt(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
756 }
a61af66fc99e Initial load
duke
parents:
diff changeset
757 delete[] buf;
a61af66fc99e Initial load
duke
parents:
diff changeset
758 myComm.writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
759 myComm.flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
760 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
761 }
a61af66fc99e Initial load
duke
parents:
diff changeset
762
a61af66fc99e Initial load
duke
parents:
diff changeset
763 extern "C"
a61af66fc99e Initial load
duke
parents:
diff changeset
764 int loadobj_iterator(const rd_loadobj_t* loadobj, void *) {
a61af66fc99e Initial load
duke
parents:
diff changeset
765 if (loadobj != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
766 fprintf(stderr, "loadobj_iterator: visited loadobj \"%p\"\n", (void*) loadobj->rl_nameaddr);
a61af66fc99e Initial load
duke
parents:
diff changeset
767 return 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
768 }
a61af66fc99e Initial load
duke
parents:
diff changeset
769
a61af66fc99e Initial load
duke
parents:
diff changeset
770 fprintf(stderr, "loadobj_iterator: NULL loadobj\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
771 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
772 }
a61af66fc99e Initial load
duke
parents:
diff changeset
773
a61af66fc99e Initial load
duke
parents:
diff changeset
774 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
775 ServiceabilityAgentDbxModule::handleLookup(char* data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
776 // Debugging: iterate over loadobjs
a61af66fc99e Initial load
duke
parents:
diff changeset
777 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
778 rd_agent_t* rld_agent = rd_new((ps_prochandle*) ps_get_prochandle2(1));
a61af66fc99e Initial load
duke
parents:
diff changeset
779 rd_loadobj_iter(rld_agent, &loadobj_iterator, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
780 rd_delete(rld_agent);
a61af66fc99e Initial load
duke
parents:
diff changeset
781 */
a61af66fc99e Initial load
duke
parents:
diff changeset
782
a61af66fc99e Initial load
duke
parents:
diff changeset
783 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
784 scanTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
785 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
786
a61af66fc99e Initial load
duke
parents:
diff changeset
787 char* object_name = scanSymbol(&data);
a61af66fc99e Initial load
duke
parents:
diff changeset
788 if (object_name == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
789 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
790 }
a61af66fc99e Initial load
duke
parents:
diff changeset
791 char* symbol_name = scanSymbol(&data);
a61af66fc99e Initial load
duke
parents:
diff changeset
792 if (symbol_name == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
793 delete[] object_name;
a61af66fc99e Initial load
duke
parents:
diff changeset
794 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
795 }
a61af66fc99e Initial load
duke
parents:
diff changeset
796
a61af66fc99e Initial load
duke
parents:
diff changeset
797 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
798 scanTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
799 workTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
800 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
801
a61af66fc99e Initial load
duke
parents:
diff changeset
802 ps_sym_t sym;
a61af66fc99e Initial load
duke
parents:
diff changeset
803 // FIXME: check return values from write routines
a61af66fc99e Initial load
duke
parents:
diff changeset
804 ps_prochandle* process = (ps_prochandle*) ps_get_prochandle2(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
805 ps_err_e lookup_res = ps_pglobal_sym(process,
a61af66fc99e Initial load
duke
parents:
diff changeset
806 object_name, symbol_name, &sym);
a61af66fc99e Initial load
duke
parents:
diff changeset
807 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
808 workTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
809 writeTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
810 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
811
a61af66fc99e Initial load
duke
parents:
diff changeset
812 delete[] object_name;
a61af66fc99e Initial load
duke
parents:
diff changeset
813 delete[] symbol_name;
a61af66fc99e Initial load
duke
parents:
diff changeset
814 if (lookup_res != PS_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
815 // This is too noisy
a61af66fc99e Initial load
duke
parents:
diff changeset
816 // debug_only(fprintf(stderr, "ServiceabilityAgentDbxModule::handleLookup: error %d\n", lookup_res));
a61af66fc99e Initial load
duke
parents:
diff changeset
817 myComm.writeString("0x0");
a61af66fc99e Initial load
duke
parents:
diff changeset
818 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
819 myComm.writeAddress((void *)sym.st_value);
a61af66fc99e Initial load
duke
parents:
diff changeset
820 }
a61af66fc99e Initial load
duke
parents:
diff changeset
821 myComm.writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
822 myComm.flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
823
a61af66fc99e Initial load
duke
parents:
diff changeset
824 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
825 writeTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
826 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
827
a61af66fc99e Initial load
duke
parents:
diff changeset
828 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
829 }
a61af66fc99e Initial load
duke
parents:
diff changeset
830
a61af66fc99e Initial load
duke
parents:
diff changeset
831 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
832 ServiceabilityAgentDbxModule::handleThrGRegs(char* data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
833 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
834 scanTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
835 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
836
a61af66fc99e Initial load
duke
parents:
diff changeset
837 unsigned int num;
a61af66fc99e Initial load
duke
parents:
diff changeset
838 // Get the thread ID
a61af66fc99e Initial load
duke
parents:
diff changeset
839 if (!scanUnsignedInt(&data, &num)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
840 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
841 }
a61af66fc99e Initial load
duke
parents:
diff changeset
842
a61af66fc99e Initial load
duke
parents:
diff changeset
843 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
844 scanTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
845 workTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
846 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
847
a61af66fc99e Initial load
duke
parents:
diff changeset
848 // Map tid to thread handle
a61af66fc99e Initial load
duke
parents:
diff changeset
849 td_thrhandle_t thread_handle;
a61af66fc99e Initial load
duke
parents:
diff changeset
850 if ((*td_ta_map_id2thr_fn)(_tdb_agent, num, &thread_handle) != TD_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
851 // fprintf(stderr, "Error mapping thread ID %d to thread handle\n", num);
a61af66fc99e Initial load
duke
parents:
diff changeset
852 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
853 }
a61af66fc99e Initial load
duke
parents:
diff changeset
854
a61af66fc99e Initial load
duke
parents:
diff changeset
855 // Fetch register set
a61af66fc99e Initial load
duke
parents:
diff changeset
856 prgregset_t reg_set;
a61af66fc99e Initial load
duke
parents:
diff changeset
857 memset(reg_set, 0, sizeof(reg_set));
a61af66fc99e Initial load
duke
parents:
diff changeset
858 td_err_e result = (*td_thr_getgregs_fn)(&thread_handle, reg_set);
a61af66fc99e Initial load
duke
parents:
diff changeset
859 if ((result != TD_OK) && (result != TD_PARTIALREG)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
860 // fprintf(stderr, "Error fetching registers for thread handle %d: error = %d\n", num, result);
a61af66fc99e Initial load
duke
parents:
diff changeset
861 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
862 }
a61af66fc99e Initial load
duke
parents:
diff changeset
863
a61af66fc99e Initial load
duke
parents:
diff changeset
864 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
865 workTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
866 writeTimer.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
867 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
868
a61af66fc99e Initial load
duke
parents:
diff changeset
869 #if (defined(__sparc) || defined(__i386))
a61af66fc99e Initial load
duke
parents:
diff changeset
870 myComm.writeInt(NPRGREG);
a61af66fc99e Initial load
duke
parents:
diff changeset
871 myComm.writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
872 for (int i = 0; i < NPRGREG; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
873 myComm.writeAddress((void *)reg_set[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
874 if (i == NPRGREG - 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
875 myComm.writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
876 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
877 myComm.writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
878 }
a61af66fc99e Initial load
duke
parents:
diff changeset
879 }
a61af66fc99e Initial load
duke
parents:
diff changeset
880 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
881 #error Please port ServiceabilityAgentDbxModule::handleThrGRegs to your current platform
a61af66fc99e Initial load
duke
parents:
diff changeset
882 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
883
a61af66fc99e Initial load
duke
parents:
diff changeset
884 myComm.flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
885
a61af66fc99e Initial load
duke
parents:
diff changeset
886 #ifdef PROFILING
a61af66fc99e Initial load
duke
parents:
diff changeset
887 writeTimer.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
888 #endif /* PROFILING */
a61af66fc99e Initial load
duke
parents:
diff changeset
889
a61af66fc99e Initial load
duke
parents:
diff changeset
890 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
891 }
a61af66fc99e Initial load
duke
parents:
diff changeset
892
a61af66fc99e Initial load
duke
parents:
diff changeset
893 //
a61af66fc99e Initial load
duke
parents:
diff changeset
894 // Input routines
a61af66fc99e Initial load
duke
parents:
diff changeset
895 //
a61af66fc99e Initial load
duke
parents:
diff changeset
896
a61af66fc99e Initial load
duke
parents:
diff changeset
897 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
898 ServiceabilityAgentDbxModule::scanAddress(char** data, psaddr_t* addr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
899 *addr = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
900
a61af66fc99e Initial load
duke
parents:
diff changeset
901 // Skip whitespace
a61af66fc99e Initial load
duke
parents:
diff changeset
902 while ((**data != 0) && (isspace(**data))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
903 ++*data;
a61af66fc99e Initial load
duke
parents:
diff changeset
904 }
a61af66fc99e Initial load
duke
parents:
diff changeset
905
a61af66fc99e Initial load
duke
parents:
diff changeset
906 if (**data == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
907 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
908 }
a61af66fc99e Initial load
duke
parents:
diff changeset
909
a61af66fc99e Initial load
duke
parents:
diff changeset
910 if (strncmp(*data, "0x", 2) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
911 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
912 }
a61af66fc99e Initial load
duke
parents:
diff changeset
913
a61af66fc99e Initial load
duke
parents:
diff changeset
914 *data += 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
915
a61af66fc99e Initial load
duke
parents:
diff changeset
916 while ((**data != 0) && (!isspace(**data))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
917 int val;
a61af66fc99e Initial load
duke
parents:
diff changeset
918 bool res = charToNibble(**data, &val);
a61af66fc99e Initial load
duke
parents:
diff changeset
919 if (!res) {
a61af66fc99e Initial load
duke
parents:
diff changeset
920 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
921 }
a61af66fc99e Initial load
duke
parents:
diff changeset
922 *addr <<= 4;
a61af66fc99e Initial load
duke
parents:
diff changeset
923 *addr |= val;
a61af66fc99e Initial load
duke
parents:
diff changeset
924 ++*data;
a61af66fc99e Initial load
duke
parents:
diff changeset
925 }
a61af66fc99e Initial load
duke
parents:
diff changeset
926
a61af66fc99e Initial load
duke
parents:
diff changeset
927 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
928 }
a61af66fc99e Initial load
duke
parents:
diff changeset
929
a61af66fc99e Initial load
duke
parents:
diff changeset
930 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
931 ServiceabilityAgentDbxModule::scanUnsignedInt(char** data, unsigned int* num) {
a61af66fc99e Initial load
duke
parents:
diff changeset
932 *num = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
933
a61af66fc99e Initial load
duke
parents:
diff changeset
934 // Skip whitespace
a61af66fc99e Initial load
duke
parents:
diff changeset
935 while ((**data != 0) && (isspace(**data))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
936 ++*data;
a61af66fc99e Initial load
duke
parents:
diff changeset
937 }
a61af66fc99e Initial load
duke
parents:
diff changeset
938
a61af66fc99e Initial load
duke
parents:
diff changeset
939 if (**data == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
940 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
941 }
a61af66fc99e Initial load
duke
parents:
diff changeset
942
a61af66fc99e Initial load
duke
parents:
diff changeset
943 while ((**data != 0) && (!isspace(**data))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
944 char cur = **data;
a61af66fc99e Initial load
duke
parents:
diff changeset
945 if ((cur < '0') || (cur > '9')) {
a61af66fc99e Initial load
duke
parents:
diff changeset
946 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
947 }
a61af66fc99e Initial load
duke
parents:
diff changeset
948 *num *= 10;
a61af66fc99e Initial load
duke
parents:
diff changeset
949 *num += cur - '0';
a61af66fc99e Initial load
duke
parents:
diff changeset
950 ++*data;
a61af66fc99e Initial load
duke
parents:
diff changeset
951 }
a61af66fc99e Initial load
duke
parents:
diff changeset
952
a61af66fc99e Initial load
duke
parents:
diff changeset
953 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
954 }
a61af66fc99e Initial load
duke
parents:
diff changeset
955
a61af66fc99e Initial load
duke
parents:
diff changeset
956 char*
a61af66fc99e Initial load
duke
parents:
diff changeset
957 ServiceabilityAgentDbxModule::scanSymbol(char** data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
958 // Skip whitespace
a61af66fc99e Initial load
duke
parents:
diff changeset
959 while ((**data != 0) && (isspace(**data))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
960 ++*data;
a61af66fc99e Initial load
duke
parents:
diff changeset
961 }
a61af66fc99e Initial load
duke
parents:
diff changeset
962
a61af66fc99e Initial load
duke
parents:
diff changeset
963 if (**data == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
964 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
965 }
a61af66fc99e Initial load
duke
parents:
diff changeset
966
a61af66fc99e Initial load
duke
parents:
diff changeset
967 // First count length
a61af66fc99e Initial load
duke
parents:
diff changeset
968 int len = 1; // Null terminator
a61af66fc99e Initial load
duke
parents:
diff changeset
969 char* tmpData = *data;
a61af66fc99e Initial load
duke
parents:
diff changeset
970 while ((*tmpData != 0) && (!isspace(*tmpData))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
971 ++tmpData;
a61af66fc99e Initial load
duke
parents:
diff changeset
972 ++len;
a61af66fc99e Initial load
duke
parents:
diff changeset
973 }
a61af66fc99e Initial load
duke
parents:
diff changeset
974 char* buf = new char[len];
a61af66fc99e Initial load
duke
parents:
diff changeset
975 strncpy(buf, *data, len - 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
976 buf[len - 1] = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
977 *data += len - 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
978 return buf;
a61af66fc99e Initial load
duke
parents:
diff changeset
979 }
a61af66fc99e Initial load
duke
parents:
diff changeset
980
a61af66fc99e Initial load
duke
parents:
diff changeset
981 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
982 ServiceabilityAgentDbxModule::charToNibble(char ascii, int* value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
983 if (ascii >= '0' && ascii <= '9') {
a61af66fc99e Initial load
duke
parents:
diff changeset
984 *value = ascii - '0';
a61af66fc99e Initial load
duke
parents:
diff changeset
985 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
986 } else if (ascii >= 'A' && ascii <= 'F') {
a61af66fc99e Initial load
duke
parents:
diff changeset
987 *value = 10 + ascii - 'A';
a61af66fc99e Initial load
duke
parents:
diff changeset
988 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
989 } else if (ascii >= 'a' && ascii <= 'f') {
a61af66fc99e Initial load
duke
parents:
diff changeset
990 *value = 10 + ascii - 'a';
a61af66fc99e Initial load
duke
parents:
diff changeset
991 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
992 }
a61af66fc99e Initial load
duke
parents:
diff changeset
993
a61af66fc99e Initial load
duke
parents:
diff changeset
994 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
995 }
a61af66fc99e Initial load
duke
parents:
diff changeset
996
a61af66fc99e Initial load
duke
parents:
diff changeset
997
a61af66fc99e Initial load
duke
parents:
diff changeset
998 char*
a61af66fc99e Initial load
duke
parents:
diff changeset
999 ServiceabilityAgentDbxModule::readCStringFromProcess(psaddr_t addr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1000 char c;
a61af66fc99e Initial load
duke
parents:
diff changeset
1001 int num = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1002 ps_prochandle* cur_proc = (ps_prochandle*) ps_get_prochandle2(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
1003
a61af66fc99e Initial load
duke
parents:
diff changeset
1004 // Search for null terminator
a61af66fc99e Initial load
duke
parents:
diff changeset
1005 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
1006 if (ps_pread(cur_proc, addr + num, &c, 1) != PS_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1007 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
1008 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1009 ++num;
a61af66fc99e Initial load
duke
parents:
diff changeset
1010 } while (c != 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
1011
a61af66fc99e Initial load
duke
parents:
diff changeset
1012 // Allocate string
a61af66fc99e Initial load
duke
parents:
diff changeset
1013 char* res = new char[num];
a61af66fc99e Initial load
duke
parents:
diff changeset
1014 if (ps_pread(cur_proc, addr, res, num) != PS_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1015 delete[] res;
a61af66fc99e Initial load
duke
parents:
diff changeset
1016 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
1017 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1018 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
1019 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1020
a61af66fc99e Initial load
duke
parents:
diff changeset
1021
a61af66fc99e Initial load
duke
parents:
diff changeset
1022 //--------------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1023 // Class Timer
a61af66fc99e Initial load
duke
parents:
diff changeset
1024 //
a61af66fc99e Initial load
duke
parents:
diff changeset
1025
a61af66fc99e Initial load
duke
parents:
diff changeset
1026 Timer::Timer() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1027 reset();
a61af66fc99e Initial load
duke
parents:
diff changeset
1028 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1029
a61af66fc99e Initial load
duke
parents:
diff changeset
1030 Timer::~Timer() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1031 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1032
a61af66fc99e Initial load
duke
parents:
diff changeset
1033 void
a61af66fc99e Initial load
duke
parents:
diff changeset
1034 Timer::start() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1035 gettimeofday(&startTime, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
1036 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1037
a61af66fc99e Initial load
duke
parents:
diff changeset
1038 void
a61af66fc99e Initial load
duke
parents:
diff changeset
1039 Timer::stop() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1040 struct timeval endTime;
a61af66fc99e Initial load
duke
parents:
diff changeset
1041 gettimeofday(&endTime, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
1042 totalMicroseconds += timevalDiff(&startTime, &endTime);
a61af66fc99e Initial load
duke
parents:
diff changeset
1043 ++counter;
a61af66fc99e Initial load
duke
parents:
diff changeset
1044 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1045
a61af66fc99e Initial load
duke
parents:
diff changeset
1046 long
a61af66fc99e Initial load
duke
parents:
diff changeset
1047 Timer::total() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1048 return (totalMicroseconds / 1000);
a61af66fc99e Initial load
duke
parents:
diff changeset
1049 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1050
a61af66fc99e Initial load
duke
parents:
diff changeset
1051 long
a61af66fc99e Initial load
duke
parents:
diff changeset
1052 Timer::average() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1053 return (long) ((double) total() / (double) counter);
a61af66fc99e Initial load
duke
parents:
diff changeset
1054 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1055
a61af66fc99e Initial load
duke
parents:
diff changeset
1056 void
a61af66fc99e Initial load
duke
parents:
diff changeset
1057 Timer::reset() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1058 totalMicroseconds = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1059 counter = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1060 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1061
a61af66fc99e Initial load
duke
parents:
diff changeset
1062 long long
a61af66fc99e Initial load
duke
parents:
diff changeset
1063 Timer::timevalDiff(struct timeval* start, struct timeval* end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1064 long long secs = end->tv_sec - start->tv_sec;
a61af66fc99e Initial load
duke
parents:
diff changeset
1065 secs *= 1000000;
a61af66fc99e Initial load
duke
parents:
diff changeset
1066 long long usecs = end->tv_usec - start->tv_usec;
a61af66fc99e Initial load
duke
parents:
diff changeset
1067 return (secs + usecs);
a61af66fc99e Initial load
duke
parents:
diff changeset
1068 }