LCOV - code coverage report
Current view: top level - backends - x86_initreg_sample.c (source / functions) Coverage Total Hit
Test: elfutils-0.194 Lines: 0.0 % 46 0
Test Date: 2025-10-28 03:46:40 Functions: 0.0 % 2 0
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 0.0 % 42 0

             Branch data     Line data    Source code
       1                 :             : /* x86 stack sample register handling, pieces common to x86-64 and i386.
       2                 :             :    Copyright (C) 2025 Red Hat, Inc.
       3                 :             :    This file is part of elfutils.
       4                 :             : 
       5                 :             :    This file is free software; you can redistribute it and/or modify
       6                 :             :    it under the terms of either
       7                 :             : 
       8                 :             :      * the GNU Lesser General Public License as published by the Free
       9                 :             :        Software Foundation; either version 3 of the License, or (at
      10                 :             :        your option) any later version
      11                 :             : 
      12                 :             :    or
      13                 :             : 
      14                 :             :      * the GNU General Public License as published by the Free
      15                 :             :        Software Foundation; either version 2 of the License, or (at
      16                 :             :        your option) any later version
      17                 :             : 
      18                 :             :    or both in parallel, as here.
      19                 :             : 
      20                 :             :    elfutils is distributed in the hope that it will be useful, but
      21                 :             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      22                 :             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      23                 :             :    General Public License for more details.
      24                 :             : 
      25                 :             :    You should have received copies of the GNU General Public License and
      26                 :             :    the GNU Lesser General Public License along with this program.  If
      27                 :             :    not, see <http://www.gnu.org/licenses/>.  */
      28                 :             : 
      29                 :             : static bool
      30                 :           0 : x86_sample_sp_pc (const Dwarf_Word *regs, uint32_t n_regs,
      31                 :             :                   const int *regs_mapping, uint32_t n_regs_mapping,
      32                 :             :                   Dwarf_Word *sp, uint sp_index /* into dwarf_regs */,
      33                 :             :                   Dwarf_Word *pc, uint pc_index /* into dwarf_regs */)
      34                 :             : {
      35         [ #  # ]:           0 :   if (sp != NULL) *sp = 0;
      36         [ #  # ]:           0 :   if (pc != NULL) *pc = 0;
      37                 :             : #if !defined(__x86_64__)
      38                 :             :   (void)regs;
      39                 :             :   (void)n_regs;
      40                 :             :   (void)regs_mapping;
      41                 :             :   (void)n_regs_mapping;
      42                 :             :   (void)sp;
      43                 :             :   (void)sp_index;
      44                 :             :   (void)pc;
      45                 :             :   (void)pc_index;
      46                 :             :   return false;
      47                 :             : #else /* __x86_64__ */
      48                 :             :   /* TODO: Register locations could be cached and rechecked on a
      49                 :             :      fastpath without needing to loop? */
      50                 :           0 :   int j, need_sp = (sp != NULL), need_pc = (pc != NULL);
      51   [ #  #  #  # ]:           0 :   for (j = 0; (need_sp || need_pc) && n_regs_mapping > (uint32_t)j; j++)
      52                 :             :     {
      53         [ #  # ]:           0 :       if (n_regs < (uint32_t)j) break;
      54   [ #  #  #  # ]:           0 :       if (need_sp && regs_mapping[j] == (int)sp_index)
      55                 :             :         {
      56                 :           0 :           *sp = regs[j]; need_sp = false;
      57                 :             :         }
      58   [ #  #  #  # ]:           0 :       if (need_pc && regs_mapping[j] == (int)pc_index)
      59                 :             :         {
      60                 :           0 :           *pc = regs[j]; need_pc = false;
      61                 :             :         }
      62                 :             :     }
      63                 :           0 :   return (!need_sp && !need_pc);
      64                 :             : #endif
      65                 :             : }
      66                 :             : 
      67                 :             : static bool
      68                 :           0 : x86_sample_perf_regs_mapping (Ebl *ebl,
      69                 :             :                               uint64_t perf_regs_mask, uint32_t abi,
      70                 :             :                               const int **regs_mapping,
      71                 :             :                               size_t *n_regs_mapping)
      72                 :             : {
      73   [ #  #  #  # ]:           0 :   if (perf_regs_mask != 0 && ebl->cached_perf_regs_mask == perf_regs_mask)
      74                 :             :     {
      75                 :           0 :       *regs_mapping = ebl->cached_regs_mapping;
      76                 :           0 :       *n_regs_mapping = ebl->cached_n_regs_mapping;
      77                 :           0 :       return true;
      78                 :             :     }
      79                 :             : 
      80                 :             :   /* The following facts are needed to translate x86 registers correctly:
      81                 :             :      - perf register order seen in linux arch/x86/include/uapi/asm/perf_regs.h
      82                 :             :        The registers array is built in the same order as the enum!
      83                 :             :        (See the code in tools/perf/util/intel-pt.c intel_pt_add_gp_regs().)
      84                 :             :      - EBL PERF_FRAME_REGS_MASK specifies all registers except segment and
      85                 :             :        flags.  However, regs_mask might be a different set of registers.
      86                 :             :        Again, regs_mask bits are in asm/perf_regs.h enum order.
      87                 :             :      - dwarf register order seen in elfutils backends/{x86_64,i386}_initreg.c
      88                 :             :        (matching pt_regs struct in linux arch/x86/include/asm/ptrace.h)
      89                 :             :        and it's a fairly different register order!
      90                 :             : 
      91                 :             :      For comparison, you can study codereview.qt-project.org/gitweb?p=qt-creator/perfparser.git;a=blob;f=app/perfregisterinfo.cpp;hb=HEAD
      92                 :             :      and follow the code which uses those tables of magic numbers.
      93                 :             :      But it's better to follow original sources of truth for this.  */
      94                 :             : 
      95                 :           0 :   bool is_abi32 = (abi == PERF_SAMPLE_REGS_ABI_32);
      96                 :             : 
      97                 :             :   /* Locations of dwarf_regs in the perf_event_x86_regs enum order,
      98                 :             :      not the regs[] array (which will include a subset of the regs):  */
      99                 :           0 :   static const int regs_i386[] = {0, 2, 3, 1, 7/*sp*/, 6, 4, 5, 8/*ip*/};
     100                 :           0 :   static const int regs_x86_64[] = {0, 3, 2, 1, 4, 5, 6, 7/*sp*/,
     101                 :             :                                     16/*r8 after flags+segment*/, 17, 18, 19, 20, 21, 22, 23,
     102                 :             :                                     8/*ip*/};
     103         [ #  # ]:           0 :   const int *dwarf_to_perf = is_abi32 ? regs_i386 : regs_x86_64;
     104                 :             : 
     105                 :             :   /* Count bits and allocate regs_mapping:  */
     106                 :           0 :   int j, k, kmax, count; uint64_t bit;
     107                 :           0 :   for (k = 0, kmax = -1, count = 0, bit = 1;
     108         [ #  # ]:           0 :        k < PERF_REG_X86_64_MAX; k++, bit <<= 1)
     109                 :             :     {
     110         [ #  # ]:           0 :       if ((bit & perf_regs_mask)) {
     111                 :           0 :         count++;
     112                 :           0 :         kmax = k;
     113                 :             :       }
     114                 :             :     }
     115                 :           0 :   ebl->cached_perf_regs_mask = perf_regs_mask;
     116                 :           0 :   ebl->cached_regs_mapping = (int *)calloc (count, sizeof(int));
     117                 :           0 :   ebl->cached_n_regs_mapping = count;
     118                 :             : 
     119                 :             :   /* Locations of perf_regs in the regs[] array, according to
     120                 :             :      perf_regs_mask:  */
     121                 :           0 :   int perf_to_regs[PERF_REG_X86_64_MAX];
     122                 :           0 :   uint64_t expected_mask = is_abi32 ?
     123         [ #  # ]:           0 :     PERF_FRAME_REGISTERS_I386 : PERF_FRAME_REGISTERS_X86_64;
     124         [ #  # ]:           0 :   for (j = 0, k = 0, bit = 1; k <= kmax; k++, bit <<= 1)
     125                 :             :     {
     126   [ #  #  #  # ]:           0 :       if ((bit & expected_mask) && (bit & perf_regs_mask))
     127                 :             :         {
     128                 :           0 :           perf_to_regs[k] = j;
     129                 :           0 :           j++;
     130                 :             :         }
     131                 :             :       else
     132                 :             :         {
     133                 :           0 :           perf_to_regs[k] = -1;
     134                 :             :         }
     135                 :             :     }
     136         [ #  # ]:           0 :   if (j > (int)ebl->cached_n_regs_mapping)
     137                 :             :       return false;
     138                 :             : 
     139                 :             :   /* Locations of perf_regs in the dwarf_regs array, according to
     140                 :             :      perf_regs_mask and perf_to_regs[]:  */
     141         [ #  # ]:           0 :   for (size_t i = 0; i < ebl->frame_nregs; i++)
     142                 :             :     {
     143                 :           0 :       k = dwarf_to_perf[i];
     144                 :           0 :       j = perf_to_regs[k];
     145         [ #  # ]:           0 :       if (j < 0) continue;
     146                 :           0 :       ebl->cached_regs_mapping[j] = i;
     147                 :             :     }
     148                 :             : 
     149                 :           0 :   *regs_mapping = ebl->cached_regs_mapping;
     150                 :           0 :   *n_regs_mapping = ebl->cached_n_regs_mapping;
     151                 :           0 :   return true;
     152                 :             : }
        

Generated by: LCOV version 2.0-1