author | nloodin |
Wed, 31 Aug 2011 13:48:04 +0200 | |
changeset 10292 | ed7db6a12c2a |
parent 5506 | 202f599c92aa |
child 12047 | 320a714614e9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
|
5 |
* modification, are permitted provided that the following conditions |
|
6 |
* are met: |
|
7 |
* |
|
8 |
* - Redistributions of source code must retain the above copyright |
|
9 |
* notice, this list of conditions and the following disclaimer. |
|
10 |
* |
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
|
12 |
* notice, this list of conditions and the following disclaimer in the |
|
13 |
* documentation and/or other materials provided with the distribution. |
|
14 |
* |
|
5506 | 15 |
* - Neither the name of Oracle nor the names of its |
2 | 16 |
* contributors may be used to endorse or promote products derived |
17 |
* from this software without specific prior written permission. |
|
18 |
* |
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 |
*/ |
|
31 |
||
10292
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
5506
diff
changeset
|
32 |
/* |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
5506
diff
changeset
|
33 |
* This source code is provided to illustrate the usage of a given feature |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
5506
diff
changeset
|
34 |
* or technique and has been deliberately simplified. Additional steps |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
5506
diff
changeset
|
35 |
* required for a production-quality application, such as security checks, |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
5506
diff
changeset
|
36 |
* input validation and proper error handling, might not be present in |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
5506
diff
changeset
|
37 |
* this sample code. |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
5506
diff
changeset
|
38 |
*/ |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
5506
diff
changeset
|
39 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
5506
diff
changeset
|
40 |
|
2 | 41 |
#include <sys/types.h> |
42 |
#include <sys/stat.h> |
|
43 |
#include <fcntl.h> |
|
44 |
||
45 |
#ifndef LINUX |
|
46 |
#include <procfs.h> |
|
47 |
#endif |
|
48 |
||
49 |
#include <stdio.h> |
|
50 |
#include <stdlib.h> |
|
51 |
#include <string.h> |
|
52 |
#include <sys/socket.h> |
|
53 |
#include <sys/errno.h> |
|
54 |
#include <unistd.h> |
|
55 |
#include <errno.h> |
|
56 |
#include <dlfcn.h> |
|
57 |
#include <sys/time.h> |
|
58 |
||
59 |
#include <netdb.h> |
|
60 |
#include <netinet/in.h> |
|
61 |
#include <sys/param.h> |
|
62 |
#include <time.h> |
|
63 |
||
64 |
#include "jni.h" |
|
65 |
#include "hprof.h" |
|
66 |
||
67 |
int |
|
68 |
md_getpid(void) |
|
69 |
{ |
|
70 |
static int pid = -1; |
|
71 |
||
72 |
if ( pid >= 0 ) { |
|
73 |
return pid; |
|
74 |
} |
|
75 |
pid = getpid(); |
|
76 |
return pid; |
|
77 |
} |
|
78 |
||
79 |
void |
|
80 |
md_sleep(unsigned seconds) |
|
81 |
{ |
|
82 |
sleep(seconds); |
|
83 |
} |
|
84 |
||
85 |
void |
|
86 |
md_init(void) |
|
87 |
{ |
|
88 |
#ifdef LINUX |
|
89 |
/* No Hi-Res timer option? */ |
|
90 |
#else |
|
91 |
if ( gdata->micro_state_accounting ) { |
|
92 |
char proc_ctl_fn[48]; |
|
93 |
int procfd; |
|
94 |
||
95 |
/* Turn on micro state accounting, once per process */ |
|
96 |
(void)md_snprintf(proc_ctl_fn, sizeof(proc_ctl_fn), |
|
97 |
"/proc/%d/ctl", md_getpid()); |
|
98 |
||
99 |
procfd = open(proc_ctl_fn, O_WRONLY); |
|
100 |
if (procfd >= 0) { |
|
101 |
long ctl_op[2]; |
|
102 |
||
103 |
ctl_op[0] = PCSET; |
|
104 |
ctl_op[1] = PR_MSACCT; |
|
105 |
(void)write(procfd, ctl_op, sizeof(ctl_op)); |
|
106 |
(void)close(procfd); |
|
107 |
} |
|
108 |
} |
|
109 |
#endif |
|
110 |
} |
|
111 |
||
112 |
int |
|
113 |
md_connect(char *hostname, unsigned short port) |
|
114 |
{ |
|
115 |
struct hostent *hentry; |
|
116 |
struct sockaddr_in s; |
|
117 |
int fd; |
|
118 |
||
119 |
/* create a socket */ |
|
120 |
fd = socket(AF_INET, SOCK_STREAM, 0); |
|
121 |
||
122 |
/* find remote host's addr from name */ |
|
123 |
if ((hentry = gethostbyname(hostname)) == NULL) { |
|
124 |
return -1; |
|
125 |
} |
|
126 |
(void)memset((char *)&s, 0, sizeof(s)); |
|
127 |
/* set remote host's addr; its already in network byte order */ |
|
128 |
(void)memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list), |
|
129 |
(int)sizeof(s.sin_addr.s_addr)); |
|
130 |
/* set remote host's port */ |
|
131 |
s.sin_port = htons(port); |
|
132 |
s.sin_family = AF_INET; |
|
133 |
||
134 |
/* now try connecting */ |
|
135 |
if (-1 == connect(fd, (struct sockaddr*)&s, sizeof(s))) { |
|
136 |
return 0; |
|
137 |
} |
|
138 |
return fd; |
|
139 |
} |
|
140 |
||
141 |
int |
|
142 |
md_recv(int f, char *buf, int len, int option) |
|
143 |
{ |
|
144 |
return recv(f, buf, len, option); |
|
145 |
} |
|
146 |
||
147 |
int |
|
148 |
md_shutdown(int filedes, int option) |
|
149 |
{ |
|
150 |
return shutdown(filedes, option); |
|
151 |
} |
|
152 |
||
153 |
int |
|
154 |
md_open(const char *filename) |
|
155 |
{ |
|
156 |
return open(filename, O_RDONLY); |
|
157 |
} |
|
158 |
||
159 |
int |
|
160 |
md_open_binary(const char *filename) |
|
161 |
{ |
|
162 |
return md_open(filename); |
|
163 |
} |
|
164 |
||
165 |
int |
|
166 |
md_creat(const char *filename) |
|
167 |
{ |
|
168 |
return open(filename, O_WRONLY | O_CREAT | O_TRUNC, |
|
169 |
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
|
170 |
} |
|
171 |
||
172 |
int |
|
173 |
md_creat_binary(const char *filename) |
|
174 |
{ |
|
175 |
return md_creat(filename); |
|
176 |
} |
|
177 |
||
178 |
jlong |
|
179 |
md_seek(int filedes, jlong cur) |
|
180 |
{ |
|
181 |
jlong new_pos; |
|
182 |
||
183 |
if ( cur == (jlong)-1 ) { |
|
184 |
new_pos = lseek(filedes, 0, SEEK_END); |
|
185 |
} else { |
|
186 |
new_pos = lseek(filedes, cur, SEEK_SET); |
|
187 |
} |
|
188 |
return new_pos; |
|
189 |
} |
|
190 |
||
191 |
void |
|
192 |
md_close(int filedes) |
|
193 |
{ |
|
194 |
(void)close(filedes); |
|
195 |
} |
|
196 |
||
197 |
int |
|
198 |
md_send(int s, const char *msg, int len, int flags) |
|
199 |
{ |
|
200 |
int res; |
|
201 |
||
202 |
do { |
|
203 |
res = send(s, msg, len, flags); |
|
204 |
} while ((res < 0) && (errno == EINTR)); |
|
205 |
||
206 |
return res; |
|
207 |
} |
|
208 |
||
209 |
int |
|
210 |
md_write(int filedes, const void *buf, int nbyte) |
|
211 |
{ |
|
212 |
int res; |
|
213 |
||
214 |
do { |
|
215 |
res = write(filedes, buf, nbyte); |
|
216 |
} while ((res < 0) && (errno == EINTR)); |
|
217 |
||
218 |
return res; |
|
219 |
} |
|
220 |
||
221 |
int |
|
222 |
md_read(int filedes, void *buf, int nbyte) |
|
223 |
{ |
|
224 |
int res; |
|
225 |
||
226 |
do { |
|
227 |
res = read(filedes, buf, nbyte); |
|
228 |
} while ((res < 0) && (errno == EINTR)); |
|
229 |
||
230 |
return res; |
|
231 |
} |
|
232 |
||
233 |
/* Time of day in milli-seconds */ |
|
234 |
static jlong |
|
235 |
md_timeofday(void) |
|
236 |
{ |
|
237 |
struct timeval tv; |
|
238 |
||
239 |
if ( gettimeofday(&tv, (void *)0) != 0 ) { |
|
240 |
return (jlong)0; /* EOVERFLOW ? */ |
|
241 |
} |
|
242 |
/*LINTED*/ |
|
243 |
return ((jlong)tv.tv_sec * (jlong)1000) + (jlong)(tv.tv_usec / 1000); |
|
244 |
} |
|
245 |
||
246 |
/* Hi-res timer in micro-seconds */ |
|
247 |
jlong |
|
248 |
md_get_microsecs(void) |
|
249 |
{ |
|
250 |
#ifdef LINUX |
|
251 |
return (jlong)(md_timeofday() * (jlong)1000); /* Milli to micro */ |
|
252 |
#else |
|
253 |
return (jlong)(gethrtime()/(hrtime_t)1000); /* Nano seconds to micro seconds */ |
|
254 |
#endif |
|
255 |
} |
|
256 |
||
257 |
/* Time of day in milli-seconds */ |
|
258 |
jlong |
|
259 |
md_get_timemillis(void) |
|
260 |
{ |
|
261 |
return md_timeofday(); |
|
262 |
} |
|
263 |
||
264 |
/* Current CPU hi-res CPU time used */ |
|
265 |
jlong |
|
266 |
md_get_thread_cpu_timemillis(void) |
|
267 |
{ |
|
268 |
#ifdef LINUX |
|
269 |
return md_timeofday(); |
|
270 |
#else |
|
271 |
return (jlong)(gethrvtime()/1000); /* Nano seconds to milli seconds */ |
|
272 |
#endif |
|
273 |
} |
|
274 |
||
275 |
void |
|
276 |
md_get_prelude_path(char *path, int path_len, char *filename) |
|
277 |
{ |
|
278 |
void *addr; |
|
279 |
char libdir[FILENAME_MAX+1]; |
|
280 |
Dl_info dlinfo; |
|
281 |
||
282 |
libdir[0] = 0; |
|
283 |
#ifdef LINUX |
|
284 |
addr = (void*)&Agent_OnLoad; |
|
285 |
#else |
|
286 |
/* Just using &Agent_OnLoad will get the first external symbol with |
|
287 |
* this name in the first .so, which may not be libhprof.so. |
|
288 |
* On Solaris we can actually ask for the address of our Agent_OnLoad. |
|
289 |
*/ |
|
290 |
addr = dlsym(RTLD_SELF, "Agent_OnLoad"); |
|
291 |
/* Just in case the above didn't work (missing linker patch?). */ |
|
292 |
if ( addr == NULL ) { |
|
293 |
addr = (void*)&Agent_OnLoad; |
|
294 |
} |
|
295 |
#endif |
|
296 |
||
297 |
/* Use dladdr() to get the full path to libhprof.so, which we use to find |
|
298 |
* the prelude file. |
|
299 |
*/ |
|
300 |
dlinfo.dli_fname = NULL; |
|
301 |
(void)dladdr(addr, &dlinfo); |
|
302 |
if ( dlinfo.dli_fname != NULL ) { |
|
303 |
char * lastSlash; |
|
304 |
||
305 |
/* Full path to library name, need to move up one directory to 'lib' */ |
|
306 |
(void)strcpy(libdir, (char *)dlinfo.dli_fname); |
|
307 |
lastSlash = strrchr(libdir, '/'); |
|
308 |
if ( lastSlash != NULL ) { |
|
309 |
*lastSlash = '\0'; |
|
310 |
} |
|
311 |
lastSlash = strrchr(libdir, '/'); |
|
312 |
if ( lastSlash != NULL ) { |
|
313 |
*lastSlash = '\0'; |
|
314 |
} |
|
315 |
} |
|
316 |
(void)snprintf(path, path_len, "%s/%s", libdir, filename); |
|
317 |
} |
|
318 |
||
319 |
||
320 |
int |
|
321 |
md_vsnprintf(char *s, int n, const char *format, va_list ap) |
|
322 |
{ |
|
323 |
return vsnprintf(s, n, format, ap); |
|
324 |
} |
|
325 |
||
326 |
int |
|
327 |
md_snprintf(char *s, int n, const char *format, ...) |
|
328 |
{ |
|
329 |
int ret; |
|
330 |
va_list ap; |
|
331 |
||
332 |
va_start(ap, format); |
|
333 |
ret = md_vsnprintf(s, n, format, ap); |
|
334 |
va_end(ap); |
|
335 |
return ret; |
|
336 |
} |
|
337 |
||
338 |
void |
|
339 |
md_system_error(char *buf, int len) |
|
340 |
{ |
|
341 |
char *p; |
|
342 |
||
343 |
buf[0] = 0; |
|
344 |
p = strerror(errno); |
|
345 |
if ( p != NULL ) { |
|
346 |
(void)strcpy(buf, p); |
|
347 |
} |
|
348 |
} |
|
349 |
||
350 |
unsigned |
|
351 |
md_htons(unsigned short s) |
|
352 |
{ |
|
353 |
return htons(s); |
|
354 |
} |
|
355 |
||
356 |
unsigned |
|
357 |
md_htonl(unsigned l) |
|
358 |
{ |
|
359 |
return htonl(l); |
|
360 |
} |
|
361 |
||
362 |
unsigned |
|
363 |
md_ntohs(unsigned short s) |
|
364 |
{ |
|
365 |
return ntohs(s); |
|
366 |
} |
|
367 |
||
368 |
unsigned |
|
369 |
md_ntohl(unsigned l) |
|
370 |
{ |
|
371 |
return ntohl(l); |
|
372 |
} |
|
373 |
||
374 |
/* Create the actual fill filename for a dynamic library. */ |
|
375 |
void |
|
376 |
md_build_library_name(char *holder, int holderlen, char *pname, char *fname) |
|
377 |
{ |
|
378 |
int pnamelen; |
|
379 |
||
380 |
/* Length of options directory location. */ |
|
381 |
pnamelen = pname ? strlen(pname) : 0; |
|
382 |
||
383 |
/* Quietly truncate on buffer overflow. Should be an error. */ |
|
384 |
if (pnamelen + (int)strlen(fname) + 10 > holderlen) { |
|
385 |
*holder = '\0'; |
|
386 |
return; |
|
387 |
} |
|
388 |
||
389 |
/* Construct path to library */ |
|
390 |
if (pnamelen == 0) { |
|
391 |
(void)snprintf(holder, holderlen, "lib%s.so", fname); |
|
392 |
} else { |
|
393 |
(void)snprintf(holder, holderlen, "%s/lib%s.so", pname, fname); |
|
394 |
} |
|
395 |
} |
|
396 |
||
397 |
/* Load this library (return NULL on error, and error message in err_buf) */ |
|
398 |
void * |
|
399 |
md_load_library(const char *name, char *err_buf, int err_buflen) |
|
400 |
{ |
|
401 |
void * result; |
|
402 |
||
403 |
result = dlopen(name, RTLD_LAZY); |
|
404 |
if (result == NULL) { |
|
405 |
(void)strncpy(err_buf, dlerror(), err_buflen-2); |
|
406 |
err_buf[err_buflen-1] = '\0'; |
|
407 |
} |
|
408 |
return result; |
|
409 |
} |
|
410 |
||
411 |
/* Unload this library */ |
|
412 |
void |
|
413 |
md_unload_library(void *handle) |
|
414 |
{ |
|
415 |
(void)dlclose(handle); |
|
416 |
} |
|
417 |
||
418 |
/* Find an entry point inside this library (return NULL if not found) */ |
|
419 |
void * |
|
420 |
md_find_library_entry(void *handle, const char *name) |
|
421 |
{ |
|
422 |
void * sym; |
|
423 |
||
424 |
sym = dlsym(handle, name); |
|
425 |
return sym; |
|
426 |
} |