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