Search code examples
clinuxdebuggingposixfile-descriptor

Extract file descriptors from fds_bits


I am debugging timing-dependent bug (mthe bug appears after months of execution, as far as I know). I suspect that the problem is a call to pselect() with a set of file descriptors which contains only "bad/broken" ones.

What I want to do

In order to reproduce the error, I want to run the program and delete the file descriptors it is using (apart from 0, 1 and 2). To achieve this, I want to go to /proc/<pid>/fd and unlink file descrpitors while the program is running.

The problem

When I debug the program with gdb, the set of file descriptors given to pselect() appears as following:

{fds_bits = {16384, 8, 0 <repeats 30 times>}}

Is there any way to obtain the file descriptors from the fds_bits? Is there any other way to achieve what I want to do?


Solution

  • What the macros FD_ISSET(nr, set) etc. do is basically to interpret the array fds_bits as a large bitfield. Bit n represents filedescriptor n.

    So in the first array member 16384 (binary 100000000000000) bit nr 14 is set, which represents filedescriptor 14.

    In the second array-member 8 (binary 1000) is set, which is the 4th bit. The array members are of type unsigned long int, so on a 64-bit-machine, the least significant bit of fdbits[1] represents filedescriptor 64 and the set bit therefore stands for filedescriptor 68. On a 32-bit machine this would be fd 36 (32 + 4).

    Together, the fd-set you are inspecting therefore represents the two filedescriptors 14 and 68.