Function Reference
AddProbe()
Initializes and starts the TCP event monitoring system in a background thread.const char *
required
Path to the BPF program source file (C code) to be compiled and loaded into the kernel. This program defines the eBPF probe logic for intercepting TCP state changes.
- Prints version information:
"tcpTracer Ver 1.03e with BCC <version>" - Spawns a detached thread that calls
setupBPF() - Returns immediately (non-blocking)
- Background thread continues indefinitely until
cleanup()is called
setupBPF()
Performs the actual BPF initialization, probe attachment, and event polling (runs in background thread).const char *
required
Path to the BPF program source file.
0on success (never reached in normal operation due to infinite loop)1on failure (initialization error)
- Initializes BPF subsystem with the provided program
- Attaches kprobe to
tcp_set_statekernel function - Opens perf buffer named
TABLE(“tcpEvents”) - Frees BCC compiler memory
- Sets status flag to
1(ready) - Enters infinite loop polling the perf buffer
- Calls
handle_output()callback when events arrive
- Prints error messages to stderr on failure
- Calls
exit(1)on any initialization error
AddProbe(). Do not call directly from multiple threads.
Example (typically called internally):
DequeuePerfEvent()
Returns the next TCP event from the queue, blocking if no events are available.struct tcp_event_t containing enriched TCP connection event data (see Data Structures for field details)
Behavior:
- Blocking: Waits on condition variable until events are available
- Lazy-initializes netlink probe thread on first call
- Dequeues event from front of queue (FIFO)
- Converts internal
event_tto consumer-facingtcp_event_t:- Adjusts timestamps from boot-relative to absolute epoch nanoseconds
- Converts binary IP addresses to string format (SADDR/DADDR)
- Copies process name (task)
- Reclaims memory for internal event structure
- Returns populated
tcp_event_tstruct by value
cleanup()
Detaches the BPF probe and cleans up resources.- Prints
"Cleaning up!" - Detaches kprobe from
tcp_set_statekernel function - Cancels BPF polling thread (if active)
- Cancels netlink probe thread (if active)
- Calls
exit(1)if detach fails
pthread_cancel() to terminate background threads.
Example:
getStatus()
Returns the initialization status of the BPF probe.0- BPF probe not yet initialized1- BPF probe initialized and active
- Acquires read lock on status variable
- Returns current status value
- Releases read lock
pthread_rwlock_t).
Example:
printCharArray()
Utility function to print a character array (debugging).const char *
required
Pointer to null-terminated character array to print.
- Prints
"Array: "followed by the string content to stdout - Appends newline