Commit 68ede064 authored by Alastair Robertson's avatar Alastair Robertson

Add cpu builtin

parent 8a362f07
...@@ -98,6 +98,7 @@ Variables: ...@@ -98,6 +98,7 @@ Variables:
- `uid` - User ID - `uid` - User ID
- `gid` - Group ID - `gid` - Group ID
- `nsecs` - Nanosecond timestamp - `nsecs` - Nanosecond timestamp
- `cpu` - Processor ID
- `comm` - Process name - `comm` - Process name
- `stack` - Kernel stack trace - `stack` - Kernel stack trace
- `ustack` - User stack trace - `ustack` - User stack trace
......
...@@ -60,6 +60,10 @@ void CodegenLLVM::visit(Builtin &builtin) ...@@ -60,6 +60,10 @@ void CodegenLLVM::visit(Builtin &builtin)
expr_ = b_.CreateLShr(uidgid, 32); expr_ = b_.CreateLShr(uidgid, 32);
} }
} }
else if (builtin.ident == "cpu")
{
expr_ = b_.CreateGetCpuId();
}
else if (builtin.ident == "comm") else if (builtin.ident == "comm")
{ {
AllocaInst *buf = b_.CreateAllocaBPF(builtin.type, "comm"); AllocaInst *buf = b_.CreateAllocaBPF(builtin.type, "comm");
......
...@@ -258,6 +258,19 @@ CallInst *IRBuilderBPF::CreateGetUidGid() ...@@ -258,6 +258,19 @@ CallInst *IRBuilderBPF::CreateGetUidGid()
return CreateCall(getuidgid_func, {}, "get_uid_gid"); return CreateCall(getuidgid_func, {}, "get_uid_gid");
} }
CallInst *IRBuilderBPF::CreateGetCpuId()
{
// u32 bpf_raw_smp_processor_id(void)
// Return: SMP processor ID
FunctionType *getcpuid_func_type = FunctionType::get(getInt64Ty(), false);
PointerType *getcpuid_func_ptr_type = PointerType::get(getcpuid_func_type, 0);
Constant *getcpuid_func = ConstantExpr::getCast(
Instruction::IntToPtr,
getInt64(BPF_FUNC_get_smp_processor_id),
getcpuid_func_ptr_type);
return CreateCall(getcpuid_func, {}, "get_cpu_id");
}
CallInst *IRBuilderBPF::CreateGetStackId(Value *ctx, bool ustack) CallInst *IRBuilderBPF::CreateGetStackId(Value *ctx, bool ustack)
{ {
Value *map_ptr = CreateBpfPseudoCall(bpftrace_.stackid_map_->mapfd_); Value *map_ptr = CreateBpfPseudoCall(bpftrace_.stackid_map_->mapfd_);
......
...@@ -32,6 +32,7 @@ public: ...@@ -32,6 +32,7 @@ public:
CallInst *CreateGetNs(); CallInst *CreateGetNs();
CallInst *CreateGetPidTgid(); CallInst *CreateGetPidTgid();
CallInst *CreateGetUidGid(); CallInst *CreateGetUidGid();
CallInst *CreateGetCpuId();
CallInst *CreateGetStackId(Value *ctx, bool ustack); CallInst *CreateGetStackId(Value *ctx, bool ustack);
void CreateGetCurrentComm(AllocaInst *buf, size_t size); void CreateGetCurrentComm(AllocaInst *buf, size_t size);
......
...@@ -28,6 +28,7 @@ void SemanticAnalyser::visit(Builtin &builtin) ...@@ -28,6 +28,7 @@ void SemanticAnalyser::visit(Builtin &builtin)
builtin.ident == "tid" || builtin.ident == "tid" ||
builtin.ident == "uid" || builtin.ident == "uid" ||
builtin.ident == "gid" || builtin.ident == "gid" ||
builtin.ident == "cpu" ||
builtin.ident == "retval") { builtin.ident == "retval") {
builtin.type = SizedType(Type::integer, 8); builtin.type = SizedType(Type::integer, 8);
} }
......
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