10 static int (*fputs2) (const char *s, FILE *stream) = NULL;
11 static size_t (*fwrite2) (const void *ptr, size_t size, size_t nmemb,
15 static void __attribute__((constructor))
18 if (getenv ("WRITEW_IS_GDB"))
21 unsetenv ("LD_PRELOAD");
27 setenv ("WRITEW_IS_GDB", "1", 1);
29 fputs2 = dlsym (RTLD_NEXT, "fputs");
30 assert (fputs2 != NULL);
31 fwrite2 = dlsym (RTLD_NEXT, "fwrite");
32 assert (fwrite2 != NULL);
36 pid_is_sleeping (pid_t pid)
42 snprintf (buf, sizeof (buf), "/proc/%d/status", (int) pid);
43 status_file = fopen (buf, "r");
44 if (status_file != NULL)
48 while (fgets (buf, sizeof (buf), status_file))
50 if (strncmp (buf, "State:", 6) == 0)
56 if (have_state && strstr (buf, "S (sleeping)") != NULL)
66 pid_t expect_pid = getppid ();
69 usleep (1000 * 1000 / 100);
70 while (!pid_is_sleeping (expect_pid));
74 fputs (const char *s, FILE *stream)
76 int retval = fputs2 (s, stream);
78 if (is_gdb && fileno (stream) == STDOUT_FILENO && strchr (s, '\n'))
85 fwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)
87 size_t retval = fwrite2 (ptr, size, nmemb, stream);
89 if (is_gdb && fileno (stream) == STDOUT_FILENO
90 && memchr (ptr, '\n', size * nmemb))