Commit 21eb3db3 authored by Alastair Robertson's avatar Alastair Robertson

Add names to most LLVM Allocas and Calls to make debugging easier

parent a48fce1c
...@@ -56,7 +56,7 @@ void CodegenLLVM::visit(Builtin &builtin) ...@@ -56,7 +56,7 @@ void CodegenLLVM::visit(Builtin &builtin)
{ {
int arg_num = atoi(builtin.ident.substr(3).c_str()); int arg_num = atoi(builtin.ident.substr(3).c_str());
AllocaInst *dst = b_.CreateAllocaBPF(); AllocaInst *dst = b_.CreateAllocaBPF(builtin.ident);
int offset = arch::arg_offset(arg_num) * sizeof(uintptr_t); int offset = arch::arg_offset(arg_num) * sizeof(uintptr_t);
Value *src = b_.CreateGEP(ctx_, b_.getInt64(offset)); Value *src = b_.CreateGEP(ctx_, b_.getInt64(offset));
b_.CreateProbeRead(dst, b_.getInt64(8), src); b_.CreateProbeRead(dst, b_.getInt64(8), src);
...@@ -64,7 +64,7 @@ void CodegenLLVM::visit(Builtin &builtin) ...@@ -64,7 +64,7 @@ void CodegenLLVM::visit(Builtin &builtin)
} }
else if (builtin.ident == "retval") else if (builtin.ident == "retval")
{ {
AllocaInst *dst = b_.CreateAllocaBPF(); AllocaInst *dst = b_.CreateAllocaBPF(builtin.ident);
int offset = arch::ret_offset() * sizeof(uintptr_t); int offset = arch::ret_offset() * sizeof(uintptr_t);
Value *src = b_.CreateGEP(ctx_, b_.getInt64(offset)); Value *src = b_.CreateGEP(ctx_, b_.getInt64(offset));
b_.CreateProbeRead(dst, b_.getInt64(8), src); b_.CreateProbeRead(dst, b_.getInt64(8), src);
...@@ -126,7 +126,7 @@ void CodegenLLVM::visit(Unop &unop) ...@@ -126,7 +126,7 @@ void CodegenLLVM::visit(Unop &unop)
case bpftrace::Parser::token::BNOT: expr_ = b_.CreateNeg(expr_); break; case bpftrace::Parser::token::BNOT: expr_ = b_.CreateNeg(expr_); break;
case bpftrace::Parser::token::MUL: case bpftrace::Parser::token::MUL:
{ {
AllocaInst *dst = b_.CreateAllocaBPF(); AllocaInst *dst = b_.CreateAllocaBPF("deref");
b_.CreateProbeRead(dst, b_.getInt64(8), expr_); b_.CreateProbeRead(dst, b_.getInt64(8), expr_);
expr_ = b_.CreateLoad(dst); expr_ = b_.CreateLoad(dst);
break; break;
...@@ -144,7 +144,7 @@ void CodegenLLVM::visit(AssignMapStatement &assignment) ...@@ -144,7 +144,7 @@ void CodegenLLVM::visit(AssignMapStatement &assignment)
{ {
Map &map = *assignment.map; Map &map = *assignment.map;
AllocaInst *val = b_.CreateAllocaBPF(); AllocaInst *val = b_.CreateAllocaBPF(map.ident + "_val");
assignment.expr->accept(*this); assignment.expr->accept(*this);
b_.CreateStore(expr_, val); b_.CreateStore(expr_, val);
...@@ -162,7 +162,7 @@ void CodegenLLVM::visit(AssignMapCallStatement &assignment) ...@@ -162,7 +162,7 @@ void CodegenLLVM::visit(AssignMapCallStatement &assignment)
{ {
AllocaInst *key = getMapKey(map); AllocaInst *key = getMapKey(map);
Value *oldval = b_.CreateMapLookupElem(map, key); Value *oldval = b_.CreateMapLookupElem(map, key);
AllocaInst *newval = b_.CreateAllocaBPF(); AllocaInst *newval = b_.CreateAllocaBPF(map.ident + "_val");
b_.CreateStore(b_.CreateAdd(oldval, b_.getInt64(1)), newval); b_.CreateStore(b_.CreateAdd(oldval, b_.getInt64(1)), newval);
b_.CreateMapUpdateElem(map, key, newval); b_.CreateMapUpdateElem(map, key, newval);
} }
...@@ -174,7 +174,7 @@ void CodegenLLVM::visit(AssignMapCallStatement &assignment) ...@@ -174,7 +174,7 @@ void CodegenLLVM::visit(AssignMapCallStatement &assignment)
AllocaInst *key = getQuantizeMapKey(map, log2); AllocaInst *key = getQuantizeMapKey(map, log2);
Value *oldval = b_.CreateMapLookupElem(map, key); Value *oldval = b_.CreateMapLookupElem(map, key);
AllocaInst *newval = b_.CreateAllocaBPF(); AllocaInst *newval = b_.CreateAllocaBPF(map.ident + "_val");
b_.CreateStore(b_.CreateAdd(oldval, b_.getInt64(1)), newval); b_.CreateStore(b_.CreateAdd(oldval, b_.getInt64(1)), newval);
b_.CreateMapUpdateElem(map, key, newval); b_.CreateMapUpdateElem(map, key, newval);
} }
...@@ -246,7 +246,7 @@ AllocaInst *CodegenLLVM::getMapKey(Map &map) ...@@ -246,7 +246,7 @@ AllocaInst *CodegenLLVM::getMapKey(Map &map)
{ {
AllocaInst *key; AllocaInst *key;
if (map.vargs) { if (map.vargs) {
key = b_.CreateAllocaBPF(map.vargs->size()); key = b_.CreateAllocaBPF(map.ident + "_key", map.vargs->size());
int i = 0; int i = 0;
for (Expression *expr : *map.vargs) { for (Expression *expr : *map.vargs) {
expr->accept(*this); expr->accept(*this);
...@@ -256,7 +256,7 @@ AllocaInst *CodegenLLVM::getMapKey(Map &map) ...@@ -256,7 +256,7 @@ AllocaInst *CodegenLLVM::getMapKey(Map &map)
} }
else else
{ {
key = b_.CreateAllocaBPF(); key = b_.CreateAllocaBPF(map.ident + "_key");
b_.CreateStore(b_.getInt64(0), key); b_.CreateStore(b_.getInt64(0), key);
} }
return key; return key;
...@@ -266,7 +266,7 @@ AllocaInst *CodegenLLVM::getQuantizeMapKey(Map &map, Value *log2) ...@@ -266,7 +266,7 @@ AllocaInst *CodegenLLVM::getQuantizeMapKey(Map &map, Value *log2)
{ {
AllocaInst *key; AllocaInst *key;
if (map.vargs) { if (map.vargs) {
key = b_.CreateAllocaBPF(map.vargs->size() + 1); key = b_.CreateAllocaBPF(map.ident + "_key", map.vargs->size() + 1);
int i = 0; int i = 0;
for (Expression *expr : *map.vargs) { for (Expression *expr : *map.vargs) {
expr->accept(*this); expr->accept(*this);
...@@ -278,7 +278,7 @@ AllocaInst *CodegenLLVM::getQuantizeMapKey(Map &map, Value *log2) ...@@ -278,7 +278,7 @@ AllocaInst *CodegenLLVM::getQuantizeMapKey(Map &map, Value *log2)
} }
else else
{ {
key = b_.CreateAllocaBPF(); key = b_.CreateAllocaBPF(map.ident + "_key");
b_.CreateStore(log2, key); b_.CreateStore(log2, key);
} }
return key; return key;
......
...@@ -25,22 +25,22 @@ IRBuilderBPF::IRBuilderBPF(LLVMContext &context, ...@@ -25,22 +25,22 @@ IRBuilderBPF::IRBuilderBPF(LLVMContext &context,
&module_); &module_);
} }
AllocaInst *IRBuilderBPF::CreateAllocaBPF(int num_items, const std::string &name) AllocaInst *IRBuilderBPF::CreateAllocaBPF(const std::string &name, int num_items)
{ {
llvm::Type *ty = getInt64Ty(); llvm::Type *ty = getInt64Ty();
Value *array_size = getInt64(num_items); Value *array_size = getInt64(num_items);
Function *parent = GetInsertBlock()->getParent(); Function *parent = GetInsertBlock()->getParent();
BasicBlock &entry_block = parent->getEntryBlock(); BasicBlock &entry_block = parent->getEntryBlock();
if (entry_block.empty()) if (entry_block.empty())
return new AllocaInst(ty, array_size, "", &entry_block); return new AllocaInst(ty, array_size, name, &entry_block);
else else
return new AllocaInst(ty, array_size, "", &entry_block.front()); return new AllocaInst(ty, array_size, name, &entry_block.front());
} }
CallInst *IRBuilderBPF::CreateBpfPseudoCall(int mapfd) CallInst *IRBuilderBPF::CreateBpfPseudoCall(int mapfd)
{ {
Function *pseudo_func = module_.getFunction("llvm.bpf.pseudo"); Function *pseudo_func = module_.getFunction("llvm.bpf.pseudo");
return CreateCall(pseudo_func, {getInt64(BPF_PSEUDO_MAP_FD), getInt64(mapfd)}); return CreateCall(pseudo_func, {getInt64(BPF_PSEUDO_MAP_FD), getInt64(mapfd)}, "pseudo");
} }
CallInst *IRBuilderBPF::CreateBpfPseudoCall(Map &map) CallInst *IRBuilderBPF::CreateBpfPseudoCall(Map &map)
...@@ -64,7 +64,7 @@ LoadInst *IRBuilderBPF::CreateMapLookupElem(Map &map, AllocaInst *key) ...@@ -64,7 +64,7 @@ LoadInst *IRBuilderBPF::CreateMapLookupElem(Map &map, AllocaInst *key)
Instruction::IntToPtr, Instruction::IntToPtr,
getInt64(BPF_FUNC_map_lookup_elem), getInt64(BPF_FUNC_map_lookup_elem),
lookup_func_ptr_type); lookup_func_ptr_type);
CallInst *call = CreateCall(lookup_func, {map_ptr, key}); CallInst *call = CreateCall(lookup_func, {map_ptr, key}, "lookup_elem");
// Check if result == 0 // Check if result == 0
Function *parent = GetInsertBlock()->getParent(); Function *parent = GetInsertBlock()->getParent();
...@@ -72,7 +72,7 @@ LoadInst *IRBuilderBPF::CreateMapLookupElem(Map &map, AllocaInst *key) ...@@ -72,7 +72,7 @@ LoadInst *IRBuilderBPF::CreateMapLookupElem(Map &map, AllocaInst *key)
BasicBlock *lookup_failure_block = BasicBlock::Create(module_.getContext(), "lookup_failure", parent); BasicBlock *lookup_failure_block = BasicBlock::Create(module_.getContext(), "lookup_failure", parent);
BasicBlock *lookup_merge_block = BasicBlock::Create(module_.getContext(), "lookup_merge", parent); BasicBlock *lookup_merge_block = BasicBlock::Create(module_.getContext(), "lookup_merge", parent);
Value *value = CreateAllocaBPF(); Value *value = CreateAllocaBPF("lookup_elem_val");
Value *condition = CreateICmpNE( Value *condition = CreateICmpNE(
CreateIntCast(call, getInt8PtrTy(), true), CreateIntCast(call, getInt8PtrTy(), true),
ConstantExpr::getCast(Instruction::IntToPtr, getInt64(0), getInt8PtrTy()), ConstantExpr::getCast(Instruction::IntToPtr, getInt64(0), getInt8PtrTy()),
...@@ -105,7 +105,7 @@ void IRBuilderBPF::CreateMapUpdateElem(Map &map, AllocaInst *key, Value *val) ...@@ -105,7 +105,7 @@ void IRBuilderBPF::CreateMapUpdateElem(Map &map, AllocaInst *key, Value *val)
Instruction::IntToPtr, Instruction::IntToPtr,
getInt64(BPF_FUNC_map_update_elem), getInt64(BPF_FUNC_map_update_elem),
update_func_ptr_type); update_func_ptr_type);
CallInst *call = CreateCall(update_func, {map_ptr, key, val, flags}); CallInst *call = CreateCall(update_func, {map_ptr, key, val, flags}, "update_elem");
} }
void IRBuilderBPF::CreateMapDeleteElem(Map &map, AllocaInst *key) void IRBuilderBPF::CreateMapDeleteElem(Map &map, AllocaInst *key)
...@@ -124,7 +124,7 @@ void IRBuilderBPF::CreateMapDeleteElem(Map &map, AllocaInst *key) ...@@ -124,7 +124,7 @@ void IRBuilderBPF::CreateMapDeleteElem(Map &map, AllocaInst *key)
Instruction::IntToPtr, Instruction::IntToPtr,
getInt64(BPF_FUNC_map_delete_elem), getInt64(BPF_FUNC_map_delete_elem),
delete_func_ptr_type); delete_func_ptr_type);
CallInst *call = CreateCall(delete_func, {map_ptr, key}); CallInst *call = CreateCall(delete_func, {map_ptr, key}, "delete_elem");
} }
void IRBuilderBPF::CreateProbeRead(AllocaInst *dst, Value *size, Value *src) void IRBuilderBPF::CreateProbeRead(AllocaInst *dst, Value *size, Value *src)
...@@ -140,7 +140,7 @@ void IRBuilderBPF::CreateProbeRead(AllocaInst *dst, Value *size, Value *src) ...@@ -140,7 +140,7 @@ void IRBuilderBPF::CreateProbeRead(AllocaInst *dst, Value *size, Value *src)
Instruction::IntToPtr, Instruction::IntToPtr,
getInt64(BPF_FUNC_probe_read), getInt64(BPF_FUNC_probe_read),
proberead_func_ptr_type); proberead_func_ptr_type);
CallInst *call = CreateCall(proberead_func, {dst, size, src}); CallInst *call = CreateCall(proberead_func, {dst, size, src}, "probe_read");
} }
CallInst *IRBuilderBPF::CreateGetNs() CallInst *IRBuilderBPF::CreateGetNs()
...@@ -153,7 +153,7 @@ CallInst *IRBuilderBPF::CreateGetNs() ...@@ -153,7 +153,7 @@ CallInst *IRBuilderBPF::CreateGetNs()
Instruction::IntToPtr, Instruction::IntToPtr,
getInt64(BPF_FUNC_ktime_get_ns), getInt64(BPF_FUNC_ktime_get_ns),
gettime_func_ptr_type); gettime_func_ptr_type);
return CreateCall(gettime_func); return CreateCall(gettime_func, {}, "get_ns");
} }
CallInst *IRBuilderBPF::CreateGetPidTgid() CallInst *IRBuilderBPF::CreateGetPidTgid()
...@@ -166,7 +166,7 @@ CallInst *IRBuilderBPF::CreateGetPidTgid() ...@@ -166,7 +166,7 @@ CallInst *IRBuilderBPF::CreateGetPidTgid()
Instruction::IntToPtr, Instruction::IntToPtr,
getInt64(BPF_FUNC_get_current_pid_tgid), getInt64(BPF_FUNC_get_current_pid_tgid),
getpidtgid_func_ptr_type); getpidtgid_func_ptr_type);
return CreateCall(getpidtgid_func); return CreateCall(getpidtgid_func, {}, "get_pid_tgid");
} }
CallInst *IRBuilderBPF::CreateGetUidGid() CallInst *IRBuilderBPF::CreateGetUidGid()
...@@ -179,7 +179,7 @@ CallInst *IRBuilderBPF::CreateGetUidGid() ...@@ -179,7 +179,7 @@ CallInst *IRBuilderBPF::CreateGetUidGid()
Instruction::IntToPtr, Instruction::IntToPtr,
getInt64(BPF_FUNC_get_current_uid_gid), getInt64(BPF_FUNC_get_current_uid_gid),
getuidgid_func_ptr_type); getuidgid_func_ptr_type);
return CreateCall(getuidgid_func); return CreateCall(getuidgid_func, {}, "get_uid_gid");
} }
CallInst *IRBuilderBPF::CreateGetStackId(Value *ctx, bool ustack) CallInst *IRBuilderBPF::CreateGetStackId(Value *ctx, bool ustack)
...@@ -202,7 +202,7 @@ CallInst *IRBuilderBPF::CreateGetStackId(Value *ctx, bool ustack) ...@@ -202,7 +202,7 @@ CallInst *IRBuilderBPF::CreateGetStackId(Value *ctx, bool ustack)
Instruction::IntToPtr, Instruction::IntToPtr,
getInt64(BPF_FUNC_get_stackid), getInt64(BPF_FUNC_get_stackid),
getstackid_func_ptr_type); getstackid_func_ptr_type);
return CreateCall(getstackid_func, {ctx, map_ptr, flags_val}); return CreateCall(getstackid_func, {ctx, map_ptr, flags_val}, "get_stackid");
} }
} // namespace ast } // namespace ast
......
...@@ -17,7 +17,7 @@ public: ...@@ -17,7 +17,7 @@ public:
Module &module, Module &module,
BPFtrace &bpftrace); BPFtrace &bpftrace);
AllocaInst *CreateAllocaBPF(int array_size=1, const std::string &name=""); AllocaInst *CreateAllocaBPF(const std::string &name="", int num_items=1);
CallInst *CreateBpfPseudoCall(int mapfd); CallInst *CreateBpfPseudoCall(int mapfd);
CallInst *CreateBpfPseudoCall(Map &map); CallInst *CreateBpfPseudoCall(Map &map);
LoadInst *CreateMapLookupElem(Map &map, AllocaInst *key); LoadInst *CreateMapLookupElem(Map &map, AllocaInst *key);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment