Last active
February 27, 2023 16:24
-
-
Save mauriciovasquezbernal/603435f2fb4582f84d8ae2ad7124f53c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) | |
/* Copyright (c) 2020 Facebook */ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/resource.h> | |
#include "bpf/libbpf.h" | |
#include "bpf/btf.h" | |
#include "assert.h" | |
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) | |
{ | |
return vfprintf(stderr, format, args); | |
} | |
int main(int argc, char **argv) | |
{ | |
libbpf_set_strict_mode(LIBBPF_STRICT_ALL); | |
/* Set up libbpf errors and debug info callback */ | |
libbpf_set_print(libbpf_print_fn); | |
struct btf *btf; | |
int ret, myint, myintvar, mydatasec, mystruct, mytypedef, mystructvar; | |
// This test fails as vars are defined after datasec. | |
// Type IDs: | |
// 1: datasec | |
// 2: mystructvar | |
// 3: myintvar | |
// 4: mystruct | |
// 5: int | |
// 6: typedef | |
btf = btf__new_empty(); | |
assert(btf != NULL); | |
mydatasec = btf__add_datasec(btf, "mydatasec", 4+4); | |
assert(mydatasec > 0); | |
ret = btf__add_datasec_var_info(btf, 2, 0, 4); | |
assert(ret == 0); | |
ret = btf__add_datasec_var_info(btf, 3, 4, 4); | |
assert(ret == 0); | |
mystructvar = btf__add_var(btf, "mystructvar", BTF_VAR_STATIC, 4); | |
assert(mystructvar > 0); | |
myintvar = btf__add_var(btf, "myintvar", BTF_VAR_STATIC, 6); | |
assert(myintvar > 0); | |
mystruct = btf__add_struct(btf, "mystruct", 4); | |
assert(mystruct > 0); | |
myint = btf__add_int(btf, "myint", 4, 0); | |
assert(myint > 0); | |
mytypedef = btf__add_typedef(btf, "mytypedef", myint); | |
assert(mytypedef > 0); | |
ret = btf__load_into_kernel(btf); | |
assert(ret == 0); | |
printf("all good. enter to exit: "); | |
getchar(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) | |
/* Copyright (c) 2020 Facebook */ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/resource.h> | |
#include "bpf/libbpf.h" | |
#include "bpf/btf.h" | |
#include "assert.h" | |
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) | |
{ | |
return vfprintf(stderr, format, args); | |
} | |
int main(int argc, char **argv) | |
{ | |
libbpf_set_strict_mode(LIBBPF_STRICT_ALL); | |
/* Set up libbpf errors and debug info callback */ | |
libbpf_set_print(libbpf_print_fn); | |
struct btf *btf; | |
int ret, myint, myintvar, mydatasec, mystruct, mytypedef, mystructvar; | |
// This test works as vars are defined before datasec. | |
// Type IDs: | |
// 1: mystructvar | |
// 2: myintvar | |
// 3: datasec | |
// 4: mystruct | |
// 5: int | |
// 6: typedef | |
btf = btf__new_empty(); | |
assert(btf != NULL); | |
mystructvar = btf__add_var(btf, "mystructvar", BTF_VAR_STATIC, 4); | |
assert(mystructvar > 0); | |
myintvar = btf__add_var(btf, "myintvar", BTF_VAR_STATIC, 6); | |
assert(myintvar > 0); | |
mydatasec = btf__add_datasec(btf, "mydatasec", 4+4); | |
assert(mydatasec > 0); | |
ret = btf__add_datasec_var_info(btf, 1, 0, 4); | |
assert(ret == 0); | |
ret = btf__add_datasec_var_info(btf, 2, 4, 4); | |
assert(ret == 0); | |
mystruct = btf__add_struct(btf, "mystruct", 4); | |
assert(mystruct > 0); | |
myint = btf__add_int(btf, "myint", 4, 0); | |
assert(myint > 0); | |
mytypedef = btf__add_typedef(btf, "mytypedef", myint); | |
assert(mytypedef > 0); | |
ret = btf__load_into_kernel(btf); | |
assert(ret == 0); | |
printf("all good. enter to exit: "); | |
getchar(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"os" | |
"github.com/cilium/ebpf" | |
"github.com/cilium/ebpf/btf" | |
) | |
func main() { | |
ds := &btf.Datasec{ | |
Name: "a", | |
Size: 8 + 1, | |
Vars: []btf.VarSecinfo{ | |
{ | |
Size: 8, | |
Offset: 0, | |
Type: &btf.Var{Name: "a", Type: &btf.Pointer{ | |
Target: &btf.Void{}, | |
}}, | |
}, | |
{ | |
Size: 1, | |
Offset: 8, | |
Type: &btf.Var{ | |
Name: "c", | |
Type: &btf.Typedef{ | |
Name: "a", | |
Type: &btf.Int{Name: "foo", Size: 1}, | |
}, | |
}, | |
}, | |
}, | |
} | |
s := btf.NewSpec() | |
id, err := s.Add(ds) | |
if err != nil { | |
exit(err) | |
} | |
fmt.Println("Datasec added with id", id) | |
h, err := btf.NewHandle(s) | |
var ve *ebpf.VerifierError | |
if errors.As(err, &ve) { | |
fmt.Printf("%+v\n", ve) | |
os.Exit(1) | |
} | |
if err != nil { | |
exit(err) | |
} | |
h.Close() | |
fmt.Println("BTF loaded successfully") | |
} | |
func exit(err error) { | |
fmt.Println(err) | |
os.Exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment