int machine__init(struct machine *machine, const char *root_dir, pid_t pid) { map_groups__init(&machine->kmaps); RB_CLEAR_NODE(&machine->rb_node); INIT_LIST_HEAD(&machine->user_dsos); INIT_LIST_HEAD(&machine->kernel_dsos); machine->threads = RB_ROOT; INIT_LIST_HEAD(&machine->dead_threads); machine->last_match = NULL; machine->kmaps.machine = machine; machine->pid = pid; machine->symbol_filter = NULL; machine->id_hdr_size = 0; machine->root_dir = strdup(root_dir); if (machine->root_dir == NULL) return -ENOMEM; if (pid != HOST_KERNEL_ID) { struct thread *thread = machine__findnew_thread(machine, 0, pid); char comm[64]; if (thread == NULL) return -ENOMEM; snprintf(comm, sizeof(comm), "[guest/%d]", pid); thread__set_comm(thread, comm, 0); } return 0; }
static struct thread *thread__new(pid_t pid) { struct thread *self = zalloc(sizeof(*self)); if (self != NULL) { map_groups__init(&self->mg); self->pid = pid; self->comm = malloc(32); if (self->comm) snprintf(self->comm, 32, ":%d", self->pid); } return self; }
struct perf_session *perf_session__new(const char *filename, int mode, bool force) { size_t len = filename ? strlen(filename) + 1 : 0; struct perf_session *self = zalloc(sizeof(*self) + len); if (self == NULL) goto out; if (perf_header__init(&self->header) < 0) goto out_free; memcpy(self->filename, filename, len); self->threads = RB_ROOT; self->last_match = NULL; self->mmap_window = 32; self->cwd = NULL; self->cwdlen = 0; self->unknown_events = 0; map_groups__init(&self->kmaps); if (mode == O_RDONLY) { if (perf_session__open(self, force) < 0) goto out_delete; } else if (mode == O_WRONLY) { /* * In O_RDONLY mode this will be performed when reading the * kernel MMAP event, in event__process_mmap(). */ if (perf_session__create_kernel_maps(self) < 0) goto out_delete; } self->sample_type = perf_header__sample_type(&self->header); out: return self; out_free: free(self); return NULL; out_delete: perf_session__delete(self); return NULL; }