コード例 #1
0
ファイル: thread.c プロジェクト: oscarlab/graphene
static int parse_thread_name (const char * name,
                              const char ** next, int * next_len,
                              const char ** nextnext)
{
    const char * p = name;
    int pid = 0;

    if (*p == '/')
        p++;

    if (strpartcmp_static(p, "self")) {
        p += static_strlen("self");
        if (*p && *p != '/')
            return -ENOENT;
        pid = get_cur_tid();
    } else {
        for ( ; *p && *p != '/' ; p++) {
            if (*p < '0' || *p > '9')
                return -ENOENT;

            pid = pid * 10 + *p - '0';
        }
    }

    if (next) {
        if (*(p++) == '/' && *p) {
            *next = p;

            if (next_len || nextnext)
                for ( ; *p && *p != '/' ; p++);

            if (next_len)
                *next_len = p - *next;

            if (nextnext)
                *nextnext = (*(p++) == '/' && *p) ? p : NULL;
        } else {
            *next = NULL;
        }
    }

    return pid;
}
コード例 #2
0
ファイル: db_rtld.c プロジェクト: brianmcgillion/graphene
void _DkDebugAddMap (struct link_map * map)
{
    const ElfW(Ehdr) * ehdr = (void *) map->l_map_start;
    int shdrsz = sizeof(ElfW(Shdr)) * ehdr->e_shnum;
    ElfW(Shdr) * shdr = NULL;
    ElfW(Phdr) * phdr = (void *) (map->l_map_start + ehdr->e_phoff);
    const ElfW(Phdr) * ph;

    int fd = ocall_open(map->l_name, O_RDONLY, 0);
    if (fd < 0)
        return;

    for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
        if (ph->p_type == PT_LOAD &&
            ehdr->e_shoff >= ph->p_offset &&
            ehdr->e_shoff < ph->p_offset + ph->p_filesz) {
            shdr = (void *) map->l_addr + ph->p_vaddr +
                (ehdr->e_shoff - ph->p_offset);
            break;
        }

    if (!shdr) {
        shdr = __alloca(shdrsz);
        unsigned long s = ALLOC_ALIGNDOWN(ehdr->e_shoff);
        unsigned long e = ALLOC_ALIGNUP(ehdr->e_shoff + shdrsz);
        void * umem;
        ocall_map_untrusted(fd, s, e - s, PROT_READ, &umem);
        memcpy(shdr, umem + ehdr->e_shoff - s, shdrsz);
        ocall_unmap_untrusted(umem, e - s);
    }

    ElfW(Shdr) * shdrend = (void *) shdr + shdrsz;
    int shstroff = shdr[ehdr->e_shstrndx].sh_offset;
    size_t shstrsz = shdr[ehdr->e_shstrndx].sh_size;
    const char * shstrtab = NULL;

    for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
        if (ph->p_type == PT_LOAD &&
            shstroff >= ph->p_offset &&
            shstroff < ph->p_offset + ph->p_filesz) {
            shstrtab = (void *) map->l_addr + ph->p_vaddr +
                (shstroff - ph->p_offset);
            break;
        }

    if (!shstrtab) {
        shstrtab = __alloca(shstrsz);
        unsigned long s = ALLOC_ALIGNDOWN(shstroff);
        unsigned long e = ALLOC_ALIGNUP(shstroff + shstrsz);
        void * umem;
        ocall_map_untrusted(fd, s, e - s, PROT_READ, &umem);
        memcpy((void *) shstrtab, umem + shstroff - s, shstrsz);
        ocall_unmap_untrusted(umem, e - s);
    }

    ocall_close(fd);

    ElfW(Addr) text_addr = 0;
    for (ElfW(Shdr) * s = shdr ; s < shdrend ; s++)
        if (strcmp_static(shstrtab + s->sh_name, ".text")) {
            text_addr = map->l_addr + s->sh_addr;
            break;
        }

    if (!text_addr)
        return;

#define BUFFER_LENGTH 4096

    char buffer[BUFFER_LENGTH], * ptr = buffer;

    snprintf(ptr, BUFFER_LENGTH - (ptr - buffer),
             "add-symbol-file %s 0x%016llx -readnow", map->l_name, text_addr);
    ptr += strlen(ptr);

    for (ElfW(Shdr) * s = shdr ; s < shdrend ; s++) {
        if (!s->sh_name || !s->sh_addr)
            continue;
        if (strcmp_static(shstrtab + s->sh_name, ".text"))
            continue;
        if (s->sh_type == SHT_NULL)
            continue;
        if (strpartcmp_static(shstrtab + s->sh_name, ".debug_"))
            continue;

        snprintf(ptr, BUFFER_LENGTH - (ptr - buffer),
                 " -s %s 0x%016llx", shstrtab + s->sh_name,
                 map->l_addr + s->sh_addr);
        ptr += strlen(ptr);
    }

    ocall_load_debug(buffer);
}