Changing the execution path of methods at runtime

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

    Changing the execution path of methods at runtime

    Hi All,

    Can i change the execution path of methods in my process at runtime?

    e.g

    a()->b()->c()->d()->e()

    Now, i want execution to be altered at runtime as -

    a()->b()->myfun1()->myfun2()->myfun3()... ->e()

    Can this be done without changing the actual source file, may be by
    adding a new module at runtime?

    Thanks

    --pradeep

  • Walter Roberson

    #2
    Re: Changing the execution path of methods at runtime

    In article <1180449520.473 891.53760@q19g2 000prn.googlegr oups.com>,
    blufox <2500.pradeep@g mail.comwrote:
    >Can i change the execution path of methods in my process at runtime?
    Not in C, because C does not have methods.

    >e.g
    >a()->b()->c()->d()->e()
    >Now, i want execution to be altered at runtime as -
    >a()->b()->myfun1()->myfun2()->myfun3()... ->e()
    >Can this be done without changing the actual source file, may be by
    >adding a new module at runtime?
    C does not have modules either. Furthermore, C does not offer any
    method of activating new code at runtime.

    Some operating systems provide mechanisms for dynamically loadable
    objects. In the Windows world, these are usually called 'dll';
    in the Unix world, these are usually called 'dso' but the
    Unix operating system calls usually begin with dlopen() .
    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson

    Comment

    • blufox

      #3
      Re: Changing the execution path of methods at runtime

      On May 29, 7:43 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
      wrote:
      In article <1180449520.473 891.53...@q19g2 000prn.googlegr oups.com>,
      >
      blufox <2500.prad...@g mail.comwrote:
      Can i change the execution path of methods in my process at runtime?
      >
      Not in C, because C does not have methods.
      Right. Apologies i meant kernel functions in linux which are usually
      called methods.
      >
      e.g
      a()->b()->c()->d()->e()
      Now, i want execution to be altered at runtime as -
      a()->b()->myfun1()->myfun2()->myfun3()... ->e()
      Can this be done without changing the actual source file, may be by
      adding a new module at runtime?
      >
      C does not have modules either. Furthermore, C does not offer any
      method of activating new code at runtime.
      >
      Some operating systems provide mechanisms for dynamically loadable
      objects. In the Windows world, these are usually called 'dll';
      in the Unix world, these are usually called 'dso' but the
      Unix operating system calls usually begin with dlopen() .
      Right, and i mean a Linux kernel module here.
      Essentially it is C(with loads of GCCism) i guess.

      So, is it possible using modules in unix/linux then. i must ask?

      Thanks for the reply
      --pradeep
      --
      Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson

      Comment

      • jacob navia

        #4
        Re: Changing the execution path of methods at runtime

        blufox wrote:
        Hi All,
        >
        Can i change the execution path of methods in my process at runtime?
        >
        e.g
        >
        a()->b()->c()->d()->e()
        >
        Now, i want execution to be altered at runtime as -
        >
        a()->b()->myfun1()->myfun2()->myfun3()... ->e()
        >
        Can this be done without changing the actual source file, may be by
        adding a new module at runtime?
        >
        Thanks
        >
        --pradeep
        >
        Hi

        a()->b()->c()->d()->e()

        means (in C):

        call function a(), that returns some pointer to a
        structure that has a field called "b". This field is a function
        pointer. Call that, and that will return some structure with
        a field "c". That field is a function pointer...

        etc

        Note that the type of the return of a() is known at compile
        time. The compiler has then calculated at which offset the "b"
        pointer is, and generates code to access that pointer at that
        offset. You can't change that. You can only change the value
        in the field "b", in the structure returned by a().

        If a returns the same structure but with a function pointer to
        "myfun", the generated code will work the same, but you will
        end calling another function, not the one that you were calling

        This requires a modification at compile time to a() or to b(), etc.

        Somehow those functions will need to figure out when they should put
        a certain function pointer in field "b" or not.

        But all this looks far too complicated.

        WHAT are you trying to do?

        Explain that first.

        jacob

        Comment

        • blufox

          #5
          Re: Changing the execution path of methods at runtime

          On May 29, 8:01 pm, jacob navia <j...@jacob.rem comp.frwrote:
          blufox wrote:
          Hi All,
          >
          Can i change the execution path of methods in my process at runtime?
          >
          e.g
          >
          a()->b()->c()->d()->e()
          >
          Now, i want execution to be altered at runtime as -
          >
          a()->b()->myfun1()->myfun2()->myfun3()... ->e()
          >
          Can this be done without changing the actual source file, may be by
          adding a new module at runtime?
          >
          Thanks
          >
          --pradeep
          >
          Hi
          >
          a()->b()->c()->d()->e()
          >
          means (in C):
          >
          call function a(), that returns some pointer to a
          structure that has a field called "b". This field is a function
          pointer. Call that, and that will return some structure with
          a field "c". That field is a function pointer...
          >
          etc
          >
          Note that the type of the return of a() is known at compile
          time. The compiler has then calculated at which offset the "b"
          pointer is, and generates code to access that pointer at that
          offset. You can't change that. You can only change the value
          in the field "b", in the structure returned by a().
          >
          If a returns the same structure but with a function pointer to
          "myfun", the generated code will work the same, but you will
          end calling another function, not the one that you were calling
          >
          This requires a modification at compile time to a() or to b(), etc.
          >
          Somehow those functions will need to figure out when they should put
          a certain function pointer in field "b" or not.
          >
          But all this looks far too complicated.
          >
          WHAT are you trying to do?
          >
          Explain that first.
          Oops i messed it up i must admit. :-(
          No it is not pointer dereferencing literally here.
          Just to illustrate the flow of program here, i used arrows "->" .

          What I intend to do?

          I want to invoke my function without changing the actual source code
          at runtime.

          Is this somehow possible on a Linux system?

          Thanks

          --pradeep
          >
          jacob

          Comment

          • ais523

            #6
            Re: Changing the execution path of methods at runtime

            On May 29, 4:07 pm, blufox <2500.prad...@g mail.comwrote:
            On May 29, 8:01 pm, jacob navia <j...@jacob.rem comp.frwrote:
            >
            >
            >
            blufox wrote:
            Hi All,
            >
            Can i change the execution path of methods in my process at runtime?
            >
            e.g
            >
            a()->b()->c()->d()->e()
            >
            Now, i want execution to be altered at runtime as -
            >
            a()->b()->myfun1()->myfun2()->myfun3()... ->e()
            >
            Can this be done without changing the actual source file, may be by
            adding a new module at runtime?
            >
            Thanks
            >
            --pradeep
            >
            Hi
            >
            a()->b()->c()->d()->e()
            >
            means (in C):
            >
            call function a(), that returns some pointer to a
            structure that has a field called "b". This field is a function
            pointer. Call that, and that will return some structure with
            a field "c". That field is a function pointer...
            >
            etc
            >
            Note that the type of the return of a() is known at compile
            time. The compiler has then calculated at which offset the "b"
            pointer is, and generates code to access that pointer at that
            offset. You can't change that. You can only change the value
            in the field "b", in the structure returned by a().
            >
            If a returns the same structure but with a function pointer to
            "myfun", the generated code will work the same, but you will
            end calling another function, not the one that you were calling
            >
            This requires a modification at compile time to a() or to b(), etc.
            >
            Somehow those functions will need to figure out when they should put
            a certain function pointer in field "b" or not.
            >
            But all this looks far too complicated.
            >
            WHAT are you trying to do?
            >
            Explain that first.
            >
            Oops i messed it up i must admit. :-(
            No it is not pointer dereferencing literally here.
            Just to illustrate the flow of program here, i used arrows "->" .
            >
            What I intend to do?
            >
            I want to invoke my function without changing the actual source code
            at runtime.
            >
            Is this somehow possible on a Linux system?
            >
            Thanks
            >
            --pradeep
            >
            >
            >
            jacob
            It's just about possible using portable C, but only if you design the
            source file to make it possible. For instance:

            /* whichfunc.c */
            extern void c(void);
            extern void myfun1(void);
            void (*func)()=c; /* =myfun1; */

            /* prog.c */
            extern void (*func)();

            void a(void)
            {
            b();
            }

            void b(void)
            {
            (*func)(); /* the key line */
            }

            /* ... whatever ... */

            Then you can link both files together using whatever implementation-
            specific method your implementation provides (if you're on Linux,
            you're quite possibly using gcc, in which case you specify both files
            on the command line or probably use a makefile if it's a complex
            project). Changing func will change the path of execution b takes.

            Of course, modifying the source code is nearly always going to be
            easier (the only advantage of doing it like this is that you can
            change the flow pattern at runtime if you want to, by assigning to
            func).

            In the general case, in which you haven't deliberately written the
            source code in this unusual form, you can't substitute a call to one
            function with another using what's available in standard C89/C99.
            --
            ais523

            Comment

            • Keith Thompson

              #7
              Re: Changing the execution path of methods at runtime

              blufox <2500.pradeep@g mail.comwrites:
              On May 29, 7:43 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
              wrote:
              >In article <1180449520.473 891.53...@q19g2 000prn.googlegr oups.com>,
              >>
              >blufox <2500.prad...@g mail.comwrote:
              >Can i change the execution path of methods in my process at runtime?
              >>
              >Not in C, because C does not have methods.
              Right. Apologies i meant kernel functions in linux which are usually
              called methods.
              They are? That's news to me.

              In any case, you should ask on a Linux-specific newsgroup.

              --
              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."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • blufox

                #8
                Re: Changing the execution path of methods at runtime

                On May 30, 3:00 am, Keith Thompson <k...@mib.orgwr ote:
                blufox <2500.prad...@g mail.comwrites:
                On May 29, 7:43 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
                wrote:
                In article <1180449520.473 891.53...@q19g2 000prn.googlegr oups.com>,
                >
                blufox <2500.prad...@g mail.comwrote:
                Can i change the execution path of methods in my process at runtime?
                >
                Not in C, because C does not have methods.
                Right. Apologies i meant kernel functions in linux which are usually
                called methods.
                >
                They are? That's news to me.
                Yes they are called methods in the kernel AFAIK.
                May be i am misunderstandin g it.
                >
                In any case, you should ask on a Linux-specific newsgroup.
                Tried that, didn't get satisfying answers so resorted to clc.
                I 'll give another try though.

                Thank you
                --psr
                >
                --
                Keith Thompson (The_Other_Keit h) k...@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."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • blufox

                  #9
                  Re: Changing the execution path of methods at runtime

                  On May 29, 8:30 pm, ais523 <ais...@bham.ac .ukwrote:
                  On May 29, 4:07 pm, blufox <2500.prad...@g mail.comwrote:
                  >
                  >
                  >
                  On May 29, 8:01 pm, jacob navia <j...@jacob.rem comp.frwrote:
                  >
                  blufox wrote:
                  Hi All,
                  >
                  Can i change the execution path of methods in my process at runtime?
                  >
                  e.g
                  >
                  a()->b()->c()->d()->e()
                  >
                  Now, i want execution to be altered at runtime as -
                  >
                  a()->b()->myfun1()->myfun2()->myfun3()... ->e()
                  >
                  Can this be done without changing the actual source file, may be by
                  adding a new module at runtime?
                  >
                  Thanks
                  >
                  --pradeep
                  >
                  Hi
                  >
                  a()->b()->c()->d()->e()
                  >
                  means (in C):
                  >
                  call function a(), that returns some pointer to a
                  structure that has a field called "b". This field is a function
                  pointer. Call that, and that will return some structure with
                  a field "c". That field is a function pointer...
                  >
                  etc
                  >
                  Note that the type of the return of a() is known at compile
                  time. The compiler has then calculated at which offset the "b"
                  pointer is, and generates code to access that pointer at that
                  offset. You can't change that. You can only change the value
                  in the field "b", in the structure returned by a().
                  >
                  If a returns the same structure but with a function pointer to
                  "myfun", the generated code will work the same, but you will
                  end calling another function, not the one that you were calling
                  >
                  This requires a modification at compile time to a() or to b(), etc.
                  >
                  Somehow those functions will need to figure out when they should put
                  a certain function pointer in field "b" or not.
                  >
                  But all this looks far too complicated.
                  >
                  WHAT are you trying to do?
                  >
                  Explain that first.
                  >
                  Oops i messed it up i must admit. :-(
                  No it is not pointer dereferencing literally here.
                  Just to illustrate the flow of program here, i used arrows "->" .
                  >
                  What I intend to do?
                  >
                  I want to invoke my function without changing the actual source code
                  at runtime.
                  >
                  Is this somehow possible on a Linux system?
                  >
                  Thanks
                  >
                  --pradeep
                  >
                  jacob
                  >
                  It's just about possible using portable C, but only if you design the
                  source file to make it possible. For instance:
                  >
                  /* whichfunc.c */
                  extern void c(void);
                  extern void myfun1(void);
                  void (*func)()=c; /* =myfun1; */
                  >
                  /* prog.c */
                  extern void (*func)();
                  >
                  void a(void)
                  {
                  b();
                  >
                  }
                  >
                  void b(void)
                  {
                  (*func)(); /* the key line */
                  >
                  }
                  >
                  /* ... whatever ... */
                  >
                  Then you can link both files together using whatever implementation-
                  specific method your implementation provides (if you're on Linux,
                  you're quite possibly using gcc, in which case you specify both files
                  on the command line or probably use a makefile if it's a complex
                  project). Changing func will change the path of execution b takes.
                  >
                  Of course, modifying the source code is nearly always going to be
                  easier (the only advantage of doing it like this is that you can
                  change the flow pattern at runtime if you want to, by assigning to
                  func).
                  >
                  In the general case, in which you haven't deliberately written the
                  source code in this unusual form, you can't substitute a call to one
                  function with another using what's available in standard C89/C99.
                  This is fruitful.
                  I ll look into this and see if i can get something concrete from this
                  hint.

                  Thanks a ton for the help.

                  --psr
                  --
                  ais523

                  Comment

                  • CBFalconer

                    #10
                    Re: Changing the execution path of methods at runtime

                    blufox wrote:
                    ais523 <ais...@bham.ac .ukwrote:
                    >
                    .... snip about 100 lines ...
                    >>
                    >Of course, modifying the source code is nearly always going to be
                    >easier (the only advantage of doing it like this is that you can
                    >change the flow pattern at runtime if you want to, by assigning to
                    >func).
                    >>
                    >In the general case, in which you haven't deliberately written the
                    >source code in this unusual form, you can't substitute a call to one
                    >function with another using what's available in standard C89/C99.
                    >
                    This is fruitful. I ll look into this and see if i can get something
                    concrete from this hint.
                    Please snip irrelevant data from the quoted portion of your
                    replies. The links below (in my sig) may be helpful.

                    --
                    Some useful links on quoting:
                    <http://www.xs4all.nl/%7ewijnands/nnq/nquote.html>
                    <http://www.complang.tu wien.ac.at/anton/mail-news-errors.html>
                    <http://www.netmeister. org/news/learn2quote2.ht ml>
                    <http://www.star-one.org.uk/computer/format.htm>



                    --
                    Posted via a free Usenet account from http://www.teranews.com

                    Comment

                    • Mark McIntyre

                      #11
                      Re: Changing the execution path of methods at runtime

                      On 29 May 2007 20:42:00 -0700, in comp.lang.c , blufox
                      <2500.pradeep@g mail.comwrote:
                      >On May 30, 3:00 am, Keith Thompson <k...@mib.orgwr ote:
                      >blufox <2500.prad...@g mail.comwrites:
                      Right. Apologies i meant kernel functions in linux which are usually
                      called methods.
                      >They are? That's news to me.
                      >Yes they are called methods in the kernel AFAIK.
                      I've never heard anything called a method in C or C++. Some other
                      languages such as VB and the ilk have methods.
                      >May be i am misunderstandin g it.
                      Its possible...

                      --
                      Mark McIntyre

                      "Debugging is twice as hard as writing the code in the first place.
                      Therefore, if you write the code as cleverly as possible, you are,
                      by definition, not smart enough to debug it."
                      --Brian Kernighan

                      Comment

                      • Kenny McCormack

                        #12
                        Re: Changing the execution path of methods at runtime

                        In article <5mvr53570pi03q amr05lmtfa5au3c k593d@4ax.com>,
                        Mark McIntyre <markmcintyre@s pamcop.netwrote :
                        >On 29 May 2007 20:42:00 -0700, in comp.lang.c , blufox
                        ><2500.pradeep@ gmail.comwrote:
                        >
                        >>On May 30, 3:00 am, Keith Thompson <k...@mib.orgwr ote:
                        >>blufox <2500.prad...@g mail.comwrites:
                        >
                        >Right. Apologies i meant kernel functions in linux which are usually
                        >called methods.
                        >
                        >>They are? That's news to me.
                        >
                        >>Yes they are called methods in the kernel AFAIK.
                        >
                        >I've never heard anything called a method in C or C++. Some other
                        >languages such as VB and the ilk have methods.
                        Of course you have (heard someone refer to C++ as having methods).
                        That part of your statement (that "I've never heard...") is plain and
                        simply a lie.

                        The more substantive question is whether or not C++ has methods. Pretty
                        clearly, it does (although of course this is all OT here), but I would
                        imagine that you or one of your buddies will come up with some bit of
                        sophistry to explain why it doesn't.

                        Comment

                        • Richard

                          #13
                          Re: Changing the execution path of methods at runtime

                          Mark McIntyre <markmcintyre@s pamcop.netwrite s:
                          On 29 May 2007 20:42:00 -0700, in comp.lang.c , blufox
                          <2500.pradeep@g mail.comwrote:
                          >
                          >>On May 30, 3:00 am, Keith Thompson <k...@mib.orgwr ote:
                          >>blufox <2500.prad...@g mail.comwrites:
                          >
                          >Right. Apologies i meant kernel functions in linux which are usually
                          >called methods.
                          >
                          >>They are? That's news to me.
                          >
                          >>Yes they are called methods in the kernel AFAIK.
                          >
                          I've never heard anything called a method in C or C++. Some other
                          languages such as VB and the ilk have methods.
                          Then you are purposely complicating things for some reason or have not,
                          in any shape or form, had any worthwhile experience in C++.

                          The term "method" is widely used to C++. Not necessarily in the Linux
                          kernel of course since that is C and Assembler.
                          >
                          >>May be i am misunderstandin g it.
                          >
                          Its possible...


                          Or not.

                          Comment

                          • CBFalconer

                            #14
                            Re: Changing the execution path of methods at runtime

                            Richard wrote:
                            Mark McIntyre <markmcintyre@s pamcop.netwrite s:
                            >
                            .... snip ...
                            >>
                            >I've never heard anything called a method in C or C++. Some other
                            >languages such as VB and the ilk have methods.
                            >
                            Then you are purposely complicating things for some reason or have
                            not, in any shape or form, had any worthwhile experience in C++.
                            >
                            The term "method" is widely used to C++. Not necessarily in the
                            Linux kernel of course since that is C and Assembler.
                            You have obviously failed to read the name of this newsgroup. It
                            is "comp.lang. c". Note the absence of ++. C++ is another
                            language, and has its own newsgroup, where you may discuss it.
                            Meanwhile C++ is off-topic here.

                            --
                            <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
                            <http://www.securityfoc us.com/columnists/423>
                            <http://www.aaxnet.com/editor/edit043.html>
                            <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
                            cbfalconer at maineline dot net



                            --
                            Posted via a free Usenet account from http://www.teranews.com

                            Comment

                            • Harald van =?UTF-8?B?RMSzaw==?=

                              #15
                              Re: Changing the execution path of methods at runtime

                              CBFalconer wrote:
                              Richard wrote:
                              >Mark McIntyre <markmcintyre@s pamcop.netwrite s:
                              >>
                              ... snip ...
                              >>>
                              >>I've never heard anything called a method in C or C++. Some other
                              >>languages such as VB and the ilk have methods.
                              >>
                              >Then you are purposely complicating things for some reason or have
                              >not, in any shape or form, had any worthwhile experience in C++.
                              >>
                              >The term "method" is widely used to C++. Not necessarily in the
                              >Linux kernel of course since that is C and Assembler.
                              >
                              You have obviously failed to read the name of this newsgroup. It
                              is "comp.lang. c". Note the absence of ++. C++ is another
                              language, and has its own newsgroup, where you may discuss it.
                              Meanwhile C++ is off-topic here.
                              Then complain to Mark, not to Richard.

                              Comment

                              Working...