/* Build a machine dependent library name out of a path and file name.  */
void
md_build_library_name(char *holder, int holderlen, char *pname, char *fname)
{
    int n;
    int i;
    char  c;
    char  **pelements;
    const int pnamelen = pname ? (int)strlen(pname) : 0;
    c = (pnamelen > 0) ? pname[pnamelen-1] : 0;

    /* Quietly truncates on buffer overflow. Should be an error. */
    if (pnamelen + strlen(fname) + 10 > (unsigned int)holderlen) {
        *holder = '\0';
        return;
    }

    if (pnamelen == 0) {
        md_snprintf(holder, holderlen, "%s.dll", fname);
    } else if (c == ':' || c == '\\') {
        md_snprintf(holder, holderlen, "%s%s.dll", pname, fname);
    } else if (strchr(pname, PATH_SEPARATOR_CHAR) != NULL) {
        pelements = split_path(pname, &n);
        for (i = 0 ; i < n ; i++) {
            char* path = pelements[i];
            char lastchar;
            // really shouldn't be NULL but what the heck, check can't hurt
            size_t plen = (path == NULL) ? 0 : strlen(path);
            if (plen == 0) {
                continue; // skip empty path values
            }
            lastchar = path[plen - 1];
            if (lastchar == ':' || lastchar == '\\') {
                md_snprintf(holder, holderlen, "%s%s.dll", pelements[i], fname);
            } else {
                md_snprintf(holder, holderlen, "%s\\%s.dll", pelements[i], fname);
            }
            if (GetFileAttributes(holder) != INVALID_FILE_ATTRIBUTES) {
                break;
            }
        }
        // release the storage
        for (i = 0 ; i < n ; i++) {
           if (pelements[i] != NULL) {
              free(pelements[i]);
	   }
        }
        if (pelements != NULL) {
           free(pelements);
	}
    } else {
        md_snprintf(holder, holderlen, "%s\\%s.dll", pname, fname);
    }
}
Exemple #2
0
void
md_init(void)
{
#if defined(LINUX) || defined(_ALLBSD_SOURCE)
    /* No Hi-Res timer option? */
#else
    if ( gdata->micro_state_accounting ) {
        char proc_ctl_fn[48];
        int  procfd;

        /* Turn on micro state accounting, once per process */
        (void)md_snprintf(proc_ctl_fn, sizeof(proc_ctl_fn),
                "/proc/%d/ctl", md_getpid());

        procfd = open(proc_ctl_fn, O_WRONLY);
        if (procfd >= 0) {
            long ctl_op[2];

            ctl_op[0] = PCSET;
            ctl_op[1] = PR_MSACCT;
            (void)write(procfd, ctl_op, sizeof(ctl_op));
            (void)close(procfd);
        }
    }
#endif
}
Exemple #3
0
/* System error routine */
static void
system_error(const char *system_call, int rc, int errnum)
{
    char buf[256];
    char details[256];

    details[0] = 0;
    if ( errnum != 0 ) {
        md_system_error(details, (int)sizeof(details));
    } else if ( rc >= 0 ) {
        (void)strcpy(details,"Only part of buffer processed");
    }
    if ( details[0] == 0 ) {
        (void)strcpy(details,"Unknown system error condition");
    }
    (void)md_snprintf(buf, sizeof(buf), "System %s failed: %s\n",
                            system_call, details);
    HPROF_ERROR(JNI_TRUE, buf);
}
void 
md_get_prelude_path(char *path, int path_len, char *filename)
{
    char libdir[FILENAME_MAX+1];
    char *lastSlash;

    GetModuleFileName(hJavaInst, libdir, FILENAME_MAX);

    /* This is actually in the bin directory, so move above bin for lib */
    lastSlash = strrchr(libdir, '\\');
    if ( lastSlash != NULL ) {
	*lastSlash = '\0';
    }
    lastSlash = strrchr(libdir, '\\');
    if ( lastSlash != NULL ) {
	*lastSlash = '\0';
    }
    (void)md_snprintf(path, path_len, "%s\\lib\\%s", libdir, filename);
}
/* This function prints out a memory error for the memory function
 *   'name' which was called in file 'file' at line number 'line'.  The malloc
 *   pointer with the error is in 'mptr'.
 */
static void
memory_error(void *mptr, const char *name, int mid, const char *mfile, int mline, const char *file, int line)
{
    char  nice_words[512];
    char  temp[256];
    int   len;
    void *mptr_walk;

    if (name == NULL)
        name = "UNKNOWN_NAME";
    if (file == NULL)
        file = "UNKNOWN_FILE";
    md_system_error(temp, (int)sizeof(temp));
    (void)strcpy(nice_words, temp);
    if ( debug_check!=NULL ) {
       (void)md_snprintf(nice_words, sizeof(nice_words),
                    "%s The %s at %p appears to have been hit.",
                    temp, debug_check, clobbered_ptr);
    }
    len = -nsize1_(mptr);
    error_message("Error: "
                   "%s The malloc space #%d is at %p [user size=%d(0x%x)],"
                   " and was allocated from file \"%s\" at line %d."
                   " [The debug function %s() detected this error "
                   "in file \"%s\" at line %d.]",
                   nice_words, mid, mptr, len, len, mfile, mline,
                   name, file, line);

    /* Print out contents of this allocation */
    {
        int i;
        void *uptr = malloc2user_(mptr);
        char *pmess;
        pmess = temp;
        for(i=0;i<(int)sizeof(temp);i++) {
            int ch = ((unsigned char*)uptr)[i];
            if ( isprint(ch) ) {
                *pmess++ = ch;
            } else {
                *pmess++ = '\\';
                *pmess++ = 'x';
                (void)sprintf(pmess,"%02x",ch);
                pmess+=2;
            }
        }
        *pmess = 0;
        error_message("Error: %p contains user data: %s", uptr, temp);
    }

    /* Try and print out table */
    if (!malloc_watch) {
        return;
    }
    mptr_walk = first_warrant_mptr;
    if (mptr_walk != NULL) {
        error_message("Active allocations: "
           "count=%d, largest_size=%d, address range (%p,%p)",
                        id_counter, largest_size, smallest_addr, largest_addr);
        do {
            int size1;
            int size2;
            char *mfile_walk;

            if ( mptr_walk > largest_addr || mptr_walk < smallest_addr ) {
                error_message("Terminating list due to pointer corruption");
                break;
            }
            size1 = -nsize1_(mptr_walk);
            size2 = -nsize2_(mptr_walk);
            mfile_walk = MFILE(mptr_walk);
            error_message("#%d: addr=%p size1=%d size2=%d file=\"%.*s\" line=%d",
                MID(mptr_walk), mptr_walk, size1, size2,
                WARRANT_NAME_MAX, mfile_walk, MLINE(mptr_walk));
            if ( size1 != size2 || size1 > largest_size || size1 < 0 ) {
                error_message("Terminating list due to size corruption");
                break;
            }
            mptr_walk = warrant_link_(mptr_walk);
        } while (mptr_walk != NULL);
    }
    abort();
}