Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get cwd for relative paths?

Tags:

linux

strace

How can I get current working directory in strace output, for system calls that are being called with relative paths? I'm trying to debug complex application that spawns multiple processes and fails to open particular file.

stat("some_file", 0x7fff6b313df0) = -1 ENOENT (No such file or directory)

Since some_file exists I believe that its located in the wrong directory. I'd tried to trace chdir calls too, but since output is interleaved its hard to deduce working directory that way. Is there a better way?

like image 531
user1199933 Avatar asked Apr 08 '15 17:04

user1199933


1 Answers

You can use the -y option and it will print the full path. Another useful flag in this situation is -P which only traces syscalls relating to a specific path, e.g.

strace -y -P "some_file"

Unfortunately -y will only print the path of file descriptors, and since your call doesn't load any it doesn't have one. A possible workaround is to interrupt the process when that syscall is run in a debugger, then you can get its working directory by inspecting /proc/<PID>/cwd. Something like this (totally untested!)

gdb --args strace -P "some_file" -e inject=open:signal=SIGSEGV

Or you may be able to use a conditional breakpoint. Something like this should work, but I had difficulty with getting GDB to follow child processes after a fork. If you only have one process it should be fine I think.

gdb your_program
break open if $_streq((char*)$rdi, "some_file")
run
print getpid()
like image 103
Timmmm Avatar answered Nov 13 '22 23:11

Timmmm