determining function name in runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shyam

    #1

    determining function name in runtime

    Hi all

    is there any way the determine which function currently is executing in
    a process ?

    For example we can find the file name using __FILE__
    and line number using __LINE__ macros

    simillarly is there any macro which gives the function name

    thanks shyam

  • Vladimir S. Oka

    #2
    Re: determining function name in runtime


    shyam wrote:[color=blue]
    > Hi all
    >
    > is there any way the determine which function currently is executing in
    > a process ?[/color]

    I assume you mean program...
    [color=blue]
    >
    > For example we can find the file name using __FILE__
    > and line number using __LINE__ macros
    >
    > simillarly is there any macro which gives the function name[/color]

    Yes, and it's __func__ (note double underscores before and after).

    --
    BR, Vladimir

    Comment

    • Lew Pitcher

      #3
      Re: determining function name in runtime

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1

      Vladimir S. Oka wrote:[color=blue]
      > shyam wrote:[color=green]
      >> Hi all
      >>
      >> is there any way the determine which function currently is executing in
      >> a process ?[/color]
      >
      > I assume you mean program...
      >[color=green]
      >> For example we can find the file name using __FILE__
      >> and line number using __LINE__ macros
      >>
      >> simillarly is there any macro which gives the function name[/color]
      >
      > Yes, and it's __func__ (note double underscores before and after).[/color]

      Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
      predefined identifier of local (to the function) scope.

      Thus,
      char *where_I_am = "I am at line " __LINE__ "\n";
      would work, but
      char *where_I_am = "I am in function " __func__ "\n";
      would not.

      OTOH,
      printf("I am at line %d in function %s\n",__LINE__, __func__);
      will work.


      - --

      Lew Pitcher, IT Specialist, Corporate Technology Solutions,
      Enterprise Technology Solutions, TD Bank Financial Group

      (Opinions expressed here are my own, not my employer's)
      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.2 (MingW32)
      Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

      iD8DBQFEBwFDagV FX4UWr64RAlQdAJ 9XT4qQS65GzFFRP O0RDFqxskDA+wCd GWGd
      evG1lEWtpMvGxVd vP/Y97RM=
      =vy6r
      -----END PGP SIGNATURE-----

      Comment

      • MrG{DRGN}

        #4
        Re: determining function name in runtime


        "Lew Pitcher" <Lew.Pitcher@td securities.com> wrote in message
        news:8jDNf.4274 $RM2.570635@new s20.bellglobal. com...[color=blue]
        > -----BEGIN PGP SIGNED MESSAGE-----
        > Hash: SHA1
        >
        > Vladimir S. Oka wrote:[/color]
        -snip

        , and it's __func__ (note double underscores before and after).[color=blue]
        >
        > Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
        > predefined identifier of local (to the function) scope.
        >
        > Thus,
        > char *where_I_am = "I am at line " __LINE__ "\n";
        > would work, but
        > char *where_I_am = "I am in function " __func__ "\n";
        > would not.
        >
        > OTOH,
        > printf("I am at line %d in function %s\n",__LINE__, __func__);
        > will work.
        >
        >[/color]

        Let me guess, __func__ is a C99 feature?
        <OT> (which means I can't use it with my compiler msvc 6.0).<OT>

        --
        MrG{DRGN}


        Comment

        • Vladimir S. Oka

          #5
          Re: determining function name in runtime


          MrG{DRGN} wrote:[color=blue]
          > "Lew Pitcher" <Lew.Pitcher@td securities.com> wrote in message
          > news:8jDNf.4274 $RM2.570635@new s20.bellglobal. com...[color=green]
          > > -----BEGIN PGP SIGNED MESSAGE-----
          > > Hash: SHA1
          > >
          > > Vladimir S. Oka wrote:[/color]
          > -snip
          >
          > , and it's __func__ (note double underscores before and after).[color=green]
          > >
          > > Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
          > > predefined identifier of local (to the function) scope.
          > >
          > > Thus,
          > > char *where_I_am = "I am at line " __LINE__ "\n";
          > > would work, but
          > > char *where_I_am = "I am in function " __func__ "\n";
          > > would not.
          > >
          > > OTOH,
          > > printf("I am at line %d in function %s\n",__LINE__, __func__);
          > > will work.
          > >
          > >[/color]
          >
          > Let me guess, __func__ is a C99 feature?
          > <OT> (which means I can't use it with my compiler msvc 6.0).<OT>[/color]

          Yes it is.
          <OT>I know nothing about M$VC, but it may provide it as an
          extension?<OT>

          Comment

          • Ico

            #6
            Re: determining function name in runtime

            Lew Pitcher <Lew.Pitcher@td securities.com> wrote:
            [color=blue]
            > Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
            > predefined identifier of local (to the function) scope.[/color]


            Is there a reason why __func__ is not a macro ?

            --
            :wq
            ^X^Cy^K^X^C^C^C ^C

            Comment

            • tmp123

              #7
              Re: determining function name in runtime


              Ico wrote:[color=blue]
              > Lew Pitcher <Lew.Pitcher@td securities.com> wrote:
              >[color=green]
              > > Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
              > > predefined identifier of local (to the function) scope.[/color]
              >
              >
              > Is there a reason why __func__ is not a macro ?
              >[/color]

              It is easy for the preprocessor to know the current file and line.
              However, it can not known the function name (even it can not known what
              is a function) if it doesn't knowns C syntax (and the preprocessor
              doesn't).

              Comment

              • Lew Pitcher

                #8
                Re: determining function name in runtime

                -----BEGIN PGP SIGNED MESSAGE-----
                Hash: SHA1

                Ico wrote:[color=blue]
                > Lew Pitcher <Lew.Pitcher@td securities.com> wrote:
                >[color=green]
                >> Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
                >> predefined identifier of local (to the function) scope.[/color]
                >
                >
                > Is there a reason why __func__ is not a macro ?[/color]

                My copy of the draft C99 standard doesn't give a reason.

                My /guess/ is that __FILE__ and __LINE__ can be implemented as macros
                because they do not require any interpretation of the C code (they are
                strictly derived from the mechanics of reading the source files).

                OTOH, __func__ requires the compiler's interpretation of the code in
                order to work, so it cannot be implemented in the same manner as __FILE_
                and __LINE__.


                - --

                Lew Pitcher, IT Specialist, Corporate Technology Solutions,
                Enterprise Technology Solutions, TD Bank Financial Group

                (Opinions expressed here are my own, not my employer's)
                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.4.2 (MingW32)
                Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                iD8DBQFEByGZagV FX4UWr64RAjQJAJ 9unyR7P+JPi14Yw Ci+eKaYF+VeMgCg s0N/
                yRzUKfTTmLVMySm RWJUv6us=
                =KubY
                -----END PGP SIGNATURE-----

                Comment

                • Micah Cowan

                  #9
                  Re: determining function name in runtime

                  "tmp123" <tmp123@menta.n et> writes:
                  [color=blue]
                  > Ico wrote:[color=green]
                  > > Lew Pitcher <Lew.Pitcher@td securities.com> wrote:
                  > >[color=darkred]
                  > > > Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
                  > > > predefined identifier of local (to the function) scope.[/color]
                  > >
                  > >
                  > > Is there a reason why __func__ is not a macro ?
                  > >[/color]
                  >
                  > It is easy for the preprocessor to know the current file and line.
                  > However, it can not known the function name (even it can not known what
                  > is a function) if it doesn't knowns C syntax (and the preprocessor
                  > doesn't).[/color]

                  s/doesn't/might not/

                  Several implementations have provided such a macro. While they /may/
                  have been handled differently from other macros (I don't actually
                  know), they were still handled as string literals, and thus you could
                  make use of the concatenation feature, etc.

                  But I think the reason you give above is at least why the standard
                  didn't want to /require/ __func__ to be a macro (and so they required
                  it to be something else).

                  -Micah

                  Comment

                  • Jordan Abel

                    #10
                    Re: determining function name in runtime

                    On 2006-03-02, MrG{DRGN} <Iamnot@here.co m> wrote:[color=blue]
                    >
                    > "Lew Pitcher" <Lew.Pitcher@td securities.com> wrote in message
                    > news:8jDNf.4274 $RM2.570635@new s20.bellglobal. com...[color=green]
                    >> -----BEGIN PGP SIGNED MESSAGE-----
                    >> Hash: SHA1
                    >>
                    >> Vladimir S. Oka wrote:[/color]
                    > -snip
                    >
                    > , and it's __func__ (note double underscores before and after).[color=green]
                    >>
                    >> Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
                    >> predefined identifier of local (to the function) scope.
                    >>
                    >> Thus,
                    >> char *where_I_am = "I am at line " __LINE__ "\n";
                    >> would work, but
                    >> char *where_I_am = "I am in function " __func__ "\n";
                    >> would not.
                    >>
                    >> OTOH,
                    >> printf("I am at line %d in function %s\n",__LINE__, __func__);
                    >> will work.
                    >>
                    >>[/color]
                    >
                    > Let me guess, __func__ is a C99 feature?
                    > <OT> (which means I can't use it with my compiler msvc 6.0).<OT>[/color]

                    No, but i have a suggestion:

                    #if __STDC_VERSION_ _ < 199901l
                    #define WHERE_FMT __FILE__ ":%d"
                    #define WHERE_ARGS __LINE__
                    #else
                    #define WHERE_FMT __FILE__ ":%s:%d"
                    #define WHERE_ARGS __LINE__,__func __
                    #endif

                    fprintf(stderr, "Line printed to stderr at "WHERE_FMT"\n", WHERE_ARGS);

                    will print file.c:function :42 on c99 systems, file.c:42 on others.

                    Comment

                    • Jordan Abel

                      #11
                      Re: determining function name in runtime

                      On 2006-03-02, Lew Pitcher <Lew.Pitcher@td securities.com> wrote:[color=blue]
                      >
                      > Ico wrote:[color=green]
                      >> Lew Pitcher <Lew.Pitcher@td securities.com> wrote:
                      >>[color=darkred]
                      >>> Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
                      >>> predefined identifier of local (to the function) scope.[/color]
                      >>
                      >>
                      >> Is there a reason why __func__ is not a macro ?[/color]
                      >
                      > My copy of the draft C99 standard doesn't give a reason.[/color]

                      That's because that's not the right place to look


                      lines 29-31 on page 50 [PDF page 57] read as follows:

                      A new feature of C99: C99 introduces predefined identifiers, which have
                      block scope (as distinct 30 from predefined macros which have file
                      scope), and one such predefined identifier, __func__, which allows the
                      function name to be used at execution time.

                      That seems to be saying that the reason is because a macro has file
                      scope, and thus couldn't have a different value on different lines
                      unless undefined and redefined by the user.

                      Note, however, that string literal concatenation takes place in phase 6,
                      whereas semantic analysis [which is when { } becomes a block scope, etc]
                      is phase 7.

                      Comment

                      • Ben Bacarisse

                        #12
                        Re: determining function name in runtime

                        On Thu, 02 Mar 2006 21:48:10 +0000, Jordan Abel wrote:
                        [color=blue]
                        > No, but i have a suggestion:
                        >
                        > #if __STDC_VERSION_ _ < 199901l
                        > #define WHERE_FMT __FILE__ ":%d"
                        > #define WHERE_ARGS __LINE__
                        > #else
                        > #define WHERE_FMT __FILE__ ":%s:%d"[/color]

                        Micro correction: swap %s and %d (or __LINE__ and __func__ below)
                        [color=blue]
                        > #define WHERE_ARGS __LINE__,__func __
                        > #endif
                        >
                        > fprintf(stderr, "Line printed to stderr at "WHERE_FMT"\n", WHERE_ARGS);[/color]

                        --
                        Ben.

                        Comment

                        • Keith Thompson

                          #13
                          Re: determining function name in runtime

                          Jordan Abel <random832@gmai l.com> writes:[color=blue]
                          > On 2006-03-02, Lew Pitcher <Lew.Pitcher@td securities.com> wrote:[color=green]
                          >>
                          >> Ico wrote:[color=darkred]
                          >>> Lew Pitcher <Lew.Pitcher@td securities.com> wrote:
                          >>>
                          >>>> Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
                          >>>> predefined identifier of local (to the function) scope.
                          >>>
                          >>>
                          >>> Is there a reason why __func__ is not a macro ?[/color]
                          >>
                          >> My copy of the draft C99 standard doesn't give a reason.[/color]
                          >
                          > That's because that's not the right place to look
                          > http://www.open-std.org/jtc1/sc22/wg...onaleV5.10.pdf
                          >
                          > lines 29-31 on page 50 [PDF page 57] read as follows:
                          >
                          > A new feature of C99: C99 introduces predefined identifiers, which have
                          > block scope (as distinct 30 from predefined macros which have file
                          > scope), and one such predefined identifier, __func__, which allows the
                          > function name to be used at execution time.
                          >
                          > That seems to be saying that the reason is because a macro has file
                          > scope, and thus couldn't have a different value on different lines
                          > unless undefined and redefined by the user.[/color]

                          __LINE__ has a different value on different lines, and the
                          preprocessor has no problem handling that.

                          __func__ isn't a macro because making it one would have required the
                          preprocessor (more precisely, translation phase 4) to understand more
                          of the language grammar than is currently required.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                          We must do something. This is something. Therefore, we must do this.

                          Comment

                          Working...