jdk/src/windows/demo/jvmti/hprof/hprof_md.c
author jgish
Thu, 18 Apr 2013 16:33:11 -0400
changeset 17159 bb566a21b661
parent 16726 f76b2e6bd199
child 20484 5ded5b92c173
permissions -rw-r--r--
8012005: LogManager needs test to ensure stack trace is not being done to find bundle Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
     2
 * Copyright (c) 2003, 2012, 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: 9035
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
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: 9035
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: 9035
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: 9035
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: 9035
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    40
7999
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
    41
// To ensure winsock2.h is used, it has to be included ahead of
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
    42
// windows.h, which includes winsock.h by default.
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
    43
#include <winsock2.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include <mmsystem.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include <process.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
md_getpid(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static int pid = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    if ( pid >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    pid = getpid();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
md_sleep(unsigned seconds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    Sleep((DWORD)seconds*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
md_init(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
md_connect(char *hostname, unsigned short port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    struct hostent *hentry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    struct sockaddr_in s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /* create a socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    fd = (int)socket(AF_INET, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /* find remote host's addr from name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    if ((hentry = gethostbyname(hostname)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    (void)memset((char *)&s, 0, sizeof(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /* set remote host's addr; its already in network byte order */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    (void)memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
           (int)sizeof(s.sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /* set remote host's port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    s.sin_port = htons(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    s.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /* now try connecting */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    if (-1 == connect(fd, (struct sockaddr*)&s, sizeof(s))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
md_recv(int f, char *buf, int len, int option)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    return recv(f, buf, len, option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
md_shutdown(int filedes, int option)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    return shutdown(filedes, option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
md_open(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    return open(filename, O_RDONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
md_open_binary(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    return open(filename, O_RDONLY|O_BINARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
md_creat(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    return open(filename, O_CREAT | O_WRONLY | O_TRUNC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                             _S_IREAD | _S_IWRITE);
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_creat_binary(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    return open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                            _S_IREAD | _S_IWRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
md_seek(int filedes, jlong pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    jlong new_pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    if ( pos == (jlong)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        new_pos = _lseeki64(filedes, 0L, SEEK_END);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        new_pos = _lseeki64(filedes, pos, SEEK_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    return new_pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
md_close(int filedes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
{
7999
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
   161
    (void)closesocket(filedes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
md_send(int s, const char *msg, int len, int flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    return send(s, msg, len, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
md_read(int filedes, void *buf, int nbyte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    return read(filedes, buf, nbyte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
md_write(int filedes, const void *buf, int nbyte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    return write(filedes, buf, nbyte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
md_get_microsecs(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    return (jlong)(timeGetTime())*(jlong)1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
#define FT2JLONG(ft) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        ((jlong)(ft).dwHighDateTime << 32 | (jlong)(ft).dwLowDateTime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
md_get_timemillis(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    static jlong fileTime_1_1_70 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    SYSTEMTIME st0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    FILETIME   ft0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    if (fileTime_1_1_70 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        /* Initialize fileTime_1_1_70 -- the Win32 file time of midnight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         * 1/1/70.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        memset(&st0, 0, sizeof(st0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        st0.wYear  = 1970;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        st0.wMonth = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        st0.wDay   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        SystemTimeToFileTime(&st0, &ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        fileTime_1_1_70 = FT2JLONG(ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    GetSystemTime(&st0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    SystemTimeToFileTime(&st0, &ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    return (FT2JLONG(ft0) - fileTime_1_1_70) / 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
md_get_thread_cpu_timemillis(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    return md_get_timemillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
HINSTANCE hJavaInst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
static int nError = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
BOOL WINAPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    WSADATA wsaData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    switch (reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        case DLL_PROCESS_ATTACH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            hJavaInst = hinst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            nError = WSAStartup(MAKEWORD(2,0), &wsaData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        case DLL_PROCESS_DETACH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            WSACleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            hJavaInst = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
md_get_prelude_path(char *path, int path_len, char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    char libdir[FILENAME_MAX+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    char *lastSlash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    GetModuleFileName(hJavaInst, libdir, FILENAME_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /* This is actually in the bin directory, so move above bin for lib */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    lastSlash = strrchr(libdir, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    if ( lastSlash != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        *lastSlash = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    lastSlash = strrchr(libdir, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    if ( lastSlash != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        *lastSlash = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    (void)md_snprintf(path, path_len, "%s\\lib\\%s", libdir, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
md_vsnprintf(char *s, int n, const char *format, va_list ap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    return _vsnprintf(s, n, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
md_snprintf(char *s, int n, const char *format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    ret = md_vsnprintf(s, n, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
md_system_error(char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    long errval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    errval = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    buf[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    if (errval != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                              NULL, errval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                              0, buf, len, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (n > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            /* Drop final '.', CR, LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (buf[n - 1] == '\n') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (buf[n - 1] == '\r') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (buf[n - 1] == '.') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
md_htons(unsigned short s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    return htons(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
md_htonl(unsigned l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    return htonl(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
md_ntohs(unsigned short s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    return ntohs(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
md_ntohl(unsigned l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    return ntohl(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
get_last_error_string(char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    long errval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    errval = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    if (errval != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        /* DOS error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                              NULL, errval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                              0, buf, len, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (n > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            /* Drop final '.', CR, LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (buf[n - 1] == '\n') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (buf[n - 1] == '\r') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (buf[n - 1] == '.') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    if (errno != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        /* C runtime error that has no corresponding DOS error code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        const char *s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        int         n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        s = strerror(errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        n = (int)strlen(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (n >= len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            n = len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        (void)strncpy(buf, s, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   370
static void dll_build_name(char* buffer, size_t buflen,
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   371
                           const char* paths, const char* fname) {
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   372
    char *path, *paths_copy, *next_token;
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   373
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   374
    paths_copy = strdup(paths);
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   375
    if (paths_copy == NULL) {
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   376
        return;
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   377
    }
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   378
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   379
    next_token = NULL;
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   380
    path = strtok_s(paths_copy, ";", &next_token);
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   381
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   382
    while (path != NULL) {
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   383
        _snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   384
        if (_access(buffer, 0) == 0) {
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   385
            break;
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   386
        }
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   387
        *buffer = '\0';
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   388
        path = strtok_s(NULL, ";", &next_token);
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   389
    }
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   390
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   391
    free(paths_copy);
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   392
}
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   393
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
/* Build a machine dependent library name out of a path and file name.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
void
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   396
md_build_library_name(char *holder, int holderlen, const char *pname, const char *fname)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    int   pnamelen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    pnamelen = pname ? (int)strlen(pname) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   402
    *holder = '\0';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /* Quietly truncates on buffer overflow. Should be an error. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    if (pnamelen + strlen(fname) + 10 > (unsigned int)holderlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    if (pnamelen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        sprintf(holder, "%s.dll", fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    } else {
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   411
      dll_build_name(holder, holderlen, pname, fname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
md_load_library(const char * name, char *err_buf, int err_buflen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    void *result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    result = LoadLibrary(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    if (result == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        /* Error message is pretty lame, try to make a better guess. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        long errcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        errcode = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (errcode == ERROR_MOD_NOT_FOUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            strncpy(err_buf, "Can't find dependent libraries", err_buflen-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            err_buf[err_buflen-1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            get_last_error_string(err_buf, err_buflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
md_unload_library(void *handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    FreeLibrary(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
md_find_library_entry(void *handle, const char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    return GetProcAddress(handle, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
}