GNU bug report logs - #21683
25.0.50; `advice-add` doesn't work for compiled primitive functions

Previous Next

Package: emacs;

Reported by: Vitalie Spinu <spinuvit <at> gmail.com>

Date: Wed, 14 Oct 2015 01:55:02 UTC

Severity: minor

Tags: wontfix

Merged with 5863

Found in version 25.0.50

Done: Stefan Kangas <stefan <at> marxist.se>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 21683 in the body.
You can then email your comments to 21683 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#21683; Package emacs. (Wed, 14 Oct 2015 01:55:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Vitalie Spinu <spinuvit <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 14 Oct 2015 01:55:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Vitalie Spinu <spinuvit <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 25.0.50; `advice-add` doesn't work for compiled primitive functions
Date: Wed, 14 Oct 2015 03:52:47 +0200
Hi,

Put this function into a file, compile the file and then load the file:

    
  (defun foobar ()
   (save-restriction
     (widen)))


then:

  
  (defun restrict-widen (orig-widen)
    (message "here")
    (apply orig-widen nil))
  
  (advice-add 'widen :around #restrict-widen)
  
  (foobar)



The advice is not triggered.


Eval the `foobar` definition interactively and the advice will start
working. The problem occurs only for primitive c functions. Elisp
functions are fine. It doesn't work with 24.4.1 either.


  Vitalie




In GNU Emacs 25.0.50.3 (x86_64-unknown-linux-gnu, GTK+ Version 3.12.2)
 of 2015-09-26
Repository revision: 139e55a58466058a8c9ae54536cb790ac7b7cc64
Windowing system distributor 'The X.Org Foundation', version 11.0.11600000
System Description:	Ubuntu 14.10




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21683; Package emacs. (Sun, 18 Oct 2015 21:17:01 GMT) Full text and rfc822 format available.

Message #8 received at 21683 <at> debbugs.gnu.org (full text, mbox):

From: Philipp Stephani <p.stephani2 <at> gmail.com>
To: Vitalie Spinu <spinuvit <at> gmail.com>, 21683 <at> debbugs.gnu.org
Subject: Re: bug#21683: 25.0.50; `advice-add` doesn't work for compiled
 primitive functions
Date: Sun, 18 Oct 2015 21:16:35 +0000
[Message part 1 (text/plain, inline)]
This is documented behavior:
https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Named-Functions.html
It is possible to advise a primitive (see What Is a Function
<https://www.gnu.org/software/emacs/manual/html_node/elisp/What-Is-a-Function.html#What-Is-a-Function>),
but one should typically *not* do so, for two reasons. Firstly, some
primitives are used by the advice mechanism, and advising them could cause
an infinite recursion. Secondly, many primitives are called directly from
C, and such calls ignore advice; hence, one ends up in a confusing
situation where some calls (occurring from Lisp code) obey the advice and
other calls (from C code) do not.

Vitalie Spinu <spinuvit <at> gmail.com> schrieb am Mi., 14. Okt. 2015 um
03:55 Uhr:

>
> Hi,
>
> Put this function into a file, compile the file and then load the file:
>
>
>   (defun foobar ()
>    (save-restriction
>      (widen)))
>
>
> then:
>
>
>   (defun restrict-widen (orig-widen)
>     (message "here")
>     (apply orig-widen nil))
>
>   (advice-add 'widen :around #restrict-widen)
>
>   (foobar)
>
>
>
> The advice is not triggered.
>
>
> Eval the `foobar` definition interactively and the advice will start
> working. The problem occurs only for primitive c functions. Elisp
> functions are fine. It doesn't work with 24.4.1 either.
>
>
>   Vitalie
>
>
>
>
> In GNU Emacs 25.0.50.3 (x86_64-unknown-linux-gnu, GTK+ Version 3.12.2)
>  of 2015-09-26
> Repository revision: 139e55a58466058a8c9ae54536cb790ac7b7cc64
> Windowing system distributor 'The X.Org Foundation', version 11.0.11600000
> System Description:     Ubuntu 14.10
>
>
>
>
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21683; Package emacs. (Sun, 18 Oct 2015 21:26:01 GMT) Full text and rfc822 format available.

Message #11 received at 21683 <at> debbugs.gnu.org (full text, mbox):

From: Daniel Colascione <dancol <at> dancol.org>
To: Philipp Stephani <p.stephani2 <at> gmail.com>,
 Vitalie Spinu <spinuvit <at> gmail.com>, 21683 <at> debbugs.gnu.org
Subject: Re: bug#21683: 25.0.50; `advice-add` doesn't work for compiled
 primitive functions
Date: Sun, 18 Oct 2015 14:25:42 -0700
[Message part 1 (text/plain, inline)]
On 10/18/2015 02:16 PM, Philipp Stephani wrote:
> This is documented behavior:
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Named-Functions.html
> It is possible to advise a primitive (see What Is a Function
> <https://www.gnu.org/software/emacs/manual/html_node/elisp/What-Is-a-Function.html#What-Is-a-Function>),
> but one should typically /not/ do so, for two reasons. Firstly, some
> primitives are used by the advice mechanism, and advising them could
> cause an infinite recursion. Secondly, many primitives are called
> directly from C, and such calls ignore advice; hence, one ends up in a
> confusing situation where some calls (occurring from Lisp code) obey the
> advice and other calls (from C code) do not.

If we wanted to allow advising of primitives such that we also redirect
direct calls from C, it wouldn't be that hard: the idea is to either 1)
insert enough NOPs at the start of every function to encode an absolute
jump[1], or 2) associate with every function a Lisp variable and rest it
for nil-ness (which would be a very cheap and very predictable test
against zero with Qnil being all zero bits) on entry.

Option #1 is more clever and thus more fun to write, but option #2
probably suffices.

I just haven't had a good reason to want to advise a primitive, but if
someone wants to do this work, I wouldn't object to it.

[1] You don't actually have to encode enough bytes for an absolute jump.
You just need to be able to encode enough bytes for a backward jump to
the region before the function; this region can then encode your
absolute jump. If we align functions properly, we get this space for
free half the time. See
http://blogs.msdn.com/b/oldnewthing/archive/2011/09/21/10214405.aspx

[signature.asc (application/pgp-signature, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21683; Package emacs. (Mon, 19 Oct 2015 15:52:01 GMT) Full text and rfc822 format available.

Message #14 received at 21683 <at> debbugs.gnu.org (full text, mbox):

From: Vitalie Spinu <spinuvit <at> gmail.com>
To: Daniel Colascione <dancol <at> dancol.org>
Cc: Philipp Stephani <p.stephani2 <at> gmail.com>, 21683 <at> debbugs.gnu.org
Subject: Re: bug#21683: 25.0.50;
 `advice-add` doesn't work for compiled primitive functions
Date: Mon, 19 Oct 2015 17:51:46 +0200

>> On Sun, Oct 18 2015 14:25, Daniel Colascione wrote:

> On 10/18/2015 02:16 PM, Philipp Stephani wrote:

>> This is documented behavior:

Well, conceptually the call is from elisp, even if it's a compiled code. What is
technically happening in the compiled code shouldn't be the user's concern.

I think that piece of documentation should be nuanced that advice doesn't work
in compiled elisp calls. I took me 4 hours to figure out why my font-lock code
was chaotically working here and not there.

> I just haven't had a good reason to want to advise a primitive, but if someone
> wants to do this work, I wouldn't object to it.

I am trying hard to make polymode [1] functional for complex sub-modes like
c-lang. For that I need to restrict `widen` and `parse-partial-sexp` in order
for the sub-mode to have an illusion that a region of a buffer is a whole
buffer.

> If we wanted to allow advising of primitives such that we also redirect direct
> calls from C, it wouldn't be that hard:

That would be so great!

 Vitalie

[1] https://github.com/vspinu/polymode/






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21683; Package emacs. (Mon, 19 Oct 2015 15:57:02 GMT) Full text and rfc822 format available.

Message #17 received at 21683 <at> debbugs.gnu.org (full text, mbox):

From: Dmitry Gutov <dgutov <at> yandex.ru>
To: Vitalie Spinu <spinuvit <at> gmail.com>, Daniel Colascione <dancol <at> dancol.org>
Cc: Philipp Stephani <p.stephani2 <at> gmail.com>, 21683 <at> debbugs.gnu.org
Subject: Re: bug#21683: 25.0.50; `advice-add` doesn't work for compiled
 primitive functions
Date: Mon, 19 Oct 2015 18:56:25 +0300
On 10/19/2015 06:51 PM, Vitalie Spinu wrote:
> I am trying hard to make polymode [1] functional for complex sub-modes like
> c-lang. For that I need to restrict `widen` and `parse-partial-sexp` in order
> for the sub-mode to have an illusion that a region of a buffer is a whole
> buffer.

Yesss. This has been discussed a few times already: you can't advice 
`widen', and hence you can't make a "perfect" multiple-major-modes solution.

The current thinking on that subject is to use prog-indentation-context 
for indentation (it must be supported in the respective major modes as 
well), and introduce something similar for font-lock/syntax-ppss.

Doing anything with parse-partial-sexp is dommed to fail.




Forcibly Merged 5863 21683. Request was from npostavs <at> users.sourceforge.net to control <at> debbugs.gnu.org. (Fri, 01 Jul 2016 03:39:02 GMT) Full text and rfc822 format available.

Added tag(s) wontfix. Request was from Stefan Kangas <stefan <at> marxist.se> to control <at> debbugs.gnu.org. (Fri, 27 Nov 2020 11:46:02 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 5863 <at> debbugs.gnu.org and Lennart Borgman <lennart.borgman <at> gmail.com> Request was from Stefan Kangas <stefan <at> marxist.se> to control <at> debbugs.gnu.org. (Fri, 27 Nov 2020 11:46:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 25 Dec 2020 12:24:07 GMT) Full text and rfc822 format available.

This bug report was last modified 3 years and 115 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.