<div dir="ltr">Hi, I have found a strange behavior using strace with an application that spawns threads. The output is printed two times and I cannot understand why. <div><br></div><div>Here it is the source code of the application : </div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br>#include <stdio.h><br>#include <sys/time.h><br>
#include <stdlib.h><br>#include <errno.h><br>#include <sys/syscall.h><br>#include <sys/types.h><br>#include <sched.h><br>#include <sched.h><br>#include <linux/sched.h><br>#include <unistd.h> <br>
#include <sys/wait.h><br>#include <string.h><br>#include <sys/mman.h><br>#define STACK_SIZE 0x10000<br>#define BUFSIZE 200<br>#define _GNU_SOURCE<br>void hello (void * a){<br> fprintf(stdout,"Hello word hope %ld\n", syscall(SYS_gettid)); <br>
_exit(0); <br>}<br><br>int main() <br>{<br>int res; <br>void *stack = mmap(0, STACK_SIZE, PROT_READ|PROT_WRITE,<br> MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);<br> <br>printf("Stack %p\n", stack + STACK_SIZE);<br>
memset(stack, 0, STACK_SIZE); <br>res=clone(hello,stack + STACK_SIZE, CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES);<br>printf("Clone result %x\n", res); <br>waitpid(-1, NULL, __WALL); <br>return 0; <br>}</blockquote>
<div><br></div><div>Here there is the output of strace -ff -o calls.txt ./clone.out </div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Clone result 6142<br>Clone result 6142 <br>Hello word hope 24898</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Stack 0x7f9bfb13f000<br></blockquote></div><div><br></div><div>The clone result should be printed just once. </div><div><br></div><div>System call sequences :</div><div><br></div><div><div>24897 execve("./clone.out", ["./clone.out"], [/* 58 vars */]) = 0</div>
<div>24897 brk(0) = 0x25b1000</div><div>24897 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9bfb13f000</div><div>24897 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)</div>
<div>24897 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3</div><div>24897 fstat(3, {st_mode=S_IFREG|0644, st_size=177209, ...}) = 0</div><div>24897 mmap(NULL, 177209, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f9bfb113000</div>
<div>24897 close(3) = 0</div><div>24897 open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3</div><div>24897 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`L\2\0\0\0\0\0"..., 832) = 832</div>
<div>24897 fstat(3, {st_mode=S_IFREG|0755, st_size=1736952, ...}) = 0</div><div>24897 mmap(NULL, 3849272, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f9bfab73000</div><div>24897 mprotect(0x7f9bfad15000, 2097152, PROT_NONE) = 0</div>
<div>24897 mmap(0x7f9bfaf15000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1a2000) = 0x7f9bfaf15000</div><div>24897 mmap(0x7f9bfaf1b000, 15416, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f9bfaf1b000</div>
<div>24897 close(3) = 0</div><div>24897 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9bfb112000</div><div>24897 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9bfb111000</div>
<div>24897 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9bfb110000</div><div>24897 arch_prctl(ARCH_SET_FS, 0x7f9bfb111700) = 0</div><div>24897 mprotect(0x7f9bfaf15000, 16384, PROT_READ) = 0</div>
<div>24897 mprotect(0x600000, 4096, PROT_READ) = 0</div><div>24897 mprotect(0x7f9bfb140000, 4096, PROT_READ) = 0</div><div>24897 munmap(0x7f9bfb113000, 177209) = 0</div><div>24897 mmap(NULL, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9bfb12f000</div>
<div>24897 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 3), ...}) = 0</div><div>24897 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9bfb12e000</div><div>24897 write(1, "Stack 0x7f9bfb13f000\n", 21) = 21</div>
<div>24897 clone(child_stack=0x7f9bfb13eff0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND) = 24898</div><div>24898 gettid( <unfinished ...></div><div>24897 write(1, "Clone result 6142\n", 18 <unfinished ...></div>
<div>24898 <... gettid resumed> ) = 24898</div><div>24897 <... write resumed> ) = 18</div><div>24898 write(1, "Clone result 6142\nHello word hop"..., 40 <unfinished ...></div>
<div>24897 wait4(-1, <unfinished ...></div><div>24898 <... write resumed> ) = 40</div><div>24898 exit_group(0) = ?</div><div>24898 +++ exited with 0 +++</div><div>24897 <... wait4 resumed> NULL, __WALL, NULL) = 24898</div>
<div>24897 exit_group(0) = ?</div><div>24897 +++ exited with 0 +++</div></div><div><br></div><div>I guess the problem is due to the unfinished system calls. </div><div><br></div><div>Could you help me to understand it ? </div>
<div><br></div><div><br></div><div><br></div><div> </div><div><br></div></div>