From: ·············@spamgourmet.com
Subject: How to get the name of a function object? (CMUCL)
Date: 
Message-ID: <1169398326.522699.173910@s34g2000cwa.googlegroups.com>
This is probably a simple question, but it's got me stuck.  This is
under CMUCL, but my basic problem is that I don't get CLOS.  I think.

I'm trying to write an error page for my web server that nicely formats
the stack of function calls that led to the error.  For the moment, all
I need is the names of the functions.  And I can't figure out how to
extract that from a function object.

That is, I have a list of objects of type 'EVAL:INTERPRETED-FUNCTION.
When I (describe 'EVAL:INTERPRETED-FUNCTION), it tells me that there's
a slot called "%NAME" (documentation "", thanks a lot buddy).  So I try
(slot-value (...blah...) '%name), and it tells me:

   When attempting to read the slot's value (slot-value), the slot
%NAME is
   missing from the object #<Interpreted Function GET-STACK-FRAMES...

The "how to use CLOS" stuff I see on the web seems to imply that this
should work.  It works for the types I've defined myself.  So I'm
confused.  Anyone wanna tell me how this works?

Thanks,
Mat

From: Pascal Bourguignon
Subject: Re: How to get the name of a function object? (CMUCL)
Date: 
Message-ID: <87tzykpa2h.fsf@thalassa.informatimago.com>
·············@spamgourmet.com writes:

> This is probably a simple question, but it's got me stuck.  This is
> under CMUCL, but my basic problem is that I don't get CLOS.  I think.
>
> I'm trying to write an error page for my web server that nicely formats
> the stack of function calls that led to the error.  For the moment, all
> I need is the names of the functions.  And I can't figure out how to
> extract that from a function object.

That is not possible.


C/USER1[197]> (defun function-name (x) 
                (print x)
                '| now what? |)
FUNCTION-NAME
C/USER1[198]> (function-name (compile nil (lambda (x) (* 2 x))))

#<COMPILED-FUNCTION NIL> 
| now what? |



> That is, I have a list of objects of type 'EVAL:INTERPRETED-FUNCTION.
> When I (describe 'EVAL:INTERPRETED-FUNCTION), it tells me that there's
> a slot called "%NAME" (documentation "", thanks a lot buddy).  So I try
> (slot-value (...blah...) '%name), and it tells me:
>
>    When attempting to read the slot's value (slot-value), the slot
> %NAME is
>    missing from the object #<Interpreted Function GET-STACK-FRAMES...

Ah! This is something else!


CMU Common Lisp 19c (19C), running on thalassa
With core: /a6/languages/cmucl-19c/lib/cmucl/lib/lisp.core
Dumped on: Thu, 2005-11-17 15:12:58+01:00 on lorien
See <http://www.cons.org/cmucl/> for support information.
Loaded subsystems:
    Python 1.1, target Intel x86
    CLOS based on Gerd's PCL 2004/04/14 03:32:47
* (defun f (x) (* 2 x))

F
* (symbol-function 'f)

#<Interpreted Function F {58748291}>
* (type-of (symbol-function 'f))

EVAL:INTERPRETED-FUNCTION
* (inspect (symbol-function 'f))

#<Interpreted Function F {58748291}> is a funcallable-instance.
0. %NAME: F
1. ARGLIST: (X)
2. LAMBDA: (LAMBDA (X) (BLOCK F (* 2 X)))
3. DEFINITION: NIL
4. GCS: 0
5. CONVERTED-ONCE: NIL
6. CLOSURE: NIL
> :q

#<Interpreted Function F {58748291}>
* (apropos "%NAME")

EVAL::%NAME
EVAL::INTERPRETED-FUNCTION-%NAME [function] (kernel::object)
:%NAME [constant] value: :%NAME
UNIX::%NAME
UNIX::UNIX-SIGNAL-%NAME [function] 
DEBUG-INTERNALS::%NAME
DEBUG-INTERNALS::BOGUS-DEBUG-FUNCTION-%NAME [function] 
KERNEL:DSD-%NAME [function] (dsd)
LISP::PACKAGE-%NAME [function] 
LISP::%NAME
* (slot-value (symbol-function 'f) 'EVAL::%NAME)

F

But it cannot return anything significant:


* (slot-value (lambda (x) (* 2 x)) 'EVAL::%NAME)

NIL
* (type-of  (lambda (x) (* 2 x)))

EVAL:INTERPRETED-FUNCTION
* (setf (symbol-function 'g) (symbol-function 'f))

#<Interpreted Function F {58748291}>
* (defun f (x) (* 3 x))

F
* (slot-value (symbol-function 'g) 'EVAL::%NAME)

F
* (equal (symbol-function 'f) (slot-value (symbol-function 'f) 'EVAL::%NAME))

NIL
* (equal (symbol-function 'g) (slot-value (symbol-function 'g) 'EVAL::%NAME))

NIL
* 

Rather useless a slot, ain't it?


> The "how to use CLOS" stuff I see on the web seems to imply that this
> should work.  It works for the types I've defined myself.  So I'm
> confused.  Anyone wanna tell me how this works?

Here, I guess what you're missing is two things:

1- packages and symbols.  Symbol names are not unique. You can have
   several symbols with the same name, as long as they are interned in
   different packages or not interned at all.


2- functions don't have names.  Only symbols.  You may bind a function
   to a symbol function slot, or a symbol value slot, or a in a
   lexical scope, or you may not bind it to anything.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

The world will now reboot.  don't bother saving your artefacts.
From: ·············@spamgourmet.com
Subject: Re: How to get the name of a function object? (CMUCL)
Date: 
Message-ID: <1169402169.365850.294130@m58g2000cwm.googlegroups.com>
Pascal Bourguignon wrote:

> Here, I guess what you're missing is two things:
>
> 1- packages and symbols.  Symbol names are not unique. You can have
>    several symbols with the same name, as long as they are interned in
>    different packages or not interned at all.

Yes, I've had this problem before.  I would've thought that there was
enough context in slot-value to figure out which slot I meant, and it
doesn't help that when it prints out the available slots, it neglects
to mention which ones are in a different package :-(

I also haven't used "inspect" before.  That's something I should keep
in mind.

> 2- functions don't have names.  Only symbols.  You may bind a function
>    to a symbol function slot, or a symbol value slot, or a in a
>    lexical scope, or you may not bind it to anything.

But I've just discovered that I've been talking about the wrong kind of
functions.  Sorry.

(defun get-stack-frames-from (frame)
  (if frame
      (cons frame (get-stack-frames-from (debug-internals:frame-down
frame)))))

(defun get-stack-frames ()
  (get-stack-frames-from (debug-internals:top-frame)))

(defun get-debug-functions ()
  (mapcar 'debug-internals:frame-debug-function (get-stack-frames)))

(defun get-stack-functions ()
  (mapcar 'debug-internals:debug-function-function
(get-debug-functions)))

* (concatenate 'string 1 2)
...
0] (get-stack-functions)

(NIL NIL NIL #<Function DEBUG::DEBUG-EVAL-PRINT {10216F99}>
 #<Function DEBUG::DEBUG-LOOP {10214C01}>
 #<Function DEBUG:INTERNAL-DEBUG {10214899}>
 #<Function DEBUG::INVOKE-TTY-DEBUGGER {10213C09}>
 #<Function DEBUG::REAL-INVOKE-DEBUGGER {10213B69}>
 #<Function INVOKE-DEBUGGER {100645A1}> #<Function ERROR {10008849}>
 #<Function KERNEL::OBJECT-NOT-TYPE-ERROR-HANDLER {10299449}>
 #<Function KERNEL::INTERNAL-ERROR {105070A1}> NIL NIL NIL NIL NIL
 #<Function LISP::CONCAT-TO-SIMPLE* {100C3609}>
 #<Function INTERACTIVE-EVAL {103425C1}>
 #<Function LISP::%TOP-LEVEL {103419F9}>
 #<Function (LABELS LISP::RESTART-LISP SAVE-LISP) {1047B301}>)

These are the things I'm trying to get the "name" (symbol) of, and I
thought they were what you get from symbol-function.  But clearly
they're not:

0] (class-of (nth 4 (get-stack-functions)))

#<BUILT-IN-CLASS FUNCTION {281AD725}>

Oops.  So that's what I'm trying to inspect.  And I know that the
symbol must be there somewhere, because the function is able to print
its own description.  But this doesn't seem to be a CLOS thing - like
it says, it's built-in.  And I can't find anything in CMUCL's
documentation about these objects.  So I'm still lost.
From: Pascal Bourguignon
Subject: Re: How to get the name of a function object? (CMUCL)
Date: 
Message-ID: <87lkjwp0dn.fsf@thalassa.informatimago.com>
·············@spamgourmet.com writes:

> Pascal Bourguignon wrote:
>
>> Here, I guess what you're missing is two things:
>>
>> 1- packages and symbols.  Symbol names are not unique. You can have
>>    several symbols with the same name, as long as they are interned in
>>    different packages or not interned at all.
>
> Yes, I've had this problem before.  I would've thought that there was
> enough context in slot-value to figure out which slot I meant, and it
> doesn't help that when it prints out the available slots, it neglects
> to mention which ones are in a different package :-(
>
> I also haven't used "inspect" before.  That's something I should keep
> in mind.

Theorically, setting *PRINT-READABLY* to true, and using PRINT or
PRIN1 instead of PRINC (ie. FORMAT ~S instead of ~A), it should
display something unambiguous.  However, implementations are free to
do whatever they want in INSPECT, including using other printing code,
or rebinding *PRINT-READABLY* to NIL to give a nicer display.


>> 2- functions don't have names.  Only symbols.  You may bind a function
>>    to a symbol function slot, or a symbol value slot, or a in a
>>    lexical scope, or you may not bind it to anything.
>
> But I've just discovered that I've been talking about the wrong kind of
> functions.  Sorry.
>
> (defun get-stack-frames-from (frame)
>   (if frame
>       (cons frame (get-stack-frames-from (debug-internals:frame-down
> frame)))))
>
> (defun get-stack-frames ()
>   (get-stack-frames-from (debug-internals:top-frame)))
>
> (defun get-debug-functions ()
>   (mapcar 'debug-internals:frame-debug-function (get-stack-frames)))
>
> (defun get-stack-functions ()
>   (mapcar 'debug-internals:debug-function-function
> (get-debug-functions)))
>
> * (concatenate 'string 1 2)
> ...
> 0] (get-stack-functions)
>
> (NIL NIL NIL #<Function DEBUG::DEBUG-EVAL-PRINT {10216F99}>
>  #<Function DEBUG::DEBUG-LOOP {10214C01}>
>  #<Function DEBUG:INTERNAL-DEBUG {10214899}>
>  #<Function DEBUG::INVOKE-TTY-DEBUGGER {10213C09}>
>  #<Function DEBUG::REAL-INVOKE-DEBUGGER {10213B69}>
>  #<Function INVOKE-DEBUGGER {100645A1}> #<Function ERROR {10008849}>
>  #<Function KERNEL::OBJECT-NOT-TYPE-ERROR-HANDLER {10299449}>
>  #<Function KERNEL::INTERNAL-ERROR {105070A1}> NIL NIL NIL NIL NIL
>  #<Function LISP::CONCAT-TO-SIMPLE* {100C3609}>
>  #<Function INTERACTIVE-EVAL {103425C1}>
>  #<Function LISP::%TOP-LEVEL {103419F9}>
>  #<Function (LABELS LISP::RESTART-LISP SAVE-LISP) {1047B301}>)

Note that printing these functions displays a "name" of the function.

When you see #<Function DEBUG::DEBUG-LOOP {10214C01}> you may assume
that this functions was created by a DEFUN of this symbol
DEBUG::DEBUG-LOOP.

But see how the name of the function #<Function (LABELS
LISP::RESTART-LISP SAVE-LISP) {1047B301}>) is actually (LABELS
LISP::RESTART-LISP SAVE-LISP) which seems to indicate that this
function on the other hand was created by a LABELS form with the name
LISP::RESTART-LISP in some function DEFUN'ed with the SAVE-LISP name.
Not that you can do anything with such a non standard name...


> These are the things I'm trying to get the "name" (symbol) of, and I
> thought they were what you get from symbol-function.  But clearly
> they're not:
>
> 0] (class-of (nth 4 (get-stack-functions)))
>
> #<BUILT-IN-CLASS FUNCTION {281AD725}>

Builit-in functions have the same problem.  Read CLHS CAR and CLHS FIRST.  

An implementation could define them as:

(let ((function (lambda (x) (primitive:car x))))
  (setf (symbol-function 'cl:car)   function
        (symbol-function 'cl:first) function))

What would the name of this function be? CAR or FIRST?



> Oops.  So that's what I'm trying to inspect.  And I know that the
> symbol must be there somewhere, because the function is able to print
> its own description.  But this doesn't seem to be a CLOS thing - like
> it says, it's built-in.  And I can't find anything in CMUCL's
> documentation about these objects.  So I'm still lost.


Now, the question is why do you need that?  



Is that only for debugging purpose?  In this case, you have several
options, including using an implementation with a better debugger, or
using implementation specific stuff, or even modifying the
implementation to add the needed feature.




Is that for general introspection at the application level?  In this
case, I'd rather advise to come with a better definition of what's a
function name.  Try:

   (mapcar (lambda (x) (error "Testing")) '(a b))

LAMBDA produces by definitions anonymous functions.  When you reach
the debugger, you have in the stack frame functions that have no name.
You could for example define your own defun labels flet and lambda
macros, to keep the name associated with the function in a hash table.

(mapcar (my:lambda |Anonymous Testing Function| (x) (error "Testing")) '(a b))

and them you could call (my:function-name fun) and get |Anonymous Testing Function|.




If the problem is only to find _if_ there is a symbol that is bound to
a function EQ(ual) to a given function, happily you can do it, using
LIST-ALL-PACKAGES, DO-SYMBOLS, FBOUNDP and SYMBOL-FUNCTION.
(Note also a possible difficulty with EQ, EQL and EQUAL of functions...).


(defun find-names-for-function (fun)
  (let ((names '()))
    (dolist (pack (list-all-packages) names)
      (do-symbols (sym pack)
        (when (and (fboundp sym) (eq (symbol-function sym) fun))
            (pushnew sym names))))))


* (find-names-for-function (function car))

(SCHEME-TRANSLATOR::CONTINUATION-TYPE SCHEME-TRANSLATOR::SYNTAX-SPEC-NAME
                                      SCHEME-TRANSLATOR::APPLICATION-FORM-PROCEDURE
                                      CAR)






-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Nobody can fix the economy.  Nobody can be trusted with their finger
on the button.  Nobody's perfect.  VOTE FOR NOBODY.
From: ·············@spamgourmet.com
Subject: Re: How to get the name of a function object? (CMUCL)
Date: 
Message-ID: <1169582891.901192.32630@q2g2000cwa.googlegroups.com>
Sorry for the delay in replying - snowed under at work.

> Builit-in functions have the same problem.  Read CLHS CAR and CLHS FIRST.
>
> An implementation could define them as:
>
> (let ((function (lambda (x) (primitive:car x))))
>   (setf (symbol-function 'cl:car)   function
>         (symbol-function 'cl:first) function))
>
> What would the name of this function be? CAR or FIRST?

I just tried to find out, by defining a function like that and seeing
what happens inside the stack.  It looks  like break is invoked
directly by interactive-eval?  Seems kinda weird to me.

(let ((function (lambda (x) (break))))
  (setf (symbol-function 'matfoo) function
        (symbol-function 'matbar) function))

0] (get-stack-functions)

(NIL NIL NIL #<Function DEBUG::DEBUG-EVAL-PRINT {10216F99}>
 #<Function DEBUG::DEBUG-LOOP {10214C01}>
 #<Function DEBUG:INTERNAL-DEBUG {10214899}>
 #<Function DEBUG::INVOKE-TTY-DEBUGGER {10213C09}>
 #<Function DEBUG::REAL-INVOKE-DEBUGGER {10213B69}>
 #<Function INVOKE-DEBUGGER {100645A1}> #<Function BREAK {10063ED1}>
 #<Function INTERACTIVE-EVAL {103425C1}>
 #<Function LISP::%TOP-LEVEL {103419F9}>
 #<Function (LABELS LISP::RESTART-LISP SAVE-LISP) {1047B301}>)

In fact, I also find that in the real code, parts of my stack are
"missing", that is, get-stack-functions returns nil in places where
typing "down" gives me a sensible function name and arguments.  (But in
other places the function names are there, as expected.)  Hmm.  I may
be on the wrong track entirely.  Or it could be something to do with
optimisation ("optimisation" being a handy excuse for anything
inexplicable that goes wrong.)

> > Oops.  So that's what I'm trying to inspect.  And I know that the
> > symbol must be there somewhere, because the function is able to print
> > its own description.  But this doesn't seem to be a CLOS thing - like
> > it says, it's built-in.  And I can't find anything in CMUCL's
> > documentation about these objects.  So I'm still lost.
>
>
> Now, the question is why do you need that?

And always, the answer isn't necessarily a good one :-)

I have a database which has the names of (some of) these functions in,
with other useful information about the functions.  I'd like to
retrieve that information and show it on the debug page.  So I need a
link from the thing in the stack to some data of my own.  The name
(symbol) isn't perfect, but it'll do.

Now, as Madhu suggested, KERNEL:%FUNCTION-NAME seems like it does the
trick.  But I don't understand why it's a Bad Idea.  Mind you, this is
just a big experiment, so Bad Ideas aren't necessarily bad ideas here.

Thanks for the help.
From: Pascal Bourguignon
Subject: Re: How to get the name of a function object? (CMUCL)
Date: 
Message-ID: <87irewewwk.fsf@thalassa.informatimago.com>
·············@spamgourmet.com writes:

>> Now, the question is why do you need that?
>
> And always, the answer isn't necessarily a good one :-)
>
> I have a database which has the names of (some of) these functions in,
> with other useful information about the functions.  I'd like to
> retrieve that information and show it on the debug page.  So I need a
> link from the thing in the stack to some data of my own.  The name
> (symbol) isn't perfect, but it'll do.
>
> Now, as Madhu suggested, KERNEL:%FUNCTION-NAME seems like it does the
> trick.  But I don't understand why it's a Bad Idea.  Mind you, this is
> just a big experiment, so Bad Ideas aren't necessarily bad ideas here.
>
> Thanks for the help.

Then I would advise that you keep yourself a hashtable between the
function objects and the database keys (the function names).

You could do that with your own DEFUN macro (have a look at 
http://www.informatimago.com/develop/lisp/small-cl-pgms/ibcl/ ),

or you could initialize the hash table by loading the function names
from the database and:
(setf (gethash (symbol-function fname-from-db) *fun-to-name*) fname-from-db)

Then when you get a function, you can find it's name with:

      (gethash (symbol-function fname-from-db) *fun-to-name*)

[if it's in the database anyways].

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
I need a new toy.
Tail of black dog keeps good time.
Pounce! Good dog! Good dog!
From: ·············@spamgourmet.com
Subject: Re: How to get the name of a function object? (CMUCL)
Date: 
Message-ID: <1169665705.752722.296050@s48g2000cws.googlegroups.com>
On Jan 24, 7:55 am, Pascal Bourguignon <····@informatimago.com> wrote:

> Then I would advise that you keep yourself a hashtable between the
> function objects and the database keys (the function names).

That is an excellent suggestion, and I shall implement it forthwith :-)
 You can mark this down as a case of having my brain so ruined by
programming languages that Can't Do That that the correct solution
never occurred to me.

> You could do that with your own DEFUN macro (have a look athttp://www.informatimago.com/develop/lisp/small-cl-pgms/ibcl/),

It just so happens that I'm already using my own DEFUN macro to define
all my functions, so this works nicely.

Thanks again!
From: ·············@spamgourmet.com
Subject: Re: How to get the name of a function object? (CMUCL)
Date: 
Message-ID: <1170175528.264242.75860@h3g2000cwc.googlegroups.com>
On Jan 24, 8:08 pm, ·············@spamgourmet.com wrote:

I'm sure that no-one except google cares, but just to follow up on my 
original question...

I am very silly.  When I read the CMUCL documentation more carefully 
(http://common-lisp.net/project/cmucl/doc/cmu-user/debug-
internals.html#debug-internals), I discovered debug-internals:debug-
function-name.  This allows me to skip the "getting the function" step 
and go straight to the name.  So when I now write:

(defun get-stack-frames-from (frame)
  (if frame
      (cons frame (get-stack-frames-from (debug-internals:frame-down 
frame)))))

(defun get-stack-frames ()
  (get-stack-frames-from (debug-internals:top-frame)))

(defun get-debug-functions ()
  (mapcar 'debug-internals:frame-debug-function (get-stack-frames)))

(defun get-stack-functions ()
  (mapcar 'debug-internals:debug-function-name (get-debug-functions)))

...it does exactly what I wanted all along.  And, furthermore, since 
some of those functions "aren't available", the resulting list doesn't 
have the gaps that my previous strategy had.  So this is the correct 
solution.

I will try to RTFM more carefully next time.
From: robert maas, see http://tinyurl.com/uh3t
Subject: Re: How to get the name of a function object? (CMUCL)
Date: 
Message-ID: <rem-2007jan31-001@yahoo.com>
> From: ·············@spamgourmet.com
> I'm trying to write an error page for my web server that nicely
> formats the stack of function calls that led to the error.  For
> the moment, all I need is the names of the functions.

If the only kinds of function calls ever made in your application
are where the code explicitly contains a form (fnname arg1 arg2 ...),
never anything like ((lambda (param1 param2 ...) arg1 arg2 ...)
nor like (apply var1 listofargs) nor like (funcall var1 arg1 arg2 ...),
then you can probably get that fnname from the stack frame if your
code isn't too much compiled to where they disappeared. Check the
debug and speed settings when you compile the code.

> And I can't figure out how to extract that from a function
> object.

That makes no sense, because a function object is just a thing
which takes input from arguments and produces return value(s). It
doesn't have a name. What is the name of (lambda (x) (* x x)) ?
Suppose you define a function and bind it to the function cell of
a symbol like this:
(setf (symbol-function 'foo) (compile nil '(lambda (x) (* x x))))
Now suppose you bind exactly the function cell of another symbol to
exactly (EQ) that same function object:
(setf (symbol-function 'baz) (symbol-function 'foo))
Now you bind the value cell of another symbol to exactly (EQ) that
same function object:
(setq gar (symbol-function 'foo))
By the way, that compiled lambda expression prints out as:
#<Function "LAMBDA (X)" {902F031}>
at the moment in my CMUCL environment here.
Now somebody says (funcall gar :HELLO) which is bad, signalling the error:
Argument X is not a NUMBER: :HELLO.
So you figure out that it was that call to
#<Function "LAMBDA (X)" {902F031}>
What is the name of that?? That makes no sense. It doesn't have a name.
The backtrace (CMUCL) here says:
0: (KERNEL:TWO-ARG-* :HELLO :HELLO)
1: ("LAMBDA (X)" :HELLO)
2: (INTERACTIVE-EVAL (FUNCALL GAR :HELLO))
Is "LAMBDA (X)" the name of that function??

Here's a metaphor: You have a variable n with value 5, and a
variable k with value 2. So I suppose you can say that in the
context of your program the number 5 has the name n and the number
2 has the name k. But now you evaluate the expression (+ n k), to
get the integer value 7, and just print it out, not assign it to
any variable. What is the name of that 7??

Or consider this example: You set up several functions as follows:
(setq fns
  (list (compile nil '(lambda (x) (* x x)))
        (compile nil '(lambda (x) (sqrt (+ 2 x))))
        (compile nil '(lambda (x) (+ 3 (/ 2 x))))))
which value prints out here-and-now as:
(#<Function "LAMBDA (X)" {9048281}>
 #<Function "LAMBDA (X)" {9054F01}>
 #<Function "LAMBDA (X)" {90616D9}>)
So now you do (mapcar #'(lambda (fn) (funcall fn 2)) fns)
which produces the value (4 2.0 4)
Next you try (mapcar #'(lambda (fn) (funcall fn 0)) fns)
which bombs out with this message:
Arithmetic error DIVISION-BY-ZERO signalled.
Operation was KERNEL::DIVISION, operands (2 0).
The backtrace says:
0: (KERNEL::INTEGER-/-INTEGER 2 0)
1: ("LAMBDA (X)" 0)
2: (COMMON-LISP::MAP1
    #<Interpreted Function (LAMBDA (FN) (FUNCALL FN 0)) {90732B1}>
    ((#<Function "LAMBDA (X)" {9048281}> #<Function "LAMBDA (X)" {9054F01}>
      #<Function "LAMBDA (X)" {90616D9}>))
    :LIST
    T)
3: (INTERACTIVE-EVAL (MAPCAR #'(LAMBDA # #) FNS))
So what's the name of the function that caused the trouble by being
called with bad argument?

The point is that however you get to the point where the only thing
you know is the function object, you might have already discarded
the information you really want (if the actual call was of the
usual type (fnname arg1 arg2 ...) where there really was a name of
a function used in the call).

(defun foo (x) (* x x))
(compile 'foo)
(defun baz (x) (+ 3 (/ 2 x)))
(compile 'baz)
(setq fns (list (symbol-function 'foo) (symbol-function 'baz)))
which produces the following value:
(#<Function FOO {908EBE1}> #<Function BAZ {909C369}>)
Isn't that nice. It tags the function object with the name.
(setf (symbol-function 'foo) (cadr fns))
(setf (symbol-function 'baz) (car fns))
So now the function cell of foo has the object #<Function BAZ {909C369}>,
and the fuction cell of baz has the object #<Function FOO {908EBE1}>
So now somebody says (foo 0)
Arithmetic error DIVISION-BY-ZERO signalled.
Operation was KERNEL::DIVISION, operands (2 0).
the backtrace says:
0: (KERNEL::INTEGER-/-INTEGER 2 0)
1: (BAZ 0)
2: (INTERACTIVE-EVAL (FOO 0))
Isn't that fun? So what's the name of the function that bombed??

If defun is the only way you define functions, in CMUCL they are
tagged with the name of the symbol you're binding them to at the
time you define them. If you never copy the function object to
anywhere else, then I suppose it makes sense to say that the name
of the function object is the name of that symbol whose function
cell defun bound it to and which name defun installed into the
function object and which name appears in the print representation
of the object. In that case, you can "cheat" by using format or
similar print-to-string function to capture the descriptive name
within a string, the parse that string to recover the name. But
that may be implementation dependent. There's nothing in the CL
standard that says the print form of a function object must include
the print form of that symbol. On the other hand, if you're dealing
with stack frames, that's probably implementation-dependent
already. Just make sure your print-form parser is robust for
various kinds of interpreted and compiled function objects,
including those which don't have a name apparent in print form.

Note:
<http://www.lispworks.com/documentation/HyperSpec/Body/m_defun.htm>
   block-name---the function block name of the function-name.
   defun implicitly puts a block named block-name around the body forms
   (but not the forms in the lambda-list) of the function defined.
That's probably where the symbol foo or baz is coming from in the
examples above. Let's try an experiment:
(setf (symbol-function 'foo) (compile nil '(lambda (x) (block yuk (/ 2 x)))))
(foo 0)
Hmm, backtrace shows:
0: (KERNEL::INTEGER-/-INTEGER 2 0)
1: ("LAMBDA (X)" 0)
2: (INTERACTIVE-EVAL (FOO 0))
So my guess was wrong. Let me try something else:
(defun foo (x) (/ 2 x))
(symbol-function 'foo)
#<Interpreted Function FOO {90CB251}>
(compile nil (symbol-function 'foo))
#<Function "LAMBDA (X)" {90D3501}>
Aha, interpreted function objects do have the symbol in them, but
if you compile that function object in a vacuum the symbol is lost!

flet doesn't let me do what I want, so forgive the hackery where I
implement temporary binding of a function cell:
(let ((globyuk))
  (unwind-protect
    (progn
      (setq globyuk (if (fboundp 'yuk) (symbol-function 'yuk)))
      (setf (symbol-function 'yuk) (symbol-function 'foo))
      (format t "temp yuk: ~S~%" (symbol-function 'yuk))
      (compile 'yuk)
      (format t "comptemp yuk: ~S~%" (symbol-function 'yuk))
      (symbol-function 'yuk))
    (if globyuk (setf (symbol-function 'yuk) globyuk)
      (fmakunbound 'yuk))
    ))
#<Function YUK {915EC81}>
Yeah, so that's how to take an existing interpreted function by one
name and compile it under another name, so that other (temporary)
name is encapsulated in the compiled-function object.

I suppose I could have created an uninterned symbol YUK. Let's try:
(let ((yuksym (make-symbol "yuk")))
  (setf (symbol-function yuksym) (symbol-function 'foo))
  (format t "temp yuk: ~S~%" (symbol-function yuksym))
  (compile yuksym)
  (format t "comptemp yuk: ~S~%" (symbol-function yuksym))
  (symbol-function yuksym))
#<Function #:|yuk| {9177479}>
Smartass!!! I guess there's no way around saving and restoring the
function cell of the interned symbol yuk if I really want to
achieve the devious effect I want.

Now if the function foo is already compiled, is there any way to do
that trick, create a copy of the compiled function whose name is
different? Hey, I tried compiling foo, then executing that
globalyuk stuff again, and it worked!!
temp yuk: #<Function FOO {918DFD9}>     ;;Compiled function already
Compiling LAMBDA (X):                   ;;Recompiling from lambda expression
Compiling Top-Level Form:
comptemp yuk: #<Function YUK {91B5251}> ;;Voila!!
#<Function YUK {91B5251}>
How???? Is the original interpreted form of the function still
around inside the compiled-function object so that it can be
recompiled again and again if desired? It's not on the property
list of foo.

Anyway, back to your original question... The fact that PRIN1 et al
are able to recover the actual symbol which the compiler built into
the compiled-function object, whether that symbol is interned or
not (and print accordingly using special notation of not interned),
indicates that somewhere in the system implementation package there
may be a function that peeks inside the function object to fetch
the name (and also the machine address, which is also printed). If
you could find that, it would be cleaner than parsing the print
representation. Have you had any luck finding it?