Studio:How do I access global variables?: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
No edit summary
m (Text replace - 'Category:SCL' to 'Category:Studio:SCL')
 
(9 intermediate revisions by 4 users not shown)
Line 3: Line 3:
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.
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'''
==Example: Reading a number of bytes from the target==
 
<source lang="c">
<code>static __inline__ void SCL_get_num_bytes ( void *ptr, unsigned char* data, int num_bytes)</code><br><br>
static void SCL_get_num_bytes ( void *ptr, unsigned char* data, int num_bytes)
<code>{ </code><br><br>
{
<code>    memcpy ( data, ptr, num_bytes ); </code><br><br>
    memcpy ( data, ptr, num_bytes );
<code>} </code>
}
 
</source>
'''Reading and writing global data'''
==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.
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'''
==Example: STRIDE header file==
 
<source lang="c">
<code>/* extern definition for global */</code><br><br>
/* extern definition for global */
<code>extern int global_state</code><br><br>
extern int global_state
<code>/* helper function to set the state */</code><br><br>
/* helper function to set the state */
<code>static __inline__ void SCL_set_state(int state)</code><br><br>
static void SCL_set_state(int state)
<code> { </code><br><br>
{  
<code> global_state = state; </code><br><br>
    global_state = state;  
<code> } </code><br><br>
}  
<code> /* helper function to get (read) the state */ </code><br><br>
/* helper function to get (read) the state */  
<code> static __inline__ int SCL_get_state(void) </code><br><br>
static int SCL_get_state(void)  
<code> { </code><br><br>
{  
<code> return global_state;</code><br><br>
  return global_state;
<code> } </code><br><br>


[[Category:KB]]
}
</source>
[[Category:Studio:SCL]]

Latest revision as of 16:13, 21 August 2009

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;

}