# How to write a program to track the value of a certain variable at run-time using debug features??? - Linux

This is a discussion on How to write a program to track the value of a certain variable at run-time using debug features??? - Linux ; Hello everyone, I'm totally new to the group and I would like to learn from you. Thank you in advance! I need to write a program to track whether a mathematical function has changed during run-time or not. The program ...

# Thread: How to write a program to track the value of a certain variable at run-time using debug features???

1. ## How to write a program to track the value of a certain variable at run-time using debug features???

Hello everyone,

I'm totally new to the group and I would like to learn from you. Thank

I need to write a program to track whether a mathematical function has
changed during run-time or not. The program should work with any
mathematical function provided by users.

Let's take an example in the C language:
//================================================== =
//The users define the two parameters a, b as two global variables.
The users can define as many parameters as possible and my program
int a;
int b;

//below is the prototype for our mathematical function
void f(vector x);

//at first, an user wants f = 1x+1y, so he sets the value of a, b
accordingly
a=b=1;

double value=f(x); //calculate the value of function f

//now the user want f = 1x +2y, so he set b = 2
b =2;

//and invoke the function f again
value=f(x);
//=================================================

As can be seen from the code above, the function f has changed from f
= x+y to f=x+2y and my program needs to track this at run-time (The
function f, along with its parameters (a and b), will be integrated
with my program).

However, the problem is that my program will NOT KNOW which function
to be provided by the users and which/ how many parameters (defined as
global variables) the function may take

I think that one solution for the problem might be:

1. List all global variables currently used in the program
2. Find out which global variables are being used by f (I can do that
by parsing the source code file of f)
3. Tracking the value of all these global variables to see whether
they have been changed outside of f or not. If they have, then
possibly that f has been changed too.

Now the question is: How to list all global variables and how to track
the value of a certain global variable at run-time???

I wonder whether the existing debuggers under Linux can provide me
with the necessary APIs to answer the question above?

Thank you so much for your help

Best regards,

Thanh

2. ## Re: How to write a program to track the value of a certain variable at run-time using debug features???

trungthanh78@gmail.com wrote:
> I'm totally new to the group and I would like to learn from you. Thank

Okay, one thing up front: please read the descriptions of groups you post
to. Your issue has nothing to do with the development of the Linux system
(i.e. the kernel and closely related tools). In fact, there is nothing in
your question that even makes it relevant for Linux in general because...

> I need to write a program to track whether a mathematical function has
> changed during run-time or not. The program should work with any
> mathematical function provided by users.
>
> Let's take an example in the C language:

.... the C language is inherently portable across systems. In fact, your
question might be best asked in alt.comp.lang.learn.c-c++.

> //The users define the two parameters a, b as two global variables.
> The users can define as many parameters as possible and my program
> int a;
> int b;

Okay, first misunderstanding here: C is a compiled language, so any
variables are defined by _you_ in the code before compilation. You can't
mistake: you already assumed a particular implementation and mixed this

[...]
> As can be seen from the code above, the function f has changed from f
> = x+y to f=x+2y and my program needs to track this at run-time (The
> function f, along with its parameters (a and b), will be integrated
> with my program).
>
> However, the problem is that my program will NOT KNOW which function
> to be provided by the users and which/ how many parameters (defined as
> global variables) the function may take

Give me an example how your program is supposed to behave in action. IOW,
what is the user-input and what is the program output? Collecting
use-cases is a good start for defining the behaviour in general.

> I think that one solution for the problem might be:
>
> 1. List all global variables currently used in the program
> 2. Find out which global variables are being used by f (I can do that
> by parsing the source code file of f)
> 3. Tracking the value of all these global variables to see whether
> they have been changed outside of f or not. If they have, then
> possibly that f has been changed too.

This is possible, but very complicated in C because it is a statically
compiled language. If you want the user to enter code that is to be
executed, you might want to take a look at dynamic languages like Python
instead. If you want to stay with C, you need to learn about the ELF
binary format (which is used by Linux) and you need to learn about memory
protection methods in order to track accesses to variables.

Uli

3. ## Re: How to write a program to track the value of a certain variable at run-time using debug features???

On Mar 18, 10:38 pm, trungthan...@gmail.com wrote:

> I need to write a program to track whether a mathematical function has
> changed during run-time or not. The program should work with any
> mathematical function provided by users.

This really makes no sense. If the user provides you a function, how
can the user change it now that *you* have it? The user could provide
you a new and different function, but I don't understand how the user
could change a function after it has given it to you.

For example, you and I are talking on the phone. You say, "x=2a+b".
How can you "change" that function now that you have given it to me?
Clearly, you can give me a new function by saying, "x=a+2b", but I'll
know the function has changed because I can compare the function you
just gave me to the one I had before.

If you mean, I give you numbers and you give me results and I'm
supposed to tell somehow when the rules you are using to calculate the
results changes, that is impossible in principle.

How could you ever tell if the function changed from "y=3 for all x"
to "y=3 for all x except y is 0 if x is 2.12309732495834509872034897"
unless you were told the function changed?

DS

4. ## Re: How to write a program to track the value of a certain variable at run-time using debug features???

Dear David,

David Schwartz wrote:
> On Mar 18, 10:38 pm, trungthan...@gmail.com wrote:
>
> This really makes no sense. If the user provides you a function, how
> can the user change it now that *you* have it? The user could provide
> you a new and different function, but I don't understand how the user
> could change a function after it has given it to you.
>

Yes it makes no sense if users provides me a function. But in my
situation I will provide them with my algorithm/solver so that they
can integrate it with their functions and source code.

> How could you ever tell if the function changed from "y=3 for all x"
> to "y=3 for all x except y is 0 if x is 2.12309732495834509872034897"
> unless you were told the function changed?
>

If this was the case, then it would be much easier. What I am
concerning is the situation when the routine source code for the
function is the same, but the parameters are changing.

Best regards,

Thanh.

5. ## Re: How to write a program to track the value of a certain variable at run-time using debug features???

"classicalmania" wrote in

> If this was the case, then it would be much easier. What I am
> concerning is the situation when the routine source code for the
> function is the same, but the parameters are changing.

I've been hesitant to respond so far because, as others
have opined, the problem statement seems to make no sense.

Perhaps you have simply failed to mention some aspect of
the problem that would make it more meaningful to the
reader. Could you go through an end-to-end scenario of
how you would expect the user to use this software, including
how the user's function gets "provided" and how it gets
"changed" at run time.

At the high level discussed so far, the only way for the
software to know that the function has changed is for the
mechanism that changes it to tell the software.

MV

--
I do not want replies; please follow-up to the group.

6. ## Re: How to write a program to track the value of a certain variable at run-time using debug features???

On 2007-03-19, trungthanh78@gmail.com wrote:
> Hello everyone,
>
> I'm totally new to the group and I would like to learn from you. Thank
>
> I need to write a program to track whether a mathematical function has
> changed during run-time or not. The program should work with any
> mathematical function provided by users.

the problem is either insoluble, or incompletely specified, consider these
functions (in c):

int foo(double a)
{
return a == 42134892345234856346.58490685042346;
}

int bar(double a)
{
return a == 1232198390213821903821.0399801230912;
}

Bye.
Jasen

7. ## Re: How to write a program to track the value of a certain variable at run-time using debug features???

On Mar 20, 5:25 am, "classicalmania" wrote:

> > On Mar 18, 10:38 pm, trungthan...@gmail.com wrote:

> > This really makes no sense. If the user provides you a function, how
> > can the user change it now that *you* have it? The user could provide
> > you a new and different function, but I don't understand how the user
> > could change a function after it has given it to you.

> Yes it makes no sense if users provides me a function. But in my
> situation I will provide them with my algorithm/solver so that they
> can integrate it with their functions and source code.

The normal way for this to work is when the user hands you a function,
it is your function until you are done with it. If the user breaks
it so that it is no longer a function, that is not your problem.

The next time the user hands you a function, you should not care if it
is the same function as last time or not. So your requirement normally
never comes up.

> > How could you ever tell if the function changed from "y=3 for all x"
> > to "y=3 for all x except y is 0 if x is 2.12309732495834509872034897"
> > unless you were told the function changed?

> If this was the case, then it would be much easier. What I am
> concerning is the situation when the routine source code for the
> function is the same, but the parameters are changing.

I think we are talking past each other. When you say "function" do you
mean in the mathematical sense, in the programming sense, or something
halfway in-between?

I think the simplest solution is simply to require the user to provide
you with a true function (in the mathematical sense) and allow the
user to control the lifetime of that function. If he wants to change
it, he must do it outside the time you use it.

You can have a method the user calls to give you a function (perhaps
returning the user a handle if more than one function might be in use
at a time), methods to solve it and operate on it, and a method to
close it when he is done with that particular function. A "change" to
a function simply makes it another function entirely.

DS

8. ## Re: How to write a program to track the value of a certain variable at run-time using debug features???

trungthanh78@gmail.com writes:

> I need to write a program to track whether a mathematical function has
> changed during run-time or not. The program should work with any
> mathematical function provided by users.

Just to follow up with my 2ยข following on from everyone else's
replies. Note my answer assumes using C++, but could be used with
other languages, including C.

Have you considered using the Command (a.k.a. Despatch) design
pattern?

http://www.exciton.cs.rice.edu/JavaR...ns/command.htm
http://en.wikipedia.org/wiki/Command_pattern
http://www.codeproject.com/books/DesignPatterns.asp

While this is not the same as the example you proposed, it offers some

- The command is an abstract base class; specific "functions" are
derived from it. This:
- allows typeid/dynamic_cast<> typechecking, so you can easily
compare two functions to see if they are the same (simple
equality); you can also compare them to see if they are the same
object by comparing the object address (&) or defining operator==.
- allows any user to write a new function by deriving from this base
class
- functions can take any number of arguments and store as much
internal state as they want; no global variables needed, and the
function which does the evaluation doesn't need to be aware of this
- functions are "objects" (function objects/functors), so you can
keep track of them, create them and destroy them like any other
object

- Your program just uses a pointer/reference to the abstract base
class, and calls a suitable evaluate() method; it could even be
operator() to make the object behave exactly like a function.

There's no need for complex introspection or ptrace/ELF nightmares,
since the things (I think) you want fall out of the design.

Regards,
Roger

--
.''`. Roger Leigh
: :' : Debian GNU/Linux http://people.debian.org/~rleigh/
`. `' Printing on GNU/Linux? http://gutenprint.sourceforge.net/