Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nPIng-00020x-L5 for pgsql-docs@arkaria.postgresql.org; Wed, 02 Mar 2022 06:49:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nPInf-00010s-5l for pgsql-docs@arkaria.postgresql.org; Wed, 02 Mar 2022 06:49:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nPIne-00010j-U3 for pgsql-docs@lists.postgresql.org; Wed, 02 Mar 2022 06:49:26 +0000 Received: from mx0a-0050f201.pphosted.com ([148.163.145.86]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nPInc-0007jB-HX for pgsql-docs@lists.postgresql.org; Wed, 02 Mar 2022 06:49:26 +0000 Received: from pps.filterd (m0203369.ppops.net [127.0.0.1]) by mx0b-0050f201.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2221vilQ008957 for ; Wed, 2 Mar 2022 15:49:19 +0900 Received: from sraihe.sra.co.jp (sraihe.sra.co.jp [221.255.117.38]) by mx0b-0050f201.pphosted.com (PPS) with ESMTP id 3ehhv28xpj-1 for ; Wed, 02 Mar 2022 15:49:19 +0900 Received: from srascb.sra.co.jp (srascb [133.137.8.65]) by sraihe.sra.co.jp (Postfix) with ESMTP id 875A82A686A for ; Wed, 2 Mar 2022 15:49:18 +0900 (JST) Received: from sranhm.sra.co.jp (osspc47 [133.137.174.164]) by srascb.sra.co.jp (Postfix) with ESMTP id 7421958022A for ; Wed, 2 Mar 2022 15:49:18 +0900 (JST) Received: from yugon-CFSV7-1 (sraihb-hub.sra.co.jp [133.137.8.6]) by sranhm.sra.co.jp (Postfix) with SMTP id 42976341598; Wed, 2 Mar 2022 15:49:18 +0900 (JST) Date: Wed, 2 Mar 2022 15:47:04 +0900 From: Yugo NAGATA To: Fujii Masao Cc: Peter Eisentraut , Tom Lane , pgsql-docs@lists.postgresql.org Subject: Re: maximum number of backtrace frames logged by backtrace_functions Message-Id: <20220302154704.516286896b6f8ca924b2e384@sraoss.co.jp> In-Reply-To: <070467b6-1ead-8921-89a1-5ec39d1f2103@oss.nttdata.com> References: <0f0ed9f3-3892-e8a3-51c9-ed268dff6bdd@oss.nttdata.com> <4e16a3e9-e717-05e1-d905-6c21beba80f8@enterprisedb.com> <252159.1643899696@sss.pgh.pa.us> <33675df8-60e1-7da6-8995-3743668fd682@oss.nttdata.com> <9b3e1b97-4d2f-25af-fa52-1b2c31511444@enterprisedb.com> <7fce4874-1433-42e2-6649-e2c57ce50d4e@oss.nttdata.com> <5c089e04-1f73-f9f1-b60d-d71d6c33e3f2@enterprisedb.com> <070467b6-1ead-8921-89a1-5ec39d1f2103@oss.nttdata.com> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Proofpoint-GUID: 0_0ycBZkSu2BTkYP84xDvuR7PM_5oMSw X-Proofpoint-ORIG-GUID: 0_0ycBZkSu2BTkYP84xDvuR7PM_5oMSw X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10273 signatures=685966 X-Proofpoint-Spam-Details: rule=spam_low_notspam policy=spam_low score=0 mlxscore=0 priorityscore=1501 adultscore=0 phishscore=0 impostorscore=0 malwarescore=0 lowpriorityscore=0 suspectscore=0 bulkscore=0 clxscore=1034 spamscore=0 mlxlogscore=974 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2203020028 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sat, 19 Feb 2022 01:05:04 +0900 Fujii Masao wrote: > > > On 2022/02/18 19:59, Peter Eisentraut wrote: > > On 18.02.22 09:24, Fujii Masao wrote: > >> Or even backtrace should be logged by write_stderr() so that it's written to eventlog if necessary? I just wonder why backtrace_symbols_fd() is used only in ExceptionalCondition(). > > > > Probably because it was simpler.  It would also make sense to convert the whole thing to use write_stderr() consistently. > +1 > Attached is the updated version of the patch that uses write_stderr() to log the backtrace in assertion failure case. > > + if (nframes >= lengthof(buf)) > + appendStringInfo(&errtrace, "\n(backtrace limited to %zu frames)", > + lengthof(buf)); > > I found this doesn't work on FreeBSD, at least FreeBSD 13 that cfbot uses on Cirrus CI. When the backtrace is larger than 100, on FreeBSD, backtrace() seems to write the *99* (not 100) most recent function calls to the buffer. That is, the variable "nframes" is 99 while lengthof(buf) indicates 100. So the above message about backtrace limit will never be logged on FreeBSD. OTOH, on Linux and MacOS, backtrace() writes the 100 most recent function calls. I'm not sure if such a behavior on FreeBSD is expected or a bug. > > To issue the message whatever OS is, probably we need to modify set_backtrace() as follows, for example. But is this overkill? Thought? > > > - void *buf[100]; > +#define BACKTRACE_MAX_FRAMES 100 > + void *buf[BACKTRACE_MAX_FRAMES + 1]; > int nframes; > char **strfrms; > > nframes = backtrace(buf, lengthof(buf)); > + if (nframes > BACKTRACE_MAX_FRAMES) > + nframes = BACKTRACE_MAX_FRAMES; > strfrms = backtrace_symbols(buf, nframes); > if (strfrms == NULL) > return; > > for (int i = num_skip; i < nframes; i++) > appendStringInfo(&errtrace, "\n%s", strfrms[i]); > + if (nframes >= BACKTRACE_MAX_FRAMES) > + appendStringInfo(&errtrace, "\n(backtrace limited to %d frames)", > + BACKTRACE_MAX_FRAMES); I think it would be better that users can get the same results in different OS as as possible. I have another comment. When the backtrace is output from elog, two rows are skipped by num_skip and only 98 frames will appear even though it is said "backtrace limited to 100 frames". So, how about reporting the limiting number subtracted by num_skip, like this: if (nframes >= lengthof(buf)) appendStringInfo(&errtrace, "\n(backtrace limited to %zu frames)", lengthof(buf) - num_skip); or if (nframes >= BACKTRACE_MAX_FRAMES) appendStringInfo(&errtrace, "\n(backtrace limited to %d frames)", BACKTRACE_MAX_FRAMES - num_skip); Regards, Yugo Nagata -- Yugo NAGATA