linux gdb调试时,使用p命令打印一个(void *)变量时会报错:
Attempt to dereference a generic pointer.
此时,只需要将(void *)变量强制类型转化成需要打印的类型,使用p命令即可打印。
例如,需要打印(int *)型entries变量,则使用命令格式:

(gdb) p *(int *)map->entries
$38 = 0

 

示例:

struct channel_map结构体:

/**
 * channel映射表, key为对应的socket描述字
 */
struct channel_map {
    void **entries;

    /* The number of entries available in entries */
    int nentries;
};

struct event_loop结构体

struct event_loop {
    int quit;
    const struct event_dispatcher *eventDispatcher;

    /** 对应的event_dispatcher的数据. */
    void *event_dispatcher_data;
    struct channel_map *channelMap;

    int is_handle_pending;
    struct channel_element *pending_head;
    struct channel_element *pending_tail;

    pthread_t owner_thread_id;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    int socketPair[2];
    char *thread_name;
};

使用(map)->entries的地方怎么在gdb中打印

int event_loop_handle_pending_add(struct event_loop *eventLoop, int fd, struct channel *channel) {
    yolanda_msgx("add channel fd == %d, %s", fd, eventLoop->thread_name);
    struct channel_map *map = eventLoop->channelMap;

    if (fd < 0)
        return 0;

    if (fd >= map->nentries) {
        if (map_make_space(map, fd, sizeof(struct channel *)) == -1)
            return (-1);
    }

    //第一次创建,增加
    if ((map)->entries[fd] == NULL) {
        map->entries[fd] = channel;
        //add channel
        struct event_dispatcher *eventDispatcher = eventLoop->eventDispatcher;
        eventDispatcher->add(eventLoop, channel);
        return 1;
    }

    return 0;
}

gdb打印方法:

(gdb) p *(struct channel *)map->entries[fd]
$43 = {fd = 10, events = 2, eventReadCallback = 0x402b3d <handle_connection_established>, eventWriteCallback = 0x0, data = 0x608890}

 

 

Logo

更多推荐