在进行 gdb 调试的时候,有时候,我们会遇到使用的符号表对应的源文件目录和实际机器上的源文件目录不一致的情况。
在这个时候,在 gdb 内输入layout src命令并不能显示出对应的源文件
我们可以通过 gdb 的set substitute-path /path/to/symbol/source/dir /path/to/actual/source/dir重定位源文件目录解决
rcvbuf.c#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/netlink.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
        int sockfd;
        int32_t buflen;
        size_t len = sizeof(buflen);
        int ret;
        sockfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_AUDIT);
        if (sockfd < 0) {
                fprintf(stderr, "create socket fail\n");
                return -1;
        }
        ret = getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &buflen, &len);
        if (ret < 0) {
                fprintf(stderr, "getsockopt fail\n");
        }
        printf("SO_RCVBUF is %u\n", buflen);
        printf("sizelen is %lu\n", len);
out:
        close(sockfd);
        return ret;
}
$ gcc -g -o rcvbuf rcvbuf.c
$ mv /path/to/current/rcvbuf.c /path/to/another/rcvbuf.c
$ gdb rcvbuf
(gdb) b main
(gdb) r
(gdb) layout src  // 这是看不到源文件
(gdb) set substitute-path /path/to/current /path/to/another  // 这是可以看到源文件
原文:https://www.cnblogs.com/sun-ye/p/15151813.html