1 1.1 christos /* Python implementation of ui_out 2 1.1 christos 3 1.1 christos Copyright (C) 2023-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This file is part of GDB. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.1 christos #ifndef GDB_PYTHON_PY_UIOUT_H 21 1.1 christos #define GDB_PYTHON_PY_UIOUT_H 22 1.1 christos 23 1.1 christos #include "python-internal.h" 24 1.1 christos #include "ui-out.h" 25 1.1 christos 26 1.1 christos /* A ui_out subclass that creates a Python object based on the data 27 1.1 christos that is passed in. */ 28 1.1 christos 29 1.1 christos class py_ui_out : public ui_out 30 1.1 christos { 31 1.1 christos public: 32 1.1 christos 33 1.1 christos py_ui_out () 34 1.1 christos : ui_out (fix_multi_location_breakpoint_output 35 1.1 christos | fix_breakpoint_script_output) 36 1.1 christos { 37 1.1 christos do_begin (ui_out_type_tuple, nullptr); 38 1.1 christos } 39 1.1 christos 40 1.1 christos bool can_emit_style_escape () const override 41 1.1 christos { return false; } 42 1.1 christos 43 1.1 christos bool do_is_mi_like_p () const override 44 1.1 christos { return true; } 45 1.1 christos 46 1.1 christos /* Return the Python object that was created. If a Python error 47 1.1 christos occurred during the processing, set the Python error and return 48 1.1 christos nullptr. */ 49 1.1 christos gdbpy_ref<> result () 50 1.1 christos { 51 1.1 christos if (m_error.has_value ()) 52 1.1 christos { 53 1.1 christos m_error->restore (); 54 1.1 christos return nullptr; 55 1.1 christos } 56 1.1 christos return std::move (current ().obj); 57 1.1 christos } 58 1.1 christos 59 1.1 christos ui_file *current_stream () const override 60 1.1 christos { return nullptr; } 61 1.1 christos 62 1.1 christos protected: 63 1.1 christos 64 1.1 christos void do_progress_end () override { } 65 1.1 christos void do_progress_start () override { } 66 1.1 christos void do_progress_notify (const std::string &, const char *, double, double) 67 1.1 christos override 68 1.1 christos { } 69 1.1 christos 70 1.1 christos void do_table_begin (int nbrofcols, int nr_rows, const char *tblid) override 71 1.1 christos { 72 1.1 christos do_begin (ui_out_type_list, tblid); 73 1.1 christos } 74 1.1 christos void do_table_body () override 75 1.1 christos { } 76 1.1 christos void do_table_end () override 77 1.1 christos { 78 1.1 christos do_end (ui_out_type_list); 79 1.1 christos } 80 1.1 christos void do_table_header (int width, ui_align align, 81 1.1 christos const std::string &col_name, 82 1.1 christos const std::string &col_hdr) override 83 1.1 christos { } 84 1.1 christos 85 1.1 christos void do_begin (ui_out_type type, const char *id) override; 86 1.1 christos void do_end (ui_out_type type) override; 87 1.1 christos 88 1.1 christos void do_field_signed (int fldno, int width, ui_align align, 89 1.1 christos const char *fldname, LONGEST value) override; 90 1.1 christos void do_field_unsigned (int fldno, int width, ui_align align, 91 1.1 christos const char *fldname, ULONGEST value) override; 92 1.1 christos 93 1.1 christos void do_field_skip (int fldno, int width, ui_align align, 94 1.1 christos const char *fldname) override 95 1.1 christos { } 96 1.1 christos 97 1.1 christos void do_field_string (int fldno, int width, ui_align align, 98 1.1 christos const char *fldname, const char *string, 99 1.1 christos const ui_file_style &style) override; 100 1.1 christos void do_field_fmt (int fldno, int width, ui_align align, 101 1.1 christos const char *fldname, const ui_file_style &style, 102 1.1 christos const char *format, va_list args) override 103 1.1 christos ATTRIBUTE_PRINTF (7, 0); 104 1.1 christos 105 1.1 christos void do_spaces (int numspaces) override 106 1.1 christos { } 107 1.1 christos 108 1.1 christos void do_text (const char *string) override 109 1.1 christos { } 110 1.1 christos 111 1.1 christos void do_message (const ui_file_style &style, 112 1.1 christos const char *format, va_list args) 113 1.1 christos override ATTRIBUTE_PRINTF (3,0) 114 1.1 christos { } 115 1.1 christos 116 1.1 christos void do_wrap_hint (int indent) override 117 1.1 christos { } 118 1.1 christos 119 1.1 christos void do_flush () override 120 1.1 christos { } 121 1.1 christos 122 1.1 christos void do_redirect (struct ui_file *outstream) override 123 1.1 christos { } 124 1.1 christos 125 1.1 christos private: 126 1.1 christos 127 1.1 christos /* When constructing Python objects, this class keeps a stack of 128 1.1 christos objects being constructed. Each such object has this type. */ 129 1.1 christos struct object_desc 130 1.1 christos { 131 1.1 christos /* Name of the field (or empty for lists) that this object will 132 1.1 christos eventually become. */ 133 1.1 christos std::string field_name; 134 1.1 christos /* The object under construction. */ 135 1.1 christos gdbpy_ref<> obj; 136 1.1 christos /* The type of structure being created. Note that tables are 137 1.1 christos treated as lists here. */ 138 1.1 christos ui_out_type type; 139 1.1 christos }; 140 1.1 christos 141 1.1 christos /* The stack of objects being created. */ 142 1.1 christos std::vector<object_desc> m_objects; 143 1.1 christos 144 1.1 christos /* If an error occurred, this holds the exception information for 145 1.1 christos use by the 'release' method. */ 146 1.1 christos std::optional<gdbpy_err_fetch> m_error; 147 1.1 christos 148 1.1 christos /* Return a reference to the object under construction. */ 149 1.1 christos object_desc ¤t () 150 1.1 christos { return m_objects.back (); } 151 1.1 christos 152 1.1 christos /* Add a new field to the current object under construction. */ 153 1.1 christos void add_field (const char *name, const gdbpy_ref<> &obj); 154 1.1 christos }; 155 1.1 christos 156 1.1 christos #endif /* GDB_PYTHON_PY_UIOUT_H */ 157