Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vEv58-003eIG-Tb for pgsql-hackers@arkaria.postgresql.org; Fri, 31 Oct 2025 19:46:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vEv57-0005Wf-9w for pgsql-hackers@arkaria.postgresql.org; Fri, 31 Oct 2025 19:46:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vEv56-0005Uv-9N for pgsql-hackers@lists.postgresql.org; Fri, 31 Oct 2025 19:46:40 +0000 Received: from fhigh-b4-smtp.messagingengine.com ([202.12.124.155]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1vEv53-004mr3-0I for pgsql-hackers@postgresql.org; Fri, 31 Oct 2025 19:46:38 +0000 Received: from phl-compute-06.internal (phl-compute-06.internal [10.202.2.46]) by mailfhigh.stl.internal (Postfix) with ESMTP id 2F1817A015B; Fri, 31 Oct 2025 15:46:36 -0400 (EDT) Received: from phl-imap-05 ([10.202.2.95]) by phl-compute-06.internal (MEProxy); Fri, 31 Oct 2025 15:46:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eulerto.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1761939996; x=1762026396; bh=fSS5RBw1MBaVKtMHgsZtnFNQxCdoTEoW2U3fs5YpDT4=; b= anF7K20bPEdAb6tATCKSlpN2sdsWoaqmlWdyYnVsDd1IMub6HCmkEruYUUxa00YP /TlHIWZj9v80GCzQcG6lW4KTLufiqwKSE/KcgggTW3/Hi3/ZecrlWnT0fN8svZ2Y 4BLTT7IjX63nJ4I+GWfhKuD2RIT6+7m6AgKXhCTyrArNdNp/9Pqg9EpE0kBhsx0J BvmsBkqVfNxGQf5IrtxZH3+g/NSxkbZ8m6w0u5fjvVaFXThHT9ta62gj2oBjtVcB 8MPgd1lUQsVjLjxMie9/ZCcLUDPv4pfd9QGWRpY74+EnM3kC47FSFkbPnki556mS A22XpADuG5OuHL9mTh6e4A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1761939996; x= 1762026396; bh=fSS5RBw1MBaVKtMHgsZtnFNQxCdoTEoW2U3fs5YpDT4=; b=3 BESb22XLRZpl24iIxQdcCPV2anjobwbhzOFsKlRxDi0kMB2eWLJfVneWi8tllQDW joDxIIxf91DBhL3MCz82mdfLv7kdTJ9xos+mZUzYqNB6W7Y/YDE0S9FtV/JqdSFI 11AwRs5bXTLN5cQKijfFMD2NjgEp5Uc4aURgJOvWP7IzCGlE1ZJLgwujV9Ti3eg5 VOsherqUDDWUitFWVf+6Spz/raBEqBgItthIEkTS6PkxQlT11cYhp3aXOdscmPdw Udr3gq1o/4tWvGlgIbODJO5dKQzwzZ3kRtltYO0OmOqtAMdYoQcWn2UC3kts8nUA n479VEM0egVzKeH56XGOA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddujedtfeejucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepofggfffhvfevkfgjfhfutgfgsehtjeertdertddtnecuhfhrohhmpedfgfhulhgv rhcuvfgrvhgvihhrrgdfuceovghulhgvrhesvghulhgvrhhtohdrtghomheqnecuggftrf grthhtvghrnhepffegvdfgieejtdejfedtfeevgffhuedtueeugfejveeltedujeejgedu hfelgedunecuffhomhgrihhnpehmihgtrhhoshhofhhtrdgtohhmpdhpohhsthhgrhgvsh hqlhdrohhrghdpvghnthgvrhhprhhishgvuggsrdgtohhmnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepvghulhgvrhesvghulhgvrhhtohdrtg homhdpnhgspghrtghpthhtohephedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohep jhgrkhhusgdrfigrrhhtrghksegvnhhtvghrphhrihhsvggusgdrtghomhdprhgtphhtth hopegusghrhigrnhdrghhrvggvnhesghhmrghilhdrtghomhdprhgtphhtthhopegrlhhv hhgvrhhrvgeskhhurhhilhgvmhhurdguvgdprhgtphhtthhopehmihgthhgrvghlsehprg hquhhivghrrdighiiipdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrshesphhoshht ghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i0c21471d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 4F44B1820054; Fri, 31 Oct 2025 15:46:35 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 X-ThreadId: AWPeZBJywbRF Date: Fri, 31 Oct 2025 16:46:14 -0300 From: "Euler Taveira" To: "Bryan Green" , "Jakub Wartak" Cc: =?UTF-8?Q?=C3=81lvaro_Herrera?= , "Michael Paquier" , pgsql-hackers Message-Id: <2241662b-da7d-428f-beba-a416ef48e9e9@app.fastmail.com> In-Reply-To: <30aea073-109c-43cc-979c-081b922d055d@gmail.com> References: <202510300932.isdvgkzqzshk@alvherre.pgsql> <5b4c6ce0-ed17-4abb-9127-859cc95cbdf3@gmail.com> <30aea073-109c-43cc-979c-081b922d055d@gmail.com> Subject: Re: [PATCH] Add Windows support for backtrace_functions (MSVC only) Content-Type: text/plain Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Oct 30, 2025, at 11:51 AM, Bryan Green wrote: > I had reservations about the value the tests were adding, and > considering I am getting more concern around having the tests than not > having them for this initial release I have decided to remove them. v4 > patch is attached. It is the same as the initial 0001-* patch. > I spent some time on this patch. Here are some comments and suggestions. +#ifdef _MSC_VER +#include +#include +static bool win32_backtrace_symbols_initialized = false; +static HANDLE win32_backtrace_process = NULL; +#endif We usually a different style. Headers go to the top on the same section as system headers and below postgres.h. It is generally kept sorted. The same applies to variables. Add them near the other static variables. BTW does it need the win32_ prefix for a Window-only variable? + wchar_t buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME * sizeof(wchar_t)]; + PSYMBOL_INFOW symbol; According to [1], SYMBOL_INFO is an alias that automatically selects ANSI vs UNICODE. Shouldn't we use it instead of SYMBOL_INFOW? + elog(WARNING, "SymInitialize failed with error %lu", error); Is there a reason to continue if SymInitialize failed? It should return after printing the message. Per the error message style guide [2], my suggestion is "could not initialize the symbol handler: error code %lu". You can also use GetLastError() directly in the elog call. + symbol = (PSYMBOL_INFOW) buffer; + symbol ->MaxNameLen = MAX_SYM_NAME; + symbol ->SizeOfStruct = sizeof(SYMBOL_INFOW); We generally don't add spaces between variable and a member. + DWORD i; I'm curious why did you declare this variable as DWORD? Shouldn't int be sufficient? The CaptureStackBackTrace function returns an unsigned short (UShort). You can also declare it in the for loop. + DWORD64 address = (DWORD64) (stack[i]); The parenthesis around stack is superfluous. The code usually doesn't contain additional parenthesis (unless it improves readability). + if (frames == 0) + { + appendStringInfoString(&errtrace, "\nNo stack frames captured"); + edata->backtrace = errtrace.data; + return; + } It seems CaptureStackBackTrace function may return zero frames under certain conditions. It is a good point having this additional message. I noticed that the current code path (HAVE_BACKTRACE_SYMBOLS) doesn't have this block. IIUC, in certain circumstances (ARM vs unwind-tables flag), the backtrace() also returns zero frames. Should we add this block for the backtrace() code path? + sym_result = SymFromAddrW(win32_backtrace_process, + address, + &displacement, + symbol); You should use SymFromAddr, no? [3] I saw that you used the Unicode functions instead of the generic functions [4]. + /* Convert symbol name to UTF-8 */ + utf8_len = WideCharToMultiByte(CP_UTF8, 0, symbol->Name, -1, + NULL, 0, NULL, NULL); + if (utf8_len > 0) + { + char *filename_utf8; + int filename_len; + + utf8_buffer = palloc(utf8_len); + WideCharToMultiByte(CP_UTF8, 0, symbol->Name, -1, + utf8_buffer, utf8_len, NULL, NULL); + /* Convert symbol name to UTF-8 */ + utf8_len = WideCharToMultiByte(CP_UTF8, 0, symbol->Name, -1, + NULL, 0, NULL, NULL); You are calling WideCharToMultiByte twice. The reason is to allocate the exact memory size. However, you can adopt another logic to avoid the first call. maxlen = symbol->NameLen * pg_database_encoding_max_length(); symbol_name = palloc(maxlen + 1); (I suggest symbol_name instead of ut8_buffer.) You are considering only the UTF-8 case. Shouldn't it use wcstombs or wcstombs_l? Maybe (re)use wchar2char -- see pg_locale_libc.c. + if (filename_len > 0) + { + filename_utf8 = palloc(filename_len); + WideCharToMultiByte(CP_UTF8, 0, line.FileName, -1, + filename_utf8, filename_len, + NULL, NULL); + + appendStringInfo(&errtrace, + "\n%s+0x%llx [%s:%lu]", + utf8_buffer, + (unsigned long long) displacement, + filename_utf8, + (unsigned long) line.LineNumber); + + pfree(filename_utf8); + } + else + { + appendStringInfo(&errtrace, + "\n%s+0x%llx [0x%llx]", + utf8_buffer, + (unsigned long long) displacement, + (unsigned long long) address); + } Maybe I missed something but is there a reason for not adding the address in the first condition? [1] https://learn.microsoft.com/en-us/windows/win32/api/dbghelp/ns-dbghelp-symbol_infow [2] https://www.postgresql.org/docs/current/error-style-guide.html [3] https://learn.microsoft.com/en-us/windows/win32/api/dbghelp/nf-dbghelp-symfromaddrw [4] https://learn.microsoft.com/en-us/windows/win32/intl/conventions-for-function-prototypes -- Euler Taveira EDB https://www.enterprisedb.com/