diff --git a/node.gyp b/node.gyp index 597df9d1262cc5..b1ab2ab6be232e 100644 --- a/node.gyp +++ b/node.gyp @@ -231,6 +231,7 @@ 'src/node_revert.h', 'src/node_i18n.h', 'src/pipe_wrap.h', + 'src/print.h', 'src/tty_wrap.h', 'src/tcp_wrap.h', 'src/udp_wrap.h', diff --git a/src/backtrace_posix.cc b/src/backtrace_posix.cc index 8fd798757a544a..5aad9c66b219a6 100644 --- a/src/backtrace_posix.cc +++ b/src/backtrace_posix.cc @@ -1,4 +1,5 @@ #include "node.h" +#include "print.h" #if defined(__linux__) #include @@ -30,21 +31,21 @@ void DumpBacktrace(FILE* fp) { } for (int i = 1; i < size; i += 1) { void* frame = frames[i]; - fprintf(fp, "%2d: ", i); + FPrintF(fp, "%2d: ", i); Dl_info info; const bool have_info = dladdr(frame, &info); if (!have_info || info.dli_sname == nullptr) { - fprintf(fp, "%p", frame); + FPrintF(fp, "%p", frame); } else if (char* demangled = abi::__cxa_demangle(info.dli_sname, 0, 0, 0)) { - fprintf(fp, "%s", demangled); + FPrintF(fp, "%s", demangled); free(demangled); } else { - fprintf(fp, "%s", info.dli_sname); + FPrintF(fp, "%s", info.dli_sname); } if (have_info && info.dli_fname != nullptr) { - fprintf(fp, " [%s]", info.dli_fname); + FPrintF(fp, " [%s]", info.dli_fname); } - fprintf(fp, "\n"); + FPrintF(fp, "\n"); } #endif // HAVE_EXECINFO_H } diff --git a/src/env.cc b/src/env.cc index 40f0c9ebd66a07..415bc449c00491 100644 --- a/src/env.cc +++ b/src/env.cc @@ -1,6 +1,7 @@ #include "env.h" #include "env-inl.h" #include "async-wrap.h" +#include "print.h" #include "v8.h" #include "v8-profiler.h" @@ -117,7 +118,7 @@ void Environment::PrintSyncTrace() const { Local stack = StackTrace::CurrentStackTrace(isolate(), 10, StackTrace::kDetailed); - fprintf(stderr, "(node:%d) WARNING: Detected use of sync API\n", getpid()); + FPrintF(stderr, "(node:%d) WARNING: Detected use of sync API\n", getpid()); for (int i = 0; i < stack->GetFrameCount() - 1; i++) { Local stack_frame = stack->GetFrame(i); @@ -128,9 +129,9 @@ void Environment::PrintSyncTrace() const { if (stack_frame->IsEval()) { if (stack_frame->GetScriptId() == Message::kNoScriptIdInfo) { - fprintf(stderr, " at [eval]:%i:%i\n", line_number, column); + FPrintF(stderr, " at [eval]:%i:%i\n", line_number, column); } else { - fprintf(stderr, + FPrintF(stderr, " at [eval] (%s:%i:%i)\n", *script_name, line_number, @@ -140,9 +141,9 @@ void Environment::PrintSyncTrace() const { } if (fn_name_s.length() == 0) { - fprintf(stderr, " at %s:%i:%i\n", *script_name, line_number, column); + FPrintF(stderr, " at %s:%i:%i\n", *script_name, line_number, column); } else { - fprintf(stderr, + FPrintF(stderr, " at %s (%s:%i:%i)\n", *fn_name_s, *script_name, @@ -150,7 +151,7 @@ void Environment::PrintSyncTrace() const { column); } } - fflush(stderr); + FFlush(stderr); } } // namespace node diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index 34ba5a7fc9d4be..5251e298f713b5 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -7,6 +7,7 @@ #include "node_crypto.h" #include "node_mutex.h" #include "node_version.h" +#include "print.h" #include "v8-inspector.h" #include "v8-platform.h" #include "util.h" @@ -468,8 +469,8 @@ void AgentImpl::WaitForDisconnect() { if (state_ == State::kConnected) { shutting_down_ = true; Write(TransportAction::kStop, 0, StringView()); - fprintf(stderr, "Waiting for the debugger to disconnect...\n"); - fflush(stderr); + FPrintF(stderr, "Waiting for the debugger to disconnect...\n"); + FFlush(stderr); inspector_->runMessageLoopOnPause(0); } } @@ -679,7 +680,7 @@ void AgentImpl::DispatchMessages() { CHECK_EQ(State::kAccepting, state_); session_id_ = std::get<1>(task); state_ = State::kConnected; - fprintf(stderr, "Debugger attached.\n"); + FPrintF(stderr, "Debugger attached.\n"); inspector_->connectFrontend(); break; case InspectorAction::kEndSession: diff --git a/src/inspector_socket.cc b/src/inspector_socket.cc index 6edc08248b6a85..a77a8e81c34785 100644 --- a/src/inspector_socket.cc +++ b/src/inspector_socket.cc @@ -1,4 +1,5 @@ #include "inspector_socket.h" +#include "print.h" #include "util.h" #include "util-inl.h" @@ -36,18 +37,18 @@ static void dump_hex(const char* buf, size_t len) { while (ptr < end) { cptr = ptr; for (i = 0; i < 16 && ptr < end; i++) { - printf("%2.2X ", static_cast(*(ptr++))); + PrintF("%2.2X ", static_cast(*(ptr++))); } for (i = 72 - (i * 4); i > 0; i--) { - printf(" "); + PrintF(" "); } for (i = 0; i < 16 && cptr < end; i++) { c = *(cptr++); - printf("%c", (c > 0x19) ? c : '.'); + PrintF("%c", (c > 0x19) ? c : '.'); } - printf("\n"); + PrintF("\n"); } - printf("\n\n"); + PrintF("\n\n"); } #endif @@ -101,7 +102,7 @@ static int write_to_client(InspectorSocket* inspector, size_t len, uv_write_cb write_cb = write_request_cleanup) { #if DUMP_WRITES - printf("%s (%ld bytes):\n", __FUNCTION__, len); + PrintF("%s (%ld bytes):\n", __FUNCTION__, len); dump_hex(msg, len); #endif @@ -337,7 +338,7 @@ static void websockets_data_cb(uv_stream_t* stream, ssize_t nread, } } else { #if DUMP_READS - printf("%s read %ld bytes\n", __FUNCTION__, nread); + PrintF("%s read %ld bytes\n", __FUNCTION__, nread); if (nread > 0) { dump_hex(inspector->buffer.data() + inspector->buffer.size() - nread, nread); @@ -513,10 +514,10 @@ static void data_received_cb(uv_stream_s* client, ssize_t nread, const uv_buf_t* buf) { #if DUMP_READS if (nread >= 0) { - printf("%s (%ld bytes)\n", __FUNCTION__, nread); + PrintF("%s (%ld bytes)\n", __FUNCTION__, nread); dump_hex(buf->base, nread); } else { - printf("[%s:%d] %s\n", __FUNCTION__, __LINE__, uv_err_name(nread)); + PrintF("[%s:%d] %s\n", __FUNCTION__, __LINE__, uv_err_name(nread)); } #endif InspectorSocket* inspector = inspector_from_stream(client); diff --git a/src/inspector_socket_server.cc b/src/inspector_socket_server.cc index 1c8fa70b65a3b3..a63adc92c368d4 100644 --- a/src/inspector_socket_server.cc +++ b/src/inspector_socket_server.cc @@ -1,6 +1,7 @@ #include "inspector_socket_server.h" #include "node.h" +#include "print.h" #include "uv.h" #include "zlib.h" @@ -81,22 +82,22 @@ void PrintDebuggerReadyMessage(const std::string& host, if (out == NULL) { return; } - fprintf(out, + FPrintF(out, "Debugger listening on port %d.\n" "Warning: This is an experimental feature " "and could change at any time.\n", port); if (ids.size() == 1) - fprintf(out, "To start debugging, open the following URL in Chrome:\n"); + FPrintF(out, "To start debugging, open the following URL in Chrome:\n"); if (ids.size() > 1) - fprintf(out, "To start debugging, open the following URLs in Chrome:\n"); + FPrintF(out, "To start debugging, open the following URLs in Chrome:\n"); for (const std::string& id : ids) { - fprintf(out, + FPrintF(out, " chrome-devtools://devtools/bundled/inspector.html?" "experiments=true&v8only=true&ws=%s\n", GetWsUrl(host, port, id).c_str()); } - fflush(out); + FFlush(out); } void SendHttpResponse(InspectorSocket* socket, const std::string& response) { @@ -395,9 +396,9 @@ bool InspectorSocketServer::Start(uv_loop_t* loop) { } if (err != 0 && connected_sessions_.empty()) { if (out_ != NULL) { - fprintf(out_, "Starting inspector on %s:%d failed: %s\n", + FPrintF(out_, "Starting inspector on %s:%d failed: %s\n", host_.c_str(), port_, uv_strerror(err)); - fflush(out_); + FFlush(out_); } uv_close(reinterpret_cast(&server_), nullptr); return false; diff --git a/src/node.cc b/src/node.cc index 77e6a5826ee957..07ac3b16de132b 100644 --- a/src/node.cc +++ b/src/node.cc @@ -29,6 +29,7 @@ #include "node_internals.h" #include "node_revert.h" #include "node_debug_options.h" +#include "print.h" #if defined HAVE_PERFCTR #include "node_counters.h" @@ -271,7 +272,7 @@ static struct { } void StartTracingAgent() { - fprintf(stderr, "Node compiled with NODE_USE_V8_PLATFORM=0, " + FPrintF(stderr, "Node compiled with NODE_USE_V8_PLATFORM=0, " "so event tracing is not available.\n"); } void StopTracingAgent() {} @@ -293,7 +294,7 @@ static void PrintErrorString(const char* format, ...) { if (stderr_handle == INVALID_HANDLE_VALUE || stderr_handle == nullptr || uv_guess_handle(_fileno(stderr)) != UV_TTY) { - vfprintf(stderr, format, ap); + VFPrintF(stderr, format, ap); va_end(ap); return; } @@ -313,7 +314,7 @@ static void PrintErrorString(const char* format, ...) { CHECK_GT(n, 0); WriteConsoleW(stderr_handle, wbuf.data(), n - 1, nullptr, nullptr); #else - vfprintf(stderr, format, ap); + VFPrintF(stderr, format, ap); #endif va_end(ap); } @@ -1118,7 +1119,7 @@ void SetupDomainUse(const FunctionCallbackInfo& args) { process_object->Get(tick_callback_function_key).As(); if (!tick_callback_function->IsFunction()) { - fprintf(stderr, "process._tickDomainCallback assigned to non-function\n"); + FPrintF(stderr, "process._tickDomainCallback assigned to non-function\n"); ABORT(); } @@ -1714,7 +1715,7 @@ static void ReportException(Environment* env, } } - fflush(stderr); + FFlush(stderr); } @@ -1815,7 +1816,7 @@ void GetActiveHandles(const FunctionCallbackInfo& args) { NO_RETURN void Abort() { DumpBacktrace(stderr); - fflush(stderr); + FFlush(stderr); ABORT_NO_BACKTRACE(); } @@ -1836,10 +1837,10 @@ NO_RETURN void Assert(const char* const (*args)[4]) { snprintf(pid, sizeof(pid), "[%u]", getpid()); #endif - fprintf(stderr, "%s%s: %s:%s:%s%s Assertion `%s' failed.\n", + FPrintF(stderr, "%s%s: %s:%s:%s%s Assertion `%s' failed.\n", exepath, pid, filename, linenum, function, *function ? ":" : "", message); - fflush(stderr); + FFlush(stderr); Abort(); } @@ -2513,7 +2514,7 @@ static void OnFatalError(const char* location, const char* message) { } else { PrintErrorString("FATAL ERROR: %s\n", message); } - fflush(stderr); + FFlush(stderr); ABORT(); } @@ -3444,7 +3445,7 @@ static void RawDebug(const FunctionCallbackInfo& args) { "must be called with a single string"); node::Utf8Value message(args.GetIsolate(), args[0]); PrintErrorString("%s\n", *message); - fflush(stderr); + FFlush(stderr); } @@ -3515,7 +3516,7 @@ void LoadEnvironment(Environment* env) { static void PrintHelp() { // XXX: If you add an option here, please also add it to doc/node.1 and // doc/api/cli.md - printf("Usage: node [options] [ -e script | script.js ] [arguments]\n" + PrintF("Usage: node [options] [ -e script | script.js ] [arguments]\n" " node debug script.js [arguments]\n" "\n" "Options:\n" @@ -3663,7 +3664,7 @@ static void ParseArgs(int* argc, if (debug_options.ParseOption(arg)) { // Done, consumed by DebugOptions::ParseOption(). } else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) { - printf("%s\n", NODE_VERSION); + PrintF("%s\n", NODE_VERSION); exit(0); } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) { PrintHelp(); @@ -3681,7 +3682,7 @@ static void ParseArgs(int* argc, args_consumed += 1; eval_string = argv[index + 1]; if (eval_string == nullptr) { - fprintf(stderr, "%s: %s requires an argument\n", argv[0], arg); + FPrintF(stderr, "%s: %s requires an argument\n", argv[0], arg); exit(9); } } else if ((index + 1 < nargs) && @@ -3698,7 +3699,7 @@ static void ParseArgs(int* argc, strcmp(arg, "-r") == 0) { const char* module = argv[index + 1]; if (module == nullptr) { - fprintf(stderr, "%s: %s requires an argument\n", argv[0], arg); + FPrintF(stderr, "%s: %s requires an argument\n", argv[0], arg); exit(9); } args_consumed += 1; @@ -3724,7 +3725,7 @@ static void ParseArgs(int* argc, } else if (strcmp(arg, "--trace-event-categories") == 0) { const char* categories = argv[index + 1]; if (categories == nullptr) { - fprintf(stderr, "%s: %s requires an argument\n", argv[0], arg); + FPrintF(stderr, "%s: %s requires an argument\n", argv[0], arg); exit(9); } args_consumed += 1; @@ -3792,7 +3793,7 @@ static void ParseArgs(int* argc, #if HAVE_OPENSSL if (use_openssl_ca && use_bundled_ca) { - fprintf(stderr, + FPrintF(stderr, "%s: either --use-openssl-ca or --use-bundled-ca can be used, " "not both\n", argv[0]); @@ -3846,9 +3847,9 @@ static void StartDebug(Environment* env, const char* path, DispatchMessagesDebugAgentCallback); debugger_running = env->debugger_agent()->Start(debug_options); if (debugger_running == false) { - fprintf(stderr, "Starting debugger on %s:%d failed\n", + FPrintF(stderr, "Starting debugger on %s:%d failed\n", debug_options.host_name().c_str(), debug_options.port()); - fflush(stderr); + FFlush(stderr); } } } @@ -3894,7 +3895,7 @@ static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle) { Mutex::ScopedLock scoped_lock(node_isolate_mutex); if (auto isolate = node_isolate) { if (debugger_running == false) { - fprintf(stderr, "Starting debugger agent.\n"); + FPrintF(stderr, "Starting debugger agent.\n"); HandleScope scope(isolate); Environment* env = Environment::GetCurrent(isolate); @@ -3983,8 +3984,8 @@ static int RegisterDebugSignalHandler() { CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr)); CHECK_EQ(0, pthread_attr_destroy(&attr)); if (err != 0) { - fprintf(stderr, "node[%d]: pthread_create: %s\n", getpid(), strerror(err)); - fflush(stderr); + FPrintF(stderr, "node[%d]: pthread_create: %s\n", getpid(), strerror(err)); + FFlush(stderr); // Leave SIGUSR1 blocked. We don't install a signal handler, // receiving the signal would terminate the process. return -err; @@ -4335,7 +4336,7 @@ void Init(int* argc, // Anything that's still in v8_argv is not a V8 or a node option. for (int i = 1; i < v8_argc; i++) { - fprintf(stderr, "%s: bad option: %s\n", argv[0], v8_argv[i]); + FPrintF(stderr, "%s: bad option: %s\n", argv[0], v8_argv[i]); } delete[] v8_argv; v8_argv = nullptr; @@ -4605,7 +4606,7 @@ int Start(int argc, char** argv) { v8_platform.Initialize(v8_thread_pool_size); // Enable tracing when argv has --trace-events-enabled. if (trace_enabled) { - fprintf(stderr, "Warning: Trace event is an experimental feature " + FPrintF(stderr, "Warning: Trace event is an experimental feature " "and could change at any time.\n"); v8_platform.StartTracingAgent(); } diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 45b06eaff50385..da7fc1d4bb9ac0 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -25,6 +25,7 @@ #include "node_crypto.h" #include "node_crypto_bio.h" #include "node_crypto_groups.h" +#include "print.h" #include "tls_wrap.h" // TLSWrap #include "async-wrap.h" @@ -6020,7 +6021,7 @@ void InitCryptoOnce() { CONF_MFLAGS_DEFAULT_SECTION); int err = ERR_get_error(); if (0 != err) { - fprintf(stderr, + FPrintF(stderr, "openssl config failed: %s\n", ERR_error_string(err, NULL)); CHECK_NE(err, 0); @@ -6043,7 +6044,7 @@ void InitCryptoOnce() { } } if (0 != err) { - fprintf(stderr, "openssl fips failed: %s\n", ERR_error_string(err, NULL)); + FPrintF(stderr, "openssl fips failed: %s\n", ERR_error_string(err, NULL)); UNREACHABLE(); } #endif // NODE_FIPS_MODE diff --git a/src/node_debug_options.cc b/src/node_debug_options.cc index 410e23acb3101b..ec9240ee8917a2 100644 --- a/src/node_debug_options.cc +++ b/src/node_debug_options.cc @@ -1,4 +1,5 @@ #include "node_debug_options.h" +#include "print.h" #include #include @@ -23,7 +24,7 @@ int parse_and_validate_port(const std::string& port) { errno = 0; const long result = strtol(port.c_str(), &endptr, 10); // NOLINT(runtime/int) if (errno != 0 || *endptr != '\0'|| result < 1024 || result > 65535) { - fprintf(stderr, "Debug port must be in range 1024 to 65535.\n"); + FPrintF(stderr, "Debug port must be in range 1024 to 65535.\n"); exit(12); } return static_cast(result); @@ -117,7 +118,7 @@ bool DebugOptions::ParseOption(const std::string& option) { #if HAVE_INSPECTOR inspector_enabled_ = true; #else - fprintf(stderr, + FPrintF(stderr, "Inspector support is not available with this Node.js build\n"); return false; #endif diff --git a/src/node_main.cc b/src/node_main.cc index 3194eb78cab130..f52e363c986912 100644 --- a/src/node_main.cc +++ b/src/node_main.cc @@ -20,6 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. #include "node.h" +#include "print.h" #ifdef _WIN32 #include @@ -27,7 +28,7 @@ int wmain(int argc, wchar_t *wargv[]) { if (!IsWindows7OrGreater()) { - fprintf(stderr, "This application is only supported on Windows 7, " + FPrintF(stderr, "This application is only supported on Windows 7, " "Windows Server 2008 R2, or higher."); exit(ERROR_EXE_MACHINE_TYPE_MISMATCH); } @@ -46,7 +47,7 @@ int wmain(int argc, wchar_t *wargv[]) { nullptr); if (size == 0) { // This should never happen. - fprintf(stderr, "Could not convert arguments to utf8."); + FPrintF(stderr, "Could not convert arguments to utf8."); exit(1); } // Do the actual conversion @@ -61,7 +62,7 @@ int wmain(int argc, wchar_t *wargv[]) { nullptr); if (result == 0) { // This should never happen. - fprintf(stderr, "Could not convert arguments to utf8."); + FPrintF(stderr, "Could not convert arguments to utf8."); exit(1); } } diff --git a/src/node_revert.cc b/src/node_revert.cc index 48f03a89a3131e..434cea561eca5c 100644 --- a/src/node_revert.cc +++ b/src/node_revert.cc @@ -1,4 +1,5 @@ #include "node_revert.h" +#include "print.h" #include #include @@ -18,7 +19,7 @@ const char* RevertMessage(const unsigned int cve) { void Revert(const unsigned int cve) { reverted |= 1 << cve; - printf("SECURITY WARNING: Reverting %s\n", RevertMessage(cve)); + PrintF("SECURITY WARNING: Reverting %s\n", RevertMessage(cve)); } void Revert(const char* cve) { @@ -31,7 +32,7 @@ void Revert(const char* cve) { } while (0); REVERSIONS(V) #undef V - printf("Error: Attempt to revert an unknown CVE [%s]\n", cve); + PrintF("Error: Attempt to revert an unknown CVE [%s]\n", cve); exit(12); } diff --git a/src/print.h b/src/print.h new file mode 100644 index 00000000000000..ff41b197c1668d --- /dev/null +++ b/src/print.h @@ -0,0 +1,14 @@ +#ifndef SRC_PRINT_H_ +#define SRC_PRINT_H_ + +#include + +// A minimal subset of print functions which are actually +// used in the code. To let the embedders to redefine them. + +#define PrintF printf +#define FPrintF fprintf +#define VFPrintF vfprintf +#define FFlush fflush + +#endif // SRC_PRINT_H_ diff --git a/src/signal_wrap.cc b/src/signal_wrap.cc index 55f1563438362f..99f0ca1adba751 100644 --- a/src/signal_wrap.cc +++ b/src/signal_wrap.cc @@ -24,6 +24,7 @@ #include "env.h" #include "env-inl.h" #include "handle_wrap.h" +#include "print.h" #include "util.h" #include "util-inl.h" #include "v8.h" @@ -89,7 +90,7 @@ class SignalWrap : public HandleWrap { if (signum == SIGPROF) { Environment* env = Environment::GetCurrent(args); if (env->inspector_agent()->IsStarted()) { - fprintf(stderr, "process.on(SIGPROF) is reserved while debugging\n"); + FPrintF(stderr, "process.on(SIGPROF) is reserved while debugging\n"); return; } }