libcwdversion 2.0.0
The C++ Debugging Support Library
Loading...
Searching...
No Matches
Location.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2000-2005, 2018-2021, 2023, 2026 Carlo Wood
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
9
10#ifndef LIBCWD_CLASS_LOCATION_H
11#define LIBCWD_CLASS_LOCATION_H
12
13#include "libcwd/config.h"
14
15#if CWDEBUG_LOCATION
16
17#include "ObjectFileName.h"
18#include "lockable_auto_ptr.h"
20
21#include <cstring>
22#include <iosfwd>
23#include <string>
24
25namespace libcwd {
26
27// Forward declaration.
28class Location;
29
30namespace _private_ {
31
32// Forward declaration.
33template <class OSTREAM>
34void print_location_on(OSTREAM& os, Location const& location);
35
36} // namespace _private_
37} // namespace libcwd
38
39namespace libcwd {
40
43
45extern char const* const unknown_function_c;
46
58{
59 protected:
62 char const* function_name_;
63 lockable_auto_ptr<char, true>
65 union
66 {
67 char const* filename_;
71 void const* unknown_pc_;
73 };
74 unsigned int line_;
75 bool known_;
76 private:
77 protected:
78 // function_name_ can point to one of these constants, or to libcwd::unknown_function_c
79 // or to a static string with the mangled function name.
80 static char const uninitialized_location[];
81 static char const pre_libcwd_initialization[];
82 static char const cleared_location[];
83
84 public:
85 explicit Location(void const* addr);
86 // Construct a location object for address `addr'.
87 explicit Location(void const* addr, LIBCWD_TSD_PARAM);
88 // Idem, but with passing the TSD.
89 ~Location();
90
97 Location();
98
107 Location(Location const& location);
108
117 Location& operator=(Location const& location); // Assignment operator
118
127 {
128 if (known_)
129 filepath_.lock();
130 }
131
135 void pc_location(void const* pc);
136
137 // Only public because libcwd calls it directly.
138 void pc_location(void const* pc, LIBCWD_TSD_PARAM);
139
143 void clear();
144
145 public:
146 // Accessors
151 bool is_known() const;
152
159 std::string file() const;
160
162 unsigned int line() const;
163
170 char const* mangled_function_name() const;
171
173 size_t filename_length() const { return known_ ? std::strlen(filename_) : 0; }
175 size_t filepath_length() const { return known_ ? std::strlen(filepath_.get()) : 0; }
176
183
184 // Printing
186 void print_filepath_on(std::ostream& os) const;
188 void print_filename_on(std::ostream& os) const;
189 template <class OSTREAM>
190 friend void _private_::print_location_on(OSTREAM& os, Location const& location);
191#if (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
192 // This doesn't need to be a friend, but g++ 3.3.x and lower are broken.
193 // We need to declare an operator<< this way as a workaround.
194 friend std::ostream& operator<<(std::ostream& os, Location const& location)
195 {
196 _private_::print_location_on(os, location);
197 return os;
198 }
199#endif
200
201 // Return the program counter that still needs lazy symbol resolution, if any.
202 bool initialization_delayed() const { return (!object_file_name_ && function_name_ == pre_libcwd_initialization); }
203 void const* unknown_pc() const
204 {
206 : initialization_delayed() ? initialization_delayed_
207 : 0;
208 }
209};
210
211// #if (__GNUC__ > 3 || __GNUC_MINOR__ >= 4)
212// extern std::ostream& operator<<(std::ostream& os, Location const& location);
213// #endif
214
229 // End of group 'source-file-line-number-information'
231
232} // namespace libcwd
233
234#endif // CWDEBUG_LOCATION
235#endif // LIBCWD_CLASS_LOCATION_H
A source file location.
Definition Location.h:58
unsigned int line_
The line number of this location.
Definition Location.h:74
void print_filepath_on(std::ostream &os) const
Write the full path to an ostream.
Definition location.cxx:213
size_t filepath_length() const
The size of the full path name.
Definition Location.h:175
ObjectFileName const * object_file_name_
Definition Location.h:60
void print_filename_on(std::ostream &os) const
Write the file name to an ostream.
Definition location.cxx:219
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
lockable_auto_ptr< char, true > filepath_
The full source file name of this location. Allocated in `pc_location' using new [].
Definition Location.h:64
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
void const * initialization_delayed_
Definition Location.h:68
bool known_
Set when filepath_ (and filename_) point to valid data and line_ contains a valid line number.
Definition Location.h:75
ObjectFileName const * object_file() const
Corresponding object file.
Definition Location.h:182
Location & operator=(Location const &location)
Assignment operator.
Definition location.cxx:194
void const * unknown_pc_
Definition Location.h:71
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
size_t filename_length() const
The size of the file name.
Definition Location.h:173
Location(void const *addr)
Construct a location for address addr.
Definition Location.inl.h:25
char const * function_name_
Pointer to static string containing the mangled function name of this location.
Definition Location.h:62
void lock_ownership()
Keep ownership of the stored path in this object.
Definition Location.h:126
An object representing the main executable or a shared library.
Definition ObjectFileName.h:38
char const *const unknown_function_c
This constant (pointer) is returned by Location::mangled_function_name() when no function is known.
Definition dwarf.cxx:1446
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
namespace for libcwd.