Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93488 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 4729 invoked from network); 24 May 2016 16:48:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 May 2016 16:48:14 -0000 Authentication-Results: pb1.pair.com smtp.mail=me@kelunik.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=me@kelunik.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain kelunik.com from 81.169.146.216 cause and error) X-PHP-List-Original-Sender: me@kelunik.com X-Host-Fingerprint: 81.169.146.216 mo4-p00-ob.smtp.rzone.de Received: from [81.169.146.216] ([81.169.146.216:55575] helo=mo4-p00-ob.smtp.rzone.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CC/83-10476-BC584475 for ; Tue, 24 May 2016 12:48:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1464108488; l=15902; s=domk; d=kelunik.com; h=Content-Type:Cc:To:From:Subject:Date:References:In-Reply-To: MIME-Version; bh=AUZfg2F/QK05rR9fAkircBVb8YovnGlRiZOisrE4vvs=; b=Fm3ud6Z2pJ1xT0F20JKf4x5HPGf/drVdq3x93DbpoS0Fs9mxOM3GHECK6VW2mAuxBkt d9c4qBOSoEQKAZNYWXJIYIxx9XFSrbb8s6W8KNdGPPeZNhr5EX7FP1wRAbV0rrL2hRZyY DhIQ6OdgPrXBAAKv3s8xvE3FeYz/BSN1MZI= X-RZG-AUTH: :IWkkfkWkbvHsXQGmRYmUo9mls2vWuiu+7SLGvomb4bl9EfHtOnc6 X-RZG-CLASS-ID: mo00 Received: from mail-wm0-f54.google.com ([74.125.82.54]) by smtp.strato.de (RZmta 37.26 AUTH) with ESMTPSA id e01efbs4OGm7BfB (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp384r1 with 384 ECDH bits, eq. 7680 bits RSA)) (Client did not present a certificate) for ; Tue, 24 May 2016 18:48:07 +0200 (CEST) Received: by mail-wm0-f54.google.com with SMTP id n129so140391926wmn.1 for ; Tue, 24 May 2016 09:48:07 -0700 (PDT) X-Gm-Message-State: AOPr4FU/FhSP+3PHzfXgD1/RBoee+ocdKW2b3aUUPOj8pyCeugB1H4WEC4in/a+ve2bdyZxgnTU67DLlayIUNA== MIME-Version: 1.0 X-Received: by 10.28.226.84 with SMTP id z81mr24577291wmg.73.1464108487318; Tue, 24 May 2016 09:48:07 -0700 (PDT) Received: by 10.28.53.132 with HTTP; Tue, 24 May 2016 09:48:07 -0700 (PDT) In-Reply-To: References: <72f547d6-5fcf-3cd2-960d-cb612a429e47@php.net> Date: Tue, 24 May 2016 18:48:07 +0200 X-Gmail-Original-Message-ID: Message-ID: To: Rasmus Schultz Cc: Julien Pauli , Rowan Collins , PHP internals Content-Type: multipart/alternative; boundary=001a114b0cd44cb56c05339952dc Subject: Re: [PHP-DEV] Exception::getLine() From: me@kelunik.com (Niklas Keller) --001a114b0cd44cb56c05339952dc Content-Type: text/plain; charset=UTF-8 2016-05-24 17:32 GMT+02:00 Rasmus Schultz : > > it would be more logical to collect the trace in the ZEND_THROW Opcode > instead of in the create_handler of the Exception class > > I've been pondering a means of doing this with relatively few BC issues. > > There are four use-cases right now: > > 1. Direct: throw new Exception() > 2. Indirect: throw $factory->createException() > 3. Re-throwing: throw $exception > 4. Not throwing: $exception = new Exception() > You missed the "5. Throw later" case, e.g. for coroutines using Generator::throw. http://amphp.org/docs/amp/managing-concurrency.html#generators Regards, Niklas > The first use-case (throw new) makes up the large majority, and would > be unaffected by this change. > > The second use-case (indirect throw, factory) and third use-case > (re-throwing) are both broken as-is - in either case, it isn't > reporting the correct stack-trace now. This change corrects those > cases. Even if that's a minor BC break, it corrects an error. > > By my analysis, that leaves only the 4th case (not throwing) as a > potentially serious BC break. If somebody is presently constructing an > Exception just as a means of extracting a stack-trace, this would no > longer work. But there is a more direct way of doing that, which is > debug_backtrace(), so likely the number of cases of this in the wild > are few, and easy to correct, and likely with backwards-compatibility > of the corrected code in all cases. > > I can't think of any other case where this change affects anything, > but please correct me and point out any other case you can think of? > > As for Exception::getTrace(), I would propose the signature be changed > to Exception::getTrace($index = 0) which would be backwards compatible > in API terms - the default argument of 0 for $index would return the > most recent stack trace, whereas an index of 1 would return the > previous stack trace, e.g. after a re-throw. > > Exception::getTraceAsString() could either change in the same way, or > render out all collected traces (which might be a bit much) or could > simply render the most recently collected trace - I'm not sure which > is best or most compatible. > > Exception::getLine() could either return the line-number of the first > or last collected stack-trace - returning the first collected > line-number is more compatible, consistent with "return new" and > re-throwing, but returning the last line-number might be more correct, > I'm not sure. > > Would a change like this require an RFC (and a vote) or is it arguably > a bug? (I'm guessing this change is too substantial to be considered a > "bug fix"?) > > > On Mon, May 23, 2016 at 10:12 AM, Julien Pauli wrote: > > On Sat, May 21, 2016 at 8:16 PM, Rasmus Schultz > wrote: > >> Alright, so forget my comparison with other languages. > >> > >> My other points remain: > >> > >> Presently, "throw new" is treated as though it was one statement. > >> That's not the case. We have deferred throws and factory methods for > >> exceptions, and we have re-throws, so collecting the stack-trace at > >> construction time doesn't work. > >> > >> The construction site would only be relevant if "throw new" was in > >> deed a single statement. > >> > >> Recording the actual throw site is clearly the goal - the current > >> implementation is betting on "throw" and "new" happening at the same > >> site, which is merely circumstance. > >> > >> Ideally, an Exception should collect another stack trace for each > >> successive throw, which would enable you to trace not only the > >> original site, but the flow through any exception-handlers that might > >> have re-thrown the same Exception. > >> > >> As is, there is no information collected on throw, and thereby no > >> evidence or record of possible re-throws - on top of the fact that you > >> may be collecting and looking at bogus stack-traces from factory > >> methods or exception mappers. > >> > >> > >> On Fri, May 20, 2016 at 11:06 AM, Rowan Collins < > rowan.collins@gmail.com> wrote: > >>> On 20/05/2016 08:22, Niklas Keller wrote: > >>>> > >>>> 2016-05-20 4:13 GMT+02:00 Jesse Schalken : > >>>>> > >>>>> The top frame is the construction (get_error) and the site of the > throw > >>>>> (do_throw) doesn't appear in the stack at all. > >>>>> > >>>> > >>>> The comparison with JavaScript isn't a good one, since you can throw > >>>> everything in JS. If they didn't provide the stack trace upon throw, > you > >>>> would not have a stack trace at all if you throw a plain string. > >>>> > >>> > >>> > >>> That explanation justifies completely the opposite behaviour to what > Jesse > >>> described. > >>> > >>> According to MDN [1] the "stack" property is completley > unstandardised, and > >>> some engines may indeed populate it on throw, but there's no hint on > that > >>> page that they'll attach it to anything not constructed as an Error. > >>> > >>> So it's not a great comparison for either side (note that it was > originally > >>> brought up by Rasmus as an example where it *does* come from the throw > site) > >>> because the language doesn't actually guarantee you a stack trace at > all. > >>> > >>> [1] > >>> > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack > >>> > >>> Regards, > >>> -- > >>> Rowan Collins > >>> [IMSoP] > >>> > >>> -- > >>> PHP Internals - PHP Runtime Development Mailing List > >>> To unsubscribe, visit: http://www.php.net/unsub.php > >>> > >> > >> -- > >> PHP Internals - PHP Runtime Development Mailing List > >> To unsubscribe, visit: http://www.php.net/unsub.php > >> > > > > Hi. > > > > I explained that in my article detailing Exceptions from internal , > > http://jpauli.github.io/2015/04/09/exceptional-php.html > > > > I admit it would be more logical to collect the trace in the > > ZEND_THROW Opcode instead of in the create_handler of the Exception > > class. > > > > That would break backtraces, but we already broke them in PHP 7.0 > > So we could think about it for a 8.0 ideally , or a 7.1 ; I'm not > > sure. Anyway, that needs more debate ;-) > > > > > > > > Julien.Pauli > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --001a114b0cd44cb56c05339952dc--