Changing the execution path of methods at runtime

B

blufox

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
 
W

Walter Roberson

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

Not in C, because C does not have methods.

e.g

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() .
 
B

blufox

Not in C, because C does not have methods.
Right. Apologies i meant kernel functions in linux which are usually
called methods.
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
 
J

jacob navia

blufox said:
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
 
B

blufox

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
 
A

ais523

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

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

Keith Thompson

blufox said:
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.
 
B

blufox

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
 
C

CBFalconer

blufox said:
.... snip about 100 lines ...

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/~wijnands/nnq/nquote.html>
<http://www.complang.tuwien.ac.at/anton/mail-news-errors.html>
<http://www.netmeister.org/news/learn2quote2.html>
<http://www.star-one.org.uk/computer/format.htm>
 
M

Mark McIntyre

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

Kenny McCormack

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

Richard

Mark McIntyre said:
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.
Its possible...

http://www.google.de/search?q=c+++m...ls=com.ubuntu:en-US:official&client=firefox-a

Or not.
 
H

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

CBFalconer said:
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.
 
M

Mark McIntyre

Then complain to Mark, not to Richard.

Why? I didn't post the original question.
--
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
 
H

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

Mark said:
Why? I didn't post the original question.
blufox:
You:
I've never heard anything called a method in C or C++.

You were the one who gave the first mention of C++.

As far as I'm concerned your comment is fine. But as far as I'm concerned,
Richard's message is fine too.
 
M

Mark McIntyre

You were the one who gave the first mention of C++.

To forestall the next response which was going to be exactly what we
got.
As far as I'm concerned your comment is fine.

Good. Then why tell people to complain to me? And I didn't go off and
start lecturing people about whether or not C++ actually had methods.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top