printing line number of calling function ? 
Author Message
 printing line number of calling function ?

is it possible to write a function that prints the line-number, where it was
called, that of the calling function and so on, up to main?
Of course, this would not be portable; I need it for gcc/g++.
A variant of abort() with this behaviour would also be fine.
(I did this many years ago for fortran, but am not familiar with the calling
sequence of C).

        -ulrich

--
[nosave]
----------------------------------------------------------------------------
Ulrich Lauther          ph: +49 89 636 48834 fx: ... 636 42284



Mon, 27 Oct 1997 03:00:00 GMT  
 printing line number of calling function ?
: is it possible to write a function that prints the line-number, where it was
: called, that of the calling function and so on, up to main?
: Of course, this would not be portable; I need it for gcc/g++.
: A variant of abort() with this behaviour would also be fine.

The Borland compilers provide the following macros (I don't know
how portable they are) :

__FILE__   a string literal of the compiled source file name
__LINE__   a decimal constant of the line number in the source

ex:  line 10 of file foo.c:
printf("%s: line %d\n",__FILE__,__LINE__);

output:
foo.c: line 10

You may also want to check out the assert() macro.  assert() is
standard C.

Bob Farrell



Tue, 28 Oct 1997 03:00:00 GMT  
 printing line number of calling function ?

Quote:

>is it possible to write a function that prints the line-number, where it was
>called, that of the calling function and so on, up to main?
>Of course, this would not be portable; I need it for gcc/g++.
>A variant of abort() with this behaviour would also be fine.
>(I did this many years ago for FORTRAN, but am not familiar with the calling
>sequence of C).

>    -ulrich

>--
>----------------------------------------------------------------------------
>Ulrich Lauther          ph: +49 89 636 48834 fx: ... 636 42284


The current line of the current source file is easy:

printf("%s:%s\n", __FILE__, __LINE__);

Tracing the stack is _much_ more complicated.  You would have to compile in
debug mode and trace the stabs for each stack entry.  (Not an insignificant
task.)

- Seth

--
   Seth D. Osher     SE/I Department Computer Analysis  _/_/ _/_/ _/_/

   Tel. (703) 883-8573   Fax. (703) 883-8788          _/   _/ _/ _/_/
   PRC Inc., Mail Stop TM 431, 1505 PRC Drive, McLean, VA 22102



Tue, 28 Oct 1997 03:00:00 GMT  
 printing line number of calling function ?

Quote:


>: is it possible to write a function that prints the line-number, where it was
>: called, that of the calling function and so on, up to main?
>: Of course, this would not be portable; I need it for gcc/g++.
>: A variant of abort() with this behaviour would also be fine.
>The Borland compilers provide the following macros (I don't know
>how portable they are) :
>__FILE__   a string literal of the compiled source file name
>__LINE__   a decimal constant of the line number in the source

These are standard.  The following are manifest constants in any conforming
C compiler:

__FILE__ as stated above
__LINE__ ditto
__DATE__ String literal of compilation date for current file
__TIME__   ""     ""    ""     ""      time  ""   ""     ""
__STDC__ 1 if full conformance with ANSI C Standard
and
__cplusplus defined only if C++ program (Not defined for C)

Quote:
>ex:  line 10 of file foo.c:
>printf("%s: line %d\n",__FILE__,__LINE__);
>output:
>foo.c: line 10
>You may also want to check out the assert() macro.  assert() is
>standard C.
>Bob Farrell


--
Rick Stanley                         |              RSI

Information Technologies Institute   |        C++ & C Language
New York University   NYC, NY  USA   | Programming, Training, Consulting


Thu, 30 Oct 1997 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Printing with line numbers

2. printing a set of numbers to seperate lines

3. Printing Line numbers

4. Printing Line numbers

5. printing with line number in VC6

6. Printing source with line numbers

7. Printing with line numbers???

8. Printing with line numbers???

9. Printing line number where error occured.

10. system call to return current line number

11. system call to return current line number

12. system call to return current line number

 

 
Powered by phpBB® Forum Software