gdb/common/print-utils.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Cell-based print utility routines for GDB, the GNU debugger.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "common-defs.h"
  15. #include "print-utils.h"
  16. #include <stdint.h>

  17. /* Temporary storage using circular buffer.  */

  18. #define NUMCELLS 16
  19. #define CELLSIZE 50

  20. /* Return the next entry in the circular buffer.  */

  21. static char *
  22. get_cell (void)
  23. {
  24.   static char buf[NUMCELLS][CELLSIZE];
  25.   static int cell = 0;

  26.   if (++cell >= NUMCELLS)
  27.     cell = 0;
  28.   return buf[cell];
  29. }

  30. static char *
  31. decimal2str (char *sign, ULONGEST addr, int width)
  32. {
  33.   /* Steal code from valprint.c:print_decimal().  Should this worry
  34.      about the real size of addr as the above does?  */
  35.   unsigned long temp[3];
  36.   char *str = get_cell ();
  37.   int i = 0;

  38.   do
  39.     {
  40.       temp[i] = addr % (1000 * 1000 * 1000);
  41.       addr /= (1000 * 1000 * 1000);
  42.       i++;
  43.       width -= 9;
  44.     }
  45.   while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));

  46.   width += 9;
  47.   if (width < 0)
  48.     width = 0;

  49.   switch (i)
  50.     {
  51.     case 1:
  52.       xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]);
  53.       break;
  54.     case 2:
  55.       xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width,
  56.                  temp[1], temp[0]);
  57.       break;
  58.     case 3:
  59.       xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width,
  60.                  temp[2], temp[1], temp[0]);
  61.       break;
  62.     default:
  63.       internal_error (__FILE__, __LINE__,
  64.                       _("failed internal consistency check"));
  65.     }

  66.   return str;
  67. }

  68. static char *
  69. octal2str (ULONGEST addr, int width)
  70. {
  71.   unsigned long temp[3];
  72.   char *str = get_cell ();
  73.   int i = 0;

  74.   do
  75.     {
  76.       temp[i] = addr % (0100000 * 0100000);
  77.       addr /= (0100000 * 0100000);
  78.       i++;
  79.       width -= 10;
  80.     }
  81.   while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));

  82.   width += 10;
  83.   if (width < 0)
  84.     width = 0;

  85.   switch (i)
  86.     {
  87.     case 1:
  88.       if (temp[0] == 0)
  89.         xsnprintf (str, CELLSIZE, "%*o", width, 0);
  90.       else
  91.         xsnprintf (str, CELLSIZE, "0%0*lo", width, temp[0]);
  92.       break;
  93.     case 2:
  94.       xsnprintf (str, CELLSIZE, "0%0*lo%010lo", width, temp[1], temp[0]);
  95.       break;
  96.     case 3:
  97.       xsnprintf (str, CELLSIZE, "0%0*lo%010lo%010lo", width,
  98.                  temp[2], temp[1], temp[0]);
  99.       break;
  100.     default:
  101.       internal_error (__FILE__, __LINE__,
  102.                       _("failed internal consistency check"));
  103.     }

  104.   return str;
  105. }

  106. /* See print-utils.h.  */

  107. char *
  108. pulongest (ULONGEST u)
  109. {
  110.   return decimal2str ("", u, 0);
  111. }

  112. /* See print-utils.h.  */

  113. char *
  114. plongest (LONGEST l)
  115. {
  116.   if (l < 0)
  117.     return decimal2str ("-", -l, 0);
  118.   else
  119.     return decimal2str ("", l, 0);
  120. }

  121. /* Eliminate warning from compiler on 32-bit systems.  */
  122. static int thirty_two = 32;

  123. /* See print-utils.h.  */

  124. char *
  125. phex (ULONGEST l, int sizeof_l)
  126. {
  127.   char *str;

  128.   switch (sizeof_l)
  129.     {
  130.     case 8:
  131.       str = get_cell ();
  132.       xsnprintf (str, CELLSIZE, "%08lx%08lx",
  133.                  (unsigned long) (l >> thirty_two),
  134.                  (unsigned long) (l & 0xffffffff));
  135.       break;
  136.     case 4:
  137.       str = get_cell ();
  138.       xsnprintf (str, CELLSIZE, "%08lx", (unsigned long) l);
  139.       break;
  140.     case 2:
  141.       str = get_cell ();
  142.       xsnprintf (str, CELLSIZE, "%04x", (unsigned short) (l & 0xffff));
  143.       break;
  144.     default:
  145.       str = phex (l, sizeof (l));
  146.       break;
  147.     }

  148.   return str;
  149. }

  150. /* See print-utils.h.  */

  151. char *
  152. phex_nz (ULONGEST l, int sizeof_l)
  153. {
  154.   char *str;

  155.   switch (sizeof_l)
  156.     {
  157.     case 8:
  158.       {
  159.         unsigned long high = (unsigned long) (l >> thirty_two);

  160.         str = get_cell ();
  161.         if (high == 0)
  162.           xsnprintf (str, CELLSIZE, "%lx",
  163.                      (unsigned long) (l & 0xffffffff));
  164.         else
  165.           xsnprintf (str, CELLSIZE, "%lx%08lx", high,
  166.                      (unsigned long) (l & 0xffffffff));
  167.         break;
  168.       }
  169.     case 4:
  170.       str = get_cell ();
  171.       xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l);
  172.       break;
  173.     case 2:
  174.       str = get_cell ();
  175.       xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff));
  176.       break;
  177.     default:
  178.       str = phex_nz (l, sizeof (l));
  179.       break;
  180.     }

  181.   return str;
  182. }

  183. /* See print-utils.h.  */

  184. char *
  185. hex_string (LONGEST num)
  186. {
  187.   char *result = get_cell ();

  188.   xsnprintf (result, CELLSIZE, "0x%s", phex_nz (num, sizeof (num)));
  189.   return result;
  190. }

  191. /* See print-utils.h.  */

  192. char *
  193. hex_string_custom (LONGEST num, int width)
  194. {
  195.   char *result = get_cell ();
  196.   char *result_end = result + CELLSIZE - 1;
  197.   const char *hex = phex_nz (num, sizeof (num));
  198.   int hex_len = strlen (hex);

  199.   if (hex_len > width)
  200.     width = hex_len;
  201.   if (width + 2 >= CELLSIZE)
  202.     internal_error (__FILE__, __LINE__, _("\
  203. hex_string_custom: insufficient space to store result"));

  204.   strcpy (result_end - width - 2, "0x");
  205.   memset (result_end - width, '0', width);
  206.   strcpy (result_end - hex_len, hex);
  207.   return result_end - width - 2;
  208. }

  209. /* See print-utils.h.  */

  210. char *
  211. int_string (LONGEST val, int radix, int is_signed, int width,
  212.             int use_c_format)
  213. {
  214.   switch (radix)
  215.     {
  216.     case 16:
  217.       {
  218.         char *result;

  219.         if (width == 0)
  220.           result = hex_string (val);
  221.         else
  222.           result = hex_string_custom (val, width);
  223.         if (! use_c_format)
  224.           result += 2;
  225.         return result;
  226.       }
  227.     case 10:
  228.       {
  229.         if (is_signed && val < 0)
  230.           return decimal2str ("-", -val, width);
  231.         else
  232.           return decimal2str ("", val, width);
  233.       }
  234.     case 8:
  235.       {
  236.         char *result = octal2str (val, width);

  237.         if (use_c_format || val == 0)
  238.           return result;
  239.         else
  240.           return result + 1;
  241.       }
  242.     default:
  243.       internal_error (__FILE__, __LINE__,
  244.                       _("failed internal consistency check"));
  245.     }
  246. }

  247. /* See print-utils.h.  */

  248. const char *
  249. core_addr_to_string (const CORE_ADDR addr)
  250. {
  251.   char *str = get_cell ();

  252.   strcpy (str, "0x");
  253.   strcat (str, phex (addr, sizeof (addr)));
  254.   return str;
  255. }

  256. /* See print-utils.h.  */

  257. const char *
  258. core_addr_to_string_nz (const CORE_ADDR addr)
  259. {
  260.   char *str = get_cell ();

  261.   strcpy (str, "0x");
  262.   strcat (str, phex_nz (addr, sizeof (addr)));
  263.   return str;
  264. }

  265. /* See print-utils.h.  */

  266. const char *
  267. host_address_to_string (const void *addr)
  268. {
  269.   char *str = get_cell ();

  270.   xsnprintf (str, CELLSIZE, "0x%s", phex_nz ((uintptr_t) addr, sizeof (addr)));
  271.   return str;
  272. }