Studio:How do I access global variables?

From STRIDE Wiki
Revision as of 17:38, 4 February 2008 by Root (talk | contribs)
Jump to: navigation, search

STRIDE allows you to call functions and send messages on the target, which is in most cases all you need for validation. However, in some cases it might be necessary to read or modify global variables or certain memory locations. If there are API functions available to access the global variables or read memory, those APIs could be exposed to STRIDE and used. If no such API calls are available, they can be added; this requires changes to the target code. Although STRIDE doesn’t directly support accessing target memory with simple helper functions, you can easily access target information without changing the target code by adding code to a header file containing SCL pragmas.

This header file would then be included in the STRIDE workspace, and therefore included in the intercept module. It would not be required to include the header file in any of the customer target code, nor would modifications to the customer code be required. Since the function is known to the intercept module and the intercept module is the only one calling the function, it could be static and therefore have no impact on the rest of the code. The following demonstrates a header file which is included in the STRIDE workspace and subsequently in the intercept module; the function is defined as static, since its scope is limited to the intercept module.

Example: Reading a number of bytes from the target

static void SCL_get_num_bytes ( void *ptr, unsigned char* data, int num_bytes)

{

memcpy ( data, ptr, num_bytes );

}

Reading and writing global data

Another application is to read and write global data. For example, we could have a global state variable. Either the header file which defines the global state variable could be included, or an external definition could be added to the header file. The first option is preferred; in some cases the global state variable definition could be in a .c file.

Example: STRIDE header file

/* extern definition for global */

extern int global_state

/* helper function to set the state */

static void SCL_set_state(int state)

{

global_state = state;

}

/* helper function to get (read) the state */

static int SCL_get_state(void)

{

return global_state;

}