对于使用相对路径调用的系统调用,如何在strace输出中获取当前工作目录?我正在尝试调试产生多个进程且无法打开特定文件的复杂应用程序。
stat("some_file", 0x7fff6b313df0) = -1 ENOENT (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
既然some_file存在,我相信它位于错误的目录中。我也尝试跟踪chdir调用,但是由于输出是交错的,因此很难以这种方式推断工作目录。有没有更好的办法?
Bre*_*tão -3
这很简单,使用函数 char *realpath(const char *path, char *resolved_path) 作为当前目录。
这是我的例子:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
char *abs;
abs = realpath(".", NULL);
printf("%s\n", abs);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
root@ubuntu1504:~/patches_power_spec# pwd
/root/patches_power_spec
root@ubuntu1504:~/patches_power_spec# ./a.out
/root/patches_power_spec
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
336 次 |
| 最近记录: |