src: always use diagnostic file sequence number

This commit attaches a sequence number to all filenames that
are automatically generated by DiagnosticFilename. This prevents
accidental overwriting of existing files.

PR-URL: https://github.com/nodejs/node/pull/27142
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
cjihrig 2019-04-08 17:09:53 -04:00
parent 44a3acb627
commit 547576f530
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
3 changed files with 11 additions and 17 deletions

View File

@ -334,15 +334,13 @@ class DiagnosticFilename {
DiagnosticFilename(Environment* env, DiagnosticFilename(Environment* env,
const char* prefix, const char* prefix,
const char* ext, const char* ext) :
int seq = -1) : filename_(MakeFilename(env->thread_id(), prefix, ext)) {}
filename_(MakeFilename(env->thread_id(), prefix, ext, seq)) {}
DiagnosticFilename(uint64_t thread_id, DiagnosticFilename(uint64_t thread_id,
const char* prefix, const char* prefix,
const char* ext, const char* ext) :
int seq = -1) : filename_(MakeFilename(thread_id, prefix, ext)) {}
filename_(MakeFilename(thread_id, prefix, ext, seq)) {}
const char* operator*() const { return filename_.c_str(); } const char* operator*() const { return filename_.c_str(); }
@ -350,8 +348,7 @@ class DiagnosticFilename {
static std::string MakeFilename( static std::string MakeFilename(
uint64_t thread_id, uint64_t thread_id,
const char* prefix, const char* prefix,
const char* ext, const char* ext);
int seq = -1);
std::string filename_; std::string filename_;
}; };

View File

@ -15,7 +15,6 @@
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
#include <cwctype> #include <cwctype>
#include <atomic>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <climits> // PATH_MAX #include <climits> // PATH_MAX
@ -73,9 +72,6 @@ static void PrintLoadedLibraries(JSONWriter* writer);
static void PrintComponentVersions(JSONWriter* writer); static void PrintComponentVersions(JSONWriter* writer);
static void PrintRelease(JSONWriter* writer); static void PrintRelease(JSONWriter* writer);
// Global variables
static std::atomic_int seq = {0}; // sequence number for report filenames
// External function to trigger a report, writing to file. // External function to trigger a report, writing to file.
// The 'name' parameter is in/out: an input filename is used // The 'name' parameter is in/out: an input filename is used
// if supplied, and the actual filename is returned. // if supplied, and the actual filename is returned.
@ -99,7 +95,7 @@ std::string TriggerNodeReport(Isolate* isolate,
filename = options->report_filename; filename = options->report_filename;
} else { } else {
filename = *DiagnosticFilename(env != nullptr ? env->thread_id() : 0, filename = *DiagnosticFilename(env != nullptr ? env->thread_id() : 0,
"report", "json", seq++); "report", "json");
} }
// Open the report file stream for writing. Supports stdout/err, // Open the report file stream for writing. Supports stdout/err,

View File

@ -41,10 +41,13 @@
#include <sys/types.h> #include <sys/types.h>
#endif #endif
#include <atomic>
#include <cstdio> #include <cstdio>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
static std::atomic_int seq = {0}; // Sequence number for diagnostic filenames.
namespace node { namespace node {
// Microseconds in a second, as a float. // Microseconds in a second, as a float.
@ -225,8 +228,7 @@ void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) {
std::string DiagnosticFilename::MakeFilename( std::string DiagnosticFilename::MakeFilename(
uint64_t thread_id, uint64_t thread_id,
const char* prefix, const char* prefix,
const char* ext, const char* ext) {
int seq) {
std::ostringstream oss; std::ostringstream oss;
TIME_TYPE tm_struct; TIME_TYPE tm_struct;
LocalTime(&tm_struct); LocalTime(&tm_struct);
@ -262,8 +264,7 @@ std::string DiagnosticFilename::MakeFilename(
#endif #endif
oss << "." << uv_os_getpid(); oss << "." << uv_os_getpid();
oss << "." << thread_id; oss << "." << thread_id;
if (seq >= 0) oss << "." << std::setfill('0') << std::setw(3) << ++seq;
oss << "." << std::setfill('0') << std::setw(3) << ++seq;
oss << "." << ext; oss << "." << ext;
return oss.str(); return oss.str();
} }