[comp.lang.fortran] Can a subroutine find the name of the calling routine?

woo@acf4.UUCP (04/29/87)

Is there anything comparable to getarg and iargc for subroutines
or functions?  How can a function determine the name of the function
which calls it or the number of calling arguments, either in C
or f77?

Alex Woo
woo@acf4.nyu.edu
wooa@nyuacf.bitnet
seismo!cmcl2!acf4!woo

joe@dayton.UUCP (Joseph P. Larson) (05/01/87)

In article <13160003@acf4.UUCP> woo@acf4.UUCP (Alex C. Woo) writes:
>
>Is there anything comparable to getarg and iargc for subroutines
>or functions?  How can a function determine the name of the function
>which calls it or the number of calling arguments, either in C
>or f77?


This kind of thing is compiler- (and perhaps machine-) dependant.
DEC FORTRAN won't tell you, for instance.  I suspect most compilers
won't give you the name of the function or subroutine for the simple
reason that this means it would also have to keep this info in a
table.  If you then strip the program (an obvious UNIX term), your
program is unlikely to work anymore.

As for the number of arguments -- that's going to be dependent on
your machine and compiler, as well.  If you can't find the info,
you might be able to write an assembly-language routine that will
look at the info on the stack -- what you are looking for just might
be there.
-- 
UUCP: rutgers!dayton!joe                Dayton Hudson Department Store Company
ATT : (612) 375-3537                    Joe Larson/MIS 1060
(standard disclaimer...)                700 on the Mall      Mpls, Mn. 55408

normt@ihlpa.ATT.COM (N. R Tiedemann) (05/01/87)

In article <13160003@acf4.UUCP>, woo@acf4.UUCP (Alex C. Woo) writes:
> Is there anything comparable to getarg and iargc for subroutines
> or functions?  How can a function determine the name of the function
> which calls it or the number of calling arguments, either in C
> or f77?
> Alex Woo

	I'm not sure if this is standard or a specific implementation, but
f77 on DEC machines uses a general purpose register (r5) as the argument
pointer for both function and subroutine calls. The first thing on this 
list is the number of argument, then the address of each. The only real
different between a function and subroutine in fortran is a function returns
its value in r0. (again the r0 may be DEC specific.) I don't think there is
any way to determine the calling function short of looking at the return
address on the stack and comparing that to the loader map file, to see
in which function that address lies. Not something to do in code.

Hope this helps a little

	Norm Tiedemann		ihnp4!ihlpa!normt
	AT&T Bell Labs
	Naperville, IL
		 60566

ken@rochester.ARPA (Ken Yap) (05/04/87)

If you want to do this just for the purposes of tracing calls and you
don't want to go around mucking with machine dependent stacks, you
could do it with a preprocessor that inserts two extra arguments in
front of every call.

	call foobar(1, 3.0, 4.0)

becomes

	call foobar('parent', 3, 1, 3.0, 4.0)

and the subroutine declaration has to be changed too, of course.

Of course, don't do this to library routines or intrinsics.

I don't have any idea how much work this is. Have fun.

	Ken