2 * Copyright (c) 2017 Intel Corporation
3 * Copyright (c) 2019 Paul Chaignon <paul.chaignon@gmail.com>
6 * SPDX-License-Identifier: LGPL-2.1-or-later
10 * open_memstream returns a FILE stream that allows writing to a
11 * dynamically growing buffer, that can be either copied to
12 * tcp->outf (syscall successful) or dropped (syscall failed)
17 struct staged_output_data {
20 FILE *real_outf; /* Backup for real outf while staging */
24 strace_open_memstream(struct tcb *tcp)
28 #if HAVE_OPEN_MEMSTREAM
29 tcp->staged_output_data = xmalloc(sizeof(*tcp->staged_output_data));
30 fp = open_memstream(&tcp->staged_output_data->memfptr,
31 &tcp->staged_output_data->memfloc);
33 perror_msg_and_die("open_memstream");
35 * Call to fflush required to update tcp->memfptr,
36 * see open_memstream man page.
40 /* Store the FILE pointer for later restauration. */
41 tcp->staged_output_data->real_outf = tcp->outf;
49 strace_close_memstream(struct tcb *tcp, bool publish)
51 #if HAVE_OPEN_MEMSTREAM
52 if (!tcp->staged_output_data) {
53 debug_msg("memstream already closed");
57 if (fclose(tcp->outf))
58 perror_msg("fclose(tcp->outf)");
60 tcp->outf = tcp->staged_output_data->real_outf;
61 if (tcp->staged_output_data->memfptr) {
63 fputs_unlocked(tcp->staged_output_data->memfptr,
66 debug_msg("syscall output dropped: %s",
67 tcp->staged_output_data->memfptr);
69 free(tcp->staged_output_data->memfptr);
70 tcp->staged_output_data->memfptr = NULL;
72 free(tcp->staged_output_data);
73 tcp->staged_output_data = NULL;