16 if (dir->d_name[0] ==
'.')
30 sprintf(name,
"/proc/%d/task", pid);
31 items = scandir(name, &namelist,
filter,
NULL);
35 threads =
malloc(
sizeof(*threads) +
sizeof(
pid_t) * items);
36 if (threads !=
NULL) {
37 for (i = 0; i < items; i++)
38 threads->
map[i] = atoi(namelist[i]->d_name);
42 for (i=0; i<items; i++)
53 if (threads !=
NULL) {
54 threads->
map[0] = tid;
68 max_threads *
sizeof(
pid_t));
72 proc = opendir(
"/proc");
74 goto out_free_threads;
78 while (!readdir_r(proc, &dirent, &next) && next) {
82 pid_t pid = strtol(dirent.d_name, &end, 10);
87 snprintf(path,
sizeof(path),
"/proc/%s", dirent.d_name);
89 if (
stat(path, &st) != 0)
95 snprintf(path,
sizeof(path),
"/proc/%d/task", pid);
98 goto out_free_closedir;
100 while (threads->
nr +
items >= max_threads) {
108 tmp = realloc(threads, (
sizeof(*threads) +
109 max_threads *
sizeof(
pid_t)));
111 goto out_free_namelist;
116 for (i = 0; i <
items; i++)
117 threads->
map[threads->
nr + i] = atoi(namelist[i]->d_name);
119 for (i = 0; i < items; i++)
123 threads->
nr += items;
136 for (i = 0; i <
items; i++)
157 static struct thread_map *thread_map__new_by_pid_str(
const char *pid_str)
161 int items, total_tasks = 0;
173 pid = strtol(pos->
s, &end_ptr, 10);
176 (*end_ptr !=
'\0' && *end_ptr !=
','))
177 goto out_free_threads;
182 sprintf(name,
"/proc/%d/task", pid);
183 items = scandir(name, &namelist,
filter,
NULL);
185 goto out_free_threads;
187 total_tasks += items;
188 nt = realloc(threads, (
sizeof(*threads) +
189 sizeof(
pid_t) * total_tasks));
191 goto out_free_namelist;
195 for (i = 0; i < items; i++) {
196 threads->
map[j++] = atoi(namelist[i]->d_name);
199 threads->
nr = total_tasks;
208 for (i = 0; i < items; i++)
218 static struct thread_map *thread_map__new_by_tid_str(
const char *tid_str)
229 threads =
malloc(
sizeof(*threads) +
sizeof(
pid_t));
230 if (threads !=
NULL) {
231 threads->
map[0] = -1;
242 tid = strtol(pos->
s, &end_ptr, 10);
245 (*end_ptr !=
'\0' && *end_ptr !=
','))
246 goto out_free_threads;
252 nt = realloc(threads,
sizeof(*threads) +
sizeof(
pid_t) * ntasks);
255 goto out_free_threads;
258 threads->
map[ntasks - 1] = tid;
259 threads->
nr = ntasks;
274 return thread_map__new_by_pid_str(pid);
279 return thread_map__new_by_tid_str(tid);
290 size_t printed =
fprintf(fp,
"%d thread%s: ",
291 threads->
nr, threads->
nr > 1 ?
"s" :
"");
292 for (i = 0; i < threads->
nr; ++
i)
293 printed +=
fprintf(fp,
"%s%d", i ?
", " :
"", threads->
map[i]);
295 return printed +
fprintf(fp,
"\n");