LCOV - code coverage report
Current view: top level - app/validate-bpf - debug.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 391 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 40 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2025 Huawei Technologies Co., Ltd
       3                 :            :  */
       4                 :            : 
       5                 :            : #include "bpf_def.h"
       6                 :            : 
       7                 :            : #include "debug_command.h"
       8                 :            : #include "internal.h"
       9                 :            : 
      10                 :            : #include <rte_bpf_validate_debug.h>
      11                 :            : #include <rte_errno.h>
      12                 :            : 
      13                 :            : #include <inttypes.h>
      14                 :            : #include <stdlib.h>
      15                 :            : 
      16                 :            : 
      17                 :            : /* Write single line to the user, currently just to stdout. */
      18                 :            : #define PRINTLN(fmt, ...) do { \
      19                 :            :         RTE_LOG_CHECK_NO_NEWLINE(fmt); \
      20                 :            :         printf(fmt "\n", ##__VA_ARGS__); \
      21                 :            : } while (0)
      22                 :            : 
      23                 :            : #define PROMPT "(validate) "
      24                 :            : 
      25                 :            : #define INITIAL_CAPACITY 8u
      26                 :            : 
      27                 :            : static const char *const event_names[] = {
      28                 :            :         [RTE_BPF_VALIDATE_DEBUG_EVENT_INVALID_STATE] = "invalid-state",
      29                 :            :         [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_ENTER] = "branch-enter",
      30                 :            :         [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_PRUNE] = "branch-prune",
      31                 :            :         [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_RETURN] = "branch-return",
      32                 :            :         [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_UNREACHABLE] = "branch-unreachable",
      33                 :            :         [RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_ALWAYS] = "jump-always",
      34                 :            :         [RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_CONDITIONAL] = "jump-conditional",
      35                 :            : };
      36                 :            : 
      37                 :            : static const char *const register_names[] = {
      38                 :            :         [EBPF_REG_0] = "r0",
      39                 :            :         [EBPF_REG_1] = "r1",
      40                 :            :         [EBPF_REG_2] = "r2",
      41                 :            :         [EBPF_REG_3] = "r3",
      42                 :            :         [EBPF_REG_4] = "r4",
      43                 :            :         [EBPF_REG_5] = "r5",
      44                 :            :         [EBPF_REG_6] = "r6",
      45                 :            :         [EBPF_REG_7] = "r7",
      46                 :            :         [EBPF_REG_8] = "r8",
      47                 :            :         [EBPF_REG_9] = "r9",
      48                 :            :         [EBPF_REG_10] = "r10",
      49                 :            : };
      50                 :            : 
      51                 :            : static const char *const comparison_operator_names[] = {
      52                 :            :         [BPF_JEQ] = "==",
      53                 :            :         [BPF_JGT] = ">",
      54                 :            :         [BPF_JGE] = ">=",
      55                 :            :         [EBPF_JNE] = "!=",
      56                 :            :         [EBPF_JSGT] = "s>",
      57                 :            :         [EBPF_JSGE] = "s>=",
      58                 :            :         [EBPF_JLT] = "<",
      59                 :            :         [EBPF_JLE] = "<=",
      60                 :            :         [EBPF_JSLT] = "s<",
      61                 :            :         [EBPF_JSLE] = "s<=",
      62                 :            : };
      63                 :            : 
      64                 :            : enum point_type {
      65                 :            :         POINT_TYPE_BREAK,
      66                 :            :         POINT_TYPE_CATCH,
      67                 :            : };
      68                 :            : 
      69                 :            : /* Local representation of points with additional info for UI. */
      70                 :            : struct point_info {
      71                 :            :         struct rte_bpf_validate_debug_point *point;
      72                 :            :         enum point_type type;
      73                 :            :         union {
      74                 :            :                 uint32_t pc;
      75                 :            :                 enum rte_bpf_validate_debug_event event;
      76                 :            :         };
      77                 :            : };
      78                 :            : 
      79                 :            : /* Dynamically growing list of point infos. */
      80                 :            : struct point_infos {
      81                 :            :         struct point_info *elements;
      82                 :            :         uint32_t length;
      83                 :            :         uint32_t capacity;
      84                 :            : };
      85                 :            : 
      86                 :            : /* Information about a single conditional or unconditional jump in code path. */
      87                 :            : struct branch_info {
      88                 :            :         uint32_t jump_pc;
      89                 :            :         uint32_t target_pc;
      90                 :            :         bool is_conditional;
      91                 :            : };
      92                 :            : 
      93                 :            : /* Dynamically growing stack to track code path. */
      94                 :            : struct branch_stack {
      95                 :            :         struct branch_info *branches;
      96                 :            :         uint32_t length;
      97                 :            :         uint32_t capacity;
      98                 :            : };
      99                 :            : 
     100                 :            : /* Flags telling if we should validate again, stopping or not at start. */
     101                 :            : static bool validate_again;
     102                 :            : 
     103                 :            : /* List of point infos; element index is its ID for UI. */
     104                 :            : static struct point_infos point_infos;
     105                 :            : 
     106                 :            : /* Catchpoint invisible to the user used for step-by-step validation. */
     107                 :            : static struct rte_bpf_validate_debug_point *step_point;
     108                 :            : 
     109                 :            : /* Tracking entered branches. */
     110                 :            : static struct branch_stack branch_stack;
     111                 :            : static uint32_t pending_jump_pc = UINT32_MAX;
     112                 :            : static struct rte_bpf_validate_debug_point *jump_always_step_point;
     113                 :            : 
     114                 :            : static int
     115                 :            : step_cb(struct rte_bpf_validate_debug *debug, void *ctx);
     116                 :            : static int
     117                 :            : point_cb(struct rte_bpf_validate_debug *debug, void *ctx);
     118                 :            : 
     119                 :            : /* Find index of the string in the list. */
     120                 :            : static int
     121                 :          0 : find_name(const char *name, const char *const *names, int nb_names)
     122                 :            : {
     123                 :          0 :         if (nb_names < 0)
     124                 :            :                 return -EINVAL;
     125                 :            : 
     126                 :          0 :         for (int index = 0; index != nb_names; ++index)
     127                 :          0 :                 if (names[index] != NULL && strcmp(names[index], name) == 0)
     128                 :          0 :                         return index;
     129                 :            : 
     130                 :            :         return -ENOENT;
     131                 :            : }
     132                 :            : 
     133                 :            : /* Convert event name to its enum value. */
     134                 :            : static enum rte_bpf_validate_debug_event
     135                 :          0 : parse_event_name(const char *name)
     136                 :            : {
     137                 :          0 :         const int rc = find_name(name, event_names, RTE_DIM(event_names));
     138                 :          0 :         if (rc < 0)
     139                 :            :                 PRINTLN("Error: invalid event name.");
     140                 :            : 
     141                 :          0 :         return rc;
     142                 :            : }
     143                 :            : 
     144                 :            : /* Convert comparison operator name to opcode. */
     145                 :            : static int
     146                 :          0 : parse_comparison_operator(const char *name)
     147                 :            : {
     148                 :          0 :         const int rc = find_name(name, comparison_operator_names,
     149                 :            :                 RTE_DIM(comparison_operator_names));
     150                 :          0 :         if (rc < 0)
     151                 :            :                 PRINTLN("Error: invalid comparison operator name.");
     152                 :            : 
     153                 :          0 :         return rc;
     154                 :            : }
     155                 :            : 
     156                 :            : /* Convert register name to its number. */
     157                 :            : static int
     158                 :          0 : parse_register(const char *name)
     159                 :            : {
     160                 :          0 :         const int rc = find_name(name, register_names, RTE_DIM(register_names));
     161                 :          0 :         if (rc < 0)
     162                 :            :                 PRINTLN("Error: invalid register name.");
     163                 :            : 
     164                 :          0 :         return rc;
     165                 :            : }
     166                 :            : 
     167                 :            : /* Free local list of point infos (do not destroy points). */
     168                 :            : static void
     169                 :            : point_infos_free(void)
     170                 :            : {
     171                 :          0 :         free(point_infos.elements);
     172                 :          0 :         point_infos = (struct point_infos){};
     173                 :            : }
     174                 :            : 
     175                 :            : /* Free local branch stack. */
     176                 :            : static void
     177                 :            : branch_stack_free(void)
     178                 :            : {
     179                 :          0 :         free(branch_stack.branches);
     180                 :          0 :         branch_stack = (struct branch_stack){};
     181                 :            : }
     182                 :            : 
     183                 :            : /* Return existing element from the list of point infos. */
     184                 :            : static struct point_info *
     185                 :            : point_infos_at(uint32_t point_number)
     186                 :            : {
     187                 :            :         RTE_ASSERT(point_number < point_infos.length);
     188                 :            :         RTE_ASSERT(point_infos.elements[point_number].point != NULL);
     189                 :          0 :         return &point_infos.elements[point_number];
     190                 :            : }
     191                 :            : 
     192                 :            : /* Print existing element from the list of point infos. */
     193                 :            : static void
     194                 :          0 : point_infos_print_at(uint32_t point_number)
     195                 :            : {
     196                 :            :         const struct point_info *const point_info = point_infos_at(point_number);
     197                 :            : 
     198                 :          0 :         switch (point_info->type) {
     199                 :          0 :         case POINT_TYPE_BREAK:
     200                 :          0 :                 PRINTLN("Breakpoint %d at %d.", point_number,
     201                 :            :                         point_info->pc);
     202                 :            :                 break;
     203                 :          0 :         case POINT_TYPE_CATCH:
     204                 :          0 :                 PRINTLN("Catchpoint %d on %s.", point_number,
     205                 :            :                         event_names[point_info->event]);
     206                 :            :                 break;
     207                 :            :         default:
     208                 :            :                 PRINTLN("Point %d of unknown type", point_number);
     209                 :            :                 break;
     210                 :            :         }
     211                 :          0 : }
     212                 :            : 
     213                 :            : /* Print all point infos. */
     214                 :            : static int
     215                 :          0 : point_infos_print_all(void)
     216                 :            : {
     217                 :            :         uint32_t nb_printed = 0;
     218                 :          0 :         for (uint32_t pn = 0; pn != point_infos.length; ++pn) {
     219                 :          0 :                 const struct point_info *const point_info =
     220                 :          0 :                         &point_infos.elements[pn];
     221                 :          0 :                 if (point_info->point != NULL) {
     222                 :          0 :                         point_infos_print_at(pn);
     223                 :          0 :                         ++nb_printed;
     224                 :            :                 }
     225                 :            :         }
     226                 :            : 
     227                 :          0 :         if (nb_printed == 0) {
     228                 :            :                 PRINTLN("No breakpoints or catchpoints set.");
     229                 :          0 :                 return -ENOENT;
     230                 :            :         }
     231                 :            : 
     232                 :          0 :         return nb_printed;
     233                 :            : }
     234                 :            : 
     235                 :            : /* Allocate space for a new element in the list of point infos. */
     236                 :            : static uint32_t
     237                 :          0 : point_infos_append(void)
     238                 :            : {
     239                 :          0 :         if (point_infos.length == point_infos.capacity) {
     240                 :            :                 /* Set to initial capacity or double previous one. */
     241                 :          0 :                 point_infos.capacity = RTE_MAX(INITIAL_CAPACITY,
     242                 :            :                         point_infos.capacity * 2);
     243                 :          0 :                 point_infos.elements = realloc(point_infos.elements,
     244                 :          0 :                         point_infos.capacity * sizeof(point_infos.elements[0]));
     245                 :          0 :                 RTE_VERIFY(point_infos.elements != NULL);
     246                 :            :         }
     247                 :          0 :         return point_infos.length++;
     248                 :            : }
     249                 :            : 
     250                 :            : /* Allocate space for a new element in the branch stack. */
     251                 :            : static void
     252                 :          0 : branch_stack_append(const struct branch_info *branch)
     253                 :            : {
     254                 :          0 :         if (branch_stack.length == branch_stack.capacity) {
     255                 :            :                 /* Set to initial capacity or double previous one. */
     256                 :          0 :                 branch_stack.capacity = RTE_MAX(INITIAL_CAPACITY,
     257                 :            :                         branch_stack.capacity * 2);
     258                 :          0 :                 branch_stack.branches = realloc(branch_stack.branches,
     259                 :          0 :                         branch_stack.capacity * sizeof(branch_stack.branches[0]));
     260                 :          0 :                 RTE_VERIFY(branch_stack.branches != NULL);
     261                 :            :         }
     262                 :          0 :         branch_stack.branches[branch_stack.length++] = *branch;
     263                 :          0 : }
     264                 :            : 
     265                 :            : /* Destroy existing element from the list of point infos, printing it first. */
     266                 :            : static void
     267                 :          0 : point_infos_destroy_existing(uint32_t point_number)
     268                 :            : {
     269                 :          0 :         point_infos_print_at(point_number);
     270                 :            : 
     271                 :            :         struct point_info *const point_info = point_infos_at(point_number);
     272                 :          0 :         rte_bpf_validate_debug_point_destroy(point_info->point);
     273                 :          0 :         *point_info = (struct point_info){};
     274                 :          0 : }
     275                 :            : 
     276                 :            : /* Destroy point with specified number if it exists. */
     277                 :            : static int
     278                 :          0 : point_infos_destroy_at(uint32_t point_number)
     279                 :            : {
     280                 :          0 :         if (point_number >= point_infos.length ||
     281                 :          0 :                         point_infos.elements[point_number].point == NULL) {
     282                 :            :                 PRINTLN("No breakpoint number %d.", point_number);
     283                 :          0 :                 return -ENOENT;
     284                 :            :         }
     285                 :            : 
     286                 :          0 :         point_infos_destroy_existing(point_number);
     287                 :          0 :         return 1;
     288                 :            : }
     289                 :            : 
     290                 :            : /* Destroy all point infos. */
     291                 :            : static void
     292                 :          0 : point_infos_destroy_all(void)
     293                 :            : {
     294                 :            :         uint32_t nb_destroyed = 0;
     295                 :          0 :         for (uint32_t pn = 0; pn < point_infos.length; ++pn) {
     296                 :          0 :                 const struct point_info *const point_info =
     297                 :          0 :                         &point_infos.elements[pn];
     298                 :          0 :                 if (point_info->point != NULL) {
     299                 :          0 :                         point_infos_destroy_existing(pn);
     300                 :          0 :                         ++nb_destroyed;
     301                 :            :                 }
     302                 :            :         }
     303                 :            : 
     304                 :          0 :         if (nb_destroyed == 0)
     305                 :            :                 PRINTLN("No breakpoints or catchpoints set.");
     306                 :          0 : }
     307                 :            : 
     308                 :            : /* Destroy all breakpoints at specified location. */
     309                 :            : static void
     310                 :          0 : point_infos_destroy_breakpoints(uint32_t pc)
     311                 :            : {
     312                 :            :         uint32_t nb_destroyed = 0;
     313                 :          0 :         for (uint32_t pn = 0; pn < point_infos.length; ++pn) {
     314                 :          0 :                 const struct point_info *const point_info =
     315                 :          0 :                         &point_infos.elements[pn];
     316                 :          0 :                 if (point_info->point != NULL &&
     317                 :          0 :                                 point_info->type == POINT_TYPE_BREAK &&
     318                 :          0 :                                 point_info->pc == pc) {
     319                 :          0 :                         point_infos_destroy_existing(pn);
     320                 :          0 :                         ++nb_destroyed;
     321                 :            :                 }
     322                 :            :         }
     323                 :            : 
     324                 :          0 :         if (nb_destroyed == 0)
     325                 :            :                 PRINTLN("No breakpoint at %u.", pc);
     326                 :          0 : }
     327                 :            : 
     328                 :            : /* Destroy all catchpoints for specified event. */
     329                 :            : static void
     330                 :          0 : point_infos_destroy_catchpoints(int event)
     331                 :            : {
     332                 :          0 :         if (event < 0)
     333                 :            :                 /* Error was already printed by parse_event_name. */
     334                 :            :                 return;
     335                 :            : 
     336                 :            :         uint32_t nb_destroyed = 0;
     337                 :          0 :         for (uint32_t pn = 0; pn < point_infos.length; ++pn) {
     338                 :          0 :                 const struct point_info *const point_info =
     339                 :          0 :                         &point_infos.elements[pn];
     340                 :          0 :                 if (point_info->point != NULL &&
     341                 :          0 :                                 point_info->type == POINT_TYPE_CATCH &&
     342                 :          0 :                                 (int)point_info->event == event) {
     343                 :          0 :                         point_infos_destroy_existing(pn);
     344                 :          0 :                         ++nb_destroyed;
     345                 :            :                 }
     346                 :            :         }
     347                 :            : 
     348                 :          0 :         if (nb_destroyed == 0)
     349                 :          0 :                 PRINTLN("No catchpoint on %s.", event_names[event]);
     350                 :            : }
     351                 :            : 
     352                 :            : /* Create new breakpoint at specified location. */
     353                 :            : static int
     354                 :          0 : add_breakpoint(struct rte_bpf_validate_debug *debug, uint32_t nb_ins, uint32_t pc)
     355                 :            : {
     356                 :          0 :         const uint32_t point_number = point_infos_append();
     357                 :            : 
     358                 :          0 :         if (pc >= nb_ins) {
     359                 :            :                 PRINTLN("Error: program only has %u instructions.", nb_ins);
     360                 :          0 :                 return -ENOENT;
     361                 :            :         }
     362                 :            : 
     363                 :            :         struct rte_bpf_validate_debug_point *const point =
     364                 :          0 :                 rte_bpf_validate_debug_break(debug, pc,
     365                 :          0 :                         &(struct rte_bpf_validate_debug_callback){
     366                 :            :                                 .fn = point_cb,
     367                 :          0 :                                 .ctx = (void *)(uintptr_t)point_number,
     368                 :            :                         });
     369                 :          0 :         if (point == NULL) {
     370                 :          0 :                 PRINTLN("Library error %d.", rte_errno);
     371                 :          0 :                 return -rte_errno;
     372                 :            :         }
     373                 :            : 
     374                 :          0 :         point_infos.elements[point_number] = (struct point_info){
     375                 :            :                         .point = point,
     376                 :            :                         .type = POINT_TYPE_BREAK,
     377                 :            :                         .pc = pc,
     378                 :            :                 };
     379                 :          0 :         point_infos_print_at(point_number);
     380                 :          0 :         return 0;
     381                 :            : }
     382                 :            : 
     383                 :            : /* Create new catchpoint at specified location. */
     384                 :            : static int
     385                 :          0 : add_catchpoint(struct rte_bpf_validate_debug *debug, int event)
     386                 :            : {
     387                 :          0 :         if (event < 0)
     388                 :            :                 /* Error was already printed by parse_event_name. */
     389                 :            :                 return event;
     390                 :            : 
     391                 :          0 :         const uint32_t point_number = point_infos_append();
     392                 :            :         struct rte_bpf_validate_debug_point *const point =
     393                 :          0 :                 rte_bpf_validate_debug_catch(debug, event,
     394                 :          0 :                         &(struct rte_bpf_validate_debug_callback){
     395                 :            :                                 .fn = point_cb,
     396                 :          0 :                                 .ctx = (void *)(uintptr_t)point_number,
     397                 :            :                         });
     398                 :          0 :         if (point == NULL) {
     399                 :          0 :                 PRINTLN("Library error %d.", rte_errno);
     400                 :          0 :                 return -rte_errno;
     401                 :            :         }
     402                 :            : 
     403                 :          0 :         point_infos.elements[point_number] = (struct point_info){
     404                 :            :                         .point = point,
     405                 :            :                         .type = POINT_TYPE_CATCH,
     406                 :            :                         .event = event,
     407                 :            :                 };
     408                 :          0 :         point_infos_print_at(point_number);
     409                 :          0 :         return 0;
     410                 :            : }
     411                 :            : 
     412                 :            : static bool
     413                 :            : is_step_enabled(void)
     414                 :            : {
     415                 :          0 :         return step_point != NULL;
     416                 :            : }
     417                 :            : 
     418                 :            : /* Enable step-by-step validation: make sure catchpoint is set on step event. */
     419                 :            : static int
     420                 :          0 : enable_step(struct rte_bpf_validate_debug *debug)
     421                 :            : {
     422                 :          0 :         if (is_step_enabled())
     423                 :            :                 return 0;
     424                 :            : 
     425                 :          0 :         step_point = rte_bpf_validate_debug_catch(debug,
     426                 :            :                 RTE_BPF_VALIDATE_DEBUG_EVENT_STEP,
     427                 :          0 :                 &(struct rte_bpf_validate_debug_callback){ step_cb });
     428                 :          0 :         if (step_point == NULL) {
     429                 :          0 :                 PRINTLN("Library error %d.", rte_errno);
     430                 :          0 :                 return -rte_errno;
     431                 :            :         }
     432                 :            : 
     433                 :            :         return 0;
     434                 :            : }
     435                 :            : 
     436                 :            : /* Disable step-by-step validation: destroy catchpoint on step event if any. */
     437                 :            : static void
     438                 :            : disable_step(void)
     439                 :            : {
     440                 :          0 :         rte_bpf_validate_debug_point_destroy(step_point);
     441                 :          0 :         step_point = NULL;
     442                 :            : }
     443                 :            : 
     444                 :            : /* Format and print information about specified frame offset. */
     445                 :            : static int
     446                 :          0 : print_frame_offset(struct rte_bpf_validate_debug *debug, int32_t offset)
     447                 :            : {
     448                 :            :         char *info;
     449                 :            :         int info_size, rc;
     450                 :            : 
     451                 :          0 :         if (offset >= 0 || offset % sizeof(uint64_t) != 0) {
     452                 :            :                 PRINTLN("Invalid frame offset, must be a negative multiple of %zu.",
     453                 :            :                         sizeof(uint64_t));
     454                 :          0 :                 return -EINVAL;
     455                 :            :         }
     456                 :            : 
     457                 :          0 :         rc = rte_bpf_validate_debug_format_frame_info(debug, NULL, 0, offset);
     458                 :          0 :         if (rc == -ERANGE) {
     459                 :            :                 PRINTLN("Offset is out of frame range.");
     460                 :          0 :                 return rc;
     461                 :            :         }
     462                 :          0 :         if (rc < 0) {
     463                 :          0 :                 PRINTLN("Error %d printing information.", -rc);
     464                 :          0 :                 return rc;
     465                 :            :         }
     466                 :            : 
     467                 :          0 :         info_size = rc + 1;
     468                 :          0 :         info = malloc(info_size);
     469                 :          0 :         RTE_VERIFY(info != NULL);
     470                 :            : 
     471                 :          0 :         rc = rte_bpf_validate_debug_format_frame_info(debug, info, info_size,
     472                 :            :                 offset);
     473                 :          0 :         if (rc + 1 != info_size) {
     474                 :          0 :                 if (rc >= 0) {
     475                 :            :                         PRINTLN("Expect format return value %d, got %d.",
     476                 :            :                                 info_size, rc);
     477                 :            :                         rc = -EINVAL;
     478                 :            :                 } else
     479                 :          0 :                         PRINTLN("Error %d printing information.", -rc);
     480                 :          0 :                 free(info);
     481                 :          0 :                 return rc;
     482                 :            :         }
     483                 :            : 
     484                 :            :         printf("%5" PRId32 ": \t%s\n", offset, info);
     485                 :          0 :         free(info);
     486                 :          0 :         return 0;
     487                 :            : }
     488                 :            : 
     489                 :            : /* Format and print informatiion about the frame. */
     490                 :            : static int
     491                 :          0 : print_frame(struct rte_bpf_validate_debug *debug)
     492                 :            : {
     493                 :            :         int32_t frame_size;
     494                 :            :         int rc;
     495                 :            : 
     496                 :          0 :         frame_size = rte_bpf_validate_debug_get_frame_size(debug);
     497                 :          0 :         if (frame_size < 0) {
     498                 :          0 :                 PRINTLN("Error %d getting frame size.", -frame_size);
     499                 :          0 :                 return frame_size;
     500                 :            :         }
     501                 :            : 
     502                 :          0 :         for (int32_t neg_ofs = sizeof(uint64_t); neg_ofs <= frame_size;
     503                 :          0 :                         neg_ofs += sizeof(uint64_t)) {
     504                 :          0 :                 rc = print_frame_offset(debug, -neg_ofs);
     505                 :          0 :                 if (rc < 0)
     506                 :          0 :                         return rc;
     507                 :            :         }
     508                 :            : 
     509                 :            :         return 0;
     510                 :            : }
     511                 :            : 
     512                 :            : /* Format and print informatiion about specified register. */
     513                 :            : static int
     514                 :          0 : print_register(struct rte_bpf_validate_debug *debug, int reg)
     515                 :            : {
     516                 :            :         char *info;
     517                 :            :         int info_size, rc;
     518                 :            : 
     519                 :          0 :         if (reg < 0)
     520                 :            :                 /* Error was already printed by parse_register. */
     521                 :            :                 return reg;
     522                 :            : 
     523                 :          0 :         rc = rte_bpf_validate_debug_format_register_info(debug, NULL, 0, reg);
     524                 :          0 :         if (rc < 0) {
     525                 :          0 :                 PRINTLN("Error %d printing information.", -rc);
     526                 :          0 :                 return rc;
     527                 :            :         }
     528                 :            : 
     529                 :          0 :         info_size = rc + 1;
     530                 :          0 :         info = malloc(info_size);
     531                 :          0 :         RTE_VERIFY(info != NULL);
     532                 :            : 
     533                 :          0 :         rc = rte_bpf_validate_debug_format_register_info(debug, info, info_size,
     534                 :            :                 reg);
     535                 :          0 :         if (rc + 1 != info_size) {
     536                 :          0 :                 if (rc >= 0) {
     537                 :            :                         PRINTLN("Expect format return value %d, got %d.",
     538                 :            :                                 info_size, rc);
     539                 :            :                         rc = -EINVAL;
     540                 :            :                 } else
     541                 :          0 :                         PRINTLN("Error %d printing information.", -rc);
     542                 :          0 :                 free(info);
     543                 :          0 :                 return rc;
     544                 :            :         }
     545                 :            : 
     546                 :          0 :         printf("%5s: \t%s\n", register_names[reg], info);
     547                 :          0 :         free(info);
     548                 :          0 :         return 0;
     549                 :            : }
     550                 :            : 
     551                 :            : /* Format and print informatiion about all registers. */
     552                 :            : static int
     553                 :            : print_registers(struct rte_bpf_validate_debug *debug)
     554                 :            : {
     555                 :            :         int rc = 0;
     556                 :            : 
     557                 :          0 :         for (int reg = 0; reg != EBPF_REG_NUM; ++reg)
     558                 :          0 :                 rc = rc < 0 ? rc : print_register(debug, reg);
     559                 :            : 
     560                 :            :         return rc;
     561                 :            : }
     562                 :            : 
     563                 :            : /* List one eBPF program instruction. */
     564                 :            : static int
     565                 :          0 : list_one(const struct rte_ebpf_insn *ins, uint32_t nb_ins, uint32_t offset,
     566                 :            :         uint32_t pc, const char *comment)
     567                 :            : {
     568                 :            :         char hexadecimal[256], disassembly[256];
     569                 :            : 
     570                 :          0 :         if (offset >= nb_ins) {
     571                 :            :                 PRINTLN("Error: program only has %u instructions.", nb_ins);
     572                 :          0 :                 return -EINVAL;
     573                 :            :         }
     574                 :            : 
     575                 :          0 :         ins += offset;
     576                 :            : 
     577                 :          0 :         if (offset == nb_ins - 1 && rte_bpf_insn_is_wide(ins)) {
     578                 :            :                 PRINTLN("Error: truncated last instruction.");
     579                 :          0 :                 return -EINVAL;
     580                 :            :         }
     581                 :            : 
     582                 :          0 :         rte_bpf_format(hexadecimal, sizeof(hexadecimal), ins, 0,
     583                 :            :                 RTE_BPF_FORMAT_FLAG_HEXADECIMAL |
     584                 :            :                 RTE_BPF_FORMAT_FLAG_NEVER_WIDE);
     585                 :          0 :         rte_bpf_format(disassembly, sizeof(disassembly), ins, offset,
     586                 :            :                 RTE_BPF_FORMAT_FLAG_DISASSEMBLY |
     587                 :            :                 RTE_BPF_FORMAT_FLAG_ABSOLUTE_JUMPS);
     588                 :            : 
     589                 :          0 :         if (comment == NULL)
     590                 :            :                 comment = "";
     591                 :          0 :         PRINTLN("%2s %10u: \t%s \t%s%s%s",
     592                 :            :                 offset == pc ? "=>" : "", offset, hexadecimal, disassembly,
     593                 :            :                 comment[0] != '\0' ? " \t; " : "", comment);
     594                 :            : 
     595                 :          0 :         if (rte_bpf_insn_is_wide(ins)) {
     596                 :          0 :                 rte_bpf_format(hexadecimal, sizeof(hexadecimal), ins + 1, 0,
     597                 :            :                         RTE_BPF_FORMAT_FLAG_HEXADECIMAL |
     598                 :            :                         RTE_BPF_FORMAT_FLAG_NEVER_WIDE);
     599                 :            :                 PRINTLN("%15s\t%s", "", hexadecimal);
     600                 :            :         }
     601                 :            : 
     602                 :            :         return 0;
     603                 :            : }
     604                 :            : 
     605                 :            : /* List specified range of eBPF program instructions, updating start offset. */
     606                 :            : static int
     607                 :          0 : list(const struct rte_ebpf_insn *ins, uint32_t nb_ins, uint32_t *offset,
     608                 :            :         uint32_t count, uint32_t pc)
     609                 :            : {
     610                 :          0 :         uint32_t local_offset = 0;
     611                 :          0 :         if (offset == NULL)
     612                 :            :                 offset = &local_offset;
     613                 :            : 
     614                 :          0 :         if (*offset > nb_ins) {
     615                 :            :                 PRINTLN("Error: program only has %u instructions.", nb_ins);
     616                 :          0 :                 return -EINVAL;
     617                 :            :         }
     618                 :            : 
     619                 :          0 :         const uint32_t end =
     620                 :            :                 /* Calculate end in a way preventing overflow: */
     621                 :          0 :                 *offset + RTE_MIN(nb_ins - *offset, count);
     622                 :          0 :         while (*offset < end) {
     623                 :          0 :                 const int rc = list_one(ins, nb_ins, *offset, pc, NULL);
     624                 :          0 :                 if (rc < 0)
     625                 :          0 :                         return rc;
     626                 :            : 
     627                 :          0 :                 *offset += 1 + rte_bpf_insn_is_wide(&ins[*offset]);
     628                 :            :         }
     629                 :            : 
     630                 :            :         return 0;
     631                 :            : }
     632                 :            : 
     633                 :            : /* Print if specified conditional jump _may_ be executed. */
     634                 :            : static int
     635                 :          0 : print_if_may(struct rte_bpf_validate_debug *debug, const struct rte_ebpf_insn *jump,
     636                 :            :         uint64_t imm64)
     637                 :            : {
     638                 :          0 :         const int result = rte_bpf_validate_debug_may_jump(debug, jump, imm64);
     639                 :            : 
     640                 :          0 :         switch (result) {
     641                 :            :         case 0:
     642                 :            :         case RTE_BPF_VALIDATE_DEBUG_MAY_BE_FALSE:
     643                 :            :                 PRINTLN("NO");
     644                 :            :                 break;
     645                 :            :         case RTE_BPF_VALIDATE_DEBUG_MAY_BE_TRUE:
     646                 :            :         case RTE_BPF_VALIDATE_DEBUG_MAY_BE_FALSE | RTE_BPF_VALIDATE_DEBUG_MAY_BE_TRUE:
     647                 :            :                 PRINTLN("YES");
     648                 :            :                 break;
     649                 :          0 :         default:
     650                 :          0 :                 PRINTLN("Error %d getting result.", -result);
     651                 :            :                 break;
     652                 :            :         }
     653                 :            : 
     654                 :          0 :         return result;
     655                 :            : }
     656                 :            : 
     657                 :            : /* Print if specified condition with literal right hand side _may_ be true. */
     658                 :            : static int
     659                 :          0 : print_if_may_literal_rhs(struct rte_bpf_validate_debug *debug,
     660                 :            :         const struct debug_command_comparison *comparison)
     661                 :            : {
     662                 :          0 :         const int lhs = parse_register(comparison->lhs);
     663                 :          0 :         const int op = parse_comparison_operator(comparison->op);
     664                 :          0 :         const int64_t rhs = comparison->literal_rhs;
     665                 :            : 
     666                 :          0 :         if (lhs < 0 || op < 0)
     667                 :            :                 /* Error was already printed by parse function. */
     668                 :          0 :                 return lhs < 0 ? lhs : op;
     669                 :            : 
     670                 :          0 :         return print_if_may(debug, &(struct rte_ebpf_insn){
     671                 :          0 :                 .code = BPF_JMP | op | BPF_K,
     672                 :            :                 .dst_reg = lhs,
     673                 :            :         }, /* imm64 = */ rhs);
     674                 :            : }
     675                 :            : 
     676                 :            : /* Print if specified condition with register right hand side _may_ be true. */
     677                 :            : static int
     678                 :          0 : print_if_may_register_rhs(struct rte_bpf_validate_debug *debug,
     679                 :            :         const struct debug_command_comparison *comparison)
     680                 :            : {
     681                 :          0 :         const int lhs = parse_register(comparison->lhs);
     682                 :          0 :         const int op = parse_comparison_operator(comparison->op);
     683                 :          0 :         const int rhs = parse_register(comparison->register_rhs);
     684                 :            : 
     685                 :          0 :         if (lhs < 0 || op < 0 || rhs < 0)
     686                 :            :                 /* Error was already printed by parse function. */
     687                 :          0 :                 return lhs < 0 ? lhs : op < 0 ? op : rhs;
     688                 :            : 
     689                 :          0 :         return print_if_may(debug, &(struct rte_ebpf_insn){
     690                 :          0 :                 .code = BPF_JMP | op | BPF_X,
     691                 :            :                 .dst_reg = lhs,
     692                 :            :                 .src_reg = rhs,
     693                 :            :         }, /* imm64 = */ 0);
     694                 :            : }
     695                 :            : 
     696                 :            : /* Return 1 on validation success, 0 on failure, -EAGAIN if still running. */
     697                 :            : static int
     698                 :            : get_validation_success(struct rte_bpf_validate_debug *debug)
     699                 :            : {
     700                 :            :         int validation_result, rc;
     701                 :            : 
     702                 :          0 :         rc = rte_bpf_validate_debug_get_validation_result(debug,
     703                 :            :                 &validation_result);
     704                 :          0 :         return rc < 0 ? rc : (validation_result >= 0);
     705                 :            : }
     706                 :            : 
     707                 :            : static void
     708                 :          0 : print_status(const struct rte_ebpf_insn *ins, uint32_t nb_ins,
     709                 :            :         int validation_success, uint32_t pc)
     710                 :            : {
     711                 :          0 :         if (validation_success == 1) {
     712                 :            :                 PRINTLN("Validation succeeded.");
     713                 :          0 :                 return;
     714                 :            :         }
     715                 :            : 
     716                 :          0 :         list_one(ins, nb_ins, pc, pc, NULL);
     717                 :            : 
     718                 :          0 :         if (validation_success == 0)
     719                 :            :                 PRINTLN("Validation failed.");
     720                 :            : }
     721                 :            : 
     722                 :            : static void
     723                 :          0 : debug_command_where(const struct rte_ebpf_insn *ins, uint32_t nb_ins,
     724                 :            :         int validation_success, uint32_t pc)
     725                 :            : {
     726                 :          0 :         for (uint32_t bi = 0; bi != branch_stack.length; ++bi) {
     727                 :          0 :                 const uint32_t jump_pc = branch_stack.branches[bi].jump_pc;
     728                 :          0 :                 const uint32_t target_pc = branch_stack.branches[bi].target_pc;
     729                 :            :                 const char *const comment =
     730                 :          0 :                         !branch_stack.branches[bi].is_conditional ? NULL :
     731                 :          0 :                         target_pc == jump_pc + 1 ? "fallen-through" : "taken";
     732                 :          0 :                 list_one(ins, nb_ins, jump_pc, UINT32_MAX, comment);
     733                 :            :         }
     734                 :          0 :         print_status(ins, nb_ins, validation_success, pc);
     735                 :          0 : }
     736                 :            : 
     737                 :            : /* Return true if pc is defined, otherwise print an error message. */
     738                 :            : static bool
     739                 :            : ensure_pc_defined(uint32_t pc)
     740                 :            : {
     741                 :          0 :         if (pc != UINT32_MAX)
     742                 :            :                 return true;
     743                 :            : 
     744                 :            :         PRINTLN("No current instruction.");
     745                 :            :         return false;
     746                 :            : }
     747                 :            : 
     748                 :            : /* Return true if still validating, otherwise print an error message. */
     749                 :            : static bool
     750                 :            : ensure_still_validating(int validation_success)
     751                 :            : {
     752                 :          0 :         if (validation_success == -EAGAIN)
     753                 :            :                 return true;
     754                 :            : 
     755                 :            :         PRINTLN("Finished, use `start` or `run` to restart, `quit` to quit.");
     756                 :            :         return false;
     757                 :            : }
     758                 :            : 
     759                 :            : /* Step-by-step validation callback: read and process user commands. */
     760                 :            : static int
     761                 :          0 : step_cb(struct rte_bpf_validate_debug *debug, __rte_unused void *ctx)
     762                 :            : {
     763                 :            :         int rc;
     764                 :            :         int validation_success;
     765                 :            :         const struct rte_ebpf_insn *ins;
     766                 :            :         uint32_t nb_ins, pc, list_offset;
     767                 :            : 
     768                 :          0 :         validate_again = false;
     769                 :            : 
     770                 :          0 :         rc = rte_bpf_validate_debug_get_ins(debug, &ins, &nb_ins);
     771                 :          0 :         if (rc < 0) {
     772                 :          0 :                 PRINTLN("Error %d getting program instructions.", -rc);
     773                 :          0 :                 return rc;
     774                 :            :         }
     775                 :            : 
     776                 :            :         validation_success = get_validation_success(debug);
     777                 :          0 :         if (validation_success < 0 && validation_success != -EAGAIN) {
     778                 :          0 :                 PRINTLN("Error %d getting validation result.",
     779                 :            :                         -validation_success);
     780                 :          0 :                 return validation_success;
     781                 :            :         }
     782                 :            : 
     783                 :          0 :         pc = validation_success == 1 ? UINT32_MAX :
     784                 :          0 :                 rte_bpf_validate_debug_get_pc(debug);
     785                 :            : 
     786                 :          0 :         print_status(ins, nb_ins, validation_success, pc);
     787                 :            : 
     788                 :          0 :         list_offset = pc;
     789                 :            : 
     790                 :            :         while (true) {
     791                 :          0 :                 switch (debug_command_get(PROMPT)) {
     792                 :            :                 case DEBUG_COMMAND_BREAK:
     793                 :            :                         if (ensure_pc_defined(pc))
     794                 :          0 :                                 add_breakpoint(debug, nb_ins, pc);
     795                 :          0 :                         continue;
     796                 :          0 :                 case DEBUG_COMMAND_BREAK_PC:
     797                 :          0 :                         add_breakpoint(debug, nb_ins, debug_command_parsed.pc);
     798                 :          0 :                         continue;
     799                 :          0 :                 case DEBUG_COMMAND_CATCH:
     800                 :          0 :                         add_catchpoint(debug,
     801                 :          0 :                                 parse_event_name(debug_command_parsed.event));
     802                 :          0 :                         continue;
     803                 :            :                 case DEBUG_COMMAND_CLEAR:
     804                 :            :                         if (ensure_pc_defined(pc))
     805                 :          0 :                                 point_infos_destroy_breakpoints(pc);
     806                 :          0 :                         continue;
     807                 :          0 :                 case DEBUG_COMMAND_CLEAR_EVENT:
     808                 :          0 :                         point_infos_destroy_catchpoints(
     809                 :          0 :                                 parse_event_name(debug_command_parsed.event));
     810                 :          0 :                         continue;
     811                 :          0 :                 case DEBUG_COMMAND_CLEAR_PC:
     812                 :          0 :                         point_infos_destroy_breakpoints(
     813                 :            :                                 debug_command_parsed.pc);
     814                 :          0 :                         continue;
     815                 :            :                 case DEBUG_COMMAND_CONTINUE:
     816                 :            :                         if (ensure_still_validating(validation_success)) {
     817                 :            :                                 disable_step();
     818                 :          0 :                                 return 0;
     819                 :            :                         }
     820                 :          0 :                         continue;
     821                 :          0 :                 case DEBUG_COMMAND_DELETE:
     822                 :          0 :                         point_infos_destroy_all();
     823                 :          0 :                         continue;
     824                 :          0 :                 case DEBUG_COMMAND_DELETE_NUMBER:
     825                 :          0 :                         point_infos_destroy_at(
     826                 :            :                                 debug_command_parsed.point_number);
     827                 :          0 :                         continue;
     828                 :          0 :                 case DEBUG_COMMAND_INFO_FRAME:
     829                 :          0 :                         print_frame(debug);
     830                 :          0 :                         continue;
     831                 :          0 :                 case DEBUG_COMMAND_INFO_FRAME_OFFSET:
     832                 :          0 :                         print_frame_offset(debug,
     833                 :            :                                 debug_command_parsed.frame_offset);
     834                 :          0 :                         continue;
     835                 :          0 :                 case DEBUG_COMMAND_INFO_POINTS:
     836                 :          0 :                         point_infos_print_all();
     837                 :          0 :                         continue;
     838                 :          0 :                 case DEBUG_COMMAND_INFO_REGISTER:
     839                 :          0 :                         print_register(debug,
     840                 :            :                                 parse_register(debug_command_parsed.register_));
     841                 :          0 :                         continue;
     842                 :            :                 case DEBUG_COMMAND_INFO_REGISTERS:
     843                 :            :                         print_registers(debug);
     844                 :          0 :                         continue;
     845                 :            :                 case DEBUG_COMMAND_LIST:
     846                 :            :                         if (ensure_pc_defined(pc))
     847                 :          0 :                                 list(ins, nb_ins, &list_offset, 10, pc);
     848                 :          0 :                         continue;
     849                 :            :                 case DEBUG_COMMAND_LIST_COUNT:
     850                 :            :                         if (ensure_pc_defined(pc))
     851                 :          0 :                                 list(ins, nb_ins, &list_offset,
     852                 :            :                                         debug_command_parsed.instruction_count, pc);
     853                 :          0 :                         continue;
     854                 :            :                 case DEBUG_COMMAND_LIST_PROGRAM:
     855                 :            :                         if (ensure_pc_defined(pc))
     856                 :          0 :                                 list(ins, nb_ins, NULL, UINT32_MAX, pc);
     857                 :          0 :                         continue;
     858                 :          0 :                 case DEBUG_COMMAND_MAY_LITERAL_RHS:
     859                 :          0 :                         print_if_may_literal_rhs(debug,
     860                 :            :                                 &debug_command_parsed.comparison);
     861                 :          0 :                         continue;
     862                 :          0 :                 case DEBUG_COMMAND_MAY_REGISTER_RHS:
     863                 :          0 :                         print_if_may_register_rhs(debug,
     864                 :            :                                 &debug_command_parsed.comparison);
     865                 :          0 :                         continue;
     866                 :            :                 case DEBUG_COMMAND_EOF:
     867                 :            :                 case DEBUG_COMMAND_QUIT:
     868                 :            :                         PRINTLN("Quitting...");
     869                 :          0 :                         return validation_success == -EAGAIN ? -ECANCELED : 0;
     870                 :            :                 case DEBUG_COMMAND_RUN:
     871                 :            :                         PRINTLN("Re-running...");
     872                 :            :                         disable_step();
     873                 :          0 :                         validate_again = true;
     874                 :          0 :                         return -ECANCELED;
     875                 :            :                 case DEBUG_COMMAND_START:
     876                 :            :                         PRINTLN("Re-starting...");
     877                 :          0 :                         rc = enable_step(debug);
     878                 :          0 :                         if (rc < 0)
     879                 :            :                                 return rc;
     880                 :          0 :                         validate_again = true;
     881                 :          0 :                         return -ECANCELED;
     882                 :            :                 case DEBUG_COMMAND_STEP:
     883                 :            :                         if (ensure_still_validating(validation_success))
     884                 :            :                                 return 0;
     885                 :          0 :                         continue;
     886                 :          0 :                 case DEBUG_COMMAND_WHERE:
     887                 :          0 :                         debug_command_where(ins, nb_ins, validation_success, pc);
     888                 :          0 :                         continue;
     889                 :            :                 default:
     890                 :            :                         PRINTLN("INTERNAL ERROR");
     891                 :          0 :                         return -ENOTSUP;
     892                 :            :                 }
     893                 :            :         }
     894                 :            : }
     895                 :            : 
     896                 :            : /* Any point callback: print it and enable step-by-step validation. */
     897                 :            : static int
     898                 :          0 : point_cb(struct rte_bpf_validate_debug *debug, void *ctx)
     899                 :            : {
     900                 :          0 :         const uint32_t point_number = (uintptr_t)ctx;
     901                 :          0 :         point_infos_print_at(point_number);
     902                 :          0 :         return enable_step(debug);
     903                 :            : }
     904                 :            : 
     905                 :            : /* Branch stack machinery. */
     906                 :            : 
     907                 :            : static void
     908                 :            : clear_jump_always_step_point(void)
     909                 :            : {
     910                 :          0 :         rte_bpf_validate_debug_point_destroy(jump_always_step_point);
     911                 :          0 :         jump_always_step_point = NULL;
     912                 :          0 : }
     913                 :            : 
     914                 :            : static int
     915                 :          0 : reset_branch_tracking(struct rte_bpf_validate_debug *debug __rte_unused,
     916                 :            :         void *ctx __rte_unused)
     917                 :            : {
     918                 :          0 :         branch_stack.length = 0;
     919                 :          0 :         pending_jump_pc = UINT32_MAX;
     920                 :            :         clear_jump_always_step_point();
     921                 :          0 :         return 0;
     922                 :            : }
     923                 :            : 
     924                 :            : /*
     925                 :            :  * Handling the jump-always instructions:
     926                 :            :  * - upon a jump-always event save the pc and set a custom step callback;
     927                 :            :  * - ignore the first custom step callback call (still on the same jump);
     928                 :            :  * - on the second call push the jump pc into stack and delete the callback;
     929                 :            :  */
     930                 :            : 
     931                 :            : static int
     932                 :          0 : jump_always_step_cb(struct rte_bpf_validate_debug *debug, void *ctx __rte_unused)
     933                 :            : {
     934                 :            :         /* Step event is also emitted at the end of the jump instruction itself. */
     935                 :          0 :         if (rte_bpf_validate_debug_get_pc(debug) == pending_jump_pc)
     936                 :            :                 return 0;
     937                 :            : 
     938                 :          0 :         branch_stack_append(&(struct branch_info){
     939                 :            :                 .jump_pc = pending_jump_pc,
     940                 :          0 :                 .target_pc = rte_bpf_validate_debug_get_pc(debug),
     941                 :            :                 .is_conditional = false,
     942                 :            :         });
     943                 :            :         clear_jump_always_step_point();
     944                 :          0 :         return 0;
     945                 :            : }
     946                 :            : 
     947                 :            : static int
     948                 :          0 : jump_always_cb(struct rte_bpf_validate_debug *debug, void *ctx __rte_unused)
     949                 :            : {
     950                 :          0 :         pending_jump_pc = rte_bpf_validate_debug_get_pc(debug);
     951                 :            :         clear_jump_always_step_point();
     952                 :          0 :         jump_always_step_point = rte_bpf_validate_debug_catch(debug,
     953                 :            :                 RTE_BPF_VALIDATE_DEBUG_EVENT_STEP,
     954                 :          0 :                 &(struct rte_bpf_validate_debug_callback){ jump_always_step_cb });
     955                 :          0 :         return 0;
     956                 :            : }
     957                 :            : 
     958                 :            : /*
     959                 :            :  * Handling the jump-conditional instructions:
     960                 :            :  * - upon a jump-conditional event save the pc;
     961                 :            :  * - upon a branch-enter event push the conditional jump pc into stack;
     962                 :            :  * - upon a branch-return event pop conditional jump and all unconditional jumps
     963                 :            :  *   preceding it from the stack;
     964                 :            :  * - during step execution notify the user about the step events;
     965                 :            :  */
     966                 :            : 
     967                 :            : static int
     968                 :          0 : jump_conditional_cb(struct rte_bpf_validate_debug *debug, void *ctx __rte_unused)
     969                 :            : {
     970                 :          0 :         pending_jump_pc = rte_bpf_validate_debug_get_pc(debug);
     971                 :          0 :         return 0;
     972                 :            : }
     973                 :            : 
     974                 :            : static int
     975                 :          0 : branch_enter_cb(struct rte_bpf_validate_debug *debug, void *ctx __rte_unused)
     976                 :            : {
     977                 :          0 :         branch_stack_append(&(struct branch_info){
     978                 :            :                 .jump_pc = pending_jump_pc,
     979                 :          0 :                 .target_pc = rte_bpf_validate_debug_get_pc(debug),
     980                 :            :                 .is_conditional = true,
     981                 :            :         });
     982                 :          0 :         if (is_step_enabled())
     983                 :          0 :                 PRINTLN("Entered new branch at pc %u.", pending_jump_pc);
     984                 :          0 :         return 0;
     985                 :            : }
     986                 :            : 
     987                 :            : static int
     988                 :          0 : branch_return_cb(struct rte_bpf_validate_debug *debug __rte_unused,
     989                 :            :         void *ctx __rte_unused)
     990                 :            : {
     991                 :            :         clear_jump_always_step_point();
     992                 :            : 
     993                 :          0 :         while (branch_stack.length > 0) {
     994                 :          0 :                 const struct branch_info branch_info =
     995                 :          0 :                         branch_stack.branches[--branch_stack.length];
     996                 :          0 :                 pending_jump_pc = branch_info.jump_pc;
     997                 :          0 :                 if (branch_info.is_conditional)
     998                 :            :                         break;
     999                 :            :         }
    1000                 :          0 :         if (is_step_enabled())
    1001                 :          0 :                 PRINTLN("Returned from branch at pc %u.", pending_jump_pc);
    1002                 :          0 :         return 0;
    1003                 :            : }
    1004                 :            : 
    1005                 :            : /* Notify user when skipping branches in step mode. */
    1006                 :            : 
    1007                 :            : static int
    1008                 :          0 : branch_prune_cb(struct rte_bpf_validate_debug *debug __rte_unused,
    1009                 :            :         void *ctx __rte_unused)
    1010                 :            : {
    1011                 :          0 :         if (is_step_enabled())
    1012                 :          0 :                 PRINTLN("Prunned branch at pc %u.", pending_jump_pc);
    1013                 :          0 :         return 0;
    1014                 :            : }
    1015                 :            : 
    1016                 :            : static int
    1017                 :          0 : branch_unreachable_cb(struct rte_bpf_validate_debug *debug __rte_unused,
    1018                 :            :         void *ctx __rte_unused)
    1019                 :            : {
    1020                 :          0 :         if (is_step_enabled())
    1021                 :          0 :                 PRINTLN("Unreachable branch at pc %u.", pending_jump_pc);
    1022                 :          0 :         return 0;
    1023                 :            : }
    1024                 :            : 
    1025                 :            : /* Global initialization and cleanup functions. */
    1026                 :            : 
    1027                 :            : static int
    1028                 :          0 : set_callbacks(struct rte_bpf_validate_debug *debug)
    1029                 :            : {
    1030                 :            :         static const struct rte_bpf_validate_debug_callback events_callback[
    1031                 :            :                         RTE_BPF_VALIDATE_DEBUG_EVENT_END] = {
    1032                 :            :                 [RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_START] = { reset_branch_tracking },
    1033                 :            :                 [RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_SUCCESS] = { step_cb },
    1034                 :            :                 [RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_FAILURE] = { step_cb },
    1035                 :            :                 [RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_CONDITIONAL] = { jump_conditional_cb },
    1036                 :            :                 [RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_ALWAYS] = { jump_always_cb },
    1037                 :            :                 [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_ENTER] = { branch_enter_cb },
    1038                 :            :                 [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_RETURN] = { branch_return_cb },
    1039                 :            :                 [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_PRUNE] = { branch_prune_cb },
    1040                 :            :                 [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_UNREACHABLE] = { branch_unreachable_cb },
    1041                 :            :         };
    1042                 :            : 
    1043                 :          0 :         for (enum rte_bpf_validate_debug_event event = 0;
    1044                 :          0 :                         event < RTE_BPF_VALIDATE_DEBUG_EVENT_END; ++event)
    1045                 :          0 :                 if (events_callback[event].fn != NULL &&
    1046                 :          0 :                                 rte_bpf_validate_debug_catch(debug, event,
    1047                 :            :                                         &events_callback[event]) == NULL)
    1048                 :          0 :                         return -rte_errno;
    1049                 :            : 
    1050                 :            :         return 0;
    1051                 :            : }
    1052                 :            : 
    1053                 :            : struct rte_bpf_validate_debug *
    1054                 :          0 : debug_create(void)
    1055                 :            : {
    1056                 :            : 
    1057                 :            :         int rc = 0;
    1058                 :            : 
    1059                 :          0 :         struct rte_bpf_validate_debug *const debug = rte_bpf_validate_debug_create();
    1060                 :          0 :         if (debug == NULL)
    1061                 :          0 :                 rc = -rte_errno;
    1062                 :            : 
    1063                 :          0 :         rc = rc < 0 ? rc : set_callbacks(debug);
    1064                 :            : 
    1065                 :          0 :         rc = rc < 0 ? rc : enable_step(debug);
    1066                 :            : 
    1067                 :          0 :         if (rc < 0) {
    1068                 :          0 :                 debug_destroy(debug);
    1069                 :          0 :                 rte_errno = -rc;
    1070                 :          0 :                 return NULL;
    1071                 :            :         }
    1072                 :            : 
    1073                 :            :         return debug;
    1074                 :            : }
    1075                 :            : 
    1076                 :            : void
    1077                 :          0 : debug_destroy(struct rte_bpf_validate_debug *debug)
    1078                 :            : {
    1079                 :            :         /* No need to destroy created points, destroying debug will do it. */
    1080                 :            :         point_infos_free();
    1081                 :            :         branch_stack_free();
    1082                 :          0 :         rte_bpf_validate_debug_destroy(debug);
    1083                 :          0 : }
    1084                 :            : 
    1085                 :            : bool
    1086                 :          0 : debug_validate_again(void)
    1087                 :            : {
    1088                 :          0 :         return validate_again;
    1089                 :            : }

Generated by: LCOV version 1.14