| 1 | // Debugging mode support code -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2003-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | #include <bits/move.h> |
| 26 | #include <bits/stl_iterator_base_types.h> |
| 27 | #include <ext/atomicity.h> // __is_single_threaded |
| 28 | |
| 29 | #include <debug/formatter.h> |
| 30 | #include <debug/safe_base.h> |
| 31 | #include <debug/safe_unordered_base.h> |
| 32 | #include <debug/safe_iterator.h> |
| 33 | #include <debug/safe_local_iterator.h> |
| 34 | #include <debug/vector> |
| 35 | |
| 36 | #include <cassert> |
| 37 | #include <cstdio> // for std::fprintf, stderr |
| 38 | #include <cstdlib> // for std::abort |
| 39 | #include <cctype> // for std::isspace. |
| 40 | #include <cstring> // for std::strstr. |
| 41 | #include <climits> // for INT_MAX |
| 42 | |
| 43 | #include <algorithm> // for std::min. |
| 44 | |
| 45 | #include <cxxabi.h> // for __cxa_demangle. |
| 46 | |
| 47 | #include "mutex_pool.h" |
| 48 | |
| 49 | using namespace std; |
| 50 | |
| 51 | namespace |
| 52 | { |
| 53 | /** Returns different instances of __mutex depending on the passed address |
| 54 | * in order to limit contention without breaking current library binary |
| 55 | * compatibility. */ |
| 56 | __gnu_cxx::__mutex& |
| 57 | get_safe_base_mutex(void* address) |
| 58 | { |
| 59 | // Use arbitrarily __gnu_debug::vector<int> as the container giving |
| 60 | // alignment of debug containers. |
| 61 | const auto alignbits = __builtin_ctz(alignof(__gnu_debug::vector<int>)); |
| 62 | const unsigned char index |
| 63 | = (reinterpret_cast<std::size_t>(address) >> alignbits) |
| 64 | & __gnu_internal::mask; |
| 65 | return __gnu_internal::get_mutex(index); |
| 66 | } |
| 67 | |
| 68 | #pragma GCC diagnostic push |
| 69 | // Suppress -Wabi=2 warnings due to PR c++/51322 mangling change |
| 70 | #pragma GCC diagnostic warning "-Wabi=6" |
| 71 | |
| 72 | void |
| 73 | swap_its(__gnu_debug::_Safe_sequence_base& __lhs, |
| 74 | __gnu_debug::_Safe_iterator_base*& __lhs_its, |
| 75 | __gnu_debug::_Safe_sequence_base& __rhs, |
| 76 | __gnu_debug::_Safe_iterator_base*& __rhs_its) |
| 77 | { |
| 78 | swap(__lhs_its, __rhs_its); |
| 79 | __gnu_debug::_Safe_iterator_base* __iter; |
| 80 | for (__iter = __rhs_its; __iter; __iter = __iter->_M_next) |
| 81 | __iter->_M_sequence = &__rhs; |
| 82 | for (__iter = __lhs_its; __iter; __iter = __iter->_M_next) |
| 83 | __iter->_M_sequence = &__lhs; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | swap_seq_single(__gnu_debug::_Safe_sequence_base& __lhs, |
| 88 | __gnu_debug::_Safe_sequence_base& __rhs) |
| 89 | { |
| 90 | swap(__lhs._M_version, __rhs._M_version); |
| 91 | swap_its(__lhs, __lhs._M_iterators, |
| 92 | __rhs, __rhs._M_iterators); |
| 93 | swap_its(__lhs, __lhs._M_const_iterators, |
| 94 | __rhs, __rhs._M_const_iterators); |
| 95 | } |
| 96 | #pragma GCC diagnostic pop |
| 97 | |
| 98 | template<typename _Action> |
| 99 | void |
| 100 | lock_and_run(__gnu_cxx::__mutex& lhs_mutex, __gnu_cxx::__mutex& rhs_mutex, |
| 101 | _Action action) |
| 102 | { |
| 103 | // We need to lock both sequences to run action. |
| 104 | if (&lhs_mutex == &rhs_mutex) |
| 105 | { |
| 106 | __gnu_cxx::__scoped_lock sentry(lhs_mutex); |
| 107 | action(); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | __gnu_cxx::__scoped_lock sentry1(&lhs_mutex < &rhs_mutex |
| 112 | ? lhs_mutex : rhs_mutex); |
| 113 | __gnu_cxx::__scoped_lock sentry2(&lhs_mutex < &rhs_mutex |
| 114 | ? rhs_mutex : lhs_mutex); |
| 115 | action(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | swap_seq(__gnu_cxx::__mutex& lhs_mutex, |
| 121 | __gnu_debug::_Safe_sequence_base& lhs, |
| 122 | __gnu_cxx::__mutex& rhs_mutex, |
| 123 | __gnu_debug::_Safe_sequence_base& rhs) |
| 124 | { |
| 125 | lock_and_run(lhs_mutex, rhs_mutex, |
| 126 | [&lhs, &rhs]() { swap_seq_single(lhs, rhs); }); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | swap_ucont_single(__gnu_debug::_Safe_unordered_container_base& __lhs, |
| 131 | __gnu_debug::_Safe_unordered_container_base& __rhs) |
| 132 | { |
| 133 | swap_seq_single(__lhs, __rhs); |
| 134 | swap_its(__lhs, __lhs._M_local_iterators, |
| 135 | __rhs, __rhs._M_local_iterators); |
| 136 | swap_its(__lhs, __lhs._M_const_local_iterators, |
| 137 | __rhs, __rhs._M_const_local_iterators); |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | swap_ucont(__gnu_cxx::__mutex& lhs_mutex, |
| 142 | __gnu_debug::_Safe_unordered_container_base& lhs, |
| 143 | __gnu_cxx::__mutex& rhs_mutex, |
| 144 | __gnu_debug::_Safe_unordered_container_base& rhs) |
| 145 | { |
| 146 | lock_and_run(lhs_mutex, rhs_mutex, |
| 147 | [&lhs, &rhs]() { swap_ucont_single(lhs, rhs); }); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | detach_all(__gnu_debug::_Safe_iterator_base* __iter) |
| 152 | { |
| 153 | for (; __iter;) |
| 154 | { |
| 155 | __gnu_debug::_Safe_iterator_base* __old = __iter; |
| 156 | __iter = __iter->_M_next; |
| 157 | __old->_M_reset(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void* |
| 162 | acquire_sequence_ptr_for_lock(__gnu_debug::_Safe_sequence_base*& seq) |
| 163 | { |
| 164 | #ifdef __GTHREADS |
| 165 | if (!__gnu_cxx::__is_single_threaded()) |
| 166 | return __atomic_load_n(&seq, __ATOMIC_ACQUIRE); |
| 167 | #endif |
| 168 | return seq; |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | reset_sequence_ptr(__gnu_debug::_Safe_sequence_base*& seq) |
| 173 | { |
| 174 | #ifdef __GTHREADS |
| 175 | if (!__gnu_cxx::__is_single_threaded()) |
| 176 | { |
| 177 | __atomic_store_n(&seq, (__gnu_debug::_Safe_sequence_base*)nullptr, |
| 178 | __ATOMIC_RELEASE); |
| 179 | return; |
| 180 | } |
| 181 | #endif |
| 182 | seq = nullptr; |
| 183 | } |
| 184 | |
| 185 | } // anonymous namespace |
| 186 | |
| 187 | namespace __gnu_debug |
| 188 | { |
| 189 | const char* const _S_debug_messages[] = |
| 190 | { |
| 191 | // General Checks |
| 192 | // __msg_valid_range |
| 193 | "function requires a valid iterator range [%1.name;, %2.name;)", |
| 194 | // __msg_insert_singular |
| 195 | "attempt to insert into container with a singular iterator", |
| 196 | // __msg_insert_different |
| 197 | "attempt to insert into container with an iterator" |
| 198 | " from a different container", |
| 199 | // __msg_erase_bad |
| 200 | "attempt to erase from container with a %2.state; iterator", |
| 201 | // __msg_erase_different |
| 202 | "attempt to erase from container with an iterator" |
| 203 | " from a different container", |
| 204 | // __msg_subscript_oob |
| 205 | "attempt to subscript container with out-of-bounds index %2;," |
| 206 | " but container only holds %3; elements", |
| 207 | // __msg_empty |
| 208 | "attempt to access an element in an empty container", |
| 209 | // __msg_unpartitioned |
| 210 | "elements in iterator range [%1.name;, %2.name;)" |
| 211 | " are not partitioned by the value %3;", |
| 212 | // __msg_unpartitioned_pred |
| 213 | "elements in iterator range [%1.name;, %2.name;)" |
| 214 | " are not partitioned by the predicate %3; and value %4;", |
| 215 | // __msg_unsorted |
| 216 | "elements in iterator range [%1.name;, %2.name;) are not sorted", |
| 217 | // __msg_unsorted_pred |
| 218 | "elements in iterator range [%1.name;, %2.name;)" |
| 219 | " are not sorted according to the predicate %3;", |
| 220 | // __msg_not_heap |
| 221 | "elements in iterator range [%1.name;, %2.name;) do not form a heap", |
| 222 | // __msg_not_heap_pred |
| 223 | "elements in iterator range [%1.name;, %2.name;)" |
| 224 | " do not form a heap with respect to the predicate %3;", |
| 225 | // std::bitset checks |
| 226 | // __msg_bad_bitset_write |
| 227 | "attempt to write through a singular bitset reference", |
| 228 | // __msg_bad_bitset_read |
| 229 | "attempt to read from a singular bitset reference", |
| 230 | // __msg_bad_bitset_flip |
| 231 | "attempt to flip a singular bitset reference", |
| 232 | // std::list checks |
| 233 | // __msg_self_splice |
| 234 | "attempt to splice a list into itself", |
| 235 | // __msg_splice_alloc |
| 236 | "attempt to splice lists with unequal allocators", |
| 237 | // __msg_splice_bad |
| 238 | "attempt to splice elements referenced by a %1.state; iterator", |
| 239 | // __msg_splice_other |
| 240 | "attempt to splice an iterator from a different container", |
| 241 | // __msg_splice_overlap |
| 242 | "splice destination %1.name;" |
| 243 | " occurs within source range [%2.name;, %3.name;)", |
| 244 | // iterator checks |
| 245 | // __msg_init_singular |
| 246 | "attempt to initialize an iterator that will immediately become singular", |
| 247 | // __msg_init_copy_singular |
| 248 | "attempt to copy-construct an iterator from a singular iterator", |
| 249 | // __msg_init_const_singular |
| 250 | "attempt to construct a constant iterator" |
| 251 | " from a singular mutable iterator", |
| 252 | // __msg_copy_singular |
| 253 | "attempt to copy from a singular iterator", |
| 254 | // __msg_bad_deref |
| 255 | "attempt to dereference a %1.state; iterator", |
| 256 | // __msg_bad_inc |
| 257 | "attempt to increment a %1.state; iterator", |
| 258 | // __msg_bad_dec |
| 259 | "attempt to decrement a %1.state; iterator", |
| 260 | // __msg_iter_subscript_oob |
| 261 | "attempt to subscript a %1.state; iterator %2; step from" |
| 262 | " its current position, which falls outside its dereferenceable range", |
| 263 | // __msg_advance_oob |
| 264 | "attempt to advance a %1.state; iterator %2; steps," |
| 265 | " which falls outside its valid range", |
| 266 | // __msg_retreat_oob |
| 267 | "attempt to retreat a %1.state; iterator %2; steps," |
| 268 | " which falls outside its valid range", |
| 269 | // __msg_iter_compare_bad |
| 270 | "attempt to compare a %1.state; iterator to a %2.state; iterator", |
| 271 | // __msg_compare_different |
| 272 | "attempt to compare iterators from different sequences", |
| 273 | // __msg_iter_order_bad |
| 274 | "attempt to order a %1.state; iterator to a %2.state; iterator", |
| 275 | // __msg_order_different |
| 276 | "attempt to order iterators from different sequences", |
| 277 | // __msg_distance_bad |
| 278 | "attempt to compute the difference between a %1.state;" |
| 279 | " iterator to a %2.state; iterator", |
| 280 | // __msg_distance_different |
| 281 | "attempt to compute the different between two iterators" |
| 282 | " from different sequences", |
| 283 | // istream_iterator |
| 284 | // __msg_deref_istream |
| 285 | "attempt to dereference an end-of-stream istream_iterator", |
| 286 | // __msg_inc_istream |
| 287 | "attempt to increment an end-of-stream istream_iterator", |
| 288 | // ostream_iterator |
| 289 | // __msg_output_ostream |
| 290 | "attempt to output via an ostream_iterator with no associated stream", |
| 291 | // istreambuf_iterator |
| 292 | // __msg_deref_istreambuf |
| 293 | "attempt to dereference an end-of-stream istreambuf_iterator" |
| 294 | " (this is a GNU extension)", |
| 295 | // __msg_inc_istreambuf |
| 296 | "attempt to increment an end-of-stream istreambuf_iterator", |
| 297 | // std::forward_list |
| 298 | // __msg_insert_after_end |
| 299 | "attempt to insert into container after an end iterator", |
| 300 | // __msg_erase_after_bad |
| 301 | "attempt to erase from container after a %2.state; iterator not followed" |
| 302 | " by a dereferenceable one", |
| 303 | // __msg_valid_range2 |
| 304 | "function requires a valid iterator range (%2.name;, %3.name;)" |
| 305 | ", \"%2.name;\" shall be before and not equal to \"%3.name;\"", |
| 306 | // std::unordered_container::local_iterator |
| 307 | // __msg_local_iter_compare_bad |
| 308 | "attempt to compare local iterators from different unordered container" |
| 309 | " buckets", |
| 310 | // __msg_non_empty_range |
| 311 | "function requires a non-empty iterator range [%1.name;, %2.name;)", |
| 312 | // __msg_self_move_assign |
| 313 | "attempt to self move assign", |
| 314 | // __msg_bucket_index_oob |
| 315 | "attempt to access container with out-of-bounds bucket index %2;," |
| 316 | " container only holds %3; buckets", |
| 317 | // __msg_valid_load_factor |
| 318 | "load factor shall be positive", |
| 319 | // __msg_equal_allocs |
| 320 | "allocators must be equal", |
| 321 | // __msg_insert_range_from_self |
| 322 | "attempt to insert with an iterator range [%1.name;, %2.name;) from this" |
| 323 | " container", |
| 324 | // __msg_irreflexive_ordering |
| 325 | "comparison doesn't meet irreflexive requirements, assert(!(a < a))" |
| 326 | }; |
| 327 | |
| 328 | void |
| 329 | _Safe_sequence_base:: |
| 330 | _M_detach_all() |
| 331 | { |
| 332 | __gnu_cxx::__scoped_lock sentry(_M_get_mutex()); |
| 333 | detach_all(_M_iterators); |
| 334 | _M_iterators = 0; |
| 335 | |
| 336 | detach_all(_M_const_iterators); |
| 337 | _M_const_iterators = 0; |
| 338 | } |
| 339 | |
| 340 | void |
| 341 | _Safe_sequence_base:: |
| 342 | _M_detach_singular() |
| 343 | { |
| 344 | __gnu_cxx::__scoped_lock sentry(_M_get_mutex()); |
| 345 | for (_Safe_iterator_base* __iter = _M_iterators; __iter;) |
| 346 | { |
| 347 | _Safe_iterator_base* __old = __iter; |
| 348 | __iter = __iter->_M_next; |
| 349 | if (__old->_M_singular()) |
| 350 | __old->_M_detach_single(); |
| 351 | } |
| 352 | |
| 353 | for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;) |
| 354 | { |
| 355 | _Safe_iterator_base* __old = __iter2; |
| 356 | __iter2 = __iter2->_M_next; |
| 357 | if (__old->_M_singular()) |
| 358 | __old->_M_detach_single(); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void |
| 363 | _Safe_sequence_base:: |
| 364 | _M_revalidate_singular() |
| 365 | { |
| 366 | __gnu_cxx::__scoped_lock sentry(_M_get_mutex()); |
| 367 | for (_Safe_iterator_base* __iter = _M_iterators; __iter; |
| 368 | __iter = __iter->_M_next) |
| 369 | __iter->_M_version = _M_version; |
| 370 | |
| 371 | for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2; |
| 372 | __iter2 = __iter2->_M_next) |
| 373 | __iter2->_M_version = _M_version; |
| 374 | } |
| 375 | |
| 376 | void |
| 377 | _Safe_sequence_base:: |
| 378 | _M_swap(_Safe_sequence_base& __x) noexcept |
| 379 | { swap_seq(_M_get_mutex(), *this, __x._M_get_mutex(), __x); } |
| 380 | |
| 381 | __gnu_cxx::__mutex& |
| 382 | _Safe_sequence_base:: |
| 383 | _M_get_mutex() noexcept |
| 384 | { return get_safe_base_mutex(this); } |
| 385 | |
| 386 | void |
| 387 | _Safe_sequence_base:: |
| 388 | _M_attach(_Safe_iterator_base* __it, bool __constant) |
| 389 | { |
| 390 | __gnu_cxx::__scoped_lock sentry(_M_get_mutex()); |
| 391 | _M_attach_single(__it, __constant); |
| 392 | } |
| 393 | |
| 394 | void |
| 395 | _Safe_sequence_base:: |
| 396 | _M_attach_single(_Safe_iterator_base* __it, bool __constant) noexcept |
| 397 | { |
| 398 | _Safe_iterator_base*& __its = |
| 399 | __constant ? _M_const_iterators : _M_iterators; |
| 400 | __it->_M_next = __its; |
| 401 | if (__it->_M_next) |
| 402 | __it->_M_next->_M_prior = __it; |
| 403 | __its = __it; |
| 404 | } |
| 405 | |
| 406 | void |
| 407 | _Safe_sequence_base:: |
| 408 | _M_detach(_Safe_iterator_base* __it) |
| 409 | { |
| 410 | // Remove __it from this sequence's list |
| 411 | __gnu_cxx::__scoped_lock sentry(_M_get_mutex()); |
| 412 | _M_detach_single(__it); |
| 413 | } |
| 414 | |
| 415 | void |
| 416 | _Safe_sequence_base:: |
| 417 | _M_detach_single(_Safe_iterator_base* __it) noexcept |
| 418 | { |
| 419 | // Remove __it from this sequence's list |
| 420 | __it->_M_unlink(); |
| 421 | if (_M_const_iterators == __it) |
| 422 | _M_const_iterators = __it->_M_next; |
| 423 | if (_M_iterators == __it) |
| 424 | _M_iterators = __it->_M_next; |
| 425 | } |
| 426 | |
| 427 | void |
| 428 | _Safe_iterator_base:: |
| 429 | _M_attach(_Safe_sequence_base* __seq, bool __constant) |
| 430 | { |
| 431 | _M_detach(); |
| 432 | |
| 433 | // Attach to the new sequence (if there is one) |
| 434 | if (__seq) |
| 435 | { |
| 436 | _M_sequence = __seq; |
| 437 | _M_version = _M_sequence->_M_version; |
| 438 | _M_sequence->_M_attach(this, __constant); |
| 439 | } |
| 440 | else |
| 441 | _M_version = 0; |
| 442 | } |
| 443 | |
| 444 | void |
| 445 | _Safe_iterator_base:: |
| 446 | _M_attach_single(_Safe_sequence_base* __seq, bool __constant) noexcept |
| 447 | { |
| 448 | _M_detach_single(); |
| 449 | |
| 450 | // Attach to the new sequence (if there is one) |
| 451 | if (__seq) |
| 452 | { |
| 453 | _M_sequence = __seq; |
| 454 | _M_version = _M_sequence->_M_version; |
| 455 | _M_sequence->_M_attach_single(this, __constant); |
| 456 | } |
| 457 | else |
| 458 | _M_version = 0; |
| 459 | } |
| 460 | |
| 461 | void |
| 462 | _Safe_iterator_base:: |
| 463 | _M_detach() |
| 464 | { |
| 465 | // This function can run concurrently with the sequence destructor, |
| 466 | // so there is a TOCTTOU race here: the sequence could be destroyed |
| 467 | // after we check that _M_sequence is not null. Use the pointer value |
| 468 | // to acquire the mutex (rather than via _M_sequence->_M_get_mutex()). |
| 469 | // If the sequence destructor runs between loading the pointer and |
| 470 | // locking the mutex, it will detach this iterator and set _M_sequence |
| 471 | // to null, and then _M_detach_single() will do nothing. |
| 472 | if (auto seq = acquire_sequence_ptr_for_lock(_M_sequence)) |
| 473 | { |
| 474 | __gnu_cxx::__scoped_lock sentry(get_safe_base_mutex(seq)); |
| 475 | _M_detach_single(); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | void |
| 480 | _Safe_iterator_base:: |
| 481 | _M_detach_single() noexcept |
| 482 | { |
| 483 | if (_M_sequence) |
| 484 | { |
| 485 | _M_sequence->_M_detach_single(this); |
| 486 | _M_reset(); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | void |
| 491 | _Safe_iterator_base:: |
| 492 | _M_reset() noexcept |
| 493 | { |
| 494 | reset_sequence_ptr(_M_sequence); |
| 495 | // Do not reset version, so that a detached iterator does not look like a |
| 496 | // value-initialized one. |
| 497 | // _M_version = 0; |
| 498 | _M_prior = 0; |
| 499 | _M_next = 0; |
| 500 | } |
| 501 | |
| 502 | bool |
| 503 | _Safe_iterator_base:: |
| 504 | _M_singular() const noexcept |
| 505 | { return !_M_sequence || _M_version != _M_sequence->_M_version; } |
| 506 | |
| 507 | bool |
| 508 | _Safe_iterator_base:: |
| 509 | _M_can_compare(const _Safe_iterator_base& __x) const noexcept |
| 510 | { return _M_sequence == __x._M_sequence; } |
| 511 | |
| 512 | __gnu_cxx::__mutex& |
| 513 | _Safe_iterator_base:: |
| 514 | _M_get_mutex() noexcept |
| 515 | { return _M_sequence->_M_get_mutex(); } |
| 516 | |
| 517 | _Safe_unordered_container_base* |
| 518 | _Safe_local_iterator_base:: |
| 519 | _M_get_container() const noexcept |
| 520 | { return static_cast<_Safe_unordered_container_base*>(_M_sequence); } |
| 521 | |
| 522 | void |
| 523 | _Safe_local_iterator_base:: |
| 524 | _M_attach(_Safe_sequence_base* __cont, bool __constant) |
| 525 | { |
| 526 | _M_detach(); |
| 527 | |
| 528 | // Attach to the new container (if there is one) |
| 529 | if (__cont) |
| 530 | { |
| 531 | _M_sequence = __cont; |
| 532 | _M_version = _M_sequence->_M_version; |
| 533 | _M_get_container()->_M_attach_local(this, __constant); |
| 534 | } |
| 535 | else |
| 536 | _M_version = 0; |
| 537 | } |
| 538 | |
| 539 | void |
| 540 | _Safe_local_iterator_base:: |
| 541 | _M_attach_single(_Safe_sequence_base* __cont, bool __constant) noexcept |
| 542 | { |
| 543 | _M_detach_single(); |
| 544 | |
| 545 | // Attach to the new container (if there is one) |
| 546 | if (__cont) |
| 547 | { |
| 548 | _M_sequence = __cont; |
| 549 | _M_version = _M_sequence->_M_version; |
| 550 | _M_get_container()->_M_attach_local_single(this, __constant); |
| 551 | } |
| 552 | else |
| 553 | _M_version = 0; |
| 554 | } |
| 555 | |
| 556 | void |
| 557 | _Safe_local_iterator_base:: |
| 558 | _M_detach() |
| 559 | { |
| 560 | if (auto seq = acquire_sequence_ptr_for_lock(_M_sequence)) |
| 561 | { |
| 562 | __gnu_cxx::__scoped_lock sentry(get_safe_base_mutex(seq)); |
| 563 | _M_detach_single(); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | void |
| 568 | _Safe_local_iterator_base:: |
| 569 | _M_detach_single() noexcept |
| 570 | { |
| 571 | if (_M_sequence) |
| 572 | { |
| 573 | _M_get_container()->_M_detach_local_single(this); |
| 574 | _M_reset(); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | void |
| 579 | _Safe_unordered_container_base:: |
| 580 | _M_detach_all() |
| 581 | { |
| 582 | __gnu_cxx::__scoped_lock sentry(_M_get_mutex()); |
| 583 | detach_all(_M_iterators); |
| 584 | _M_iterators = 0; |
| 585 | |
| 586 | detach_all(_M_const_iterators); |
| 587 | _M_const_iterators = 0; |
| 588 | |
| 589 | detach_all(_M_local_iterators); |
| 590 | _M_local_iterators = 0; |
| 591 | |
| 592 | detach_all(_M_const_local_iterators); |
| 593 | _M_const_local_iterators = 0; |
| 594 | } |
| 595 | |
| 596 | void |
| 597 | _Safe_unordered_container_base:: |
| 598 | _M_swap(_Safe_unordered_container_base& __x) noexcept |
| 599 | { swap_ucont(_M_get_mutex(), *this, __x._M_get_mutex(), __x); } |
| 600 | |
| 601 | void |
| 602 | _Safe_unordered_container_base:: |
| 603 | _M_attach_local(_Safe_iterator_base* __it, bool __constant) |
| 604 | { |
| 605 | __gnu_cxx::__scoped_lock sentry(_M_get_mutex()); |
| 606 | _M_attach_local_single(__it, __constant); |
| 607 | } |
| 608 | |
| 609 | void |
| 610 | _Safe_unordered_container_base:: |
| 611 | _M_attach_local_single(_Safe_iterator_base* __it, bool __constant) noexcept |
| 612 | { |
| 613 | _Safe_iterator_base*& __its = |
| 614 | __constant ? _M_const_local_iterators : _M_local_iterators; |
| 615 | __it->_M_next = __its; |
| 616 | if (__it->_M_next) |
| 617 | __it->_M_next->_M_prior = __it; |
| 618 | __its = __it; |
| 619 | } |
| 620 | |
| 621 | void |
| 622 | _Safe_unordered_container_base:: |
| 623 | _M_detach_local(_Safe_iterator_base* __it) |
| 624 | { |
| 625 | // Remove __it from this container's list |
| 626 | __gnu_cxx::__scoped_lock sentry(_M_get_mutex()); |
| 627 | _M_detach_local_single(__it); |
| 628 | } |
| 629 | |
| 630 | void |
| 631 | _Safe_unordered_container_base:: |
| 632 | _M_detach_local_single(_Safe_iterator_base* __it) noexcept |
| 633 | { |
| 634 | // Remove __it from this container's list |
| 635 | __it->_M_unlink(); |
| 636 | if (_M_const_local_iterators == __it) |
| 637 | _M_const_local_iterators = __it->_M_next; |
| 638 | if (_M_local_iterators == __it) |
| 639 | _M_local_iterators = __it->_M_next; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | namespace |
| 644 | { |
| 645 | using _Error_formatter = __gnu_debug::_Error_formatter; |
| 646 | using _Parameter = __gnu_debug::_Error_formatter::_Parameter; |
| 647 | |
| 648 | void |
| 649 | get_max_length(std::size_t& max_length) |
| 650 | { |
| 651 | const char* nptr = std::getenv("GLIBCXX_DEBUG_MESSAGE_LENGTH"); |
| 652 | if (nptr) |
| 653 | { |
| 654 | char* endptr; |
| 655 | const unsigned long ret = std::strtoul(nptr, &endptr, 0); |
| 656 | if (*nptr != '\0' && *endptr == '\0') |
| 657 | max_length = ret; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | struct PrintContext |
| 662 | { |
| 663 | PrintContext() |
| 664 | : _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false) |
| 665 | { get_max_length(_M_max_length); } |
| 666 | |
| 667 | static constexpr int _S_indent = 4; |
| 668 | std::size_t _M_max_length; |
| 669 | std::size_t _M_column; |
| 670 | bool _M_first_line; |
| 671 | bool _M_wordwrap; |
| 672 | }; |
| 673 | |
| 674 | using _Print_func_t = void (PrintContext&, const char*, ptrdiff_t); |
| 675 | |
| 676 | template<size_t Length> |
| 677 | void |
| 678 | print_literal(PrintContext& ctx, const char(&word)[Length]) |
| 679 | { print_word(ctx, word, Length - 1); } |
| 680 | |
| 681 | void |
| 682 | print_word(PrintContext& ctx, const char* word, ptrdiff_t nbc = -1) |
| 683 | { |
| 684 | size_t length = nbc >= 0 ? nbc : __builtin_strlen(word); |
| 685 | if (length == 0) |
| 686 | return; |
| 687 | |
| 688 | // First consider '\n' at the beginning because it impacts the column. |
| 689 | if (word[0] == '\n') |
| 690 | { |
| 691 | fprintf(stderr, "\n"); |
| 692 | ctx._M_column = 1; |
| 693 | ++word; |
| 694 | --length; |
| 695 | |
| 696 | if (length == 0) |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | size_t visual_length |
| 701 | = isspace((unsigned char)word[length - 1]) ? length - 1 : length; |
| 702 | if (visual_length == 0 |
| 703 | || !ctx._M_wordwrap |
| 704 | || (ctx._M_column + visual_length < ctx._M_max_length) |
| 705 | || (visual_length >= ctx._M_max_length && ctx._M_column == 1)) |
| 706 | { |
| 707 | // If this isn't the first line, indent. |
| 708 | if (ctx._M_column == 1 && !ctx._M_first_line) |
| 709 | ctx._M_column += fprintf(stderr, "%*c", PrintContext::_S_indent, ' '); |
| 710 | |
| 711 | int written = fprintf(stderr, "%.*s", (int)length, word); |
| 712 | |
| 713 | if (word[length - 1] == '\n') |
| 714 | { |
| 715 | ctx._M_first_line = false; |
| 716 | ctx._M_column = 1; |
| 717 | } |
| 718 | else |
| 719 | ctx._M_column += written; |
| 720 | } |
| 721 | else |
| 722 | { |
| 723 | print_literal(ctx, "\n"); |
| 724 | print_word(ctx, word, nbc); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | void |
| 729 | pretty_print(PrintContext& ctx, const char* str, _Print_func_t print_func) |
| 730 | { |
| 731 | const char cxx1998[] = "cxx1998::"; |
| 732 | for (;;) |
| 733 | { |
| 734 | if (auto pos = strstr(str, "__")) |
| 735 | { |
| 736 | if (pos != str) |
| 737 | print_func(ctx, str, pos - str); |
| 738 | |
| 739 | pos += 2; // advance past "__" |
| 740 | if (memcmp(pos, cxx1998, 9) == 0) |
| 741 | pos += 9; // advance past "cxx1998::" |
| 742 | |
| 743 | str = pos; |
| 744 | } |
| 745 | else |
| 746 | { |
| 747 | print_func(ctx, str, -1); |
| 748 | break; |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | template<size_t Length> |
| 754 | void |
| 755 | print_type_info(PrintContext& ctx, |
| 756 | const type_info* info, |
| 757 | const char(&unknown_name)[Length]) |
| 758 | { |
| 759 | if (!info) |
| 760 | print_literal(ctx, unknown_name); |
| 761 | else |
| 762 | { |
| 763 | int status; |
| 764 | char* demangled_name = |
| 765 | __cxxabiv1::__cxa_demangle(info->name(), NULL, NULL, &status); |
| 766 | if (status == 0) |
| 767 | pretty_print(ctx, demangled_name, &print_word); |
| 768 | else |
| 769 | print_word(ctx, info->name()); |
| 770 | free(demangled_name); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | void |
| 775 | print_address(PrintContext& ctx, const char* fmt, const void* address) |
| 776 | { |
| 777 | char buf[128]; |
| 778 | int written = __builtin_sprintf(buf, fmt, address); |
| 779 | print_word(ctx, buf, written); |
| 780 | } |
| 781 | |
| 782 | void |
| 783 | print_address(PrintContext& ctx, const void* address) |
| 784 | { print_address(ctx, "%p", address); } |
| 785 | |
| 786 | void |
| 787 | print_integer(PrintContext& ctx, long integer) |
| 788 | { |
| 789 | char buf[64]; |
| 790 | int written = __builtin_sprintf(buf, "%ld", integer); |
| 791 | print_word(ctx, buf, written); |
| 792 | } |
| 793 | |
| 794 | void |
| 795 | print_named_name(PrintContext& ctx, const _Parameter::_Named& named) |
| 796 | { |
| 797 | assert(named._M_name); |
| 798 | pretty_print(ctx, named._M_name, print_word); |
| 799 | } |
| 800 | |
| 801 | template<typename _Iterator> |
| 802 | void |
| 803 | print_iterator_constness(PrintContext& ctx, const _Iterator& iterator) |
| 804 | { |
| 805 | static const char* |
| 806 | constness_names[_Error_formatter::__last_constness] = |
| 807 | { |
| 808 | "<unknown constness>", |
| 809 | "constant", |
| 810 | "mutable" |
| 811 | }; |
| 812 | print_word(ctx, constness_names[iterator._M_constness]); |
| 813 | } |
| 814 | |
| 815 | template<typename _Iterator> |
| 816 | void |
| 817 | print_iterator_state(PrintContext& ctx, const _Iterator& iterator) |
| 818 | { |
| 819 | static const char* |
| 820 | state_names[_Error_formatter::__last_state] = |
| 821 | { |
| 822 | "<unknown state>", |
| 823 | "singular", |
| 824 | "dereferenceable (start-of-sequence)", |
| 825 | "dereferenceable", |
| 826 | "past-the-end", |
| 827 | "before-begin", |
| 828 | "dereferenceable (start-of-reverse-sequence)", |
| 829 | "dereferenceable (reverse)", |
| 830 | "past-the-reverse-end", |
| 831 | "singular (value-initialized)" |
| 832 | }; |
| 833 | print_word(ctx, state_names[iterator._M_state]); |
| 834 | } |
| 835 | |
| 836 | template<typename _Iterator> |
| 837 | void |
| 838 | print_iterator_seq_type(PrintContext& ctx, const _Iterator& iterator) |
| 839 | { print_type_info(ctx, iterator._M_seq_type, "<unknown seq_type>"); } |
| 840 | |
| 841 | bool |
| 842 | print_named_field(PrintContext& ctx, |
| 843 | const char* fname, const _Parameter::_Named& named) |
| 844 | { |
| 845 | if (__builtin_strcmp(fname, "name") == 0) |
| 846 | print_named_name(ctx, named); |
| 847 | else |
| 848 | return false; |
| 849 | |
| 850 | return true; |
| 851 | } |
| 852 | |
| 853 | bool |
| 854 | print_type_field(PrintContext& ctx, |
| 855 | const char* fname, const _Parameter::_Type& type) |
| 856 | { |
| 857 | if (print_named_field(ctx, fname, type)) |
| 858 | { } |
| 859 | else if (__builtin_strcmp(fname, "type") == 0) |
| 860 | print_type_info(ctx, type._M_type, "<unknown type>"); |
| 861 | else |
| 862 | return false; |
| 863 | |
| 864 | return true; |
| 865 | } |
| 866 | |
| 867 | bool |
| 868 | print_instance_field(PrintContext& ctx, |
| 869 | const char* fname, const _Parameter::_Instance& inst) |
| 870 | { |
| 871 | if (print_type_field(ctx, fname, inst)) |
| 872 | { } |
| 873 | else if (__builtin_strcmp(fname, "address") == 0) |
| 874 | print_address(ctx, inst._M_address); |
| 875 | else |
| 876 | return false; |
| 877 | |
| 878 | return true; |
| 879 | } |
| 880 | |
| 881 | template<typename _Iterator> |
| 882 | bool |
| 883 | print_iterator_field(PrintContext& ctx, |
| 884 | const char* fname, const _Iterator& iterator) |
| 885 | { |
| 886 | if (print_instance_field(ctx, fname, iterator)) |
| 887 | { } |
| 888 | else if (__builtin_strcmp(fname, "constness") == 0) |
| 889 | print_iterator_constness(ctx, iterator); |
| 890 | else if (__builtin_strcmp(fname, "state") == 0) |
| 891 | print_iterator_state(ctx, iterator); |
| 892 | else if (__builtin_strcmp(fname, "sequence") == 0) |
| 893 | { |
| 894 | assert(iterator._M_sequence); |
| 895 | print_address(ctx, iterator._M_sequence); |
| 896 | } |
| 897 | else if (__builtin_strcmp(fname, "seq_type") == 0) |
| 898 | print_iterator_seq_type(ctx, iterator); |
| 899 | else |
| 900 | return false; |
| 901 | |
| 902 | return true; |
| 903 | } |
| 904 | |
| 905 | void |
| 906 | print_field(PrintContext& ctx, const _Parameter& param, const char* fname) |
| 907 | { |
| 908 | assert(param._M_kind != _Parameter::__unused_param); |
| 909 | |
| 910 | const auto& variant = param._M_variant; |
| 911 | switch (param._M_kind) |
| 912 | { |
| 913 | case _Parameter::__iterator: |
| 914 | if (!print_iterator_field(ctx, fname, variant._M_iterator)) |
| 915 | assert(false); |
| 916 | break; |
| 917 | |
| 918 | case _Parameter::__sequence: |
| 919 | if (!print_instance_field(ctx, fname, variant._M_sequence)) |
| 920 | assert(false); |
| 921 | break; |
| 922 | |
| 923 | case _Parameter::__integer: |
| 924 | if (!print_named_field(ctx, fname, variant._M_integer)) |
| 925 | assert(false); |
| 926 | break; |
| 927 | |
| 928 | case _Parameter::__string: |
| 929 | if (!print_named_field(ctx, fname, variant._M_string)) |
| 930 | assert(false); |
| 931 | break; |
| 932 | |
| 933 | case _Parameter::__instance: |
| 934 | if (!print_instance_field(ctx, fname, variant._M_instance)) |
| 935 | assert(false); |
| 936 | break; |
| 937 | |
| 938 | case _Parameter::__iterator_value_type: |
| 939 | if (!print_type_field(ctx, fname, variant._M_iterator_value_type)) |
| 940 | assert(false); |
| 941 | break; |
| 942 | |
| 943 | default: |
| 944 | assert(false); |
| 945 | break; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | void |
| 950 | print_quoted_named_name(PrintContext& ctx, const _Parameter::_Named& named) |
| 951 | { |
| 952 | if (named._M_name) |
| 953 | { |
| 954 | print_literal(ctx, "\""); |
| 955 | print_named_name(ctx, named); |
| 956 | print_literal(ctx, "\" "); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | void |
| 961 | print_type_type(PrintContext& ctx, const _Parameter::_Type& type, |
| 962 | bool close_desc = true) |
| 963 | { |
| 964 | if (type._M_type) |
| 965 | { |
| 966 | print_literal(ctx, " type = "); |
| 967 | print_type_info(ctx, type._M_type, "<unknown type>"); |
| 968 | if (close_desc) |
| 969 | print_literal(ctx, ";\n"); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | void |
| 974 | print_type(PrintContext& ctx, const _Parameter::_Type& type) |
| 975 | { |
| 976 | print_quoted_named_name(ctx, type); |
| 977 | print_literal(ctx, " {\n"); |
| 978 | print_type_type(ctx, type); |
| 979 | print_literal(ctx, "}\n"); |
| 980 | } |
| 981 | |
| 982 | void |
| 983 | print_instance(PrintContext& ctx, const _Parameter::_Instance& inst, |
| 984 | bool close_desc = true) |
| 985 | { |
| 986 | print_quoted_named_name(ctx, inst); |
| 987 | print_address(ctx, "@ %p {\n", inst._M_address); |
| 988 | print_type_type(ctx, inst, close_desc); |
| 989 | |
| 990 | if (close_desc) |
| 991 | print_literal(ctx, "}\n"); |
| 992 | } |
| 993 | |
| 994 | void |
| 995 | print_description(PrintContext& ctx, const _Parameter& param) |
| 996 | { |
| 997 | const auto& variant = param._M_variant; |
| 998 | switch (param._M_kind) |
| 999 | { |
| 1000 | case _Parameter::__iterator: |
| 1001 | { |
| 1002 | const auto& ite = variant._M_iterator; |
| 1003 | |
| 1004 | print_literal(ctx, "iterator "); |
| 1005 | print_instance(ctx, ite, false); |
| 1006 | |
| 1007 | if (ite._M_type) |
| 1008 | { |
| 1009 | if (ite._M_constness != _Error_formatter::__unknown_constness) |
| 1010 | { |
| 1011 | print_literal(ctx, " ("); |
| 1012 | print_iterator_constness(ctx, ite); |
| 1013 | print_literal(ctx, " iterator)"); |
| 1014 | } |
| 1015 | |
| 1016 | print_literal(ctx, ";\n"); |
| 1017 | } |
| 1018 | |
| 1019 | if (ite._M_state != _Error_formatter::__unknown_state) |
| 1020 | { |
| 1021 | print_literal(ctx, " state = "); |
| 1022 | print_iterator_state(ctx, ite); |
| 1023 | print_literal(ctx, ";\n"); |
| 1024 | } |
| 1025 | |
| 1026 | if (ite._M_sequence) |
| 1027 | { |
| 1028 | print_literal(ctx, " references sequence "); |
| 1029 | if (ite._M_seq_type) |
| 1030 | { |
| 1031 | print_literal(ctx, "with type '"); |
| 1032 | print_iterator_seq_type(ctx, ite); |
| 1033 | print_literal(ctx, "' "); |
| 1034 | } |
| 1035 | |
| 1036 | print_address(ctx, "@ %p\n", ite._M_sequence); |
| 1037 | } |
| 1038 | |
| 1039 | print_literal(ctx, "}\n"); |
| 1040 | } |
| 1041 | break; |
| 1042 | |
| 1043 | case _Parameter::__sequence: |
| 1044 | print_literal(ctx, "sequence "); |
| 1045 | print_instance(ctx, variant._M_sequence); |
| 1046 | break; |
| 1047 | |
| 1048 | case _Parameter::__instance: |
| 1049 | print_literal(ctx, "instance "); |
| 1050 | print_instance(ctx, variant._M_instance); |
| 1051 | break; |
| 1052 | |
| 1053 | case _Parameter::__iterator_value_type: |
| 1054 | print_literal(ctx, "iterator::value_type "); |
| 1055 | print_type(ctx, variant._M_iterator_value_type); |
| 1056 | break; |
| 1057 | |
| 1058 | default: |
| 1059 | break; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | void |
| 1064 | print_string(PrintContext& ctx, const char* str, ptrdiff_t nbc, |
| 1065 | const _Parameter* parameters, std::size_t num_parameters) |
| 1066 | { |
| 1067 | const char* start = str; |
| 1068 | const char* end = nbc >= 0 ? start + nbc : nullptr; |
| 1069 | |
| 1070 | while ((end && str != end) || (!end && *str)) |
| 1071 | { |
| 1072 | if (isspace((unsigned char)*str)) |
| 1073 | { |
| 1074 | ++str; |
| 1075 | print_word(ctx, start, str - start); |
| 1076 | start = str; |
| 1077 | continue; |
| 1078 | } |
| 1079 | |
| 1080 | if (!parameters || *str != '%') |
| 1081 | { |
| 1082 | // Normal char or no parameter to look for. |
| 1083 | ++str; |
| 1084 | continue; |
| 1085 | } |
| 1086 | |
| 1087 | if (*++str == '%') |
| 1088 | { |
| 1089 | // Escaped '%' |
| 1090 | print_word(ctx, start, str - start); |
| 1091 | ++str; |
| 1092 | start = str; |
| 1093 | continue; |
| 1094 | } |
| 1095 | |
| 1096 | // We are on a parameter property reference, we need to flush buffer |
| 1097 | // first. |
| 1098 | if (str != start) |
| 1099 | { |
| 1100 | // Avoid printing the '%'. |
| 1101 | if (str - start > 1) |
| 1102 | print_word(ctx, start, str - start - 1); |
| 1103 | start = str; |
| 1104 | } |
| 1105 | |
| 1106 | // Get the parameter number |
| 1107 | assert(*str >= '1' && *str <= '9'); |
| 1108 | size_t param_index = *str - '0' - 1; |
| 1109 | assert(param_index < num_parameters); |
| 1110 | const auto& param = parameters[param_index]; |
| 1111 | |
| 1112 | // '.' separates the parameter number from the field |
| 1113 | // name, if there is one. |
| 1114 | ++str; |
| 1115 | if (*str != '.') |
| 1116 | { |
| 1117 | assert(*str == ';'); |
| 1118 | ++str; |
| 1119 | if (param._M_kind == _Parameter::__integer) |
| 1120 | print_integer(ctx, param._M_variant._M_integer._M_value); |
| 1121 | else if (param._M_kind == _Parameter::__string) |
| 1122 | print_string(ctx, param._M_variant._M_string._M_value, -1, |
| 1123 | parameters, num_parameters); |
| 1124 | start = str; |
| 1125 | continue; |
| 1126 | } |
| 1127 | |
| 1128 | // Extract the field name we want |
| 1129 | const int max_field_len = 16; |
| 1130 | char field[max_field_len]; |
| 1131 | int field_idx = 0; |
| 1132 | ++str; |
| 1133 | while (*str != ';') |
| 1134 | { |
| 1135 | assert(*str); |
| 1136 | assert(field_idx < max_field_len - 1); |
| 1137 | field[field_idx++] = *str++; |
| 1138 | } |
| 1139 | ++str; |
| 1140 | field[field_idx] = '\0'; |
| 1141 | |
| 1142 | print_field(ctx, param, field); |
| 1143 | start = str; |
| 1144 | } |
| 1145 | |
| 1146 | // Might need to flush. |
| 1147 | if (str != start) |
| 1148 | print_word(ctx, start, str - start); |
| 1149 | } |
| 1150 | |
| 1151 | void |
| 1152 | print_string(PrintContext& ctx, const char* str, ptrdiff_t nbc) |
| 1153 | { print_string(ctx, str, nbc, nullptr, 0); } |
| 1154 | |
| 1155 | #if _GLIBCXX_HAVE_STACKTRACE |
| 1156 | void |
| 1157 | print_raw(PrintContext& ctx, const char* str, ptrdiff_t nbc) |
| 1158 | { |
| 1159 | if (nbc == -1) |
| 1160 | nbc = INT_MAX; |
| 1161 | ctx._M_column += fprintf(stderr, "%.*s", (int)nbc, str); |
| 1162 | } |
| 1163 | |
| 1164 | int |
| 1165 | print_backtrace(void* data, __UINTPTR_TYPE__ pc, const char* filename, |
| 1166 | int lineno, const char* function) |
| 1167 | { |
| 1168 | const int bufsize = 64; |
| 1169 | char buf[bufsize]; |
| 1170 | |
| 1171 | PrintContext& ctx = *static_cast<PrintContext*>(data); |
| 1172 | |
| 1173 | int written = __builtin_sprintf(buf, "%p ", (void*)pc); |
| 1174 | print_word(ctx, buf, written); |
| 1175 | |
| 1176 | int ret = 0; |
| 1177 | if (function) |
| 1178 | { |
| 1179 | int status; |
| 1180 | char* demangled_name = |
| 1181 | __cxxabiv1::__cxa_demangle(function, NULL, NULL, &status); |
| 1182 | if (status == 0) |
| 1183 | pretty_print(ctx, demangled_name, &print_raw); |
| 1184 | else |
| 1185 | print_word(ctx, function); |
| 1186 | |
| 1187 | free(demangled_name); |
| 1188 | ret = strstr(function, "main") ? 1 : 0; |
| 1189 | } |
| 1190 | |
| 1191 | print_literal(ctx, "\n"); |
| 1192 | |
| 1193 | if (filename) |
| 1194 | { |
| 1195 | bool wordwrap = false; |
| 1196 | swap(wordwrap, ctx._M_wordwrap); |
| 1197 | print_word(ctx, filename); |
| 1198 | |
| 1199 | if (lineno) |
| 1200 | { |
| 1201 | written = __builtin_sprintf(buf, ":%u\n", lineno); |
| 1202 | print_word(ctx, buf, written); |
| 1203 | } |
| 1204 | else |
| 1205 | print_literal(ctx, "\n"); |
| 1206 | swap(wordwrap, ctx._M_wordwrap); |
| 1207 | } |
| 1208 | else |
| 1209 | print_literal(ctx, "???:0\n"); |
| 1210 | |
| 1211 | return ret; |
| 1212 | } |
| 1213 | |
| 1214 | void |
| 1215 | print_backtrace_error(void* data, const char* msg, int errnum) |
| 1216 | { |
| 1217 | PrintContext& ctx = *static_cast<PrintContext*>(data); |
| 1218 | |
| 1219 | print_literal(ctx, "Backtrace unavailable: "); |
| 1220 | print_word(ctx, msg ? msg : "<unknown error>"); |
| 1221 | if (errnum > 0) |
| 1222 | { |
| 1223 | char buf[64]; |
| 1224 | int written = __builtin_sprintf(buf, " (errno=%d)\n", errnum); |
| 1225 | print_word(ctx, buf, written); |
| 1226 | } |
| 1227 | else |
| 1228 | print_literal(ctx, "\n"); |
| 1229 | } |
| 1230 | #endif |
| 1231 | } |
| 1232 | |
| 1233 | namespace __gnu_debug |
| 1234 | { |
| 1235 | _Error_formatter& |
| 1236 | _Error_formatter::_M_message(_Debug_msg_id __id) const noexcept |
| 1237 | { |
| 1238 | return const_cast<_Error_formatter*>(this) |
| 1239 | ->_M_message(_S_debug_messages[__id]); |
| 1240 | } |
| 1241 | |
| 1242 | void |
| 1243 | _Error_formatter::_M_error() const |
| 1244 | { |
| 1245 | // Emit file & line number information |
| 1246 | bool go_to_next_line = false; |
| 1247 | PrintContext ctx; |
| 1248 | if (_M_file) |
| 1249 | { |
| 1250 | ctx._M_column += fprintf(stderr, "%s", _M_file); |
| 1251 | print_literal(ctx, ":"); |
| 1252 | go_to_next_line = true; |
| 1253 | } |
| 1254 | |
| 1255 | if (_M_line > 0) |
| 1256 | { |
| 1257 | ctx._M_column += fprintf(stderr, "%u", _M_line); |
| 1258 | print_literal(ctx, ":"); |
| 1259 | go_to_next_line = true; |
| 1260 | } |
| 1261 | |
| 1262 | if (go_to_next_line) |
| 1263 | print_literal(ctx, "\n"); |
| 1264 | |
| 1265 | if (ctx._M_max_length) |
| 1266 | ctx._M_wordwrap = true; |
| 1267 | |
| 1268 | if (_M_function) |
| 1269 | { |
| 1270 | print_literal(ctx, "In function:\n"); |
| 1271 | pretty_print(ctx, _M_function, &print_string); |
| 1272 | print_literal(ctx, "\n"); |
| 1273 | ctx._M_first_line = true; |
| 1274 | print_literal(ctx, "\n"); |
| 1275 | } |
| 1276 | |
| 1277 | #if _GLIBCXX_HAVE_STACKTRACE |
| 1278 | if (_M_backtrace_state) |
| 1279 | { |
| 1280 | print_literal(ctx, "Backtrace:\n"); |
| 1281 | _M_backtrace_full( |
| 1282 | _M_backtrace_state, 1, print_backtrace, print_backtrace_error, &ctx); |
| 1283 | ctx._M_first_line = true; |
| 1284 | print_literal(ctx, "\n"); |
| 1285 | } |
| 1286 | #endif |
| 1287 | |
| 1288 | print_literal(ctx, "Error: "); |
| 1289 | |
| 1290 | // Print the error message |
| 1291 | assert(_M_text); |
| 1292 | print_string(ctx, _M_text, -1, _M_parameters, _M_num_parameters); |
| 1293 | print_literal(ctx, ".\n"); |
| 1294 | |
| 1295 | // Emit descriptions of the objects involved in the operation |
| 1296 | ctx._M_first_line = true; |
| 1297 | ctx._M_wordwrap = false; |
| 1298 | bool has_header = false; |
| 1299 | for (unsigned int i = 0; i < _M_num_parameters; ++i) |
| 1300 | { |
| 1301 | switch (_M_parameters[i]._M_kind) |
| 1302 | { |
| 1303 | case _Parameter::__iterator: |
| 1304 | case _Parameter::__sequence: |
| 1305 | case _Parameter::__instance: |
| 1306 | case _Parameter::__iterator_value_type: |
| 1307 | if (!has_header) |
| 1308 | { |
| 1309 | print_literal(ctx, "\nObjects involved in the operation:\n"); |
| 1310 | has_header = true; |
| 1311 | } |
| 1312 | print_description(ctx, _M_parameters[i]); |
| 1313 | break; |
| 1314 | |
| 1315 | default: |
| 1316 | break; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | abort(); |
| 1321 | } |
| 1322 | |
| 1323 | #if !_GLIBCXX_INLINE_VERSION |
| 1324 | // Deprecated methods kept for backward compatibility. |
| 1325 | void |
| 1326 | _Error_formatter::_Parameter::_M_print_field( |
| 1327 | const _Error_formatter*, const char*) const |
| 1328 | { } |
| 1329 | |
| 1330 | void |
| 1331 | _Error_formatter::_Parameter::_M_print_description(const _Error_formatter*) const |
| 1332 | { } |
| 1333 | |
| 1334 | template<typename _Tp> |
| 1335 | void |
| 1336 | _Error_formatter::_M_format_word(char*, int, const char*, _Tp) |
| 1337 | const noexcept |
| 1338 | { } |
| 1339 | |
| 1340 | void |
| 1341 | _Error_formatter::_M_print_word(const char*) const |
| 1342 | { } |
| 1343 | |
| 1344 | void |
| 1345 | _Error_formatter::_M_print_string(const char*) const |
| 1346 | { } |
| 1347 | |
| 1348 | void |
| 1349 | _Error_formatter::_M_get_max_length() const noexcept |
| 1350 | { } |
| 1351 | |
| 1352 | // Instantiations. |
| 1353 | template |
| 1354 | void |
| 1355 | _Error_formatter::_M_format_word(char*, int, const char*, |
| 1356 | const void*) const; |
| 1357 | |
| 1358 | template |
| 1359 | void |
| 1360 | _Error_formatter::_M_format_word(char*, int, const char*, long) const; |
| 1361 | |
| 1362 | template |
| 1363 | void |
| 1364 | _Error_formatter::_M_format_word(char*, int, const char*, |
| 1365 | std::size_t) const; |
| 1366 | |
| 1367 | template |
| 1368 | void |
| 1369 | _Error_formatter::_M_format_word(char*, int, const char*, |
| 1370 | const char*) const; |
| 1371 | #endif |
| 1372 | |
| 1373 | } // namespace __gnu_debug |