Commit | Line | Data |
---|---|---|
6baa0a5a FW |
1 | #include "../perf.h" |
2 | #include <stdlib.h> | |
3 | #include <stdio.h> | |
4 | #include <string.h> | |
b3165f41 | 5 | #include "session.h" |
6baa0a5a FW |
6 | #include "thread.h" |
7 | #include "util.h" | |
6e086437 | 8 | #include "debug.h" |
6baa0a5a | 9 | |
38051234 | 10 | struct thread *thread__new(pid_t tid) |
6baa0a5a | 11 | { |
36479484 | 12 | struct thread *self = zalloc(sizeof(*self)); |
6baa0a5a FW |
13 | |
14 | if (self != NULL) { | |
9958e1f0 | 15 | map_groups__init(&self->mg); |
38051234 | 16 | self->tid = tid; |
70c57efb | 17 | self->ppid = -1; |
97ea1a7f FW |
18 | self->comm = malloc(32); |
19 | if (self->comm) | |
38051234 | 20 | snprintf(self->comm, 32, ":%d", self->tid); |
6baa0a5a FW |
21 | } |
22 | ||
23 | return self; | |
24 | } | |
25 | ||
591765fd ACM |
26 | void thread__delete(struct thread *self) |
27 | { | |
28 | map_groups__exit(&self->mg); | |
29 | free(self->comm); | |
30 | free(self); | |
31 | } | |
32 | ||
6baa0a5a FW |
33 | int thread__set_comm(struct thread *self, const char *comm) |
34 | { | |
4385d580 DM |
35 | int err; |
36 | ||
6baa0a5a FW |
37 | if (self->comm) |
38 | free(self->comm); | |
39 | self->comm = strdup(comm); | |
4385d580 DM |
40 | err = self->comm == NULL ? -ENOMEM : 0; |
41 | if (!err) { | |
42 | self->comm_set = true; | |
4385d580 DM |
43 | } |
44 | return err; | |
6baa0a5a FW |
45 | } |
46 | ||
a4fb581b FW |
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 | ||
3f067dca | 58 | size_t thread__fprintf(struct thread *thread, FILE *fp) |
9958e1f0 | 59 | { |
38051234 | 60 | return fprintf(fp, "Thread %d %s\n", thread->tid, thread->comm) + |
3f067dca | 61 | map_groups__fprintf(&thread->mg, verbose, fp); |
6baa0a5a FW |
62 | } |
63 | ||
1b46cddf ACM |
64 | void thread__insert_map(struct thread *self, struct map *map) |
65 | { | |
c6e718ff | 66 | map_groups__fixup_overlappings(&self->mg, map, verbose, stderr); |
9958e1f0 | 67 | map_groups__insert(&self->mg, map); |
6baa0a5a FW |
68 | } |
69 | ||
95011c60 ACM |
70 | int thread__fork(struct thread *self, struct thread *parent) |
71 | { | |
72 | int i; | |
6baa0a5a | 73 | |
faa5c5c3 ACM |
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 | } | |
6baa0a5a | 82 | |
95011c60 | 83 | for (i = 0; i < MAP__NR_TYPES; ++i) |
9958e1f0 | 84 | if (map_groups__clone(&self->mg, &parent->mg, i) < 0) |
6baa0a5a | 85 | return -ENOMEM; |
70c57efb | 86 | |
38051234 | 87 | self->ppid = parent->tid; |
70c57efb | 88 | |
6baa0a5a FW |
89 | return 0; |
90 | } |