LCOV - code coverage report
Current view: top level - strace - xlat.c (source / functions) Hit Total Coverage
Test: strace-4.18.0.129.97bbb Code Coverage Lines: 75 78 96.2 %
Date: 2017-07-31 03:46:08 Functions: 8 8 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 68 76 89.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
       3                 :            :  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
       4                 :            :  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
       5                 :            :  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
       6                 :            :  * Copyright (c) 1999-2017 The strace developers.
       7                 :            :  * All rights reserved.
       8                 :            :  *
       9                 :            :  * Redistribution and use in source and binary forms, with or without
      10                 :            :  * modification, are permitted provided that the following conditions
      11                 :            :  * are met:
      12                 :            :  * 1. Redistributions of source code must retain the above copyright
      13                 :            :  *    notice, this list of conditions and the following disclaimer.
      14                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      15                 :            :  *    notice, this list of conditions and the following disclaimer in the
      16                 :            :  *    documentation and/or other materials provided with the distribution.
      17                 :            :  * 3. The name of the author may not be used to endorse or promote products
      18                 :            :  *    derived from this software without specific prior written permission.
      19                 :            :  *
      20                 :            :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
      21                 :            :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      22                 :            :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      23                 :            :  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
      24                 :            :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      25                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      26                 :            :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      27                 :            :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      28                 :            :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      29                 :            :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      30                 :            :  */
      31                 :            : 
      32                 :            : #include "defs.h"
      33                 :            : #include <stdarg.h>
      34                 :            : 
      35                 :            : const char *
      36                 :      10294 : xlookup(const struct xlat *xlat, const uint64_t val)
      37                 :            : {
      38 [ +  + ][ +  + ]:     490449 :         for (; xlat->str != NULL; xlat++)
      39 [ +  + ][ +  + ]:     483752 :                 if (xlat->val == val)
      40                 :     255347 :                         return xlat->str;
      41                 :            :         return NULL;
      42                 :            : }
      43                 :            : 
      44                 :            : static int
      45                 :       5168 : xlat_bsearch_compare(const void *a, const void *b)
      46                 :            : {
      47                 :       5168 :         const uint64_t val1 = *(const uint64_t *) a;
      48                 :       5168 :         const uint64_t val2 = ((const struct xlat *) b)->val;
      49 [ +  + ][ +  + ]:       5168 :         return (val1 > val2) ? 1 : (val1 < val2) ? -1 : 0;
      50                 :            : }
      51                 :            : 
      52                 :            : const char *
      53                 :       2152 : xlat_search(const struct xlat *xlat, const size_t nmemb, const uint64_t val)
      54                 :            : {
      55                 :       2152 :         const struct xlat *e =
      56                 :            :                 bsearch((const void *) &val,
      57                 :            :                         xlat, nmemb, sizeof(*xlat), xlat_bsearch_compare);
      58                 :            : 
      59         [ +  + ]:       2152 :         return e ? e->str : NULL;
      60                 :            : }
      61                 :            : 
      62                 :            : /**
      63                 :            :  * Print entry in struct xlat table, if there.
      64                 :            :  *
      65                 :            :  * @param val  Value to search a literal representation for.
      66                 :            :  * @param dflt String (abbreviated in comment syntax) which should be emitted
      67                 :            :  *             if no appropriate xlat value has been found.
      68                 :            :  * @param xlat (And the following arguments) Pointers to arrays of xlat values.
      69                 :            :  *             The last argument should be NULL.
      70                 :            :  * @return     1 if appropriate xlat value has been found, 0 otherwise.
      71                 :            :  */
      72                 :            : int
      73                 :     251750 : printxvals(const uint64_t val, const char *dflt, const struct xlat *xlat, ...)
      74                 :            : {
      75                 :            :         va_list args;
      76                 :            : 
      77                 :     251750 :         va_start(args, xlat);
      78 [ +  - ][ +  + ]:     252900 :         for (; xlat; xlat = va_arg(args, const struct xlat *)) {
      79                 :     251750 :                 const char *str = xlookup(xlat, val);
      80                 :            : 
      81         [ +  + ]:     251750 :                 if (str) {
      82                 :     250600 :                         tprints(str);
      83                 :     250600 :                         va_end(args);
      84                 :     250600 :                         return 1;
      85                 :            :                 }
      86                 :            :         }
      87                 :            :         /* No hits -- print raw # instead. */
      88                 :       1150 :         tprintf("%#" PRIx64, val);
      89                 :       1150 :         tprints_comment(dflt);
      90                 :            : 
      91                 :       1150 :         va_end(args);
      92                 :            : 
      93                 :       1150 :         return 0;
      94                 :            : }
      95                 :            : 
      96                 :            : /**
      97                 :            :  * Print entry in sorted struct xlat table, if it is there.
      98                 :            :  *
      99                 :            :  * @param xlat      Pointer to an array of xlat values (not terminated with
     100                 :            :  *                  XLAT_END).
     101                 :            :  * @param xlat_size Number of xlat elements present in array (usually ARRAY_SIZE
     102                 :            :  *                  if array is declared in the unit's scope and not
     103                 :            :  *                  terminated with XLAT_END).
     104                 :            :  * @param val       Value to search literal representation for.
     105                 :            :  * @param dflt      String (abbreviated in comment syntax) which should be
     106                 :            :  *                  emitted if no appropriate xlat value has been found.
     107                 :            :  * @return          1 if appropriate xlat value has been found, 0
     108                 :            :  *                  otherwise.
     109                 :            :  */
     110                 :            : int
     111                 :       2152 : printxval_searchn(const struct xlat *xlat, size_t xlat_size, uint64_t val,
     112                 :            :         const char *dflt)
     113                 :            : {
     114                 :       2152 :         const char *s = xlat_search(xlat, xlat_size, val);
     115                 :            : 
     116         [ +  + ]:       2152 :         if (s) {
     117                 :       1434 :                 tprints(s);
     118                 :       1434 :                 return 1;
     119                 :            :         }
     120                 :            : 
     121                 :        718 :         tprintf("%#" PRIx64, val);
     122                 :        718 :         tprints_comment(dflt);
     123                 :            : 
     124                 :        718 :         return 0;
     125                 :            : }
     126                 :            : 
     127                 :            : /*
     128                 :            :  * Interpret `xlat' as an array of flags
     129                 :            :  * print the entries whose bits are on in `flags'
     130                 :            :  */
     131                 :            : void
     132                 :        188 : addflags(const struct xlat *xlat, uint64_t flags)
     133                 :            : {
     134         [ +  + ]:       3008 :         for (; xlat->str; xlat++) {
     135 [ +  + ][ +  + ]:       2820 :                 if (xlat->val && (flags & xlat->val) == xlat->val) {
     136                 :        202 :                         tprintf("|%s", xlat->str);
     137                 :        202 :                         flags &= ~xlat->val;
     138                 :            :                 }
     139                 :            :         }
     140         [ -  + ]:        188 :         if (flags) {
     141                 :          0 :                 tprintf("|%#" PRIx64, flags);
     142                 :            :         }
     143                 :        188 : }
     144                 :            : 
     145                 :            : /*
     146                 :            :  * Interpret `xlat' as an array of flags.
     147                 :            :  * Print to static string the entries whose bits are on in `flags'
     148                 :            :  * Return static string.
     149                 :            :  */
     150                 :            : const char *
     151                 :         92 : sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags)
     152                 :            : {
     153                 :            :         static char outstr[1024];
     154                 :            :         char *outptr;
     155                 :         92 :         int found = 0;
     156                 :            : 
     157                 :         92 :         outptr = stpcpy(outstr, prefix);
     158                 :            : 
     159 [ -  + ][ #  # ]:         92 :         if (flags == 0 && xlat->val == 0 && xlat->str) {
                 [ #  # ]
     160                 :          0 :                 strcpy(outptr, xlat->str);
     161                 :          0 :                 return outstr;
     162                 :            :         }
     163                 :            : 
     164         [ +  + ]:        480 :         for (; xlat->str; xlat++) {
     165 [ +  + ][ +  + ]:        468 :                 if (xlat->val && (flags & xlat->val) == xlat->val) {
     166         [ +  + ]:        138 :                         if (found)
     167                 :         52 :                                 *outptr++ = '|';
     168                 :        138 :                         outptr = stpcpy(outptr, xlat->str);
     169                 :        138 :                         found = 1;
     170                 :        138 :                         flags &= ~xlat->val;
     171         [ +  + ]:        138 :                         if (!flags)
     172                 :            :                                 break;
     173                 :            :                 }
     174                 :            :         }
     175         [ +  + ]:         92 :         if (flags) {
     176         [ +  + ]:         12 :                 if (found)
     177                 :          6 :                         *outptr++ = '|';
     178                 :         12 :                 outptr += sprintf(outptr, "%#" PRIx64, flags);
     179                 :            :         }
     180                 :            : 
     181                 :            :         return outstr;
     182                 :            : }
     183                 :            : 
     184                 :            : int
     185                 :     144460 : printflags_ex(uint64_t flags, const char *dflt, const struct xlat *xlat, ...)
     186                 :            : {
     187                 :     144460 :         unsigned int n = 0;
     188                 :            :         va_list args;
     189                 :            : 
     190                 :     144460 :         va_start(args, xlat);
     191 [ +  - ][ +  + ]:     289248 :         for (; xlat; xlat = va_arg(args, const struct xlat *)) {
     192 [ +  + ][ +  + ]:     186858 :                 for (; (flags || !n) && xlat->str; ++xlat) {
     193 [ +  + ][ +  + ]:     182833 :                         if ((flags == xlat->val) ||
     194         [ +  + ]:     178110 :                             (xlat->val && (flags & xlat->val) == xlat->val)) {
     195         [ +  + ]:      18609 :                                 tprintf("%s%s", (n++ ? "|" : ""), xlat->str);
     196                 :      18609 :                                 flags &= ~xlat->val;
     197                 :            :                         }
     198         [ +  + ]:     182833 :                         if (!flags)
     199                 :            :                                 break;
     200                 :            :                 }
     201                 :            :         }
     202                 :     144460 :         va_end(args);
     203                 :            : 
     204         [ +  + ]:     144460 :         if (n) {
     205         [ +  + ]:       5914 :                 if (flags) {
     206                 :       2137 :                         tprintf("|%#" PRIx64, flags);
     207                 :       2137 :                         n++;
     208                 :            :                 }
     209                 :            :         } else {
     210         [ +  + ]:     138546 :                 if (flags) {
     211                 :       1560 :                         tprintf("%#" PRIx64, flags);
     212                 :       1560 :                         tprints_comment(dflt);
     213                 :            :                 } else {
     214         [ +  + ]:     136986 :                         if (dflt)
     215                 :     136980 :                                 tprints("0");
     216                 :            :                 }
     217                 :            :         }
     218                 :            : 
     219                 :     144460 :         return n;
     220                 :            : }

Generated by: LCOV version 1.11