1 #include "../perf.h" 2 #include <stdlib.h> 3 #include <stdio.h> 4 #include <string.h> 5 #include "session.h" 6 #include "thread.h" 7 #include "util.h" 8 #include "debug.h" 9 10 struct thread *thread__new(pid_t pid) 11 { 12 struct thread *self = zalloc(sizeof(*self)); 13 14 if (self != NULL) { 15 map_groups__init(&self->mg); 16 self->pid = pid; 17 self->ppid = -1; 18 self->comm = malloc(32); 19 if (self->comm) 20 snprintf(self->comm, 32, ":%d", self->pid); 21 } 22 23 return self; 24 } 25 26 void thread__delete(struct thread *self) 27 { 28 map_groups__exit(&self->mg); 29 free(self->comm); 30 free(self); 31 } 32 33 int thread__set_comm(struct thread *self, const char *comm) 34 { 35 int err; 36 37 if (self->comm) 38 free(self->comm); 39 self->comm = strdup(comm); 40 err = self->comm == NULL ? -ENOMEM : 0; 41 if (!err) { 42 self->comm_set = true; 43 } 44 return err; 45 } 46 47 int thread__comm_len(struct thread *self) 48 { 49 if (!self->comm_len) { 50 if (!self->comm) 51 return 0; 52 self->comm_len = strlen(self->comm); 53 } 54 55 return self->comm_len; 56 } 57 58 size_t thread__fprintf(struct thread *thread, FILE *fp) 59 { 60 return fprintf(fp, "Thread %d %s\n", thread->pid, thread->comm) + 61 map_groups__fprintf(&thread->mg, verbose, fp); 62 } 63 64 void thread__insert_map(struct thread *self, struct map *map) 65 { 66 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr); 67 map_groups__insert(&self->mg, map); 68 } 69 70 int thread__fork(struct thread *self, struct thread *parent) 71 { 72 int i; 73 74 if (parent->comm_set) { 75 if (self->comm) 76 free(self->comm); 77 self->comm = strdup(parent->comm); 78 if (!self->comm) 79 return -ENOMEM; 80 self->comm_set = true; 81 } 82 83 for (i = 0; i < MAP__NR_TYPES; ++i) 84 if (map_groups__clone(&self->mg, &parent->mg, i) < 0) 85 return -ENOMEM; 86 87 self->ppid = parent->pid; 88 89 return 0; 90 } 91