@@ -36,9 +36,17 @@ void RawWrite(const char *buffer) {
36
36
37
37
void ReportFile::ReopenIfNecessary () {
38
38
mu->CheckLocked ();
39
- if (fd == kStdoutFd || fd == kStderrFd ) return ;
40
-
41
39
uptr pid = internal_getpid ();
40
+ if (fallbackToStderrActive && fd_pid != pid) {
41
+ // If fallbackToStderrActive is set then we fellback to stderr. If this is a
42
+ // new process, mark fd as invalid so we attempt to open again.
43
+ CHECK_EQ (fd, kStderrFd );
44
+ fd = kInvalidFd ;
45
+ fallbackToStderrActive = false ;
46
+ }
47
+ if (fd == kStdoutFd || fd == kStderrFd )
48
+ return ;
49
+
42
50
// If in tracer, use the parent's file.
43
51
if (pid == stoptheworld_tracer_pid)
44
52
pid = stoptheworld_tracer_ppid;
@@ -48,8 +56,7 @@ void ReportFile::ReopenIfNecessary() {
48
56
// process, close it now.
49
57
if (fd_pid == pid)
50
58
return ;
51
- else
52
- CloseFile (fd);
59
+ CloseFile (fd);
53
60
}
54
61
55
62
const char *exe_name = GetProcessName ();
@@ -65,18 +72,24 @@ void ReportFile::ReopenIfNecessary() {
65
72
error_t err;
66
73
fd = OpenFile (full_path, WrOnly, &err);
67
74
if (fd == kInvalidFd ) {
68
- const char *ErrorMsgPrefix = " ERROR: Can't open file: " ;
75
+ bool fallback = common_flags ()->log_fallback_to_stderr ;
76
+ const char *ErrorMsgPrefix =
77
+ fallback ? " WARNING: Can't open file, falling back to stderr: "
78
+ : " ERROR: Can't open file: " ;
69
79
WriteToFile (kStderrFd , ErrorMsgPrefix, internal_strlen (ErrorMsgPrefix));
70
80
WriteToFile (kStderrFd , full_path, internal_strlen (full_path));
71
81
char errmsg[100 ];
72
82
internal_snprintf (errmsg, sizeof (errmsg), " (reason: %d)\n " , err);
73
83
WriteToFile (kStderrFd , errmsg, internal_strlen (errmsg));
74
- Die ();
84
+ if (!fallback)
85
+ Die ();
86
+ fallbackToStderrActive = true ;
87
+ fd = kStderrFd ;
75
88
}
76
89
fd_pid = pid;
77
90
}
78
91
79
- static void RecursiveCreateParentDirs (char *path) {
92
+ static void RecursiveCreateParentDirs (char *path, fd_t &fd ) {
80
93
if (path[0 ] == ' \0 ' )
81
94
return ;
82
95
for (int i = 1 ; path[i] != ' \0 ' ; ++i) {
@@ -85,12 +98,19 @@ static void RecursiveCreateParentDirs(char *path) {
85
98
continue ;
86
99
path[i] = ' \0 ' ;
87
100
if (!DirExists (path) && !CreateDir (path)) {
88
- const char *ErrorMsgPrefix = " ERROR: Can't create directory: " ;
101
+ bool fallback = common_flags ()->log_fallback_to_stderr ;
102
+ const char *ErrorMsgPrefix =
103
+ fallback ? " WARNING: Can't create directory, falling back to stderr: "
104
+ : " ERROR: Can't create directory: " ;
89
105
WriteToFile (kStderrFd , ErrorMsgPrefix, internal_strlen (ErrorMsgPrefix));
90
106
WriteToFile (kStderrFd , path, internal_strlen (path));
91
107
const char *ErrorMsgSuffix = " \n " ;
92
108
WriteToFile (kStderrFd , ErrorMsgSuffix, internal_strlen (ErrorMsgSuffix));
93
- Die ();
109
+ if (!fallback)
110
+ Die ();
111
+ path[i] = save;
112
+ fd = kStderrFd ;
113
+ return ;
94
114
}
95
115
path[i] = save;
96
116
}
@@ -164,12 +184,17 @@ void ReportFile::SetReportPath(const char *path) {
164
184
if (path) {
165
185
uptr len = internal_strlen (path);
166
186
if (len > sizeof (path_prefix) - 100 ) {
167
- const char *message = " ERROR: Path is too long: " ;
187
+ bool fallback = common_flags ()->log_fallback_to_stderr ;
188
+ const char *message =
189
+ fallback ? " WARNING: Path is too long, falling back to stderr: "
190
+ : " ERROR: Path is too long: " ;
168
191
WriteToFile (kStderrFd , message, internal_strlen (message));
169
192
WriteToFile (kStderrFd , path, 8 );
170
193
message = " ...\n " ;
171
194
WriteToFile (kStderrFd , message, internal_strlen (message));
172
- Die ();
195
+ if (!fallback)
196
+ Die ();
197
+ path = " stderr" ;
173
198
}
174
199
}
175
200
@@ -183,7 +208,7 @@ void ReportFile::SetReportPath(const char *path) {
183
208
fd = kStdoutFd ;
184
209
} else {
185
210
ParseAndSetPath (path, path_prefix, kMaxPathLength );
186
- RecursiveCreateParentDirs (path_prefix);
211
+ RecursiveCreateParentDirs (path_prefix, fd );
187
212
}
188
213
}
189
214
0 commit comments