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