src/jdk.jdwp.agent/unix/native/libjdwp/linker_md.c
author bpb
Wed, 13 Mar 2019 07:16:57 -0700
changeset 54100 1a6a5a0260a7
parent 47216 71c04702a3d5
child 58092 b00b4706ec0f
permissions -rw-r--r--
6504660: HPI panic callback is dead code Reviewed-by: dcubed, cjplummer, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54100
1a6a5a0260a7 6504660: HPI panic callback is dead code
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Machine Dependent implementation of the dynamic linking support
54100
1a6a5a0260a7 6504660: HPI panic callback is dead code
bpb
parents: 47216
diff changeset
    28
 * for java.  This routine is Unix specific.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <dlfcn.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "path_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    39
#ifdef __APPLE__
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    40
#define LIB_SUFFIX "dylib"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    41
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    42
#define LIB_SUFFIX "so"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    43
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    44
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
    45
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
    46
                           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
    47
    char *path, *paths_copy, *next_token;
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
    48
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    49
    paths_copy = strdup(paths);
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    50
    if (paths_copy == NULL) {
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    51
        return;
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    52
    }
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
    53
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    54
    next_token = NULL;
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    55
    path = strtok_r(paths_copy, PATH_SEPARATOR, &next_token);
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    56
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    57
    while (path != NULL) {
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    58
        snprintf(buffer, buflen, "%s/lib%s." LIB_SUFFIX, path, fname);
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
    59
        if (access(buffer, F_OK) == 0) {
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
    60
            break;
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
    61
        }
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
    62
        *buffer = '\0';
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    63
        path = strtok_r(NULL, PATH_SEPARATOR, &next_token);
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
    64
    }
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    65
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    66
    free(paths_copy);
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
    67
}
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
    68
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * create a string for the JNI native function name by adding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * appropriate decorations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
dbgsysBuildFunName(char *name, int nameLen, int args_size, int encodingIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
{
54100
1a6a5a0260a7 6504660: HPI panic callback is dead code
bpb
parents: 47216
diff changeset
    76
    // On Unix, there is only one encoding method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    if (encodingIndex == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * create a string for the dynamic lib open call by adding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * appropriate pre and extensions to a filename and the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
void
54100
1a6a5a0260a7 6504660: HPI panic callback is dead code
bpb
parents: 47216
diff changeset
    87
dbgsysBuildLibName(char *holder, int holderlen, const char *pname,
1a6a5a0260a7 6504660: HPI panic callback is dead code
bpb
parents: 47216
diff changeset
    88
                   const char *fname)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    const int pnamelen = pname ? strlen(pname) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
    92
    *holder = '\0';
54100
1a6a5a0260a7 6504660: HPI panic callback is dead code
bpb
parents: 47216
diff changeset
    93
    // Quietly truncate on buffer overflow.  Should be an error.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    if (pnamelen == 0) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    99
        (void)snprintf(holder, holderlen, "lib%s." LIB_SUFFIX, fname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    } else {
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
   101
      dll_build_name(holder, holderlen, pname, fname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
dbgsysLoadLibrary(const char *name, char *err_buf, int err_buflen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    void * result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    result = dlopen(name, RTLD_LAZY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    if (result == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        (void)strncpy(err_buf, dlerror(), err_buflen-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        err_buf[err_buflen-1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
void dbgsysUnloadLibrary(void *handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    (void)dlclose(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
void * dbgsysFindLibraryEntry(void *handle, const char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
{
54100
1a6a5a0260a7 6504660: HPI panic callback is dead code
bpb
parents: 47216
diff changeset
   124
    return dlsym(handle, name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}