libcwdversion 2.0.0
The C++ Debugging Support Library
Loading...
Searching...
No Matches
Location.inl.h
1// SPDX-FileCopyrightText: 2001-2004, 2018, 2020, 2026 Carlo Wood
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#ifndef LIBCWD_CLASS_LOCATION_INL
7#define LIBCWD_CLASS_LOCATION_INL
8
9#include "libcwd/config.h"
10
11#if CWDEBUG_LOCATION
12
13#include "Location.h"
14#include "libraries_debug.h"
15#include "LIBCWD_ASSERT.h"
16
17#include <iomanip>
18#include <string>
19
20namespace libcwd {
21
25inline Location::Location(void const* addr) : known_(false)
26{
27 LIBCWD_TSD_DECLARATION;
28 pc_location(addr, LIBCWD_TSD);
29}
30
31/*
32 * Construct a location for address addr,
33 * taking a thread-specific-data argument.
34 */
35inline Location::Location(void const* addr, LIBCWD_TSD_PARAM) : known_(false)
36{
37 pc_location(addr, LIBCWD_TSD);
38}
39
44{
45 clear();
46}
47
48inline Location::Location() : object_file_name_(NULL), function_name_(uninitialized_location), known_(false)
49{
50}
51
55inline void Location::pc_location(void const* addr)
56{
57 clear();
58 LIBCWD_TSD_DECLARATION;
59 pc_location(addr, LIBCWD_TSD);
60}
61
62inline bool Location::is_known() const
63{
64 return known_;
65}
66
67inline std::string Location::file() const
68{
69 LIBCWD_ASSERT(known_);
70 return filename_;
71}
72
73inline unsigned int Location::line() const
74{
75 LIBCWD_ASSERT(known_);
76 return line_;
77}
78
79inline char const* Location::mangled_function_name() const
80{
81 return function_name_;
82}
83
85{
86 LIBCWD_TSD_DECLARATION;
87 location_format_t ret = __libcwd_tsd.format;
88 __libcwd_tsd.format = format;
89 return ret;
90}
91
92namespace _private_ {
93
94template <class OSTREAM>
95void print_location_on(OSTREAM& os, Location const& location)
96{
97 if (location.known_)
98 {
99 LIBCWD_TSD_DECLARATION;
100 if ((__libcwd_tsd.format & show_objectfile))
101 os << location.object_file_name_->filename() << ':';
102 if ((__libcwd_tsd.format & show_function))
103 os << location.function_name_ << ':';
104 if ((__libcwd_tsd.format & show_path))
105 os << location.filepath_.get() << ':' << std::dec << location.line_;
106 else
107 os << location.filename_ << ':' << std::dec << location.line_;
108 }
109 else if (location.object_file_name_)
110 os << location.object_file_name_->filename() << ':' << location.function_name_;
111 else
112 os << "<unknown object file> (at " << location.unknown_pc() << ')';
113}
114
115} // namespace _private_
116
117#if !(__GNUC__ == 3 && __GNUC_MINOR__ < 4) // See Location.h
128inline std::ostream& operator<<(std::ostream& os, Location const& location)
129{
130 _private_::print_location_on(os, location);
131 return os;
132}
133#endif
134
135} // namespace libcwd
136
137#endif // CWDEBUG_LOCATION
138#endif // LIBCWD_CLASS_LOCATION_INL
A source file location.
Definition Location.h:58
unsigned int line_
The line number of this location.
Definition Location.h:74
ObjectFileName const * object_file_name_
Definition Location.h:60
void clear()
Clear the current object (set the location to 'unknown').
Definition location.cxx:168
void pc_location(void const *pc)
Initialize the current object with the location that corresponds with pc.
Definition Location.inl.h:55
unsigned int line() const
Return the line number; only valid if is_known() returns true.
Definition Location.inl.h:73
std::string file() const
The source file name (without path).
Definition Location.inl.h:67
bool is_known() const
Returns false if no source-file:line-number information is known for this location (or when it is uni...
Definition Location.inl.h:62
char const * mangled_function_name() const
Returns the mangled function name or unknown_function_c when no function could be found.
Definition Location.inl.h:79
bool known_
Set when filepath_ (and filename_) point to valid data and line_ contains a valid line number.
Definition Location.h:75
Location()
The default constructor.
Definition Location.inl.h:48
~Location()
Destructor.
Definition Location.inl.h:43
char const * filename_
Points inside filepath_ just after the last '/' or to the beginning.
Definition Location.h:67
char const * function_name_
Pointer to static string containing the mangled function name of this location.
Definition Location.h:62
location_format_t const show_path
Show the full source path when printing a location.
Definition ThreadSpecificData.h:60
std::ostream & operator<<(std::ostream &os, Location const &location)
Write location to ostream os.
Definition Location.inl.h:128
location_format_t location_format(location_format_t format)
Set the output format of Location.
Definition Location.inl.h:84
unsigned short int location_format_t
The type of the argument of location_format.
Definition ThreadSpecificData.h:58
location_format_t const show_objectfile
Show the shared library or executable that owns the location.
Definition ThreadSpecificData.h:61
location_format_t const show_function
Show the mangled function name for the location.
Definition ThreadSpecificData.h:62
This is the header file that third-party library headers should include.
namespace for libcwd.