jdk/src/demo/share/jvmti/agent_util/agent_util.c
author chegar
Tue, 22 Mar 2016 10:43:19 +0000
changeset 36709 f9f252088afa
parent 25859 3317bb8137f4
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 2004, 2011, 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 <agent_util.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/* Generic C utility functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/* Send message to stdout or whatever the data output location is */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
stdout_message(const char * format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    (void)vfprintf(stdout, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/* Send message to stderr or whatever the error output location is and exit  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
fatal_error(const char * format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    (void)vfprintf(stderr, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    (void)fflush(stderr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    exit(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
/* Get a token from a string (strtok is not MT-safe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *    str       String to scan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *    seps      Separation characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *    buf       Place to put results
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *    max       Size of buf
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *  Returns NULL if no token available or can't do the scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
get_token(char *str, char *seps, char *buf, int max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    buf[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    if ( str==NULL || str[0]==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    str += strspn(str, seps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    if ( str[0]==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    len = (int)strcspn(str, seps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    if ( len >= max ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    (void)strncpy(buf, str, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    buf[len] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    return str+len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
/* Determines if a class/method is specified by a list item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   item       String that represents a pattern to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *                If it starts with a '*', then any class is allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *                If it ends with a '*', then any method is allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *   cname      Class name, e.g. "java.lang.Object"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *   mname      Method name, e.g. "<init>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *  Returns 1(true) or 0(false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
covered_by_list_item(char *item, char *cname, char *mname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    int      len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    len = (int)strlen(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    if ( item[0]=='*' ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if ( strncmp(mname, item+1, len-1)==0 ) {
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
    } else if ( item[len-1]=='*' ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if ( strncmp(cname, item, len-1)==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int cname_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        cname_len = (int)strlen(cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if ( strncmp(cname, item, (len>cname_len?cname_len:len))==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if ( cname_len >= len ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                /* No method name supplied in item, we must have matched */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                int mname_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                mname_len = (int)strlen(mname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                item += cname_len+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                len -= cname_len+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if ( strncmp(mname, item, (len>mname_len?mname_len:len))==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
/* Determines if a class/method is specified by this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *   list       String of comma separated pattern items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *   cname      Class name, e.g. "java.lang.Object"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *   mname      Method name, e.g. "<init>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *  Returns 1(true) or 0(false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
covered_by_list(char *list, char *cname, char *mname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    char  token[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    char *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    if ( list[0] == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    next = get_token(list, ",", token, sizeof(token));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    while ( next != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if ( covered_by_list_item(token, cname, mname) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        next = get_token(next, ",", token, sizeof(token));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
/* Determines which class and methods we are interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *   cname              Class name, e.g. "java.lang.Object"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *   mname              Method name, e.g. "<init>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *   include_list       Empty or an explicit list for inclusion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *   exclude_list       Empty or an explicit list for exclusion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *  Returns 1(true) or 0(false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
interested(char *cname, char *mname, char *include_list, char *exclude_list)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    if ( exclude_list!=NULL && exclude_list[0]!=0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            covered_by_list(exclude_list, cname, mname) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    if ( include_list!=NULL && include_list[0]!=0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            !covered_by_list(include_list, cname, mname) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
/* Generic JVMTI utility functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
/* Every JVMTI interface returns an error code, which should be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *   to avoid any cascading errors down the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *   The interface GetErrorName() returns the actual enumeration constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *   name, making the error messages much easier to understand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
check_jvmti_error(jvmtiEnv *jvmti, jvmtiError errnum, const char *str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    if ( errnum != JVMTI_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        char       *errnum_str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        errnum_str = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        (void)(*jvmti)->GetErrorName(jvmti, errnum, &errnum_str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        fatal_error("ERROR: JVMTI: %d(%s): %s\n", errnum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                (errnum_str==NULL?"Unknown":errnum_str),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                (str==NULL?"":str));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
/* All memory allocated by JVMTI must be freed by the JVMTI Deallocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *   interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
deallocate(jvmtiEnv *jvmti, void *ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    error = (*jvmti)->Deallocate(jvmti, ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    check_jvmti_error(jvmti, error, "Cannot deallocate memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
/* Allocation of JVMTI managed memory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
allocate(jvmtiEnv *jvmti, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    void      *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    error = (*jvmti)->Allocate(jvmti, len, (unsigned char **)&ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    check_jvmti_error(jvmti, error, "Cannot allocate memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    return ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
/* Add demo jar file to boot class path (the BCI Tracker class must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *     in the boot classpath)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *   WARNING: This code assumes that the jar file can be found at one of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *              ${JAVA_HOME}/demo/jvmti/${DEMO_NAME}/${DEMO_NAME}.jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 *              ${JAVA_HOME}/../demo/jvmti/${DEMO_NAME}/${DEMO_NAME}.jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *            where JAVA_HOME may refer to the jre directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *            Both these values are added to the boot classpath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *            These locations are only true for these demos, installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *            in the JDK area. Platform specific code could be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *            find the location of the DLL or .so library, and construct a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *            path name to the jar file, relative to the library location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
add_demo_jar_to_bootclasspath(jvmtiEnv *jvmti, char *demo_name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    char      *file_sep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    int        max_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    char      *java_home;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    char       jar_path[FILENAME_MAX+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    java_home = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    error = (*jvmti)->GetSystemProperty(jvmti, "java.home", &java_home);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    check_jvmti_error(jvmti, error, "Cannot get java.home property value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    if ( java_home == NULL || java_home[0] == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        fatal_error("ERROR: Java home not found\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
#ifdef WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    file_sep = "\\";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    file_sep = "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    max_len = (int)(strlen(java_home) + strlen(demo_name)*2 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                         strlen(file_sep)*5 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                         16 /* ".." "demo" "jvmti" ".jar" NULL */ );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    if ( max_len > (int)sizeof(jar_path) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        fatal_error("ERROR: Path to jar file too long\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    (void)strcpy(jar_path, java_home);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    (void)strcat(jar_path, file_sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    (void)strcat(jar_path, "demo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    (void)strcat(jar_path, file_sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    (void)strcat(jar_path, "jvmti");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    (void)strcat(jar_path, file_sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    (void)strcat(jar_path, demo_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    (void)strcat(jar_path, file_sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    (void)strcat(jar_path, demo_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    (void)strcat(jar_path, ".jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    error = (*jvmti)->AddToBootstrapClassLoaderSearch(jvmti, (const char*)jar_path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    check_jvmti_error(jvmti, error, "Cannot add to boot classpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    (void)strcpy(jar_path, java_home);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    (void)strcat(jar_path, file_sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    (void)strcat(jar_path, "..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    (void)strcat(jar_path, file_sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    (void)strcat(jar_path, "demo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    (void)strcat(jar_path, file_sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    (void)strcat(jar_path, "jvmti");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    (void)strcat(jar_path, file_sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    (void)strcat(jar_path, demo_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    (void)strcat(jar_path, file_sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    (void)strcat(jar_path, demo_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    (void)strcat(jar_path, ".jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    error = (*jvmti)->AddToBootstrapClassLoaderSearch(jvmti, (const char*)jar_path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    check_jvmti_error(jvmti, error, "Cannot add to boot classpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
/* ------------------------------------------------------------------- */