public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* What should a std::error_code pretty printer show?
@ 2021-08-16 11:55 Jonathan Wakely
  2021-08-16 12:11 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2021-08-16 11:55 UTC (permalink / raw)
  To: libstdc++

I'm adding a GDB printer for std::error_code.What I have now prints
the category name as a quoted string, followed by the error value:

{"system": 0}
{"system": 1234}

If the category is std::generic_category() then it also shows the
strerror description:

{"generic": 13 "Permission denied"}

But I'd also like it to show the errno macro, but I'm not sure what's
the best way to show it.

Does this seem OK?

{"generic": 13 EACCES "Permission denied"}

I think that's a bit too verbose.

Would {"generic": EACCES} be better? You can always use ec.value() to
get the numeric value, and strerror to get the description if you want
those.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: What should a std::error_code pretty printer show?
  2021-08-16 11:55 What should a std::error_code pretty printer show? Jonathan Wakely
@ 2021-08-16 12:11 ` Jonathan Wakely
  2021-08-16 16:51   ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2021-08-16 12:11 UTC (permalink / raw)
  To: libstdc++

On Mon, 16 Aug 2021, 12:55 Jonathan Wakely, <jwakely.gcc@gmail.com> wrote:

> I'm adding a GDB printer for std::error_code.What I have now prints
> the category name as a quoted string, followed by the error value:
>
> {"system": 0}
> {"system": 1234}
>
> If the category is std::generic_category() then it also shows the
> strerror description:
>

I should probably extend this special case for the generic category to also
apply to the system category when the OS is POSIX-based. For POSIX systems,
the system error numbers are generic errno values.



> {"generic": 13 "Permission denied"}
>
> But I'd also like it to show the errno macro, but I'm not sure what's
> the best way to show it.
>
> Does this seem OK?
>
> {"generic": 13 EACCES "Permission denied"}
>
> I think that's a bit too verbose.
>
> Would {"generic": EACCES} be better? You can always use ec.value() to
> get the numeric value, and strerror to get the description if you want
> those.
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: What should a std::error_code pretty printer show?
  2021-08-16 12:11 ` Jonathan Wakely
@ 2021-08-16 16:51   ` Jonathan Wakely
  2021-08-16 19:30     ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2021-08-16 16:51 UTC (permalink / raw)
  To: libstdc++; +Cc: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1320 bytes --]

On Mon, 16 Aug 2021 at 13:11, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
>
>
> On Mon, 16 Aug 2021, 12:55 Jonathan Wakely, <jwakely.gcc@gmail.com> wrote:
>>
>> I'm adding a GDB printer for std::error_code.What I have now prints
>> the category name as a quoted string, followed by the error value:
>>
>> {"system": 0}
>> {"system": 1234}
>>
>> If the category is std::generic_category() then it also shows the
>> strerror description:
>
>
> I should probably extend this special case for the generic category to also apply to the system category when the OS is POSIX-based. For POSIX systems, the system error numbers are generic errno values.
>
>
>>
>> {"generic": 13 "Permission denied"}
>>
>> But I'd also like it to show the errno macro, but I'm not sure what's
>> the best way to show it.
>>
>> Does this seem OK?
>>
>> {"generic": 13 EACCES "Permission denied"}
>>
>> I think that's a bit too verbose.
>>
>> Would {"generic": EACCES} be better? You can always use ec.value() to
>> get the numeric value, and strerror to get the description if you want
>> those.

Here's what I plan to commit. It just uses {"generic": EACCES} for
known categories that use errno values, and {"foo": 99} for other
error categories.


It also supports std::error_condition (using the same printer and the
same output formats).

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 5049 bytes --]

commit f2de11d5f5758dfe90330b0efacbea9c7819df8e
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Aug 16 17:41:50 2021

    libstdc++: Add pretty printer for std::error_code and std::error_condition
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
    libstdc++-v3/ChangeLog:
    
            * python/libstdcxx/v6/printers.py (StdErrorCodePrinter): Define.
            (build_libstdcxx_dictionary): Register printer for
            std::error_code and std::error_condition.
            * testsuite/libstdc++-prettyprinters/cxx11.cc: Test it.

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py
index 550e0ecdd22..069c1bae183 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -18,7 +18,7 @@
 import gdb
 import itertools
 import re
-import sys
+import sys, os, errno
 
 ### Python 2 + Python 3 compatibility code
 
@@ -1484,6 +1484,52 @@ class StdCmpCatPrinter:
             name = names[int(self.val)]
         return 'std::{}::{}'.format(self.typename, name)
 
+class StdErrorCatPrinter:
+    "Print an object derived from std::error_category"
+
+    def __init__ (self, typename, val):
+        self.val = val
+        self.typename = typename
+
+    def to_string (self):
+        gdb.set_convenience_variable('__cat', self.val)
+        name = gdb.parse_and_eval('$__cat->name()').string()
+        return 'error category = "{}"'.format(name)
+
+class StdErrorCodePrinter:
+    "Print a std::error_code or std::error_condition"
+
+    _errno_categories = None # List of categories that use errno values
+
+    def __init__ (self, typename, val):
+        self.val = val
+        self.typename = typename
+        # Do this only once ...
+        if StdErrorCodePrinter._errno_categories is None:
+            StdErrorCodePrinter._errno_categories = ['generic']
+            try:
+                import posix
+                StdErrorCodePrinter._errno_categories.append('system')
+            except ImportError:
+                pass
+
+    @staticmethod
+    def _category_name(cat):
+        "Call the virtual function that overrides std::error_category::name()"
+        gdb.set_convenience_variable('__cat', cat)
+        return gdb.parse_and_eval('$__cat->name()').string()
+
+    def to_string (self):
+        value = self.val['_M_value']
+        category = self._category_name(self.val['_M_cat'])
+        strval = str(value)
+        if value > 0 and category in StdErrorCodePrinter._errno_categories:
+            try:
+                strval = errno.errorcode[int(value)]
+            except:
+                pass
+        return '%s = {"%s": %s}' % (self.typename, category, strval)
+
 # A "regular expression" printer which conforms to the
 # "SubPrettyPrinter" protocol from gdb.printing.
 class RxPrinter(object):
@@ -1886,6 +1932,8 @@ def build_libstdcxx_dictionary ():
     libstdcxx_printer.add_version('std::__cxx11::', 'basic_string', StdStringPrinter)
     libstdcxx_printer.add_container('std::', 'bitset', StdBitsetPrinter)
     libstdcxx_printer.add_container('std::', 'deque', StdDequePrinter)
+    libstdcxx_printer.add_version('std::', 'error_code', StdErrorCodePrinter)
+    libstdcxx_printer.add_version('std::', 'error_condition', StdErrorCodePrinter)
     libstdcxx_printer.add_container('std::', 'list', StdListPrinter)
     libstdcxx_printer.add_container('std::__cxx11::', 'list', StdListPrinter)
     libstdcxx_printer.add_container('std::', 'map', StdMapPrinter)
diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc
index 05950513ab0..08f2c367f45 100644
--- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc
+++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc
@@ -155,6 +155,25 @@ main()
 // { dg-final { note-test tpl {std::tuple containing = {[1] = 6, [2] = 7}} } }
   ExTuple &rtpl = tpl;
 // { dg-final { note-test rtpl {std::tuple containing = {[1] = 6, [2] = 7}} } }
+
+  std::error_code e0;
+  // { dg-final { note-test e0 {std::error_code = {"system": 0}} } }
+  std::error_condition ec0;
+  // { dg-final { note-test ec0 {std::error_condition = {"generic": 0}} } }
+  std::error_code einval = std::make_error_code(std::errc::invalid_argument);
+  // { dg-final { note-test einval {std::error_code = {"generic": EINVAL}} } }
+  std::error_condition ecinval = std::make_error_condition(std::errc::invalid_argument);
+  // { dg-final { note-test ecinval {std::error_condition = {"generic": EINVAL}} } }
+
+  struct custom_cat : std::error_category {
+    const char* name() const noexcept { return "miaow"; }
+    std::string message(int) const { return ""; }
+  } cat;
+  std::error_code emiaow(42, cat);
+  // { dg-final { note-test emiaow {std::error_code = {"miaow": 42}} } }
+  std::error_condition ecmiaow(42, cat);
+  // { dg-final { note-test ecmiaow {std::error_condition = {"miaow": 42}} } }
+
   placeholder(""); // Mark SPOT
   use(efl);
   use(fl);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: What should a std::error_code pretty printer show?
  2021-08-16 16:51   ` Jonathan Wakely
@ 2021-08-16 19:30     ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2021-08-16 19:30 UTC (permalink / raw)
  To: libstdc++; +Cc: gcc-patches

On Mon, 16 Aug 2021 at 17:51, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
> On Mon, 16 Aug 2021 at 13:11, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> >
> >
> >
> > On Mon, 16 Aug 2021, 12:55 Jonathan Wakely, <jwakely.gcc@gmail.com> wrote:
> >>
> >> I'm adding a GDB printer for std::error_code.What I have now prints
> >> the category name as a quoted string, followed by the error value:
> >>
> >> {"system": 0}
> >> {"system": 1234}
> >>
> >> If the category is std::generic_category() then it also shows the
> >> strerror description:
> >
> >
> > I should probably extend this special case for the generic category to also apply to the system category when the OS is POSIX-based. For POSIX systems, the system error numbers are generic errno values.
> >
> >
> >>
> >> {"generic": 13 "Permission denied"}
> >>
> >> But I'd also like it to show the errno macro, but I'm not sure what's
> >> the best way to show it.
> >>
> >> Does this seem OK?
> >>
> >> {"generic": 13 EACCES "Permission denied"}
> >>
> >> I think that's a bit too verbose.
> >>
> >> Would {"generic": EACCES} be better? You can always use ec.value() to
> >> get the numeric value, and strerror to get the description if you want
> >> those.
>
> Here's what I plan to commit. It just uses {"generic": EACCES} for
> known categories that use errno values, and {"foo": 99} for other
> error categories.
>
>
> It also supports std::error_condition (using the same printer and the
> same output formats).

Actually, the proposal in PR 65230 would mean that we should print { }
if the object has its default-constructed state, i.e. {"system": 0}
for error_code and {"generic": 0} for error_condition. I'll make that
change before pushing anything to master.

Other suggestions for improvement (or just agreeing with the
direction) are welcome.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-08-16 19:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-16 11:55 What should a std::error_code pretty printer show? Jonathan Wakely
2021-08-16 12:11 ` Jonathan Wakely
2021-08-16 16:51   ` Jonathan Wakely
2021-08-16 19:30     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).