Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log whether a global variable has been read or written

Tags:

c

algorithm

Requirement:

Given a C program I have to identify whether the functions accessing global variables are reading them or writing them.

Example code:

#include <stdio.h>

/* global variable declaration */
int g = 20;

int main()
{

    /* writing the global variable */
    g = 10;

    /* reading the global variable */
    printf ("value of g = %d\n",  g);

    return 0;
}

Executing the above code I want to generate a log file in the below format:

1- Global variable a written in function main() "TIME_STAMP"
2- Global variable a read in function main() "TIME_STAMP"

Research:

I am cetainly able to acheive this by doing a static analysis of source code as per below logic:

  • Go through the c code and identify the statements where the global variable is read.
  • Then analysis the c code statement to identify if it is a read or write statement.(Checking if ++ or -- operator is used with global variable or any assignemnt has been made to the global variable)
  • Add a log statement above the identified statement which will execute along with this statement execution.

This is not a proper implementation.

Some studies:

I have gone through how debuggers are able to capture information.

Some links in the internet: How to catch a memory write and call function with address of write

like image 419
Sanjit Kumar Mishra Avatar asked Dec 15 '16 07:12

Sanjit Kumar Mishra


People also ask

What does globals () do in Python?

The globals() function returns the global symbol table as a dictionary.

How do you indicate that a variable is a global variable?

Variables that are created outside of a function (as in all of the examples above) are known as global variables. Global variables can be used by everyone, both inside of functions and outside.

How do I check if a global variable exists?

To check if a global variable exists or not in Python, we can use the built-in globals() function. The globals() function returns a dictionary containing the variables that are defined inside the global namespace.

Is console log a global object?

We can access the global object in node using the global keyword: console. log(global); The global object exposes a variety of useful properties about the environment.


1 Answers

Not completely answering your question, but to just log access you could do:

#include <stdio.h>

int g = 0;

#define g (*(fprintf(stderr, "accessing g from %s. g = %d\n", __FUNCTION__, g), &g))

void foo(void)
{
  g = 2;
  printf("g=%d\n", g);
}

void bar(void)
{
  g = 3;
  printf("g=%d\n", g);
}

int main(void)
{
  printf("g=%d\n", g);
  g = 1;
  foo();
  bar();
  printf("g=%d\n", g);
}

Which would print:

accessing g from main. g = 0
g=0
accessing g from main. g = 0
accessing g from foo. g = 1
accessing g from foo. g = 2
g=2
accessing g from bar. g = 2
accessing g from bar. g = 3
g=3
accessing g from main. g = 3
g=3
like image 62
alk Avatar answered Oct 25 '22 08:10

alk