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




Dear Paul,

Thank you very much for your helps

=========================
I need to write a program to track whether a mathematical function has
changed during run-time or not.

Paul Pluzhnikov <ppluzhesnikov-...@xxxxxxxxxxx> wrote:
One generally hopes that none of mathematical functions change
in ones lifetime.

Yes, we all hope that they are stationary, but unfortunately when we
use a mathematical function to describe a real-world model, it is
usually dynamic. For example, the model for the total internet
bandwidth of a server may varies during a day depending on the hours.
========================



========================

The program should work with any
mathematical function provided by users.

Provided by users as source, as object file, as shared library,
or as something else?

As a source code.

Probably my previous example is a poor one. It does not reflect what I
want. Actually I have written an algorithm to solve numerical
functions which are considered as black boxes. The algorithm will be
integrated by users with the code of their numerical functions so that
their program can solve the functions.

The functions of users may change dynamically over time (for example,
they may change from y=x+y to y=x+2y, as shown in the previous post).
My algorithm needs to track this
change without knowning anything about their source code, which means
that it does not know neither the content of the function (or C
routine) nor the variables that the routine may use and/or modify.

The only thing that my algorithm know is that the routine has the
following form: f(vector x)
============================



============================
//The users define the two parameters a, b as two global variables.

Parameters aren't global, and mathematical functions can't depend
on anything that isn't a parameter (by definition).

What you have is a "general C routine", not a "mathematical
function". Please choose your terminology more carefully next time.

I agree. But if users want to change the parameter of the mathematical
functions inside the C routine with format f(vector x), then the only
way for them to do that is to define the parameters of their math
functions as global variables, or as singleton objects
============================




============================
1. List all global variables currently used in the program

You can't in general do that -- object code may have been strip(1)ed,
and all traces of named global variables gone. You may just have
a single "blob" of global data.


But what if I compile the program with debugging assistance, for
example using "gcc -g" flag? In this case some debugger, for example
ddd, can show me the name of global variables? I am correct?

Can my program do the same thing as ddd do?
============================


============================
2. Find out which global variables are being used by f (I can do that
by parsing the source code file of f)

This isn't as easy as it sounds. Consider:

void f() { if (getenv("DO_X")) do_x(); else do_y(); }

This function depends on the state of (heap) memory, pointed to by
_environ global. If you can write a tool that can parse source code
and deduce this dependendency, you are smarter than I am.

You are absolutely right. I already thought about this difficulty
when there are if-then-else condition statement in the source code.
Probably I will only take an approximated approach: finding all global
variables that may be used by the function.
=============================


=============================

3. Tracking the value of all these global variables to see whether
they have been changed outside of f or not.

That part is easy (provided you know the address of the global).
From within the process, you can simply read *addr and compare it
with previously-saved value. From "outside" the process, use
ptrace().

Yes absolutely right, but how to know the address of the global?
=============================

Again, thank you very much for your helps. It is very glad to discuss
with you.

Best regards,

Thanh.

.