1#!/usr/bin/env python 2# Copyright (c) PLUMgrid, Inc. 3# Licensed under the Apache License, Version 2.0 (the "License") 4 5import bcc 6import distutils.version 7import os 8import unittest 9 10def kernel_version_ge(major, minor): 11 # True if running kernel is >= X.Y 12 version = distutils.version.LooseVersion(os.uname()[2]).version 13 if version[0] > major: 14 return True 15 if version[0] < major: 16 return False 17 if minor and version[1] < minor: 18 return False 19 return True 20 21 22@unittest.skipUnless(kernel_version_ge(4,6), "requires kernel >= 4.6") 23class TestStackid(unittest.TestCase): 24 def test_simple(self): 25 b = bcc.BPF(text=""" 26#include <uapi/linux/ptrace.h> 27struct bpf_map; 28BPF_STACK_TRACE(stack_traces, 10240); 29BPF_HASH(stack_entries, int, int); 30BPF_HASH(stub); 31int kprobe__htab_map_lookup_elem(struct pt_regs *ctx, struct bpf_map *map, u64 *k) { 32 int id = stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID); 33 if (id < 0) 34 return 0; 35 int key = 1; 36 stack_entries.update(&key, &id); 37 return 0; 38} 39""") 40 stub = b["stub"] 41 stack_traces = b["stack_traces"] 42 stack_entries = b["stack_entries"] 43 try: x = stub[stub.Key(1)] 44 except: pass 45 k = stack_entries.Key(1) 46 self.assertIn(k, stack_entries) 47 stackid = stack_entries[k] 48 self.assertIsNotNone(stackid) 49 stack = stack_traces[stackid].ip 50 self.assertEqual(b.ksym(stack[0]), b"htab_map_lookup_elem") 51 52 53if __name__ == "__main__": 54 unittest.main() 55