Skip to content

Instantly share code, notes, and snippets.

@mped-oticon
Created August 8, 2023 13:14
Show Gist options
  • Save mped-oticon/27aa9be1715721fff310e77c1b7cc81b to your computer and use it in GitHub Desktop.
Save mped-oticon/27aa9be1715721fff310e77c1b7cc81b to your computer and use it in GitHub Desktop.
Semantic patch to instrument C code, used in debian_foreign
@remove_traces@
@@
(
- MYTRACE(...);
|
- MYTRACE_CASE(...);
|
- MYTRACE_ELSE_ENTER(...);
|
- MYTRACE_ELSE_LEAVE(...);
|
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(...);
|
- MYTRACE_RETURN_VOID(...);
|
- MYTRACE_SCALAR_S16(...);
|
- MYTRACE_SCALAR_S32(...);
|
- MYTRACE_SCALAR_S64(...);
|
- MYTRACE_SCALAR_S8(...);
|
- MYTRACE_SCALAR_U16(...);
|
- MYTRACE_SCALAR_U32(...);
|
- MYTRACE_SCALAR_U64(...);
|
- MYTRACE_SCALAR_U8(...);
|
- MYTRACE_THEN_ENTER(...);
|
- MYTRACE_THEN_LEAVE(...);
)
// @tracecontrolflow@
// type TR;
// identifier fun;
// expression e1;
// @@
// TR fun(...) { <+...
// + MYTRACE(e1); /* tracecontrolflow */
// e1;
// ...+> }
@trace_init_uchar@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
unsigned char x = e;
+ MYTRACE_SCALAR_U8(x); /* trace_init_uchar */
...> }
@trace_init_u8_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
u8_t x = e;
+ MYTRACE_SCALAR_U8(x); /* trace_init_u8_t */
...> }
@trace_init_u16_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
u16_t x = e;
+ MYTRACE_SCALAR_U16(x); /* trace_init_u16_t */
...> }
@trace_init_u32_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
u32_t x = e;
+ MYTRACE_SCALAR_U32(x); /* trace_init_u32_t */
...> }
@trace_init_u64_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
u64_t x = e;
+ MYTRACE_SCALAR_U64(x); /* trace_init_u64_t */
...> }
@trace_init_uint8_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
uint8_t x = e;
+ MYTRACE_SCALAR_U8(x); /* trace_init_uint8_t */
...> }
@trace_init_uint16_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
uint16_t x = e;
+ MYTRACE_SCALAR_U16(x); /* trace_init_uint16_t */
...> }
@trace_init_uint32_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
uint32_t x = e;
+ MYTRACE_SCALAR_U32(x); /* trace_init_uint32_t */
...> }
@trace_init_uint64_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
uint64_t x = e;
+ MYTRACE_SCALAR_U64(x); /* trace_init_uint64_t */
...> }
@trace_init_int8_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
int8_t x = e;
+ MYTRACE_SCALAR_S8(x); /* trace_init_int8_t */
...> }
@trace_init_int16_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
int16_t x = e;
+ MYTRACE_SCALAR_S16(x); /* trace_init_int16_t */
...> }
@trace_init_int32_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
int32_t x = e;
+ MYTRACE_SCALAR_S32(x); /* trace_init_int32_t */
...> }
@trace_init_int64_t@
type TR; identifier fun;
identifier x;
expression e;
@@
TR fun(...) { <...
int64_t x = e;
+ MYTRACE_SCALAR_S64(x); /* trace_init_int64_t */
...> }
@trace_assignment_scalar_u8@
type TR; identifier fun;
{uint8_t, u8_t, unsigned char} x;
assignment operator ASSOP = { = , += , -= , *= , /= , %= , <<= , >>= , &= , ^= , |= };
expression e;
@@
TR fun(...) { <...
x ASSOP e;
+ MYTRACE_SCALAR_U8(x); /* trace_assignment_scalar_u8 */
...> }
@trace_assignment_scalar_u16@
type TR; identifier fun;
{uint16_t, u16_t} x;
assignment operator ASSOP = { = , += , -= , *= , /= , %= , <<= , >>= , &= , ^= , |= };
expression e;
@@
TR fun(...) { <...
x ASSOP e;
+ MYTRACE_SCALAR_U16(x); /* trace_assignment_scalar_u16 */
...> }
@trace_assignment_scalar_u32@
type TR; identifier fun;
{uint32_t, u32_t, unsigned int} x;
assignment operator ASSOP = { = , += , -= , *= , /= , %= , <<= , >>= , &= , ^= , |= };
expression e;
@@
TR fun(...) { <...
x ASSOP e;
+ MYTRACE_SCALAR_U32(x); /* trace_assignment_scalar_u32 */
...> }
@trace_assignment_scalar_u64@
type TR; identifier fun;
{uint64_t, u64_t} x;
assignment operator ASSOP = { = , += , -= , *= , /= , %= , <<= , >>= , &= , ^= , |= };
expression e;
@@
TR fun(...) { <...
x ASSOP e;
+ MYTRACE_SCALAR_U64(x); /* trace_assignment_scalar_u64 */
...> }
@trace_assignment_scalar_s8@
type TR; identifier fun;
{int8_t, char} x;
assignment operator ASSOP = { = , += , -= , *= , /= , %= , <<= , >>= , &= , ^= , |= };
expression e;
@@
TR fun(...) { <...
x ASSOP e;
+ MYTRACE_SCALAR_S8(x); /* trace_assignment_scalar_s8 */
...> }
@trace_sassignment_scalar_s16@
type TR; identifier fun;
int16_t x;
assignment operator ASSOP = { = , += , -= , *= , /= , %= , <<= , >>= , &= , ^= , |= };
expression e;
@@
TR fun(...) { <...
x ASSOP e;
+ MYTRACE_SCALAR_S16(x); /* trace_sassignment_scalar_s16 */
...> }
@trace_sassignment_scalar_s32@
type TR; identifier fun;
{int32_t, int} x;
assignment operator ASSOP = { = , += , -= , *= , /= , %= , <<= , >>= , &= , ^= , |= };
expression e;
@@
TR fun(...) { <...
x ASSOP e;
+ MYTRACE_SCALAR_S32(x); /* trace_sassignment_scalar_s32 */
...> }
@trace_sassignment_scalar_s64@
type TR; identifier fun;
int64_t x;
assignment operator ASSOP = { = , += , -= , *= , /= , %= , <<= , >>= , &= , ^= , |= };
expression e;
@@
TR fun(...) { <...
x ASSOP e;
+ MYTRACE_SCALAR_S64(x); /* trace_sassignment_scalar_s64 */
...> }
@ensure_return@
identifier fun;
@@
void fun(...) {
... when != return;
+ return; /* ensure_return */
}
@trace_return_val_unknown@
expression e;
@@
+ MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
return e;
@trace_return_void@
@@
+ MYTRACE_RETURN_VOID(); /* trace_return_void */
return;
@trace_return_val_uint8_t@
identifier fun;
expression e;
@@
uint8_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_U8(e); /* trace_return_val_uint8_t */
...> }
@trace_return_val_u8_t@
identifier fun;
expression e;
@@
u8_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_U8(e); /* trace_return_val_u8_t */
...> }
@trace_return_val_uint16_t@
identifier fun;
expression e;
@@
uint16_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_U16(e); /* trace_return_val_uint16_t */
...> }
@trace_return_val_u16_t@
identifier fun;
expression e;
@@
u16_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_U16(e); /* trace_return_val_u16_t */
...> }
@trace_return_val_uint32_t@
identifier fun;
expression e;
@@
uint32_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_U32(e); /* trace_return_val_uint32_t */
...> }
@trace_return_val_u32_t@
identifier fun;
expression e;
@@
u32_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_U32(e); /* trace_return_val_u32_t */
...> }
@trace_return_val_uint64_t@
identifier fun;
expression e;
@@
uint64_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_U64(e); /* trace_return_val_uint64_t */
...> }
@trace_return_val_u64_t@
identifier fun;
expression e;
@@
u64_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_U64(e); /* trace_return_val_u64_t */
...> }
@trace_return_val_int8_t@
identifier fun;
expression e;
@@
int8_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_S8(e); /* trace_return_val_int8_t */
...> }
@trace_return_val_int16_t@
identifier fun;
expression e;
@@
int16_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_S16(e); /* trace_return_val_int16_t */
...> }
@trace_return_val_int32_t@
identifier fun;
expression e;
@@
int32_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_S32(e); /* trace_return_val_int32_t */
...> }
@trace_return_val_int64_t@
identifier fun;
expression e;
@@
int64_t fun(...) { <...
- MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e);
+ MYTRACE_SCALAR_S64(e); /* trace_return_val_int64_t */
...> }
@trace_switch_case@
statement S;
@@
switch (...)
{
case ... :
+ MYTRACE_CASE(); /* trace_switch_case */
S
}
@trace_switch_default@
statement S;
@@
switch (...)
{
default :
+ MYTRACE_CASE(); /* trace_switch_default */
S
}
@trace_branches@
expression e;
@@
(
if (e) {
+ MYTRACE_THEN_ENTER(e); /* trace_branches */
...
+ MYTRACE_THEN_LEAVE(); /* trace_branches */
}
|
if (e) {
+ MYTRACE_THEN_ENTER(e); /* trace_branches */
...
+ MYTRACE_THEN_LEAVE(); /* trace_branches */
} else {
+ MYTRACE_ELSE_ENTER(e); /* trace_branches */
...
+ MYTRACE_ELSE_LEAVE(); /* trace_branches */
}
)
@mped-oticon
Copy link
Author

Example usage:

function instrument
{
    local in="$1"
    local out="${in}.cocci_res"
    
    spatch                         \
        --out-place                \
        --allow-inconsistent-paths \
        --disable-multi-pass       \
        --no-show-diff             \
        --sp-file inject.cocci     \
        "$in"                      \
    && {
        sha256sum "$out"
        meld "$in" "$out"
    }
}

@mped-oticon
Copy link
Author

#define _ISOC99_SOURCE
#include <inttypes.h>
#include <stdio.h>
#define MYTRACE(e)                          printf("MYTRACE                         %20s:%-4i : %-40s %s\n",                __FILE__, __LINE__, __FUNCTION__, #e)
#define MYTRACE_SCALAR_U8(e)                printf("MYTRACE_SCALAR_U8               %20s:%-4i : %-40s <%s> %" PRIu8 "\n",   __FILE__, __LINE__, __FUNCTION__, #e, e )
#define MYTRACE_SCALAR_S8(e)                printf("MYTRACE_SCALAR_S8               %20s:%-4i : %-40s <%s> %" PRId8 "\n",   __FILE__, __LINE__, __FUNCTION__, #e, e )
#define MYTRACE_SCALAR_U16(e)               printf("MYTRACE_SCALAR_U16              %20s:%-4i : %-40s <%s> %" PRIu16 "\n",  __FILE__, __LINE__, __FUNCTION__, #e, e )
#define MYTRACE_SCALAR_S16(e)               printf("MYTRACE_SCALAR_S16              %20s:%-4i : %-40s <%s> %" PRId16 "\n",  __FILE__, __LINE__, __FUNCTION__, #e, e )
#define MYTRACE_SCALAR_U32(e)               printf("MYTRACE_SCALAR_U32              %20s:%-4i : %-40s <%s> %" PRIu32 "\n",  __FILE__, __LINE__, __FUNCTION__, #e, e )
#define MYTRACE_SCALAR_S32(e)               printf("MYTRACE_SCALAR_S32              %20s:%-4i : %-40s <%s> %" PRId32 "\n",  __FILE__, __LINE__, __FUNCTION__, #e, e )
#define MYTRACE_SCALAR_U64(e)               printf("MYTRACE_SCALAR_U64              %20s:%-4i : %-40s <%s> %" PRIu64 "\n",  __FILE__, __LINE__, __FUNCTION__, #e, e )
#define MYTRACE_SCALAR_S64(e)               printf("MYTRACE_SCALAR_S64              %20s:%-4i : %-40s <%s> %" PRId64 "\n",  __FILE__, __LINE__, __FUNCTION__, #e, e )
#define MYTRACE_ELSE_ENTER(e)               printf("MYTRACE_ELSE_ENTER              %20s:%-4i : %-40s %s\n",                __FILE__, __LINE__, __FUNCTION__, #e)
#define MYTRACE_RETURN_VAL_UNKNOWN_TYPE(e)  printf("MYTRACE_RETURN_VAL_UNKNOWN_TYPE %20s:%-4i : %-40s %s\n",                __FILE__, __LINE__, __FUNCTION__, #e)
#define MYTRACE_THEN_ENTER(e)               printf("MYTRACE_THEN_ENTER              %20s:%-4i : %-40s %s\n",                __FILE__, __LINE__, __FUNCTION__, #e)
#define MYTRACE_CASE()                      printf("MYTRACE_CASE                    %20s:%-4i : %-40s \n",                  __FILE__, __LINE__, __FUNCTION__ )
#define MYTRACE_ELSE_LEAVE()                printf("MYTRACE_ELSE_LEAVE              %20s:%-4i : %-40s \n",                  __FILE__, __LINE__, __FUNCTION__ )
#define MYTRACE_THEN_LEAVE()                printf("MYTRACE_THEN_LEAVE              %20s:%-4i : %-40s \n",                  __FILE__, __LINE__, __FUNCTION__ )
#define MYTRACE_RETURN_VOID()               printf("MYTRACE_RETURN_VOID             %20s:%-4i : %-40s \n",                  __FILE__, __LINE__, __FUNCTION__ )

If used in Zephyr, these may be placed in posix_cheats.h.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment