0

I tried to modify Hostapd's code to read the nl_pid. After I modified code as below image and compiled it.

wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
       "handle %p", bss->nl_mgmt);
printf("%u", bss->nl_mgmt->s_local->nl_pid);

Compiler said "dereferencing pointer to incomplete type struct nl_sock".

../src/drivers/driver_nl80211.c: In function 'nl80211_mgmt_subscribe_ap':
../src/drivers/driver_nl80211.c:2449:30: error: dereferencing pointer to incomplete type 'struct nl_sock'
2449 |     printf("%u", bss->nl_mgmt->s_local->nl_pid);
     |     
make: *** [Makefile:1293: ../src/drivers/driver_nl80211.o] Error 1

So, I commented at line 2449 and recompiled again. Then using gdb to break at line 2451 and print 'bss->nl_mgmt variable'. gdb could know and read the structure. Why gdb could know and read the structure, but I couldn't? How could I solve this problem and read the 'nl_pid' I wanted?

Breakpoint 1, nl80211_mgmt_subscribe_ap (bss=0x4cdf60) 
    at ../src/drivers/driver_nl80211.c:2451 
2451        for (i = 0; i < ARRAY_SIZE(stypes); i++) { 
(gdb) print *bss->nl_mgmt 
$1 = {s_local = {nl_family = 16, nl_pad = 0, nl_pid = 2810203079, nl_groups = 0}, s_peer = {nl_family = 16, nl_pad = 0, nl_pid = 0, nl_groups = 0}, s_fd = 8, s_proto = 16, s_seq_next = 1578026322, s_seq_expect = 1578026322, s_flags = 0, s_cb = 0x4ce7b0, s_bufsize = 0}
I'm Shyan
  • 35
  • 1
  • 6

2 Answers2

1

The definition of struct nl_sock must be present in the .c you are compiling (or in an included file). The definition tells the compiler at what offset s_local is located, something it needs to know to compile that expression.

ikegami
  • 367,544
  • 15
  • 269
  • 518
0

struct nl_sock is not known to the compiler at this point in the code, but probably somewhere further it will find it. To compile it, you need to put the definition before the usage.

gdb runs on the executable, which was built by compiling the whole code, and somewhere obviously there is a definition of the structure - just to late for the line in question.

Simply find the definition, and move it up before the line in question.

Aganju
  • 6,295
  • 1
  • 12
  • 23
  • Re "*but probably somewhere further it will find it.*", Or not. It could be a linked file that tells `gdb` the structure of the `struct`. – ikegami Jan 03 '20 at 06:33
  • I used `grep -rnw -e "struct nl_sock {"` in both `libnl3` and `hostapd` folders, both of them didn't have definition. – I'm Shyan Jan 03 '20 at 13:04
  • I found the definition in `usr/src/debug/libnl3-3.4.0-8.fc30.x86_64/include/netlink-private/types.h`, it's not in ordinary libnl3 folder. – I'm Shyan Jan 03 '20 at 13:25