OLD | NEW |
(Empty) | |
| 1 Index: coregrind/m_main.c |
| 2 =================================================================== |
| 3 --- coregrind/m_main.c (revision 10399) |
| 4 +++ coregrind/m_main.c (working copy) |
| 5 @@ -755,15 +755,26 @@ |
| 6 If logging to file or a socket, write details of parent PID and |
| 7 command line args, to help people trying to interpret the |
| 8 results of a run which encompasses multiple processes. */ |
| 9 -static void print_preamble(Bool logging_to_fd, const char* toolname) |
| 10 + |
| 11 +// TODO(timurrrr): we add a non-static declaration of this function since |
| 12 +// we need it in coregrind/m_libcproc.c |
| 13 +// Should we move it to some header file? |
| 14 +void print_preamble(Bool logging_to_fd, const char* toolname); |
| 15 + |
| 16 +void print_preamble(Bool logging_to_fd, const char* toolname) |
| 17 { |
| 18 HChar* xpre = VG_(clo_xml) ? " <line>" : ""; |
| 19 HChar* xpost = VG_(clo_xml) ? "</line>" : ""; |
| 20 Int i; |
| 21 + static const char* last_toolname = NULL; |
| 22 |
| 23 vg_assert( VG_(args_for_client) ); |
| 24 vg_assert( VG_(args_for_valgrind) ); |
| 25 + // This way you may pass toolname == NULL provided the first invocation |
| 26 + // with toolname != NULL takes place in valgrind_main(). |
| 27 + toolname = (toolname == NULL ? last_toolname : toolname); |
| 28 vg_assert( toolname ); |
| 29 + last_toolname = toolname; |
| 30 |
| 31 if (VG_(clo_xml)) { |
| 32 VG_(message)(Vg_UserMsg, "<?xml version=\"1.0\"?>"); |
| 33 Index: coregrind/m_libcproc.c |
| 34 =================================================================== |
| 35 --- coregrind/m_libcproc.c (revision 10399) |
| 36 +++ coregrind/m_libcproc.c (working copy) |
| 37 @@ -33,9 +33,12 @@ |
| 38 #include "pub_core_vkiscnums.h" |
| 39 #include "pub_core_libcbase.h" |
| 40 #include "pub_core_libcassert.h" |
| 41 +#include "pub_core_libcfile.h" |
| 42 #include "pub_core_libcprint.h" |
| 43 #include "pub_core_libcproc.h" |
| 44 #include "pub_core_libcsignal.h" |
| 45 +#include "pub_core_tooliface.h" |
| 46 +#include "pub_core_options.h" |
| 47 #include "pub_core_seqmatch.h" |
| 48 #include "pub_core_mallocfree.h" |
| 49 #include "pub_core_syscall.h" |
| 50 @@ -703,10 +706,59 @@ |
| 51 (*atforks[i].parent)(tid); |
| 52 } |
| 53 |
| 54 +// Defined in m_main.c |
| 55 +void print_preamble(Bool logging_to_fd, const char* toolname); |
| 56 + |
| 57 +// If --log-file=ABC%pXYZ is specified, we'd like to have separate log files |
| 58 +// for each forked child. |
| 59 +// If %p is present in the --log-file option, this function creates |
| 60 +// a new log file and redirects the child's output to it. |
| 61 +static void open_new_logfile_for_forked_child(void) |
| 62 +{ |
| 63 + SysRes sres; |
| 64 + Int tmp_log_fd = -1; |
| 65 + Char *logfilename, *clo_log_name; |
| 66 + |
| 67 + clo_log_name = VG_(clo_log_name); |
| 68 + if (clo_log_name == NULL || !VG_(strstr)(clo_log_name, "%p")) { |
| 69 + // Don't create new log streams unless --log-file=ABC%pXYZ is specified. |
| 70 + return; |
| 71 + } |
| 72 + |
| 73 + logfilename = VG_(expand_file_name)("--log-file", clo_log_name); |
| 74 + sres = VG_(open) (logfilename, |
| 75 + VKI_O_CREAT | VKI_O_WRONLY | VKI_O_TRUNC, |
| 76 + VKI_S_IRUSR | VKI_S_IWUSR); |
| 77 + if (!sr_isError(sres)) { |
| 78 + tmp_log_fd = sr_Res(sres); |
| 79 + // Move log_fd into the safe range, |
| 80 + // so it doesn't conflict with any app fds. |
| 81 + tmp_log_fd = VG_(fcntl) (tmp_log_fd, VKI_F_DUPFD, VG_(fd_hard_limit)); |
| 82 + if (tmp_log_fd >= 0) { |
| 83 + VG_(clo_log_fd) = tmp_log_fd; |
| 84 + VG_(fcntl) (VG_(clo_log_fd), VKI_F_SETFD, VKI_FD_CLOEXEC); |
| 85 + } else { |
| 86 + VG_(message) (Vg_UserMsg, |
| 87 + "valgrind: failed to move logfile fd into safe range, " |
| 88 + "using stderr"); |
| 89 + VG_(clo_log_fd) = 2; // stderr |
| 90 + } |
| 91 + } else { |
| 92 + VG_(message) (Vg_UserMsg, |
| 93 + "Can't create log file '%s' (%s); giving up!", |
| 94 + logfilename, VG_(strerror) (sr_Err(sres))); |
| 95 + VG_(core_panic)("Error creating log file for child process"); |
| 96 + } |
| 97 + |
| 98 + print_preamble(False, NULL); |
| 99 +} |
| 100 + |
| 101 void VG_(do_atfork_child)(ThreadId tid) |
| 102 { |
| 103 Int i; |
| 104 |
| 105 + open_new_logfile_for_forked_child(); |
| 106 + |
| 107 for (i = 0; i < n_atfork; i++) |
| 108 if (atforks[i].child != NULL) |
| 109 (*atforks[i].child)(tid); |
OLD | NEW |