Identifying file causing hang from strace
Asked Answered
B

3

5

I have a GTK program running on Ubuntu 10.04 that hangs in interruptible state, and I'd like to understand the output of strace. In particular, I have this line:

read(5, 0x2ba9ac4, 4096) = -1 EAGAIN (Resource temporarily unavailable)

I suspect 5 is the file descriptor, 0x2ba9ac4 the address in this file to be read, and 4096 the amount of data to read. Can you confirm? More importantly, how can one determine which file the program is trying to read? This file descriptor does not exist in /proc/pid/fd (which is probably why the program hangs).

Brnaby answered 4/3, 2011 at 13:52 Comment(0)
L
9

You can find which file uses this file descriptor by calling strace -o log -eopen,read yourprogram. Then search in the log file the call to read of interest. From this line (and not from the first line of the file), search upwards the first occurrence of this file descriptor (returned by a call to open).

For example here, the file descriptor returned by open is 3:

open("/etc/ld.so.cache", O_RDONLY)      = 3
Lambertson answered 7/3, 2011 at 12:47 Comment(2)
this pointed to /etc/passwd... can I be sure that it is correct, i.e., is there a chance that the file descriptor is modified in between by another function than open? thanksBrnaby
If it's the first open with this file descriptor above the faulty read, then yes, the file is that one. The file descriptor can be reused only if close has been called on it. If you want to be sure, use -eopen,read,close. A file descriptor is associated uniquely to a file between the corresponding open and close.Lambertson
B
0

The second argument to read() is simply the destination pointer, it's asking for a read from file descriptor 5, and max 4096 bytes. See the manual page for read().

Bouillon answered 4/3, 2011 at 13:54 Comment(1)
Yes, I figured it out, is there any way to identify the file associated to the file descriptor 5?Brnaby
M
0

Adding to @liberforce answer, if the process is already running you can get the file name using lsof

form strace

[pid  7529] read(102, 0x7fedc64c2fd0, 16) = -1 EAGAIN (Resource temporarily unavailable)

Now, with lsof

lsof -p 7529 | grep 102
java    7529 luis  102u  0000                0,9        0     9178 anon_inode
Monofilament answered 22/6, 2016 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.