LCOV - code coverage report
Current view: top level - libdwfl_stacktrace - dwflst_process_tracker.c (source / functions) Coverage Total Hit
Test: elfutils-0.196 Lines: 87.5 % 96 84
Test Date: 2026-08-14 16:31:37 Functions: 100.0 % 6 6
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 63.0 % 46 29

             Branch data     Line data    Source code
       1                 :             : /* Track multiple Dwfl structs for multiple processes.
       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                 :             : #ifdef HAVE_CONFIG_H
      30                 :             : # include <config.h>
      31                 :             : #endif
      32                 :             : 
      33                 :             : #include "libdwfl_stacktraceP.h"
      34                 :             : 
      35                 :             : #define HTAB_DEFAULT_SIZE 1021
      36                 :             : 
      37                 :          14 : Dwflst_Process_Tracker *dwflst_tracker_begin (const Dwfl_Callbacks *callbacks)
      38                 :             : {
      39                 :          14 :   Dwflst_Process_Tracker *tracker = calloc (1, sizeof *tracker);
      40         [ -  + ]:          14 :   if (tracker == NULL)
      41                 :             :     {
      42                 :           0 :       __libdwfl_seterrno (DWFL_E_NOMEM);
      43                 :           0 :       return tracker;
      44                 :             :     }
      45                 :             : 
      46                 :          14 :   dwflst_tracker_elftab_init (&tracker->elftab, HTAB_DEFAULT_SIZE);
      47                 :          14 :   rwlock_init (tracker->elftab_lock);
      48                 :          14 :   dwflst_tracker_dwfltab_init (&tracker->dwfltab, HTAB_DEFAULT_SIZE);
      49                 :          14 :   rwlock_init (tracker->dwfltab_lock);
      50                 :             : 
      51                 :          14 :   tracker->callbacks = callbacks;
      52                 :          14 :   return tracker;
      53                 :             : }
      54                 :             : 
      55                 :           6 : Dwfl *dwflst_tracker_dwfl_begin (Dwflst_Process_Tracker *tracker)
      56                 :             : {
      57                 :           6 :   Dwfl *dwfl = INTUSE(dwfl_begin) (tracker->callbacks);
      58         [ +  - ]:           6 :   if (dwfl == NULL)
      59                 :             :     return dwfl;
      60                 :             : 
      61                 :             :   /* TODO: Could also share dwfl->debuginfod, but thread-safely? */
      62                 :           6 :   dwfl->tracker = tracker;
      63                 :             : 
      64                 :             :   /* XXX: dwfl added to dwfltab when dwfl->process set in dwfl_attach_state. */
      65                 :             :   /* XXX: dwfl removed from dwfltab in dwfl_end() */
      66                 :             : 
      67                 :           6 :   return dwfl;
      68                 :             : }
      69                 :             : 
      70                 :       11807 : Dwfl *dwflst_tracker_find_pid (Dwflst_Process_Tracker *tracker,
      71                 :             :                                pid_t pid,
      72                 :             :                                Dwfl *(*callback) (Dwflst_Process_Tracker *,
      73                 :             :                                                   pid_t, void *),
      74                 :             :                                void *arg)
      75                 :             : {
      76                 :       11807 :   Dwfl *dwfl = NULL;
      77                 :             : 
      78                 :       11807 :   rwlock_rdlock (tracker->dwfltab_lock);
      79                 :       11807 :   dwflst_tracker_dwfl_info *ent
      80                 :       11807 :     = dwflst_tracker_dwfltab_find(&tracker->dwfltab, pid);
      81                 :       11807 :   rwlock_unlock (tracker->dwfltab_lock);
      82                 :             : 
      83   [ +  +  +  - ]:       11807 :   if (ent != NULL && !ent->invalid)
      84                 :       11801 :     dwfl = ent->dwfl;
      85         [ +  + ]:       11807 :   if (dwfl == NULL && callback != NULL)
      86                 :           6 :     dwfl = callback(tracker, pid, arg);
      87         [ +  - ]:       11807 :   if (dwfl != NULL)
      88                 :             :     {
      89         [ -  + ]:       11807 :       assert (dwfl->tracker == tracker);
      90                 :             :       /* XXX: dwfl added to dwfltab when dwfl->process set in dwfl_attach_state.
      91                 :             :          Prior to that, the pid is not confirmed. */
      92                 :             :     }
      93                 :             : 
      94                 :       11807 :   return dwfl;
      95                 :             : }
      96                 :             : 
      97                 :             : void
      98                 :             : internal_function
      99                 :           6 : __libdwfl_stacktrace_add_dwfl_to_tracker (Dwfl *dwfl) {
     100                 :           6 :   Dwflst_Process_Tracker *tracker = dwfl->tracker;
     101         [ -  + ]:           6 :   assert (tracker != NULL);
     102                 :             : 
     103                 :             :   /* First try to find an existing entry to replace: */
     104                 :           6 :   dwflst_tracker_dwfl_info *ent = NULL;
     105                 :           6 :   unsigned long int hval = dwfl->process->pid;
     106                 :             : 
     107                 :           6 :   rwlock_wrlock (tracker->dwfltab_lock);
     108                 :           6 :   ent = dwflst_tracker_dwfltab_find(&tracker->dwfltab, hval);
     109         [ -  + ]:           6 :   if (ent != NULL)
     110                 :             :     {
     111                 :             :       /* TODO: This is a bare-minimum solution. Ideally
     112                 :             :          we would clean up the existing ent->dwfl, but
     113                 :             :          this needs to be coordinated with any users of
     114                 :             :          the dwfl library that might still be holding it. */
     115                 :           0 :       ent->dwfl = dwfl;
     116                 :           0 :       ent->invalid = false;
     117                 :           0 :       rwlock_unlock (tracker->dwfltab_lock);
     118                 :           0 :       return;
     119                 :             :     }
     120                 :             : 
     121                 :             :   /* Only otherwise try to insert an entry: */
     122                 :           6 :   ent = calloc (1, sizeof(dwflst_tracker_dwfl_info));
     123         [ -  + ]:           6 :   if (ent == NULL)
     124                 :             :     {
     125                 :           0 :       rwlock_unlock (tracker->dwfltab_lock);
     126                 :           0 :       __libdwfl_seterrno (DWFL_E_NOMEM);
     127                 :           0 :       return;
     128                 :             :     }
     129                 :           6 :   ent->dwfl = dwfl;
     130                 :           6 :   ent->invalid = false;
     131         [ -  + ]:           6 :   if (dwflst_tracker_dwfltab_insert(&tracker->dwfltab, hval, ent) != 0)
     132                 :             :     {
     133                 :           0 :       free(ent);
     134                 :           0 :       rwlock_unlock (tracker->dwfltab_lock);
     135                 :           0 :       assert(false); /* Should not occur due to the wrlock on dwfltab. */
     136                 :             :     }
     137                 :           6 :   rwlock_unlock (tracker->dwfltab_lock);
     138                 :             : }
     139                 :             : 
     140                 :             : void
     141                 :             : internal_function
     142                 :           6 : __libdwfl_stacktrace_remove_dwfl_from_tracker (Dwfl *dwfl) {
     143         [ +  - ]:           6 :   if (dwfl->tracker == NULL)
     144                 :             :     return;
     145                 :           6 :   Dwflst_Process_Tracker *tracker = dwfl->tracker;
     146                 :           6 :   dwflst_tracker_dwfl_info *ent = NULL;
     147         [ +  - ]:           6 :   if (dwfl->process == NULL)
     148                 :             :     return;
     149                 :           6 :   unsigned long int hval = dwfl->process->pid;
     150                 :             : 
     151                 :           6 :   rwlock_wrlock (tracker->dwfltab_lock);
     152                 :           6 :   ent = dwflst_tracker_dwfltab_find(&tracker->dwfltab, hval);
     153   [ +  -  +  - ]:           6 :   if (ent != NULL && ent->dwfl == dwfl)
     154                 :             :     {
     155                 :           6 :       ent->dwfl = NULL;
     156                 :           6 :       ent->invalid = true;
     157                 :             :     }
     158                 :           6 :   rwlock_unlock (tracker->dwfltab_lock);
     159                 :             : }
     160                 :             : 
     161                 :          14 : void dwflst_tracker_end (Dwflst_Process_Tracker *tracker)
     162                 :             : {
     163         [ +  - ]:          14 :   if (tracker == NULL)
     164                 :             :     return;
     165                 :             : 
     166                 :          14 :   size_t idx;
     167                 :             : 
     168                 :             :   /* HACK to allow iteration of dynamicsizehash_concurrent.  */
     169                 :             :   /* XXX Based on lib/dynamicsizehash_concurrent.c free().  */
     170                 :          14 :   rwlock_fini (tracker->elftab_lock);
     171                 :          14 :   pthread_rwlock_destroy(&tracker->elftab.resize_rwl);
     172         [ +  + ]:       14322 :   for (idx = 1; idx <= tracker->elftab.size; idx++)
     173                 :             :     {
     174                 :       14294 :       dwflst_tracker_elftab_ent *ent = &tracker->elftab.table[idx];
     175         [ +  + ]:       14294 :       if (ent->hashval == 0)
     176                 :       14276 :         continue;
     177                 :          18 :       dwflst_tracker_elf_info *t =
     178                 :          18 :         (dwflst_tracker_elf_info *) atomic_load_explicit (&ent->val_ptr,
     179                 :             :                                                           memory_order_relaxed);
     180                 :          18 :       free(t->module_name);
     181         [ +  - ]:          18 :       if (t->fd >= 0)
     182                 :          18 :         close(t->fd);
     183         [ +  - ]:          18 :       if (t->elf != NULL)
     184                 :          18 :         elf_end(t->elf);
     185                 :          18 :       free(t); /* TODO: Check necessity. */
     186                 :             :     }
     187                 :          14 :   free (tracker->elftab.table);
     188                 :             : 
     189                 :             :   /* XXX Based on lib/dynamicsizehash_concurrent.c free().  */
     190                 :          14 :   rwlock_fini (tracker->dwfltab_lock);
     191                 :          14 :   pthread_rwlock_destroy(&tracker->dwfltab.resize_rwl);
     192         [ +  + ]:       14322 :   for (idx = 1; idx <= tracker->dwfltab.size; idx++)
     193                 :             :     {
     194                 :       14294 :       dwflst_tracker_dwfltab_ent *ent = &tracker->dwfltab.table[idx];
     195         [ +  + ]:       14294 :       if (ent->hashval == 0)
     196                 :       14288 :         continue;
     197                 :           6 :       dwflst_tracker_dwfl_info *t =
     198                 :           6 :         (dwflst_tracker_dwfl_info *) atomic_load_explicit (&ent->val_ptr,
     199                 :             :                                                            memory_order_relaxed);
     200         [ +  - ]:           6 :       if (t->dwfl != NULL)
     201                 :           6 :         INTUSE(dwfl_end) (t->dwfl);
     202                 :           6 :       free(t);
     203                 :             :     }
     204                 :          14 :   free (tracker->dwfltab.table);
     205                 :             : 
     206                 :          14 :   free (tracker);
     207                 :             : }
        

Generated by: LCOV version 2.0-1