From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.bugs Subject: bug#9120: 24.0.50; Cannot debug lexical-binded function (closure) Date: Mon, 22 Aug 2011 17:17:03 -0400 Message-ID: References: <87oc0r3sqx.fsf@isil.kanru.info> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1314047871 19914 80.91.229.12 (22 Aug 2011 21:17:51 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 22 Aug 2011 21:17:51 +0000 (UTC) Cc: 9120-done@debbugs.gnu.org To: Kan-Ru Chen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Aug 22 23:17:46 2011 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1QvbsP-00078N-IP for geb-bug-gnu-emacs@m.gmane.org; Mon, 22 Aug 2011 23:17:45 +0200 Original-Received: from localhost ([::1]:41282 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QvbsP-0007nz-1w for geb-bug-gnu-emacs@m.gmane.org; Mon, 22 Aug 2011 17:17:45 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:57392) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QvbsM-0007nm-EI for bug-gnu-emacs@gnu.org; Mon, 22 Aug 2011 17:17:43 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QvbsK-0004q1-As for bug-gnu-emacs@gnu.org; Mon, 22 Aug 2011 17:17:42 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:51240) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QvbsK-0004pm-99 for bug-gnu-emacs@gnu.org; Mon, 22 Aug 2011 17:17:40 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.69) (envelope-from ) id 1Qvbud-0007wV-TS for bug-gnu-emacs@gnu.org; Mon, 22 Aug 2011 17:20:03 -0400 Resent-From: Stefan Monnier Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-To: bug-gnu-emacs@gnu.org Resent-Date: Mon, 22 Aug 2011 21:20:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: cc-closed 9120 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Mail-Followup-To: 9120@debbugs.gnu.org, monnier@iro.umontreal.ca Original-Received: via spool by 9120-done@debbugs.gnu.org id=D9120.131404798730477 (code D ref 9120); Mon, 22 Aug 2011 21:20:03 +0000 Original-Received: (at 9120-done) by debbugs.gnu.org; 22 Aug 2011 21:19:47 +0000 Original-Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1QvbuL-0007vO-GL for submit@debbugs.gnu.org; Mon, 22 Aug 2011 17:19:47 -0400 Original-Received: from ironport2-out.teksavvy.com ([206.248.154.181] helo=ironport2-out.pppoe.ca) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1QvbuE-0007v1-Me for 9120-done@debbugs.gnu.org; Mon, 22 Aug 2011 17:19:42 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av0EAAPHUk5Ld+HZ/2dsb2JhbABBqBR4gUABAQQBViMFCws0EhQYDSQTh3G1NIZIBJ9xhDY X-IronPort-AV: E=Sophos;i="4.68,265,1312171200"; d="scan'208";a="132235171" Original-Received: from 75-119-225-217.dsl.teksavvy.com (HELO ceviche.home) ([75.119.225.217]) by ironport2-out.pppoe.ca with ESMTP/TLS/ADH-AES256-SHA; 22 Aug 2011 17:17:03 -0400 Original-Received: by ceviche.home (Postfix, from userid 20848) id 6BCD06610A; Mon, 22 Aug 2011 17:17:03 -0400 (EDT) In-Reply-To: <87oc0r3sqx.fsf@isil.kanru.info> (Kan-Ru Chen's message of "Tue, 19 Jul 2011 10:32:22 +0800") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.11 Precedence: list Resent-Date: Mon, 22 Aug 2011 17:20:03 -0400 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 1) X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:50223 Archived-At: > The lisp debugger doesn't know the closure form: > (setq lexical-binding t) > (defun my-test nil) > (debug-on-entry 'my-test) > => (error "my-test is not a user-defined Lisp function") > And if the file was byte-compiled, for example: > (debug-on-entry 'shell) > (shell) > => call-interactively: Invalid function: (lambda 256 "Run an inferior > shell, with I/O through BUFFER (which defaults to `*shell*'). [rest of > the doc string] > Thus one cannot debug most byte-compiled lisp functions (46 files > under lisp/ have declared lexical-binding). I've installed the patch below which should fix this without breaking other uses. Please report any problem you encounter with the new code. Stefan === modified file 'lisp/ChangeLog' --- lisp/ChangeLog 2011-08-22 12:46:45 +0000 +++ lisp/ChangeLog 2011-08-22 21:15:10 +0000 @@ -1,3 +1,9 @@ +2011-08-22 Stefan Monnier + + * emacs-lisp/debug.el (debug-arglist): New function. + (debug-convert-byte-code): Use it. Handle lexical byte-codes. + (debug-on-entry-1): Handle interpreted closures (bug#9120). + 2011-08-22 Juri Linkov * progmodes/compile.el (compilation-mode-font-lock-keywords): === modified file 'lisp/emacs-lisp/debug.el' --- lisp/emacs-lisp/debug.el 2011-07-15 02:16:55 +0000 +++ lisp/emacs-lisp/debug.el 2011-08-22 21:12:18 +0000 @@ -778,6 +778,7 @@ (not (debugger-special-form-p symbol)))) t nil nil (symbol-name fn))) (list (if (equal val "") fn (intern val))))) + ;; FIXME: Use advice.el. (when (debugger-special-form-p function) (error "Function %s is a special form" function)) (if (or (symbolp (symbol-function function)) @@ -835,24 +836,30 @@ (message "Cancelling debug-on-entry for all functions") (mapcar 'cancel-debug-on-entry debug-function-list))) +(defun debug-arglist (definition) + ;; FIXME: copied from ad-arglist. + "Return the argument list of DEFINITION." + (require 'help-fns) + (help-function-arglist definition 'preserve-names)) + (defun debug-convert-byte-code (function) (let* ((defn (symbol-function function)) (macro (eq (car-safe defn) 'macro))) (when macro (setq defn (cdr defn))) - (unless (consp defn) - ;; Assume a compiled code object. - (let* ((contents (append defn nil)) + (when (byte-code-function-p defn) + (let* ((args (debug-arglist defn)) (body - (list (list 'byte-code (nth 1 contents) - (nth 2 contents) (nth 3 contents))))) - (if (nthcdr 5 contents) - (setq body (cons (list 'interactive (nth 5 contents)) body))) - (if (nth 4 contents) + `((,(if (memq '&rest args) #'apply #'funcall) + ,defn + ,@(remq '&rest (remq '&optional args)))))) + (if (> (length defn) 5) + (push `(interactive ,(aref defn 5)) body)) + (if (aref defn 4) ;; Use `documentation' here, to get the actual string, ;; in case the compiled function has a reference ;; to the .elc file. (setq body (cons (documentation function) body))) - (setq defn (cons 'lambda (cons (car contents) body)))) + (setq defn `(closure (t) ,args ,@body))) (when macro (setq defn (cons 'macro defn))) (fset function defn)))) @@ -861,11 +868,12 @@ (tail defn)) (when (eq (car-safe tail) 'macro) (setq tail (cdr tail))) - (if (not (eq (car-safe tail) 'lambda)) + (if (not (memq (car-safe tail) '(closure lambda))) ;; Only signal an error when we try to set debug-on-entry. ;; When we try to clear debug-on-entry, we are now done. (when flag (error "%s is not a user-defined Lisp function" function)) + (if (eq (car tail) 'closure) (setq tail (cdr tail))) (setq tail (cdr tail)) ;; Skip the docstring. (when (and (stringp (cadr tail)) (cddr tail)) @@ -875,9 +883,9 @@ (setq tail (cdr tail))) (unless (eq flag (equal (cadr tail) '(implement-debug-on-entry))) ;; Add/remove debug statement as needed. - (if flag - (setcdr tail (cons '(implement-debug-on-entry) (cdr tail))) - (setcdr tail (cddr tail))))) + (setcdr tail (if flag + (cons '(implement-debug-on-entry) (cdr tail)) + (cddr tail))))) defn)) (defun debugger-list-functions ()