blob: f506108080c659d5b2d40e970ed772ced5256e14 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
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 *
15 * - Neither the name of Sun Microsystems nor the names of its
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
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <fcntl.h>
35
36#ifndef LINUX
37#include <procfs.h>
38#endif
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <sys/socket.h>
44#include <sys/errno.h>
45#include <unistd.h>
46#include <errno.h>
47#include <dlfcn.h>
48#include <sys/time.h>
49
50#include <netdb.h>
51#include <netinet/in.h>
52#include <sys/param.h>
53#include <time.h>
54
55#include "jni.h"
56#include "hprof.h"
57
58int
59md_getpid(void)
60{
61 static int pid = -1;
62
63 if ( pid >= 0 ) {
64 return pid;
65 }
66 pid = getpid();
67 return pid;
68}
69
70void
71md_sleep(unsigned seconds)
72{
73 sleep(seconds);
74}
75
76void
77md_init(void)
78{
79#ifdef LINUX
80 /* No Hi-Res timer option? */
81#else
82 if ( gdata->micro_state_accounting ) {
83 char proc_ctl_fn[48];
84 int procfd;
85
86 /* Turn on micro state accounting, once per process */
87 (void)md_snprintf(proc_ctl_fn, sizeof(proc_ctl_fn),
88 "/proc/%d/ctl", md_getpid());
89
90 procfd = open(proc_ctl_fn, O_WRONLY);
91 if (procfd >= 0) {
92 long ctl_op[2];
93
94 ctl_op[0] = PCSET;
95 ctl_op[1] = PR_MSACCT;
96 (void)write(procfd, ctl_op, sizeof(ctl_op));
97 (void)close(procfd);
98 }
99 }
100#endif
101}
102
103int
104md_connect(char *hostname, unsigned short port)
105{
106 struct hostent *hentry;
107 struct sockaddr_in s;
108 int fd;
109
110 /* create a socket */
111 fd = socket(AF_INET, SOCK_STREAM, 0);
112
113 /* find remote host's addr from name */
114 if ((hentry = gethostbyname(hostname)) == NULL) {
115 return -1;
116 }
117 (void)memset((char *)&s, 0, sizeof(s));
118 /* set remote host's addr; its already in network byte order */
119 (void)memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list),
120 (int)sizeof(s.sin_addr.s_addr));
121 /* set remote host's port */
122 s.sin_port = htons(port);
123 s.sin_family = AF_INET;
124
125 /* now try connecting */
126 if (-1 == connect(fd, (struct sockaddr*)&s, sizeof(s))) {
127 return 0;
128 }
129 return fd;
130}
131
132int
133md_recv(int f, char *buf, int len, int option)
134{
135 return recv(f, buf, len, option);
136}
137
138int
139md_shutdown(int filedes, int option)
140{
141 return shutdown(filedes, option);
142}
143
144int
145md_open(const char *filename)
146{
147 return open(filename, O_RDONLY);
148}
149
150int
151md_open_binary(const char *filename)
152{
153 return md_open(filename);
154}
155
156int
157md_creat(const char *filename)
158{
159 return open(filename, O_WRONLY | O_CREAT | O_TRUNC,
160 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
161}
162
163int
164md_creat_binary(const char *filename)
165{
166 return md_creat(filename);
167}
168
169jlong
170md_seek(int filedes, jlong cur)
171{
172 jlong new_pos;
173
174 if ( cur == (jlong)-1 ) {
175 new_pos = lseek(filedes, 0, SEEK_END);
176 } else {
177 new_pos = lseek(filedes, cur, SEEK_SET);
178 }
179 return new_pos;
180}
181
182void
183md_close(int filedes)
184{
185 (void)close(filedes);
186}
187
188int
189md_send(int s, const char *msg, int len, int flags)
190{
191 int res;
192
193 do {
194 res = send(s, msg, len, flags);
195 } while ((res < 0) && (errno == EINTR));
196
197 return res;
198}
199
200int
201md_write(int filedes, const void *buf, int nbyte)
202{
203 int res;
204
205 do {
206 res = write(filedes, buf, nbyte);
207 } while ((res < 0) && (errno == EINTR));
208
209 return res;
210}
211
212int
213md_read(int filedes, void *buf, int nbyte)
214{
215 int res;
216
217 do {
218 res = read(filedes, buf, nbyte);
219 } while ((res < 0) && (errno == EINTR));
220
221 return res;
222}
223
224/* Time of day in milli-seconds */
225static jlong
226md_timeofday(void)
227{
228 struct timeval tv;
229
230 if ( gettimeofday(&tv, (void *)0) != 0 ) {
231 return (jlong)0; /* EOVERFLOW ? */
232 }
233 /*LINTED*/
234 return ((jlong)tv.tv_sec * (jlong)1000) + (jlong)(tv.tv_usec / 1000);
235}
236
237/* Hi-res timer in micro-seconds */
238jlong
239md_get_microsecs(void)
240{
241#ifdef LINUX
242 return (jlong)(md_timeofday() * (jlong)1000); /* Milli to micro */
243#else
244 return (jlong)(gethrtime()/(hrtime_t)1000); /* Nano seconds to micro seconds */
245#endif
246}
247
248/* Time of day in milli-seconds */
249jlong
250md_get_timemillis(void)
251{
252 return md_timeofday();
253}
254
255/* Current CPU hi-res CPU time used */
256jlong
257md_get_thread_cpu_timemillis(void)
258{
259#ifdef LINUX
260 return md_timeofday();
261#else
262 return (jlong)(gethrvtime()/1000); /* Nano seconds to milli seconds */
263#endif
264}
265
266void
267md_get_prelude_path(char *path, int path_len, char *filename)
268{
269 void *addr;
270 char libdir[FILENAME_MAX+1];
271 Dl_info dlinfo;
272
273 libdir[0] = 0;
274#ifdef LINUX
275 addr = (void*)&Agent_OnLoad;
276#else
277 /* Just using &Agent_OnLoad will get the first external symbol with
278 * this name in the first .so, which may not be libhprof.so.
279 * On Solaris we can actually ask for the address of our Agent_OnLoad.
280 */
281 addr = dlsym(RTLD_SELF, "Agent_OnLoad");
282 /* Just in case the above didn't work (missing linker patch?). */
283 if ( addr == NULL ) {
284 addr = (void*)&Agent_OnLoad;
285 }
286#endif
287
288 /* Use dladdr() to get the full path to libhprof.so, which we use to find
289 * the prelude file.
290 */
291 dlinfo.dli_fname = NULL;
292 (void)dladdr(addr, &dlinfo);
293 if ( dlinfo.dli_fname != NULL ) {
294 char * lastSlash;
295
296 /* Full path to library name, need to move up one directory to 'lib' */
297 (void)strcpy(libdir, (char *)dlinfo.dli_fname);
298 lastSlash = strrchr(libdir, '/');
299 if ( lastSlash != NULL ) {
300 *lastSlash = '\0';
301 }
302 lastSlash = strrchr(libdir, '/');
303 if ( lastSlash != NULL ) {
304 *lastSlash = '\0';
305 }
306 }
307 (void)snprintf(path, path_len, "%s/%s", libdir, filename);
308}
309
310
311int
312md_vsnprintf(char *s, int n, const char *format, va_list ap)
313{
314 return vsnprintf(s, n, format, ap);
315}
316
317int
318md_snprintf(char *s, int n, const char *format, ...)
319{
320 int ret;
321 va_list ap;
322
323 va_start(ap, format);
324 ret = md_vsnprintf(s, n, format, ap);
325 va_end(ap);
326 return ret;
327}
328
329void
330md_system_error(char *buf, int len)
331{
332 char *p;
333
334 buf[0] = 0;
335 p = strerror(errno);
336 if ( p != NULL ) {
337 (void)strcpy(buf, p);
338 }
339}
340
341unsigned
342md_htons(unsigned short s)
343{
344 return htons(s);
345}
346
347unsigned
348md_htonl(unsigned l)
349{
350 return htonl(l);
351}
352
353unsigned
354md_ntohs(unsigned short s)
355{
356 return ntohs(s);
357}
358
359unsigned
360md_ntohl(unsigned l)
361{
362 return ntohl(l);
363}
364
365/* Create the actual fill filename for a dynamic library. */
366void
367md_build_library_name(char *holder, int holderlen, char *pname, char *fname)
368{
369 int pnamelen;
370
371 /* Length of options directory location. */
372 pnamelen = pname ? strlen(pname) : 0;
373
374 /* Quietly truncate on buffer overflow. Should be an error. */
375 if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
376 *holder = '\0';
377 return;
378 }
379
380 /* Construct path to library */
381 if (pnamelen == 0) {
382 (void)snprintf(holder, holderlen, "lib%s.so", fname);
383 } else {
384 (void)snprintf(holder, holderlen, "%s/lib%s.so", pname, fname);
385 }
386}
387
388/* Load this library (return NULL on error, and error message in err_buf) */
389void *
390md_load_library(const char *name, char *err_buf, int err_buflen)
391{
392 void * result;
393
394 result = dlopen(name, RTLD_LAZY);
395 if (result == NULL) {
396 (void)strncpy(err_buf, dlerror(), err_buflen-2);
397 err_buf[err_buflen-1] = '\0';
398 }
399 return result;
400}
401
402/* Unload this library */
403void
404md_unload_library(void *handle)
405{
406 (void)dlclose(handle);
407}
408
409/* Find an entry point inside this library (return NULL if not found) */
410void *
411md_find_library_entry(void *handle, const char *name)
412{
413 void * sym;
414
415 sym = dlsym(handle, name);
416 return sym;
417}