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



Ulrich Eckhardt <doomster@xxxxxxxx> writes:

trungthanh78@xxxxxxxxx wrote:

Your issue has nothing to do with the development of the Linux system

But if he is looking for linux-specific solution, then his post is
right on topic.

(i.e. the kernel and closely related tools). In fact, there is nothing in

Kernel and closely related tools are *off*-topic in c.o.l.d.apps,
which is about *applications*.

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

One generally hopes that none of mathematical functions change
in ones lifetime.

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?

//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 think that one solution for the problem might be:

This will not work in general (without any additional restrictions on
"user-provided code"), but may work for your particular problem.

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.

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.

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

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
.