Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-only
2 : /*
3 : * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
4 : */
5 :
6 : #include "fixup.h"
7 :
8 : #include <errno.h>
9 : #include <stdlib.h>
10 :
11 : #include "core/dump.h"
12 : #include "core/helper.h"
13 :
14 1 : int bf_fixup_new(struct bf_fixup **fixup, enum bf_fixup_type type,
15 : size_t insn_offset, const union bf_fixup_attr *attr)
16 : {
17 1 : bf_assert(fixup);
18 :
19 1 : *fixup = calloc(1, sizeof(struct bf_fixup));
20 : if (!fixup)
21 : return -ENOMEM;
22 :
23 1 : (*fixup)->type = type;
24 1 : (*fixup)->insn = insn_offset;
25 :
26 1 : if (attr)
27 0 : (*fixup)->attr = *attr;
28 :
29 : return 0;
30 : }
31 :
32 2 : void bf_fixup_free(struct bf_fixup **fixup)
33 : {
34 2 : bf_assert(fixup);
35 :
36 2 : if (!*fixup)
37 : return;
38 :
39 : freep((void *)fixup);
40 : }
41 :
42 0 : static const char *_bf_fixup_type_to_str(enum bf_fixup_type type)
43 : {
44 : static const char *str[] = {
45 : [BF_FIXUP_TYPE_JMP_NEXT_RULE] = "BF_FIXUP_TYPE_JMP_NEXT_RULE",
46 : [BF_FIXUP_TYPE_COUNTERS_MAP_FD] = "BF_FIXUP_TYPE_COUNTERS_MAP_FD",
47 : [BF_FIXUP_TYPE_PRINTER_MAP_FD] = "BF_FIXUP_TYPE_PRINTER_MAP_FD",
48 : [BF_FIXUP_TYPE_SET_MAP_FD] = "BF_FIXUP_TYPE_SET_MAP_FD",
49 : [BF_FIXUP_TYPE_FUNC_CALL] = "BF_FIXUP_TYPE_FUNC_CALL",
50 : };
51 :
52 0 : bf_assert(0 <= type && type < _BF_FIXUP_TYPE_MAX);
53 : static_assert(ARRAY_SIZE(str) == _BF_FIXUP_TYPE_MAX);
54 :
55 0 : return str[type];
56 : }
57 :
58 0 : static const char *_bf_fixup_func_to_str(enum bf_fixup_func func)
59 : {
60 : static const char *str[] = {
61 : [BF_FIXUP_FUNC_UPDATE_COUNTERS] = "BF_FIXUP_FUNC_UPDATE_COUNTERS",
62 : };
63 :
64 0 : bf_assert(0 <= func && func < _BF_FIXUP_FUNC_MAX);
65 : static_assert(ARRAY_SIZE(str) == _BF_FIXUP_FUNC_MAX);
66 :
67 0 : return str[func];
68 : }
69 :
70 0 : void bf_fixup_dump(const struct bf_fixup *fixup, prefix_t *prefix)
71 : {
72 0 : bf_assert(fixup);
73 0 : bf_assert(prefix);
74 :
75 0 : DUMP(prefix, "struct bf_fixup at %p", fixup);
76 :
77 0 : bf_dump_prefix_push(prefix);
78 :
79 0 : DUMP(prefix, "type: %s", _bf_fixup_type_to_str(fixup->type));
80 0 : DUMP(prefix, "insn: %zu", fixup->insn);
81 :
82 0 : switch (fixup->type) {
83 : case BF_FIXUP_TYPE_JMP_NEXT_RULE:
84 : case BF_FIXUP_TYPE_COUNTERS_MAP_FD:
85 : case BF_FIXUP_TYPE_PRINTER_MAP_FD:
86 : // No specific value to dump
87 : break;
88 0 : case BF_FIXUP_TYPE_SET_MAP_FD:
89 0 : DUMP(prefix, "set_index: %lu", fixup->attr.set_index);
90 : break;
91 0 : case BF_FIXUP_TYPE_FUNC_CALL:
92 0 : DUMP(prefix, "function: %s",
93 : _bf_fixup_func_to_str(fixup->attr.function));
94 : break;
95 0 : default:
96 0 : DUMP(prefix, "unsupported bf_fixup_type: %d", fixup->type);
97 : break;
98 : };
99 :
100 0 : bf_dump_prefix_pop(prefix);
101 0 : }
|