Skip to main content

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.
Return Value: None (void) Behavior:
  • 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
Thread Safety: Safe to call once at initialization. Do not call multiple times. Example:

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.
Return Value:
  • 0 on success (never reached in normal operation due to infinite loop)
  • 1 on failure (initialization error)
Behavior:
  1. Initializes BPF subsystem with the provided program
  2. Attaches kprobe to tcp_set_state kernel function
  3. Opens perf buffer named TABLE (“tcpEvents”)
  4. Frees BCC compiler memory
  5. Sets status flag to 1 (ready)
  6. Enters infinite loop polling the perf buffer
  7. Calls handle_output() callback when events arrive
Error Handling:
  • Prints error messages to stderr on failure
  • Calls exit(1) on any initialization error
Thread Safety: Intended to run in dedicated thread spawned by 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.
Parameters: None Return Value: 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_t to consumer-facing tcp_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_t struct by value
Thread Safety: Thread-safe. Uses mutex and condition variable for synchronization. Blocking Behavior:
This function blocks indefinitely until an event is available. Design your application to handle blocking (e.g., dedicate a thread to event collection or use in an event loop).
Example:

cleanup()

Detaches the BPF probe and cleans up resources.
Parameters: None Return Value: None (void) Behavior:
  • Prints "Cleaning up!"
  • Detaches kprobe from tcp_set_state kernel function
  • Cancels BPF polling thread (if active)
  • Cancels netlink probe thread (if active)
  • Calls exit(1) if detach fails
Thread Safety: Safe to call from main thread. Uses pthread_cancel() to terminate background threads. Example:
cleanup() calls exit(1) if the kprobe detachment fails. Ensure critical data is saved before calling.

getStatus()

Returns the initialization status of the BPF probe.
Parameters: None Return Value:
  • 0 - BPF probe not yet initialized
  • 1 - BPF probe initialized and active
Behavior:
  • Acquires read lock on status variable
  • Returns current status value
  • Releases read lock
Thread Safety: Thread-safe. Uses read-write 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.
Return Value: None (void) Behavior:
  • Prints "Array: " followed by the string content to stdout
  • Appends newline
Thread Safety: Not thread-safe (uses printf without synchronization). Example:

Function Call Sequence

Typical initialization and usage sequence: