#include <dt_impl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysctl.h>
static int
dt_pid_from_process_name(const char* name)
{
static struct kinfo_proc *proc_buf = NULL;
static int nprocs;
if (proc_buf == NULL) {
size_t buf_size;
int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL};
int err;
err = sysctl(mib, 4, NULL, &buf_size, NULL, 0);
if( (err < 0) && (err != ENOMEM)) {
xyerror(D_UNKNOWN, "Failure calling sysctl to get process list buffer size");
return -1;
}
proc_buf = (struct kinfo_proc *)calloc(1, buf_size);
if(sysctl(mib, 4, proc_buf, &buf_size, NULL, 0) < 0) {
free(proc_buf);
return -1;
}
nprocs = (int)(buf_size / (sizeof(struct kinfo_proc)));
}
int i;
for (i = 0; i < nprocs; i++) {
if (strncmp(name, proc_buf[i].kp_proc.p_comm, MAXCOMLEN) == 0)
return proc_buf[i].kp_proc.p_pid;
}
return -1;
}
dt_ident_t*
dt_macro_lookup(dt_idhash_t *macros, const char *name)
{
dt_ident_t *idp;
idp = dt_idhash_lookup(macros, name);
if (idp != NULL) {
return idp;
}
else if (strncmp(name, "pid_", strlen("pid_")) == 0) {
int pid = dt_pid_from_process_name(name + strlen("pid_"));
if (pid != -1) {
idp = dt_idhash_insert(macros, name + strlen("pid_"), DT_IDENT_SCALAR, 0, pid, _dtrace_prvattr, 0, &dt_idops_thaw, NULL, 0);
return idp;
}
else {
xyerror(D_PROC_NOT_FOUND, "Could not find process with name %s\n", name + strlen("pid_"));
}
}
return NULL;
}
cpu_type_t
dtrace_str2arch(const char *str)
{
int i;
const struct {
char *name;
cpu_type_t type;
} archs[] = {
{"i386", CPU_TYPE_I386},
{"x86_64", CPU_TYPE_X86_64},
{"arm", CPU_TYPE_ARM},
{"arm64", CPU_TYPE_ARM64},
{"any", CPU_TYPE_ANY},
{NULL, NULL}
};
for (i = 0; archs[i].name != NULL; i++) {
if (strcmp(str, archs[i].name) == 0)
return archs[i].type;
}
return (cpu_type_t)0;
}
int
dt_kernel_lp64(void)
{
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };
struct kinfo_proc kp;
size_t len = sizeof(kp);
int ret = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &kp, &len, NULL, 0);
if (ret == -1) {
return 0;
}
if (kp.kp_proc.p_flag & P_LP64) {
return 1;
}
return 0;
}