Skip to content

Commit aedaca7

Browse files
author
Erik McClure
committed
Clean up struct visualization
1 parent 81318e6 commit aedaca7

5 files changed

Lines changed: 141 additions & 57 deletions

File tree

innative/debug_pdb.cpp

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
#include "util.h"
88
#include <algorithm>
99

10+
namespace innative {
11+
namespace code {
12+
#ifdef IN_64BIT
13+
__KHASH_IMPL(intset, , size_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal);
14+
#else
15+
__KHASH_IMPL(intset, , size_t, char, 0, kh_int_hash_func, kh_int_hash_equal);
16+
#endif
17+
}
18+
}
19+
1020
using namespace innative;
1121
using namespace utility;
1222
using namespace code;
@@ -15,9 +25,10 @@ using namespace llvm::dwarf;
1525
#define TEXT(x) x
1626
#define XML(x) TEXT(#x)
1727

18-
code::DebugPDB::DebugPDB(SourceMap* s, Context* context, llvm::Module& m, const char* name, const char* filepath) :
19-
DebugSourceMap(s, context, m, name, filepath), _uid(0)
28+
DebugPDB::DebugPDB(SourceMap* s, Context* context, llvm::Module& m, const char* name, const char* filepath) :
29+
DebugSourceMap(s, context, m, name, filepath), _uid(0), _deferred(kh_init_intset())
2030
{}
31+
DebugPDB::~DebugPDB() { kh_destroy_intset(_deferred); }
2132

2233
void DebugPDB::FuncDecl(llvm::Function* fn, unsigned int offset, unsigned int line, bool optimized)
2334
{
@@ -201,19 +212,26 @@ llvm::DIType* DebugPDB::GetDebugType(size_t index, llvm::DIType* parent)
201212

202213
if(index < types.size() && !types[index])
203214
{
204-
const char* name = type.name_index < sourcemap->n_names ? sourcemap->names[type.name_index] : "";
215+
const char* name = type.name_index < sourcemap->n_names ? sourcemap->names[type.name_index] : 0;
205216
llvm::DIFile* file = GetSourceFile(type.source_index);
206217

207218
switch(type.tag)
208219
{
209220
case DW_TAG_pointer_type:
210221
if(auto ty = GetDebugType(type.type_index))
211222
{
223+
if(!name)
224+
name = ty->getName().str().c_str();
225+
auto pname = std::string("p<") + name;
226+
if(pname.back() == '*') // strip one pointer indirection layer off
227+
pname.pop_back();
228+
212229
auto member =
213230
_dbuilder->createMemberType(dcu, name, file, 0, ty->getSizeInBits(), 1, 0, llvm::DINode::FlagZero, ty);
214-
types[index] = _dbuilder->createStructType(dcu, std::string("p<") + name + ">", file, 0, ty->getSizeInBits(), 1,
231+
types[index] = _dbuilder->createStructType(dcu, pname + ">", file, 0, ty->getSizeInBits(), 1,
215232
llvm::DINode::FlagZero, 0, _dbuilder->getOrCreateArray({ member }));
216-
_deferred.push_back(index);
233+
int r;
234+
kh_put_intset(_deferred, index, &r);
217235
}
218236
break;
219237
case DW_TAG_rvalue_reference_type:
@@ -242,37 +260,63 @@ void DebugPDB::Finalize()
242260
llvm::SmallVector<llvm::DIGlobalVariableExpression*, 1> expr;
243261
_context->memories[0]->getDebugInfo(expr);
244262

245-
for(auto index : _deferred)
263+
for(auto iter = kh_begin(_deferred); iter < kh_end(_deferred); ++iter)
246264
{
265+
if(!kh_exist(_deferred, iter))
266+
continue;
267+
auto index = kh_key(_deferred, iter);
247268
auto& type = sourcemap->x_innative_types[index];
248269
if(auto ty = GetDebugType(type.type_index))
249270
{
250-
auto test = ty->getName();
251-
_context->natvis += FormatString(
252-
true,
253-
"<Type Name = \"{0}\"><DisplayString>{0}:{(unsigned int)this} | {({1}.m0+(unsigned int)this)}</DisplayString><Expand>",
254-
types[index]->getName(), expr[0]->getVariable()->getName());
271+
std::string aux;
272+
_context->natvis +=
273+
FormatString(true,
274+
"<Type Name = \"{0}\"><DisplayString>{*({2}*)({1}.m0+(unsigned int)this)}</DisplayString><Expand>",
275+
types[index]->getName(), expr[0]->getVariable()->getName(), ty->getName());
255276

256277
if(auto composite = llvm::dyn_cast<llvm::DICompositeType>(ty))
257278
{
279+
std::string expansion = "<Expand>";
280+
aux += FormatString(true, "<Type Name = \"{2}\"><DisplayString>{{", types[index]->getName(),
281+
expr[0]->getVariable()->getName(), ty->getName());
258282
for(const auto& e : composite->getElements())
259283
if(auto element = llvm::dyn_cast<llvm::DIDerivedType>(e))
260284
{
261-
_context->natvis += FormatString(true, "<Item Name=\"{0}\">*({1}*)({2}.m0+(unsigned int)this+{3})</Item>",
262-
element->getName(), element->getBaseType()->getName(),
263-
expr[0]->getVariable()->getName(), element->getOffsetInBits() / 8);
264-
auto test = FormatString(true, "<Item Name=\"{0}\">*({1}*)({2}.m0+(unsigned int)this+{3})</Item>",
265-
element->getName(), element->getBaseType()->getName(),
266-
expr[0]->getVariable()->getName(), element->getOffsetInBits() / 8);
285+
if(element->getBaseType()->getName().startswith("p<"))
286+
{
287+
if(auto basetype = llvm::dyn_cast<llvm::DICompositeType>(element->getBaseType());
288+
basetype->getElements().size() > 0)
289+
if(auto baseelement = llvm::dyn_cast<llvm::DIType>(basetype->getElements()[0]); !baseelement->getName().empty())
290+
aux += FormatString(true, "{0}:{({1})({2}.m0 + *(unsigned int*)(&amp;this->{0}))}}", element->getName(),
291+
baseelement->getName(), expr[0]->getVariable()->getName(),
292+
element->getOffsetInBits() / 8);
293+
294+
_context->natvis +=
295+
FormatString(true, "<Item Name=\"{0}\">({1}*)(*(unsigned int*)({2}.m0+(unsigned int)this+{3}))</Item>",
296+
element->getName(), element->getBaseType()->getName(), expr[0]->getVariable()->getName(),
297+
element->getOffsetInBits() / 8);
298+
}
299+
else
300+
{
301+
aux += FormatString(true, "{0}:{this->{0}} ", element->getName());
302+
_context->natvis += FormatString(true, "<Item Name=\"{0}\">*({1}*)({2}.m0+(unsigned int)this+{3})</Item>",
303+
element->getName(), element->getBaseType()->getName(),
304+
expr[0]->getVariable()->getName(), element->getOffsetInBits() / 8);
305+
}
267306
}
268307

269-
// <Item Name="c">(p&lt;Complex&gt;*)(*(unsigned int*)(benchmark_debug::linearmemory_0.m0+(unsigned
270-
// int)this+8))</Item>
308+
if(aux.size()) // strip last space
309+
aux.pop_back();
310+
311+
aux += "}}</DisplayString>";
312+
aux += expansion;
313+
aux += "</Expand></Type>";
271314
}
272315
else
273-
_context->natvis += FormatString(true, "<ExpandedItem>*({0}*)({1}.m0+(unsigned int)this)</Item>", ty->getName(),
274-
expr[0]->getVariable()->getName());
316+
_context->natvis += FormatString(true, "<ExpandedItem>*({0}*)({1}.m0+(unsigned int)this)</ExpandedItem>",
317+
ty->getName(), expr[0]->getVariable()->getName());
275318
_context->natvis += "</Expand></Type>";
319+
_context->natvis += aux;
276320
}
277321
}
278322
DebugSourceMap::Finalize();

innative/debug_pdb.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66

77
#include "debug_sourcemap.h"
88
#include "stack.h"
9+
#include <unordered_set>
910

1011
namespace innative {
1112
namespace code {
13+
KHASH_DECLARE(intset, size_t, char);
14+
1215
class DebugPDB : public DebugSourceMap
1316
{
1417
public:
1518
DebugPDB(SourceMap* s, Context* context, llvm::Module& m, const char* name, const char* filepath);
19+
~DebugPDB();
1620
virtual void FuncDecl(llvm::Function* fn, unsigned int offset, unsigned int line, bool optimized) override;
1721
virtual void PostFuncBody(llvm::Function* fn, FunctionBody& body) override;
1822
virtual void FuncParam(llvm::Function* fn, size_t indice, FunctionDesc& desc) override;
@@ -25,7 +29,7 @@ namespace innative {
2529

2630
protected:
2731
uint64_t _uid;
28-
std::vector<size_t> _deferred;
32+
kh_intset_t* _deferred;
2933
};
3034
}
3135
}

innative/debug_sourcemap.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void DebugSourceMap::FuncDecl(llvm::Function* fn, unsigned int offset, unsigned
9090

9191
auto subtype =
9292
_dbuilder->createSubroutineType(_dbuilder->getOrCreateTypeArray(dwarfTys), llvm::DINode::FlagZero,
93-
(fn->getCallingConv() == llvm::CallingConv::C) ? DW_CC_normal : DW_CC_nocall);
93+
(fn->getCallingConv() == llvm::CallingConv::C) ? DW_CC_normal : DW_CC_nocall);
9494

9595
FunctionDebugInfo(fn, name, optimized, true, false, GetSourceFile(f->source_index), f->original_line, 0, subtype);
9696
}
@@ -162,8 +162,8 @@ void DebugSourceMap::UpdateLocation(Instruction& i)
162162
auto original = _curscope->getSubprogram();
163163
subprograms[s.source_index] =
164164
_dbuilder->createFunction(original->getScope(), original->getName(), original->getLinkageName(),
165-
files[s.source_index], original->getLine(), original->getType(),
166-
original->getScopeLine(), original->getFlags(), original->getSPFlags());
165+
files[s.source_index], original->getLine(), original->getType(),
166+
original->getScopeLine(), original->getFlags(), original->getSPFlags());
167167
}
168168

169169
loc = _context->builder.getCurrentDebugLocation();
@@ -216,8 +216,9 @@ void DebugSourceMap::DebugIns(llvm::Function* fn, Instruction& i)
216216
void DebugSourceMap::DebugGlobal(llvm::GlobalVariable* v, llvm::StringRef name, size_t line)
217217
{
218218
v->addDebugInfo(_dbuilder->createGlobalVariableExpression(dcu, name, v->getName(), dunit, (unsigned int)line,
219-
CreateDebugType(v->getType()->getElementType()),
220-
!v->hasValidDeclarationLinkage(), _dbuilder->createExpression()));
219+
CreateDebugType(v->getType()->getElementType()),
220+
!v->hasValidDeclarationLinkage(),
221+
_dbuilder->createExpression()));
221222
}
222223

223224
llvm::DINode::DIFlags DebugSourceMap::GetFlags(unsigned short flags)
@@ -238,6 +239,8 @@ llvm::DINode::DIFlags DebugSourceMap::GetFlags(unsigned short flags)
238239

239240
llvm::DIType* DebugSourceMap::GetDebugType(size_t index, llvm::DIType* parent)
240241
{
242+
if(index == (size_t)~0)
243+
return diVoid;
241244
auto& type = sourcemap->x_innative_types[index];
242245

243246
if(index < types.size() && !types[index])
@@ -283,10 +286,10 @@ llvm::DIType* DebugSourceMap::GetDebugType(size_t index, llvm::DIType* parent)
283286
llvm::DICompositeType* composite;
284287
if(type.tag == DW_TAG_class_type)
285288
composite = _dbuilder->createClassType(file, name, file, type.original_line, type.bit_size, type.byte_align << 3,
286-
type.bit_offset, llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
289+
type.bit_offset, llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
287290
else
288291
composite = _dbuilder->createStructType(file, name, file, type.original_line, type.bit_size, type.byte_align << 3,
289-
llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
292+
llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
290293
types[index] = composite;
291294

292295
llvm::SmallVector<llvm::Metadata*, 8> elements;
@@ -300,7 +303,7 @@ llvm::DIType* DebugSourceMap::GetDebugType(size_t index, llvm::DIType* parent)
300303
case DW_TAG_union_type:
301304
{
302305
auto composite = _dbuilder->createUnionType(file, name, file, type.original_line, type.bit_size, type.byte_align << 3,
303-
GetFlags(type.flags), llvm::DINodeArray());
306+
GetFlags(type.flags), llvm::DINodeArray());
304307
types[index] = composite;
305308

306309
llvm::SmallVector<llvm::Metadata*, 8> elements;
@@ -331,8 +334,8 @@ llvm::DIType* DebugSourceMap::GetDebugType(size_t index, llvm::DIType* parent)
331334
break;
332335
case DW_TAG_member:
333336
if(auto ty = GetDebugType(type.type_index))
334-
types[index] = _dbuilder->createMemberType(file, name, file, type.original_line, type.bit_size, type.byte_align << 3,
335-
type.bit_offset, GetFlags(type.flags), ty);
337+
types[index] = _dbuilder->createMemberType(file, name, file, type.original_line, type.bit_size,
338+
type.byte_align << 3, type.bit_offset, GetFlags(type.flags), ty);
336339
break;
337340
case DW_TAG_enumeration_type:
338341
{
@@ -350,8 +353,8 @@ llvm::DIType* DebugSourceMap::GetDebugType(size_t index, llvm::DIType* parent)
350353
}
351354
}
352355
types[index] = _dbuilder->createEnumerationType(file, name, file, type.original_line, type.bit_size,
353-
type.byte_align << 3, _dbuilder->getOrCreateArray(elements), base, "",
354-
(type.flags & IN_SOURCE_TYPE_ENUM_CLASS) != 0);
356+
type.byte_align << 3, _dbuilder->getOrCreateArray(elements), base, "",
357+
(type.flags & IN_SOURCE_TYPE_ENUM_CLASS) != 0);
355358
}
356359
break;
357360
case DW_TAG_subroutine_type:

0 commit comments

Comments
 (0)