3

我的代码是这样的:

mod_fd = open(mod_filename, O_RDONLY);
dwarf = dwarf_begin(mod_fd, DWARF_C_READ);

if (dwarf_next_unit(dwarf, 0,
                        &nextcu, &offset,
                        &version, &abbroffset,
                        &addrsize, &offsize,
                        NULL, NULL)) {
    return -ENOENT;
}

/* compile unit */
if (!dwarf_offdie(dwarf, offset, &die_of_cu)) {
    return -ENOENT;
}

if(dwarf_attr(die, DW_AT_name, &attr)) {

et_trace(CONFIG_ET_TRACE,
             "0x%lx name form: 0x%x: %p 0x%x, %s\n",
             dwarf_dieoffset(die),
             attr.form,
             attr.valp,
             *(int*)attr.valp,
             dwarf_formstring(&attr));

    switch(attr.form) {
        case DW_FORM_indirect://0x16
        case DW_FORM_strp:    //0xe
        case DW_FORM_string:  //0x8
            return dwarf_formstring(&attr);
        default:
            my_assert(0);
            break;
    }
}

如果mod_filename是用户模式应用程序,那么一切顺利,它将获得:

0xb name form: 0x8: 0x7f9753f9f2fe 0x6f727473, strobj.c
0x172 name form: 0x8: 0x7f9753f9f458 0x6f727473, strobj_t
0x183 name form: 0x8: 0x7f9753f9f469 0x66667562, buff

但是,如果它mod_filename是一个内核模块(即*.ko,或者*.o在构建内核模块时生成的),那么它不起作用,它将得到:

0xb name form: 0xe: 0x7fa5b8bc94c1 0x0, __kernel_size_t
0x4b name form: 0xe: 0x7fa5b8bc94fc 0x0, __kernel_size_t
0x123 name form: 0xe: 0x7fa5b8bc95d4 0x0, __kernel_size_t
0x755 name form: 0xe: 0x7fa5b8bc9c06 0x0, __kernel_size_t

在第二种情况下,*(int*)attr.valp总是为零,这是不正确的,但我不知道原因。

有人知道吗?帮我。谢谢。

4

0 回答 0