[Gllug] file offset in running process

Robert McKay robert at mckay.com
Fri Aug 14 16:09:21 UTC 2009


On Fri, Aug 14, 2009 at 4:46 PM, <salsaman at xs4all.nl> wrote:

>
>
> /proc/$pid/fdinfo/$fd has a field called pos:
>
> if you grep for this maybe it will return what you are looking for.
>

If fdinfo is unavailable on your kernel you can easily get this information
by calling lseek() through gdb like this;

(contrived process that slowly reads from /etc/passwd)

# while true; do read line; sleep 100; done < /etc/passwd
[3] 18255

# ls -l /proc/18255/fd
total 0
lr-x------ 1 root root 64 Aug 14 17:07 0 -> /etc/passwd
lrwx------ 1 root root 64 Aug 14 17:07 1 -> /dev/pts/0
lrwx------ 1 root root 64 Aug 14 17:07 2 -> /dev/pts/0
lrwx------ 1 root root 64 Aug 14 17:07 255 -> /dev/pts/0

notice that /etc/passwd is fd = 0 (obviously since I'm just redirecting it
from the shell)

# gdb -p 18255
GNU gdb Red Hat Linux (6.5-37.el5rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Attaching to process 18255
Reading symbols from /bin/bash...(no debugging symbols found)...done.
Using host libthread_db library "/lib64/libthread_db.so.1".
Reading symbols from /lib64/libtermcap.so.2...(no debugging symbols
found)...done.
Loaded symbols for /lib64/libtermcap.so.2
Reading symbols from /lib64/libdl.so.2...
(no debugging symbols found)...done.
Loaded symbols for /lib64/libdl.so.2
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...
(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /usr/lib64/gconv/ISO8859-1.so...(no debugging symbols
found)...done.
Loaded symbols for /usr/lib64/gconv/ISO8859-1.so

0x000000328ea97f15 in waitpid () from /lib64/libc.so.6
(gdb) call lseek(0, 0, 1)
$1 = 63
(gdb) detach
Detaching from program: /bin/bash, process 18255
(gdb) quit

lseek parameters;

0 - stdin.. change this to whatever the fd you want to check is
0 - the amount to add to the offset - don't want to change it - just find
out what it is
1 - SEEK_CUR - this means that offset is from the current position


Rob.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/gllug/attachments/20090814/d4b92453/attachment.html>
-------------- next part --------------
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug


More information about the GLLUG mailing list