如何找程序用到的配置文件路径

发布时间 2023-04-08 15:33:36作者: zwlwf

如何找程序用到的配置文件路径

工作中遇到一个问题。gdb中打印了一个信息,

add 'set debuginfod enabled off' to .gdbinit

于是问题来了,这个.gdbinit文件在哪?怎么找到这个文件去添加配置呢?

想到学习的运行时打桩技术,心中便生出一计:对open函数 运行时打桩,将gdb运行过程中的open文件的过程都打印出来,看看有没有gdbinit相关的文件

实现

下面是桩代码(测试gdb时没有对fopen插桩)。

//a.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

int open(const char*fname, int flags, mode_t mode) {
  printf("zwlDebug: open file %s\n", fname);
  int (*realFopen)(const char*,int, mode_t);
  realFopen = dlsym(RTLD_NEXT, "open");
  if(NULL == realFopen) {
    char* error = dlerror();
    fputs(error, stdout);
    return 0;
  }

  int ans = realFopen(fname, flags, mode);
  return ans;
}

/*
FILE* fopen(const char*fname, const char*mode) {
  printf("zwlDebug: fopen file %s\n", fname);
  FILE* (*realFopen)(const char*, const char*);
  realFopen = dlsym(RTLD_NEXT, "fopen");
  if(NULL == realFopen) {
    char* error = dlerror();
    fputs(error, stdout);
    return NULL;
  }

  FILE* ans = realFopen(fname, mode);
  return ans;
}
*/

使用下面的命令将a.c编译成动态库,

libfun.so: a.c
        gcc -shared -fPIC a.c -o libfun.so -g

注意: libc中open函数,或者fopen函数都是个wrapper, 内部调用的__open@@GLIBC_2.2.5, 他们本身不存在直接的调用关系。所以这里open动态插桩捕捉不到对fopen的调用,要想对文件打开抓得全一点需要对他们两个进行动态打桩。

这样对于某程序a.out进行gdb调试,运行

LD_PRELOAD=./libfun.so gdb /path/to/a.out

观察到如下打印信息

$ LD_PRELOAD=./libfun.so gdb ./test/a.out
zwlDebug: open file /home/zwl/.inputrc
zwlDebug: open file /etc/inputrc
GNU gdb (Ubuntu 12.1-3ubuntu2) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
zwlDebug: open file /etc/gdb/gdbinit
zwlDebug: open file /home/zwl/.gdbinit
zwlDebug: open file ./test/a.out
zwlDebug: open file ./test/a.out
Reading symbols from ./test/a.out...
zwlDebug: open file /home/zwl/work/daily/2023-04-08/runtime_plugin/.gdb_history
(gdb) start
zwlDebug: open file /home/zwl/work/daily/2023-04-08/runtime_plugin/test/b.c
Temporary breakpoint 1 at 0x1131: file b.c, line 2.
Starting program: /home/zwl/work/daily/2023-04-08/runtime_plugin/test/a.out
zwlDebug: open file /dev/tty
zwlDebug: open file /proc/30433/task/30433/mem
zwlDebug: open file /proc/30433/auxv
zwlDebug: open file /proc/30433/auxv
zwlDebug: open file /proc/30433/task/30433/maps
zwlDebug: open file /lib64/ld-linux-x86-64.so.2
zwlDebug: open file /lib64/ld-linux-x86-64.so.2
zwlDebug: open file /usr/lib/debug/.build-id/29/2e105c0bb3ee8e8f5b917f8af764373d206659.debug
zwlDebug: open file /usr/lib/debug/.build-id/29/2e105c0bb3ee8e8f5b917f8af764373d206659.debug
zwlDebug: open file /usr/lib/debug/.build-id/fd/81b5781056b41b10f6244300e9928598e2df2e.debug
zwlDebug: open file ./libfun.so
zwlDebug: open file /lib/x86_64-linux-gnu/libc.so.6
zwlDebug: open file /usr/lib/debug/.build-id/a7/7da01528151092da49dc2e572f6dd8fe907d5d.debug
zwlDebug: open file /usr/lib/debug/.build-id/d1/704d25fbbb72fa95d517b883131828c0883fe9.debug
zwlDebug: open file /usr/lib/debug/.build-id/d1/704d25fbbb72fa95d517b883131828c0883fe9.debug
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Temporary breakpoint 1, main () at b.c:2
zwlDebug: open file /home/zwl/work/daily/2023-04-08/runtime_plugin/test/b.c
zwlDebug: open file /home/zwl/work/daily/2023-04-08/runtime_plugin/test/b.c
2        return 1;
(gdb) n
3       }
(gdb)
__libc_start_call_main (main=main@entry=0x555555555129 <main>, argc=argc@entry=1, argv=argv@entry=0x7fffffffe2d8) at ../sysdeps/nptl/libc_start_call_main.h:74
74      ../sysdeps/nptl/libc_start_call_main.h: No such file or directory.
(gdb)
[Inferior 1 (process 30433) exited with code 01]
(gdb) q

可以看到gdb运行过程中还是打开了非常多个文件,稍微看看还挺有意思。对于最开始的问题,相关的文件应该有/etc/gdb/gdbinit

/home/zwl/.gdbinit

利用这个桩,还可以看到bash启动过程中用到的文件,下面的各种bashrc都是我们可以添加配置的地方。

$ LD_PRELOAD=./libfun.so bash
zwlDebug: open file /dev/tty
zwlDebug: fopen file /lib/terminfo/x/xterm
zwlDebug: open file /etc/bash.bashrc
zwlDebug: open file /home/zwl/.bashrc
zwlDebug: open file /home/zwl/.bash_history
zwlDebug: open file /usr/share/bash-completion/bash_completion
zwlDebug: open file /etc/bash_completion.d/apport_completion
zwlDebug: open file /etc/bash_completion.d/git-prompt
zwlDebug: open file /usr/lib/git-core/git-sh-prompt
zwlDebug: open file /dev/null
zwlDebug: open file /home/zwl/.bash_history
zwlDebug: open file /home/zwl/.bash_history
zwlDebug: open file /home/zwl/.inputrc
zwlDebug: open file /etc/inputrc