Calling function name

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

    #1

    Calling function name

    Is there an way for a function to know who called him ?

  • baumann@pan

    #2
    Re: Calling function name

    lastest gcc ,vs.net support __FUNCTION__, use it

    #include <stdio.h>

    int func1(char * str)
    {
    printf("called by %s\n",str);
    return 0;
    }

    int main(void)
    {
    func1(__FUNCTIO N__);
    return 0;
    }

    Saroj wrote:[color=blue]
    > Is there an way for a function to know who called him ?[/color]

    Comment

    • Prawit Chaivong

      #3
      Re: Calling function name



      baumann@pan wrote:[color=blue]
      > lastest gcc ,vs.net support __FUNCTION__, use it
      >
      > #include <stdio.h>
      >
      > int func1(char * str)
      > {
      > printf("called by %s\n",str);
      > return 0;
      > }
      >
      > int main(void)
      > {
      > func1(__FUNCTIO N__);
      > return 0;
      > }
      >
      > Saroj wrote:[color=green]
      > > Is there an way for a function to know who called him ?[/color][/color]

      Is there another way?
      This way look like:

      int main()
      {
      func1("main"); // but need to chage func1(char*) to
      // func1(const char*)
      return 0;
      }

      Comment

      • baumann@pan

        #4
        Re: Calling function name


        #if DISPLAY_CALLER
        #define func1_hooked() func1(__FUNCTIO N__)
        #else
        #define func1_hooked() func1(0)
        #endif

        now

        in main,

        int main(void)
        {
        func1_hooked();
        return 0;
        }


        baumann@pan wrote:[color=blue]
        > lastest gcc ,vs.net support __FUNCTION__, use it
        >
        > #include <stdio.h>
        >
        > int func1(char * str)
        > {
        > printf("called by %s\n",str);
        > return 0;
        > }
        >
        > int main(void)
        > {
        > func1(__FUNCTIO N__);
        > return 0;
        > }
        >
        > Saroj wrote:[color=green]
        > > Is there an way for a function to know who called him ?[/color][/color]

        Comment

        • baumann@pan

          #5
          Re: Calling function name

          the lastest C standard has __function__ you use as argument

          int func1(char * str)
          {
          printf("called by %s\n",str);
          return 0;
          }
          int main(void)
          {
          func1(__FUNCTIO N__);
          return 0;
          }

          Saroj wrote:[color=blue]
          > Is there an way for a function to know who called him ?[/color]

          Comment

          • Saroj

            #6
            Re: Calling function name

            all this means i should also have a access to modify the function who
            is calling.
            dont think it will work.

            Comment

            • pete

              #7
              Re: Calling function name

              baumann@pan wrote:[color=blue]
              >
              > the lastest C standard has __function__ you use as argument[/color]

              It's not in N869.
              What section is it in, in the last or latest C standard?

              --
              pete

              Comment

              • junky_fellow@yahoo.co.in

                #8
                Re: Calling function name



                Saroj wrote:[color=blue]
                > Is there an way for a function to know who called him ?[/color]

                I think it cannot be done portably. You will need to write
                an implementation specific code for that.
                You will need to know the C calling convention for that
                architecture. For eg. in PowerPC, when a function is called
                the return address is put in Link Register (LR).

                Comment

                • Richard Bos

                  #9
                  Re: Calling function name

                  pete <pfiland@mindsp ring.com> wrote:
                  [color=blue]
                  > baumann@pan wrote:[color=green]
                  > >
                  > > the lastest C standard has __function__ you use as argument[/color]
                  >
                  > It's not in N869.[/color]

                  That's because it's actually spelled __func__.
                  [color=blue]
                  > What section is it in, in the last or latest C standard?[/color]

                  6.4.2.2.

                  Richard

                  Comment

                  • Paul Mesken

                    #10
                    Re: Calling function name

                    On 6 Jun 2005 03:41:33 -0700, "Saroj" <sarojkmohapatr a@gmail.com>
                    wrote:
                    [color=blue]
                    >all this means i should also have a access to modify the function who
                    >is calling.
                    >dont think it will work.[/color]

                    You mean as in "self modifying code"? C cannot modify its own code,
                    only interpreted languages or Assembly (depending on whether the
                    system allows it) might offer this capability.

                    The return address (which can typically be extracted by using inline
                    Assembly, if your compiler allows such a thing) is of little use. It's
                    not the function pointer, only the place (in the calling function)
                    which will be returned to. Also, such a thing is highly unportable.

                    If you want to know which function has called another function then
                    you have to pass some suitable parameter indicating the calling
                    function.

                    Comment

                    • SM Ryan

                      #11
                      Re: Calling function name

                      "Saroj" <sarojkmohapatr a@gmail.com> wrote:
                      # Is there an way for a function to know who called him ?

                      There's no standard way to inspect your own call stack. On some systems
                      you can use the debugger or loader tables used by the debugger.

                      --
                      SM Ryan http://www.rawbw.com/~wyrmwif/
                      I'm not even supposed to be here today.

                      Comment

                      • pete

                        #12
                        Re: Calling function name

                        Richard Bos wrote:[color=blue]
                        >
                        > pete <pfiland@mindsp ring.com> wrote:
                        >[color=green]
                        > > baumann@pan wrote:[color=darkred]
                        > > >
                        > > > the lastest C standard has __function__ you use as argument[/color]
                        > >
                        > > It's not in N869.[/color]
                        >
                        > That's because it's actually spelled __func__.[/color]

                        Thank you.

                        --
                        pete

                        Comment

                        • Keith Thompson

                          #13
                          Re: Calling function name

                          junky_fellow@ya hoo.co.in writes:[color=blue]
                          > Saroj wrote:[color=green]
                          >> Is there an way for a function to know who called him ?[/color]
                          >
                          > I think it cannot be done portably. You will need to write
                          > an implementation specific code for that.
                          > You will need to know the C calling convention for that
                          > architecture. For eg. in PowerPC, when a function is called
                          > the return address is put in Link Register (LR).[/color]

                          That will, at best, give you the address of the caller, not its name.

                          Anything like that is horrendously non-portable, and probably not
                          worth the effort. If that's the solution, try to redefine the
                          problem.

                          --
                          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

                          • Mark McIntyre

                            #14
                            Re: Calling function name

                            On 6 Jun 2005 00:20:53 -0700, in comp.lang.c , "Saroj"
                            <sarojkmohapatr a@gmail.com> wrote:
                            [color=blue]
                            >Is there an way for a function to know who called him ?[/color]

                            Only by rewriting the function signature to include a string which is
                            the name of the caller.

                            There may be system-specific methods such as those used by debuggers,
                            eg inspecting the function call stack, but you'd need to ask in a
                            system specific group about those.
                            --
                            Mark McIntyre
                            CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                            CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                            ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                            http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                            ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                            Comment

                            • Ben Pfaff

                              #15
                              Re: Calling function name

                              "Saroj" <sarojkmohapatr a@gmail.com> writes:
                              [color=blue]
                              > Is there an way for a function to know who called him ?[/color]

                              Not portably. Some specific environments have specific
                              solutions; for example, if you are using the GNU C library, you
                              might be able to find out with backtrace(), but even that won't
                              always tell you the correct answer.

                              In some cases there might not even be a way to find out. For
                              example, if the function that called the current function did so
                              as its last action, then the compiler might have emitted a "jump"
                              instruction instead of a "call" instruction, which in turn would
                              have erased all the obvious evidence that it was the function
                              that did the call. But even the possibility for this is system
                              specific.
                              --
                              int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
                              \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
                              );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
                              );}return 0;}

                              Comment

                              Working...