Skip to content

Commit f59d623

Browse files
feat: Add DSMIL telemetry level control and annotations
This commit introduces a new command-line option and module flag to control the DSMIL telemetry instrumentation level. It also adds new annotations for various categories like network I/O, crypto, process, file, untrusted data, and error handling. The runtime library is updated to support these new features, including level-based filtering and the ability to log more detailed telemetry events. Co-authored-by: intel <[email protected]>
1 parent b77b868 commit f59d623

File tree

6 files changed

+653
-18
lines changed

6 files changed

+653
-18
lines changed

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ class CodeGenOptions : public CodeGenOptionsBase {
298298
/// file, for example with -save-temps.
299299
std::string MainFileName;
300300

301+
/// DSMIL telemetry instrumentation level: "off", "min", "normal", "debug", "trace"
302+
std::string DSMILTelemetryLevel = "normal";
303+
301304
/// The name for the split debug info file used for the DW_AT_[GNU_]dwo_name
302305
/// attribute in the skeleton CU.
303306
std::string SplitDwarfFile;

clang/include/clang/Options/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,6 +3029,12 @@ def finstrument_function_entry_bare : Flag<["-"], "finstrument-function-entry-ba
30293029
Visibility<[ClangOption, CC1Option]>,
30303030
HelpText<"Instrument function entry only, after inlining, without arguments to the instrumentation call">,
30313031
MarshallingInfoFlag<CodeGenOpts<"InstrumentFunctionEntryBare">>;
3032+
def fdsmil_telemetry_level_EQ : Joined<["-"], "fdsmil-telemetry-level=">,
3033+
Group<f_Group>,
3034+
Visibility<[ClangOption, CC1Option]>,
3035+
HelpText<"Set DSMIL telemetry instrumentation level: off, min, normal, debug, trace">,
3036+
Values<"off,min,normal,debug,trace">,
3037+
MarshallingInfoString<CodeGenOpts<"DSMILTelemetryLevel">, "normal">;
30323038
def fcf_protection_EQ : Joined<["-"], "fcf-protection=">,
30333039
Visibility<[ClangOption, CLOption, CC1Option]>, Group<f_Group>,
30343040
HelpText<"Instrument control-flow architecture protection">, Values<"return,branch,full,none">;

dsmil/include/dsmil_attributes.h

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,147 @@
15961596

15971597
/** @} */
15981598

1599+
/**
1600+
* @defgroup DSMIL_TELEMETRY_ANNOTATIONS Generic Telemetry Annotations (v1.9)
1601+
* @{
1602+
*/
1603+
1604+
/**
1605+
* @brief Mark function for network I/O telemetry
1606+
*
1607+
* Functions annotated with DSMIL_NET_IO are instrumented with telemetry
1608+
* for network operations (connect, send, recv, etc.).
1609+
*
1610+
* Example:
1611+
* @code
1612+
* DSMIL_NET_IO
1613+
* int connect_to_server(const char *host, int port) {
1614+
* // Automatically instrumented with network I/O telemetry
1615+
* return socket_connect(host, port);
1616+
* }
1617+
* @endcode
1618+
*
1619+
* @note Maps to DSMIL_TELEMETRY_NET_IO event type
1620+
* @note Category: "net"
1621+
*/
1622+
#define DSMIL_NET_IO \
1623+
__attribute__((annotate("dsmil.net_io")))
1624+
1625+
/**
1626+
* @brief Mark function for cryptographic operation telemetry
1627+
*
1628+
* Functions annotated with DSMIL_CRYPTO are instrumented with telemetry
1629+
* for cryptographic operations (encrypt, decrypt, sign, verify, etc.).
1630+
*
1631+
* Example:
1632+
* @code
1633+
* DSMIL_CRYPTO
1634+
* int aes_encrypt(const uint8_t *key, const uint8_t *plaintext, uint8_t *ciphertext) {
1635+
* // Automatically instrumented with crypto telemetry
1636+
* return do_aes_encrypt(key, plaintext, ciphertext);
1637+
* }
1638+
* @endcode
1639+
*
1640+
* @note Maps to DSMIL_TELEMETRY_CRYPTO event type
1641+
* @note Category: "crypto"
1642+
*/
1643+
#define DSMIL_CRYPTO \
1644+
__attribute__((annotate("dsmil.crypto")))
1645+
1646+
/**
1647+
* @brief Mark function for process/system operation telemetry
1648+
*
1649+
* Functions annotated with DSMIL_PROCESS are instrumented with telemetry
1650+
* for process/system operations (fork, exec, kill, etc.).
1651+
*
1652+
* Example:
1653+
* @code
1654+
* DSMIL_PROCESS
1655+
* int spawn_child_process(const char *cmd) {
1656+
* // Automatically instrumented with process telemetry
1657+
* return fork_and_exec(cmd);
1658+
* }
1659+
* @endcode
1660+
*
1661+
* @note Maps to DSMIL_TELEMETRY_PROCESS event type
1662+
* @note Category: "process"
1663+
*/
1664+
#define DSMIL_PROCESS \
1665+
__attribute__((annotate("dsmil.process")))
1666+
1667+
/**
1668+
* @brief Mark function for file I/O telemetry
1669+
*
1670+
* Functions annotated with DSMIL_FILE are instrumented with telemetry
1671+
* for file operations (open, read, write, close, etc.).
1672+
*
1673+
* Example:
1674+
* @code
1675+
* DSMIL_FILE
1676+
* FILE* open_config_file(const char *filename) {
1677+
* // Automatically instrumented with file I/O telemetry
1678+
* return fopen(filename, "r");
1679+
* }
1680+
* @endcode
1681+
*
1682+
* @note Maps to DSMIL_TELEMETRY_FILE event type
1683+
* @note Category: "file"
1684+
*/
1685+
#define DSMIL_FILE \
1686+
__attribute__((annotate("dsmil.file")))
1687+
1688+
/**
1689+
* @brief Mark function handling untrusted data
1690+
*
1691+
* Functions annotated with DSMIL_UNTRUSTED are instrumented with telemetry
1692+
* for operations on untrusted data (network input, user input, etc.).
1693+
*
1694+
* Example:
1695+
* @code
1696+
* DSMIL_UNTRUSTED
1697+
* void process_user_input(const char *input) {
1698+
* // Automatically instrumented with untrusted data telemetry
1699+
* validate_and_process(input);
1700+
* }
1701+
* @endcode
1702+
*
1703+
* @note Maps to DSMIL_TELEMETRY_UNTRUSTED event type
1704+
* @note Category: "untrusted"
1705+
* @note Related to DSMIL_UNTRUSTED_INPUT attribute
1706+
*/
1707+
#define DSMIL_UNTRUSTED \
1708+
__attribute__((annotate("dsmil.untrusted")))
1709+
1710+
/**
1711+
* @brief Mark function as error handler
1712+
*
1713+
* Functions annotated with DSMIL_ERROR_HANDLER are instrumented with telemetry
1714+
* for error handling operations. If function name suggests panic (e.g., panic,
1715+
* fatal), emits PANIC events instead of ERROR events.
1716+
*
1717+
* Example:
1718+
* @code
1719+
* DSMIL_ERROR_HANDLER
1720+
* void handle_error(int err_code, const char *msg) {
1721+
* // Automatically instrumented with error telemetry
1722+
* log_error(err_code, msg);
1723+
* }
1724+
*
1725+
* DSMIL_ERROR_HANDLER
1726+
* void panic(const char *msg) {
1727+
* // Automatically emits PANIC events (name suggests panic)
1728+
* abort();
1729+
* }
1730+
* @endcode
1731+
*
1732+
* @note Maps to DSMIL_TELEMETRY_ERROR or DSMIL_TELEMETRY_PANIC event type
1733+
* @note Category: "error"
1734+
*/
1735+
#define DSMIL_ERROR_HANDLER \
1736+
__attribute__((annotate("dsmil.error_handler")))
1737+
1738+
/** @} */
1739+
15991740
/**
16001741
* @defgroup DSMIL_MEMORY Memory and Performance Attributes
16011742
* @{

dsmil/include/dsmil_ot_telemetry.h

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ extern "C" {
2626
* @{
2727
*/
2828

29+
/**
30+
* Telemetry instrumentation levels
31+
* Lattice: off < min < normal < debug < trace
32+
*/
33+
typedef enum {
34+
DSMIL_TELEMETRY_LEVEL_OFF = 0, /**< No telemetry */
35+
DSMIL_TELEMETRY_LEVEL_MIN = 1, /**< Minimal telemetry (safety-critical only) */
36+
DSMIL_TELEMETRY_LEVEL_NORMAL = 2, /**< Normal telemetry (entry probes) */
37+
DSMIL_TELEMETRY_LEVEL_DEBUG = 3, /**< Debug telemetry (entry + exit + timing) */
38+
DSMIL_TELEMETRY_LEVEL_TRACE = 4 /**< Trace telemetry (all + sampling) */
39+
} dsmil_telemetry_level_t;
40+
2941
/**
3042
* OT telemetry event types
3143
*/
@@ -43,7 +55,16 @@ typedef enum {
4355
DSMIL_TELEMETRY_SS7_MSG_TX = 21, /**< SS7 message transmitted */
4456
DSMIL_TELEMETRY_SIGTRAN_MSG_RX = 22, /**< SIGTRAN message received */
4557
DSMIL_TELEMETRY_SIGTRAN_MSG_TX = 23, /**< SIGTRAN message transmitted */
46-
DSMIL_TELEMETRY_SIG_ANOMALY = 24 /**< Signaling anomaly detected */
58+
DSMIL_TELEMETRY_SIG_ANOMALY = 24, /**< Signaling anomaly detected */
59+
60+
// Generic annotation event types (30-36)
61+
DSMIL_TELEMETRY_NET_IO = 30, /**< Network I/O operation */
62+
DSMIL_TELEMETRY_CRYPTO = 31, /**< Cryptographic operation */
63+
DSMIL_TELEMETRY_PROCESS = 32, /**< Process/system operation */
64+
DSMIL_TELEMETRY_FILE = 33, /**< File I/O operation */
65+
DSMIL_TELEMETRY_UNTRUSTED = 34, /**< Untrusted data handling */
66+
DSMIL_TELEMETRY_ERROR = 35, /**< Error handler invocation */
67+
DSMIL_TELEMETRY_PANIC = 36 /**< Panic/fatal error */
4768
} dsmil_telemetry_event_type_t;
4869

4970
/**
@@ -84,6 +105,14 @@ typedef struct {
84105
uint32_t sigtran_rctx; /**< SIGTRAN Routing Context (M3UA/SUA), 0 if not set */
85106
uint8_t ss7_msg_class; /**< MTP3/TCAP/CAP message class (if mapped) */
86107
uint8_t ss7_msg_type; /**< Message type (approximate mapping) */
108+
109+
// Generic annotation fields (for event types 30-36)
110+
const char *category; /**< Event category: "net", "crypto", "process", "file", "untrusted", "error" */
111+
const char *op; /**< Operation name (e.g., "connect", "encrypt", "open") */
112+
int32_t status_code; /**< Status/return code (0 = success, negative = error) */
113+
const char *resource; /**< Resource identifier (e.g., filename, socket, key name) */
114+
const char *error_msg; /**< Error message (if status_code != 0) */
115+
uint64_t elapsed_ns; /**< Elapsed time in nanoseconds (debug/trace levels) */
87116
} dsmil_telemetry_event_t;
88117

89118
/**
@@ -181,6 +210,31 @@ void dsmil_ot_telemetry_shutdown(void);
181210
*/
182211
int dsmil_ot_telemetry_is_enabled(void);
183212

213+
/**
214+
* Get current telemetry level
215+
*
216+
* @return Current telemetry level (combines compile-time and runtime settings)
217+
*
218+
* Combines compile-time level (from module flag) with runtime override
219+
* (from DSMIL_TELEMETRY_LEVEL environment variable). Enforces lattice:
220+
* off < min < normal < debug < trace. Mission profile overrides may
221+
* force minimum levels unless CLI demanded stricter.
222+
*/
223+
dsmil_telemetry_level_t dsmil_telemetry_get_level(void);
224+
225+
/**
226+
* Check if telemetry level allows event category
227+
*
228+
* @param event_type Event type
229+
* @param category Event category (e.g., "net", "crypto", "process")
230+
* @return 1 if allowed, 0 if filtered
231+
*
232+
* Centralized logic for level-based gating. Events are filtered based on
233+
* current telemetry level and event category.
234+
*/
235+
int dsmil_telemetry_level_allows(dsmil_telemetry_event_type_t event_type,
236+
const char *category);
237+
184238
/** @} */
185239

186240
#ifdef __cplusplus

0 commit comments

Comments
 (0)