OpenTTD Source  1.11.2
dbg_helpers.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../rail_map.h"
12 #include "dbg_helpers.h"
13 
14 #include "../safeguards.h"
15 
17 static const char * const trackdir_names[] = {
18  "NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse",
19  "SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw",
20 };
21 
24 {
25  CStrA out;
26  out.Format("%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
27  return out.Transfer();
28 }
29 
32 {
33  CStrA out;
34  out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data());
35  return out.Transfer();
36 }
37 
38 
40 static const char * const diagdir_names[] = {
41  "NE", "SE", "SW", "NW",
42 };
43 
46 {
47  CStrA out;
48  out.Format("%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
49  return out.Transfer();
50 }
51 
52 
54 static const char * const signal_type_names[] = {
55  "NORMAL", "ENTRY", "EXIT", "COMBO", "PBS", "NOENTRY",
56 };
57 
60 {
61  CStrA out;
62  out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK"));
63  return out.Transfer();
64 }
65 
66 
69 {
70  CStrA out;
71  out.Format("0x%04X (%d, %d)", tile, TileX(tile), TileY(tile));
72  return out.Transfer();
73 }
74  size_t& DumpTarget::LastTypeId()
78 {
79  static size_t last_type_id = 0;
80  return last_type_id;
81 }
82 
85 {
86  CStrA out;
87  if (!m_cur_struct.empty()) {
88  /* we are inside some named struct, return its name */
89  out = m_cur_struct.top();
90  }
91  return out.Transfer();
92 }
93 
98 bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name)
99 {
100  KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
101  if (it != m_known_names.end()) {
102  /* we have found it */
103  name = (*it).second;
104  return true;
105  }
106  return false;
107 }
108 
111 {
112  int num_spaces = 2 * m_indent;
113  if (num_spaces > 0) {
114  memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces);
115  }
116 }
117 
119 void DumpTarget::WriteLine(const char *format, ...)
120 {
121  WriteIndent();
122  va_list args;
123  va_start(args, format);
124  m_out.AddFormatL(format, args);
125  va_end(args);
126  m_out.AppendStr("\n");
127 }
128 
130 void DumpTarget::WriteValue(const char *name, const char *value_str)
131 {
132  WriteIndent();
133  m_out.AddFormat("%s = %s\n", name, value_str);
134 }
135 
137 void DumpTarget::WriteTile(const char *name, TileIndex tile)
138 {
139  WriteIndent();
140  m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data());
141 }
142 
146 void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
147 {
148  /* make composite name */
149  CStrA cur_name = GetCurrentStructName().Transfer();
150  if (cur_name.Size() > 0) {
151  /* add name delimiter (we use structured names) */
152  cur_name.AppendStr(".");
153  }
154  cur_name.AppendStr(name);
155 
156  /* put the name onto stack (as current struct name) */
157  m_cur_struct.push(cur_name);
158 
159  /* put it also to the map of known structures */
160  m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
161 
162  WriteIndent();
163  m_out.AddFormat("%s = {\n", name);
164  m_indent++;
165 }
166 
171 {
172  m_indent--;
173  WriteIndent();
174  m_out.AddFormat("}\n");
175 
176  /* remove current struct name from the stack */
177  m_cur_struct.pop();
178 }
179 
181 /* static */ ByteBlob::BlobHeader ByteBlob::hdrEmpty[] = {{0, 0}, {0, 0}};
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
DumpTarget::LastTypeId
static size_t & LastTypeId()
Keep track of the last assigned type_id.
Definition: dbg_helpers.cpp:77
DumpTarget::WriteIndent
void WriteIndent()
Write some leading spaces into the output.
Definition: dbg_helpers.cpp:110
TrackdirBits
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
Definition: track_type.h:101
dbg_helpers.h
CStrA
Blob based case sensitive ANSI/UTF-8 string.
Definition: str.hpp:20
DumpTarget::EndStruct
void EndStruct()
Close structure '}<LF>'.
Definition: dbg_helpers.cpp:170
ItemAtT
ArrayT< T >::item_t ItemAtT(E idx, const T &t, typename ArrayT< T >::item_t t_unk)
Helper template function that returns item of array at given index or t_unk when index is out of boun...
Definition: dbg_helpers.h:38
trackdir_names
static const char *const trackdir_names[]
Trackdir & TrackdirBits short names.
Definition: dbg_helpers.cpp:17
DumpTarget::WriteLine
void CDECL WriteLine(const char *format,...) WARN_FORMAT(2
Write a line with indent at the beginning and <LF> at the end.
Definition: dbg_helpers.cpp:119
ByteBlob::hdrEmpty
static BlobHeader hdrEmpty[]
Just to silence an unsilencable GCC 4.4+ warning Note: This cannot be 'const' as we do a lot of 'hdrE...
Definition: blob.hpp:65
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
CStrA::GrowSizeNC
char * GrowSizeNC(uint count)
Grow the actual buffer and fix the trailing zero at the end.
Definition: str.hpp:42
DumpTarget::m_known_names
KNOWN_NAMES m_known_names
map of known object instances and their structured names
Definition: dbg_helpers.h:126
DumpTarget::WriteTile
void WriteTile(const char *name, TileIndex t)
Write name & TileIndex to the output.
Definition: dbg_helpers.cpp:137
DumpTarget::KnownStructKey
Used as a key into map of known object instances.
Definition: dbg_helpers.h:97
INVALID_TRACKDIR_BIT
@ INVALID_TRACKDIR_BIT
Flag for an invalid trackdirbit value.
Definition: track_type.h:117
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
SignalType
SignalType
Type of signal, i.e.
Definition: signal_type.h:23
ComposeNameT
CStrA ComposeNameT(E value, T &t, const char *t_unk, E val_inv, const char *name_inv)
Helper template function that returns compound bitfield name that is concatenation of names of each s...
Definition: dbg_helpers.h:70
DumpTarget::m_indent
int m_indent
current indent/nesting level
Definition: dbg_helpers.h:124
CBlobT::Size
size_t Size() const
Return number of items in the Blob.
Definition: blob.hpp:382
DumpTarget::FindKnownName
bool FindKnownName(size_t type_id, const void *ptr, CStrA &name)
Find the given instance in our anti-recursion repository.
Definition: dbg_helpers.cpp:98
CStrA::AppendStr
void AppendStr(const char *str)
Append zero-ended C string.
Definition: str.hpp:50
INVALID_DIAGDIR
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Definition: direction_type.h:84
INVALID_TRACKDIR
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition: track_type.h:89
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:77
CStrA::AddFormatL
int AddFormatL(const char *format, va_list args) WARN_FORMAT(2
Add formatted string (like vsprintf) at the end of existing contents.
DumpTarget::m_cur_struct
std::stack< CStrA > m_cur_struct
here we will track the current structure name
Definition: dbg_helpers.h:125
DumpTarget::WriteValue
void CDECL void WriteValue(const char *name, const char *value_str)
Write 'name = value' with indent and new-line.
Definition: dbg_helpers.cpp:130
TileStr
CStrA TileStr(TileIndex tile)
Translate TileIndex into string.
Definition: dbg_helpers.cpp:68
diagdir_names
static const char *const diagdir_names[]
DiagDirection short names.
Definition: dbg_helpers.cpp:40
DumpTarget::GetCurrentStructName
CStrA GetCurrentStructName()
Return structured name of the current class/structure.
Definition: dbg_helpers.cpp:84
signal_type_names
static const char *const signal_type_names[]
SignalType short names.
Definition: dbg_helpers.cpp:54
DumpTarget::BeginStruct
void BeginStruct(size_t type_id, const char *name, const void *ptr)
Open new structure (one level deeper than the current one) 'name = {<LF>'.
Definition: dbg_helpers.cpp:146
DumpTarget::m_out
CStrA m_out
the output string
Definition: dbg_helpers.h:123
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:70
ValueStr
CStrA ValueStr(Trackdir td)
Return name of given Trackdir.
Definition: dbg_helpers.cpp:23
ByteBlob::BlobHeader
header of the allocated memory block
Definition: blob.hpp:48