I have an extremely simple program that does nothing more than call recvfrom()
in a loop. According to its manpage, one of the arguments is a pointer to the length of the address. This address is initialized in the .data
section to the integer value 16
. I noticed some strange behavior when I attach to the already-running process to trace it which is not present when I trace the process directly (when I start it traced). Scroll to the end of the lines:
# strace -x -s 10 -e trace=recvfrom ./test
recvfrom(3, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"..., 32, 0, {sa_family=AF_INET, sin_port=htons(42134), sin_addr=inet_addr("127.0.0.1")}, [16]) = 32
recvfrom(3, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"..., 32, 0, {sa_family=AF_INET, sin_port=htons(49442), sin_addr=inet_addr("127.0.0.1")}, [16]) = 32
recvfrom(3, ^Cstrace: Process 18909 detached
<detached ...>
# ./test &
# strace -x -s 10 -e trace=recvfrom -p $!
strace: Process 18916 attached
recvfrom(3, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"..., 32, 0, {sa_family=AF_INET, sin_port=htons(50906), sin_addr=inet_addr("127.0.0.1")}, [1999040176->16]) = 32
recvfrom(3, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"..., 32, 0, {sa_family=AF_INET, sin_port=htons(52956), sin_addr=inet_addr("127.0.0.1")}, [16]) = 32
recvfrom(3, ^Cstrace: Process 18916 detached
<detached ...>
When I trace it directly, the address length argument shows as [16]
, which makes sense. After all, the address is a pointer to an int
of the value 16
. However, when I attach to the process and trace it, the very first call shows that it is not initialized, e.g. [1999040176->16]
. This happens for the first syscall every time I attach, but all subsequent calls it shows it correctly as [16]
. If I detach from the process and re-attach, the first call will show it as having uninitialized memory.
To be brief:
When I run it under
strace
, the last argument shows[16]
for everyrecvfrom()
.When I attach to it when it is already running, the last argument shows things like
[1999040176->16]
in the first call torecvfrom()
, and[16]
in all subsequent ones.If I detach from it and attach again, the first call to
recvfrom()
again displays this odd behavior, and all subsequent calls display the expected[16]
.
The program itself is correct. Here is the program (written in MIPS assembly):
.section .text .global __start __start: # socket li $v0,4183 li $a0,2 li $a1,1 li $a2,0 syscall sw $v0,sockfd # bind li $v0,4169 lw $a0,sockfd la $a1,sockaddr_b li $a2,16 syscall loop: # recvfrom li $v0,4176 lw $a0,sockfd la $a1,buffer li $a2,32 li $a3,0 la $t0,sockaddr_a sw $t0,16($sp) la $t0,addrlen sw $t0,20($sp) syscall j loop .section .bss sockaddr_a: .space 16 buffer: .space 32 sockfd: .space 4 .section .data addrlen: .int 16 .section .rodata sockaddr_b: .hword 2,1234,0,0