Re: [DynInst_API:] Displaying local variables from an inserted Snippet


Date: Wed, 01 Jul 2015 17:20:49 -0500
From: Bill Williams <bill@xxxxxxxxxxx>
Subject: Re: [DynInst_API:] Displaying local variables from an inserted Snippet
On 07/01/2015 02:22 PM, Rogers, Kelly K wrote:
Hello,

I’ve been working on inserting snippets to running processes and
displaying certain information using these snippets, such as the
function, line number, and file name of where it is being inserted as
well as the timestamp, pid of the process, a supplied text string, and
certain local variables if they are in scope at this point. I have the
Following as a simple example:

The sample program is this:

-----------------------------------------------------------------------------

void print_iteration(int value)

{

     int i = value;

//THIS IS WHERE I AM INSERTING THE SNIPPET TO DISPLAY THE INFORMATION

}

int main(int argc, char **argv) {

     int a;

     a = atoi(argv[1]);

     std::cout << "Performing " << a << " iterations." << std::endl;

     sleep(20);

     for(; a>0; --a)

     {

         print_iteration(a);

     }

     return 0;

}

-----------------------------------------------------------------------

Currently, this works and I am getting the following output:

~/Desktop/printf_IO # ./samp 5

Performing 5 iterations.

Filename: samp.c

Function: print_iteration

Line Number: 10

Timestamp: Wed Jul  1 11:56:25 2015

PID: 1684

hello

Variable: int i on line number 9 = 5

Filename: samp.c

Function: print_iteration

Line Number: 10

Timestamp: Wed Jul  1 11:56:25 2015

PID: 1684

hello

Variable: int i on line number 9 = 4

Filename: samp.c

Function: print_iteration

Line Number: 10

Timestamp: Wed Jul  1 11:56:25 2015

PID: 1684

hello

Variable: int i on line number 9 = 3

Filename: samp.c

Function: print_iteration

Line Number: 10

Timestamp: Wed Jul  1 11:56:25 2015

PID: 1684

hello

Variable: int i on line number 9 = 2

Filename: samp.c

Function: print_iteration

Line Number: 10

Timestamp: Wed Jul  1 11:56:25 2015

PID: 1684

hello

Variable: int i on line number 9 = 1

~/Desktop/printf_IO #

----------------------------------------------------------------------------------

When printing the line with the Variable information, I’m simply
creating a funcCallExpr with a printf function call, supplying the
string “Variable:  <type> x on line number y  = %<type>” and then
supplying the BPatch_variableExpr, which is a subclass of type snippet,
that I got from calling function->findVar(“i”), as arguments to the
printf call, then inserting the snippet.

At this point, it all seems to be working correctly.  The problem
arrises when my sample program has multiple local variables and I try to
display all their values.  The following happens:

Here is the new sample code:

--------------------------------------------------------------------------------

void print_iteration(int value)

{

     int i = value;

     int j = i+2;

     int k = i-2;

//I AM INSERTING SNIPPET HERE

}

int main(int argc, char **argv) {

     int a;

     a = atoi(argv[1]);

     std::cout << "Performing " << a << " iterations." << std::endl;

     sleep(20);

     for(; a>0; --a)

     {

         print_iteration(a);

     }

     return 0;

}

--------------------------------------------------------------------------

And this is the new output:

~/Desktop/printf_IO # ./samp 5

Performing 5 iterations.

Filename: samp.c

Function: print_iteration

Line Number: 12

Timestamp: Wed Jul  1 12:17:43 2015

PID: 2666

hello

Variable: int i on line number 9 = 32767

Variable: int j on line number 10 = 0

Variable: int k on line number 11 = 5

Filename: samp.c

Function: print_iteration

Line Number: 12

Timestamp: Wed Jul  1 12:17:43 2015

PID: 2666

hello

Variable: int i on line number 9 = 32767

Variable: int j on line number 10 = 0

Variable: int k on line number 11 = 4

Filename: samp.c

Function: print_iteration

Line Number: 12

Timestamp: Wed Jul  1 12:17:43 2015

PID: 2666

hello

Variable: int i on line number 9 = 32767

Variable: int j on line number 10 = 0

Variable: int k on line number 11 = 3

Filename: samp.c

Function: print_iteration

Line Number: 12

Timestamp: Wed Jul  1 12:17:43 2015

PID: 2666

hello

Variable: int i on line number 9 = 32767

Variable: int j on line number 10 = 0

Variable: int k on line number 11 = 2

Filename: samp.c

Function: print_iteration

Line Number: 12

Timestamp: Wed Jul  1 12:17:43 2015

PID: 2666

hello

Variable: int i on line number 9 = 32767

Variable: int j on line number 10 = 0

Variable: int k on line number 11 = 1

~/Desktop/printf_IO #

------------------------------------------------

As you can see, the values for the variables are not correct.  The value
of i is garbage, the value of j is always 0 and the value of k is
showing as what the value of I should be.

Are the dyninst APIs handling BPatch_variableExpr’s correctly? Or is
there another way I should be looking at to try to print the values of
local variables from an inserted snippet? Or has there been any
work/investigation/experimentation already done regarding trying to do
something like this?

Trying to get locals at/around function exit is potentially problematic; we're using DWARF information to identify locals, and this comes with some address range restrictions. What you're seeing is the sort of thing I would expect for variables that lived on the stack, and instrumentation that's inserted after stack teardown.

You should be able to get at the SymtabAPI localVar associated with the BPatch_variableExpr, which should have VariableLocation information. That in turn has a lowPC and highPC. If you insert a snippet at the minimum highPC among the 3 locals, rather than at function exit (I assume?), you should get sane results.

It would be neat to work out some sort of more flexible point abstraction that could be smart about variableExprs and their constraints and would still match folks' expectations about exit points with respect to control flow/timing, but we haven't gotten there yet.

Any help with this issue would greatly be appreciated.

Regards,

Kelly



_______________________________________________
Dyninst-api mailing list
Dyninst-api@xxxxxxxxxxx
https://lists.cs.wisc.edu/mailman/listinfo/dyninst-api



--
--bw

Bill Williams
Paradyn Project
bill@xxxxxxxxxxx
[← Prev in Thread] Current Thread [Next in Thread→]