Commit | Line | Data |
---|---|---|
d26e3629 KY |
1 | /* Linux-specific functions to retrieve OS data. |
2 | ||
0b302171 | 3 | Copyright (C) 2009-2012 Free Software Foundation, Inc. |
d26e3629 KY |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #ifdef GDBSERVER | |
21 | #include "server.h" | |
22 | #else | |
23 | #include "defs.h" | |
24 | #endif | |
25 | ||
26 | #include "linux-osdata.h" | |
27 | ||
28 | #include <sys/types.h> | |
29 | #include <sys/stat.h> | |
30 | #include <dirent.h> | |
31 | #include <ctype.h> | |
32 | #include <stdlib.h> | |
33 | #include <string.h> | |
34 | #include <utmp.h> | |
35 | #include <time.h> | |
36 | #include <unistd.h> | |
37 | #include <pwd.h> | |
38 | #include <grp.h> | |
39 | #include <netdb.h> | |
40 | #include <netinet/in.h> | |
41 | #include <arpa/inet.h> | |
42 | ||
43 | #include "xml-utils.h" | |
44 | #include "buffer.h" | |
45 | #include "gdb_assert.h" | |
46 | #include "gdb_dirent.h" | |
47 | ||
2e794194 JK |
48 | /* Compute and return the processor core of a given thread. */ |
49 | ||
d26e3629 KY |
50 | int |
51 | linux_common_core_of_thread (ptid_t ptid) | |
52 | { | |
53 | char filename[sizeof ("/proc//task//stat") | |
54 | + 2 * 20 /* decimal digits for 2 numbers, max 2^64 bit each */ | |
55 | + 1]; | |
56 | FILE *f; | |
57 | char *content = NULL; | |
58 | char *p; | |
59 | char *ts = 0; | |
60 | int content_read = 0; | |
61 | int i; | |
62 | int core; | |
63 | ||
64 | sprintf (filename, "/proc/%d/task/%ld/stat", | |
65 | ptid_get_pid (ptid), ptid_get_lwp (ptid)); | |
66 | f = fopen (filename, "r"); | |
67 | if (!f) | |
68 | return -1; | |
69 | ||
70 | for (;;) | |
71 | { | |
72 | int n; | |
73 | content = xrealloc (content, content_read + 1024); | |
74 | n = fread (content + content_read, 1, 1024, f); | |
75 | content_read += n; | |
76 | if (n < 1024) | |
77 | { | |
78 | content[content_read] = '\0'; | |
79 | break; | |
80 | } | |
81 | } | |
82 | ||
83 | p = strchr (content, '('); | |
84 | ||
85 | /* Skip ")". */ | |
86 | if (p != NULL) | |
87 | p = strchr (p, ')'); | |
88 | if (p != NULL) | |
89 | p++; | |
90 | ||
91 | /* If the first field after program name has index 0, then core number is | |
92 | the field with index 36. There's no constant for that anywhere. */ | |
93 | if (p != NULL) | |
94 | p = strtok_r (p, " ", &ts); | |
95 | for (i = 0; p != NULL && i != 36; ++i) | |
96 | p = strtok_r (NULL, " ", &ts); | |
97 | ||
98 | if (p == NULL || sscanf (p, "%d", &core) == 0) | |
99 | core = -1; | |
100 | ||
101 | xfree (content); | |
102 | fclose (f); | |
103 | ||
104 | return core; | |
105 | } | |
106 | ||
107 | static void | |
108 | command_from_pid (char *command, int maxlen, pid_t pid) | |
109 | { | |
110 | char *stat_path = xstrprintf ("/proc/%d/stat", pid); | |
111 | FILE *fp = fopen (stat_path, "r"); | |
112 | ||
113 | command[0] = '\0'; | |
114 | ||
115 | if (fp) | |
116 | { | |
117 | /* sizeof (cmd) should be greater or equal to TASK_COMM_LEN (in | |
118 | include/linux/sched.h in the Linux kernel sources) plus two | |
119 | (for the brackets). */ | |
120 | char cmd[32]; | |
121 | pid_t stat_pid; | |
122 | int items_read = fscanf (fp, "%d %32s", &stat_pid, cmd); | |
123 | ||
124 | if (items_read == 2 && pid == stat_pid) | |
125 | { | |
126 | cmd[strlen (cmd) - 1] = '\0'; /* Remove trailing parenthesis. */ | |
127 | strncpy (command, cmd + 1, maxlen); /* Ignore leading parenthesis. */ | |
128 | } | |
129 | ||
130 | fclose (fp); | |
131 | } | |
132 | else | |
133 | { | |
134 | /* Return the PID if a /proc entry for the process cannot be found. */ | |
135 | snprintf (command, maxlen, "%d", pid); | |
136 | } | |
137 | ||
138 | command[maxlen - 1] = '\0'; /* Ensure string is null-terminated. */ | |
139 | ||
140 | xfree (stat_path); | |
141 | } | |
142 | ||
143 | /* Returns the command-line of the process with the given PID. The returned | |
144 | string needs to be freed using xfree after use. */ | |
145 | ||
146 | static char * | |
147 | commandline_from_pid (pid_t pid) | |
148 | { | |
149 | char *pathname = xstrprintf ("/proc/%d/cmdline", pid); | |
150 | char *commandline = NULL; | |
151 | FILE *f = fopen (pathname, "r"); | |
152 | ||
153 | if (f) | |
154 | { | |
155 | size_t len = 0; | |
156 | ||
157 | while (!feof (f)) | |
158 | { | |
159 | char buf[1024]; | |
160 | size_t read_bytes = fread (buf, 1, sizeof (buf), f); | |
161 | ||
162 | if (read_bytes) | |
163 | { | |
164 | commandline = (char *) xrealloc (commandline, len + read_bytes + 1); | |
165 | memcpy (commandline + len, buf, read_bytes); | |
166 | len += read_bytes; | |
167 | } | |
168 | } | |
169 | ||
170 | fclose (f); | |
171 | ||
172 | if (commandline) | |
173 | { | |
174 | size_t i; | |
175 | ||
176 | /* Replace null characters with spaces. */ | |
177 | for (i = 0; i < len; ++i) | |
178 | if (commandline[i] == '\0') | |
179 | commandline[i] = ' '; | |
180 | ||
181 | commandline[len] = '\0'; | |
182 | } | |
183 | else | |
184 | { | |
185 | /* Return the command in square brackets if the command-line is empty. */ | |
186 | commandline = (char *) xmalloc (32); | |
187 | commandline[0] = '['; | |
188 | command_from_pid (commandline + 1, 31, pid); | |
189 | ||
190 | len = strlen (commandline); | |
191 | if (len < 31) | |
192 | strcat (commandline, "]"); | |
193 | } | |
194 | } | |
195 | ||
196 | xfree (pathname); | |
197 | ||
198 | return commandline; | |
199 | } | |
200 | ||
201 | static void | |
202 | user_from_uid (char *user, int maxlen, uid_t uid) | |
203 | { | |
204 | struct passwd *pwentry = getpwuid (uid); | |
205 | ||
206 | if (pwentry) | |
207 | { | |
208 | strncpy (user, pwentry->pw_name, maxlen); | |
209 | user[maxlen - 1] = '\0'; /* Ensure that the user name is null-terminated. */ | |
210 | } | |
211 | else | |
212 | user[0] = '\0'; | |
213 | } | |
214 | ||
215 | static int | |
216 | get_process_owner (uid_t *owner, pid_t pid) | |
217 | { | |
218 | struct stat statbuf; | |
219 | char procentry[sizeof ("/proc/4294967295")]; | |
220 | ||
221 | sprintf (procentry, "/proc/%d", pid); | |
222 | ||
223 | if (stat (procentry, &statbuf) == 0 && S_ISDIR (statbuf.st_mode)) | |
224 | { | |
225 | *owner = statbuf.st_uid; | |
226 | return 0; | |
227 | } | |
228 | else | |
229 | return -1; | |
230 | } | |
231 | ||
232 | static int | |
233 | get_number_of_cpu_cores (void) | |
234 | { | |
235 | int cores = 0; | |
236 | FILE *f = fopen ("/proc/cpuinfo", "r"); | |
237 | ||
238 | while (!feof (f)) | |
239 | { | |
240 | char buf[512]; | |
241 | char *p = fgets (buf, sizeof (buf), f); | |
242 | ||
243 | if (p && strncmp (buf, "processor", 9) == 0) | |
244 | ++cores; | |
245 | } | |
246 | ||
247 | fclose (f); | |
248 | ||
249 | return cores; | |
250 | } | |
251 | ||
252 | /* CORES points to an array of at least get_number_of_cpu_cores () elements. */ | |
253 | ||
254 | static int | |
255 | get_cores_used_by_process (pid_t pid, int *cores) | |
256 | { | |
257 | char taskdir[sizeof ("/proc/4294967295/task")]; | |
258 | DIR *dir; | |
259 | struct dirent *dp; | |
260 | int task_count = 0; | |
261 | ||
262 | sprintf (taskdir, "/proc/%d/task", pid); | |
263 | dir = opendir (taskdir); | |
e5798bef | 264 | if (dir) |
d26e3629 | 265 | { |
e5798bef PA |
266 | while ((dp = readdir (dir)) != NULL) |
267 | { | |
268 | pid_t tid; | |
269 | int core; | |
d26e3629 | 270 | |
e5798bef PA |
271 | if (!isdigit (dp->d_name[0]) |
272 | || NAMELEN (dp) > sizeof ("4294967295") - 1) | |
273 | continue; | |
d26e3629 | 274 | |
e5798bef PA |
275 | tid = atoi (dp->d_name); |
276 | core = linux_common_core_of_thread (ptid_build (pid, tid, 0)); | |
d26e3629 | 277 | |
e5798bef PA |
278 | if (core >= 0) |
279 | { | |
280 | ++cores[core]; | |
281 | ++task_count; | |
282 | } | |
d26e3629 | 283 | } |
d26e3629 | 284 | |
e5798bef PA |
285 | closedir (dir); |
286 | } | |
d26e3629 KY |
287 | |
288 | return task_count; | |
289 | } | |
290 | ||
291 | static LONGEST | |
292 | linux_xfer_osdata_processes (gdb_byte *readbuf, | |
293 | ULONGEST offset, LONGEST len) | |
294 | { | |
295 | /* We make the process list snapshot when the object starts to be read. */ | |
296 | static const char *buf; | |
297 | static LONGEST len_avail = -1; | |
298 | static struct buffer buffer; | |
299 | ||
300 | if (offset == 0) | |
301 | { | |
302 | DIR *dirp; | |
303 | ||
304 | if (len_avail != -1 && len_avail != 0) | |
305 | buffer_free (&buffer); | |
306 | len_avail = 0; | |
307 | buf = NULL; | |
308 | buffer_init (&buffer); | |
309 | buffer_grow_str (&buffer, "<osdata type=\"processes\">\n"); | |
310 | ||
311 | dirp = opendir ("/proc"); | |
312 | if (dirp) | |
313 | { | |
314 | const int num_cores = get_number_of_cpu_cores (); | |
315 | struct dirent *dp; | |
316 | ||
317 | while ((dp = readdir (dirp)) != NULL) | |
318 | { | |
319 | pid_t pid; | |
320 | uid_t owner; | |
321 | char user[UT_NAMESIZE]; | |
322 | char *command_line; | |
323 | int *cores; | |
324 | int task_count; | |
325 | char *cores_str; | |
326 | int i; | |
327 | ||
328 | if (!isdigit (dp->d_name[0]) | |
329 | || NAMELEN (dp) > sizeof ("4294967295") - 1) | |
330 | continue; | |
331 | ||
332 | sscanf (dp->d_name, "%d", &pid); | |
333 | command_line = commandline_from_pid (pid); | |
334 | ||
335 | if (get_process_owner (&owner, pid) == 0) | |
336 | user_from_uid (user, sizeof (user), owner); | |
337 | else | |
338 | strcpy (user, "?"); | |
339 | ||
340 | /* Find CPU cores used by the process. */ | |
341 | cores = (int *) xcalloc (num_cores, sizeof (int)); | |
342 | task_count = get_cores_used_by_process (pid, cores); | |
343 | cores_str = (char *) xcalloc (task_count, sizeof ("4294967295") + 1); | |
344 | ||
345 | for (i = 0; i < num_cores && task_count > 0; ++i) | |
346 | if (cores[i]) | |
347 | { | |
348 | char core_str[sizeof ("4294967205")]; | |
349 | ||
350 | sprintf (core_str, "%d", i); | |
351 | strcat (cores_str, core_str); | |
352 | ||
353 | task_count -= cores[i]; | |
354 | if (task_count > 0) | |
355 | strcat (cores_str, ","); | |
356 | } | |
357 | ||
358 | xfree (cores); | |
359 | ||
360 | buffer_xml_printf ( | |
361 | &buffer, | |
362 | "<item>" | |
363 | "<column name=\"pid\">%d</column>" | |
364 | "<column name=\"user\">%s</column>" | |
365 | "<column name=\"command\">%s</column>" | |
366 | "<column name=\"cores\">%s</column>" | |
367 | "</item>", | |
368 | pid, | |
369 | user, | |
370 | command_line ? command_line : "", | |
371 | cores_str); | |
372 | ||
373 | xfree (command_line); | |
374 | xfree (cores_str); | |
375 | } | |
376 | ||
377 | closedir (dirp); | |
378 | } | |
379 | ||
380 | buffer_grow_str0 (&buffer, "</osdata>\n"); | |
381 | buf = buffer_finish (&buffer); | |
382 | len_avail = strlen (buf); | |
383 | } | |
384 | ||
385 | if (offset >= len_avail) | |
386 | { | |
387 | /* Done. Get rid of the buffer. */ | |
388 | buffer_free (&buffer); | |
389 | buf = NULL; | |
390 | len_avail = 0; | |
391 | return 0; | |
392 | } | |
393 | ||
394 | if (len > len_avail - offset) | |
395 | len = len_avail - offset; | |
396 | memcpy (readbuf, buf + offset, len); | |
397 | ||
398 | return len; | |
399 | } | |
400 | ||
401 | static LONGEST | |
402 | linux_xfer_osdata_threads (gdb_byte *readbuf, | |
403 | ULONGEST offset, LONGEST len) | |
404 | { | |
405 | /* We make the process list snapshot when the object starts to be read. */ | |
406 | static const char *buf; | |
407 | static LONGEST len_avail = -1; | |
408 | static struct buffer buffer; | |
409 | ||
410 | if (offset == 0) | |
411 | { | |
412 | DIR *dirp; | |
413 | ||
414 | if (len_avail != -1 && len_avail != 0) | |
415 | buffer_free (&buffer); | |
416 | len_avail = 0; | |
417 | buf = NULL; | |
418 | buffer_init (&buffer); | |
419 | buffer_grow_str (&buffer, "<osdata type=\"threads\">\n"); | |
420 | ||
421 | dirp = opendir ("/proc"); | |
422 | if (dirp) | |
423 | { | |
424 | struct dirent *dp; | |
425 | ||
426 | while ((dp = readdir (dirp)) != NULL) | |
427 | { | |
428 | struct stat statbuf; | |
429 | char procentry[sizeof ("/proc/4294967295")]; | |
430 | ||
431 | if (!isdigit (dp->d_name[0]) | |
432 | || NAMELEN (dp) > sizeof ("4294967295") - 1) | |
433 | continue; | |
434 | ||
435 | sprintf (procentry, "/proc/%s", dp->d_name); | |
436 | if (stat (procentry, &statbuf) == 0 | |
437 | && S_ISDIR (statbuf.st_mode)) | |
438 | { | |
439 | DIR *dirp2; | |
440 | char *pathname; | |
441 | pid_t pid; | |
442 | char command[32]; | |
443 | ||
444 | pathname = xstrprintf ("/proc/%s/task", dp->d_name); | |
445 | ||
446 | pid = atoi (dp->d_name); | |
447 | command_from_pid (command, sizeof (command), pid); | |
448 | ||
449 | dirp2 = opendir (pathname); | |
450 | ||
451 | if (dirp2) | |
452 | { | |
453 | struct dirent *dp2; | |
454 | ||
455 | while ((dp2 = readdir (dirp2)) != NULL) | |
456 | { | |
457 | pid_t tid; | |
458 | int core; | |
459 | ||
460 | if (!isdigit (dp2->d_name[0]) | |
461 | || NAMELEN (dp2) > sizeof ("4294967295") - 1) | |
462 | continue; | |
463 | ||
464 | tid = atoi (dp2->d_name); | |
465 | core = linux_common_core_of_thread (ptid_build (pid, tid, 0)); | |
466 | ||
467 | buffer_xml_printf ( | |
468 | &buffer, | |
469 | "<item>" | |
470 | "<column name=\"pid\">%d</column>" | |
471 | "<column name=\"command\">%s</column>" | |
472 | "<column name=\"tid\">%d</column>" | |
473 | "<column name=\"core\">%d</column>" | |
474 | "</item>", | |
475 | pid, | |
476 | command, | |
477 | tid, | |
478 | core); | |
479 | } | |
480 | ||
481 | closedir (dirp2); | |
482 | } | |
483 | ||
484 | xfree (pathname); | |
485 | } | |
486 | } | |
487 | ||
488 | closedir (dirp); | |
489 | } | |
490 | ||
491 | buffer_grow_str0 (&buffer, "</osdata>\n"); | |
492 | buf = buffer_finish (&buffer); | |
493 | len_avail = strlen (buf); | |
494 | } | |
495 | ||
496 | if (offset >= len_avail) | |
497 | { | |
498 | /* Done. Get rid of the buffer. */ | |
499 | buffer_free (&buffer); | |
500 | buf = NULL; | |
501 | len_avail = 0; | |
502 | return 0; | |
503 | } | |
504 | ||
505 | if (len > len_avail - offset) | |
506 | len = len_avail - offset; | |
507 | memcpy (readbuf, buf + offset, len); | |
508 | ||
509 | return len; | |
510 | } | |
511 | ||
512 | struct osdata_type { | |
513 | char *type; | |
514 | char *description; | |
515 | LONGEST (*getter) (gdb_byte *readbuf, ULONGEST offset, LONGEST len); | |
516 | } osdata_table[] = { | |
517 | { "processes", "Listing of all processes", linux_xfer_osdata_processes }, | |
518 | { "threads", "Listing of all threads", linux_xfer_osdata_threads }, | |
519 | { NULL, NULL, NULL } | |
520 | }; | |
521 | ||
522 | LONGEST | |
523 | linux_common_xfer_osdata (const char *annex, gdb_byte *readbuf, | |
524 | ULONGEST offset, LONGEST len) | |
525 | { | |
526 | if (!annex || *annex == '\0') | |
527 | { | |
528 | static const char *buf; | |
529 | static LONGEST len_avail = -1; | |
530 | static struct buffer buffer; | |
531 | ||
532 | if (offset == 0) | |
533 | { | |
534 | int i; | |
535 | ||
536 | if (len_avail != -1 && len_avail != 0) | |
537 | buffer_free (&buffer); | |
538 | len_avail = 0; | |
539 | buf = NULL; | |
540 | buffer_init (&buffer); | |
541 | buffer_grow_str (&buffer, "<osdata type=\"types\">\n"); | |
542 | ||
543 | for (i = 0; osdata_table[i].type; ++i) | |
544 | buffer_xml_printf ( | |
545 | &buffer, | |
546 | "<item>" | |
547 | "<column name=\"Type\">%s</column>" | |
548 | "<column name=\"Description\">%s</column>" | |
549 | "</item>", | |
550 | osdata_table[i].type, | |
551 | osdata_table[i].description); | |
552 | ||
553 | buffer_grow_str0 (&buffer, "</osdata>\n"); | |
554 | buf = buffer_finish (&buffer); | |
555 | len_avail = strlen (buf); | |
556 | } | |
557 | ||
558 | if (offset >= len_avail) | |
559 | { | |
560 | /* Done. Get rid of the buffer. */ | |
561 | buffer_free (&buffer); | |
562 | buf = NULL; | |
563 | len_avail = 0; | |
564 | return 0; | |
565 | } | |
566 | ||
567 | if (len > len_avail - offset) | |
568 | len = len_avail - offset; | |
569 | memcpy (readbuf, buf + offset, len); | |
570 | ||
571 | return len; | |
572 | } | |
573 | else | |
574 | { | |
575 | int i; | |
576 | ||
577 | for (i = 0; osdata_table[i].type; ++i) | |
578 | { | |
579 | if (strcmp (annex, osdata_table[i].type) == 0) | |
580 | { | |
581 | gdb_assert (readbuf); | |
582 | ||
583 | return (osdata_table[i].getter) (readbuf, offset, len); | |
584 | } | |
585 | } | |
586 | ||
587 | return 0; | |
588 | } | |
589 | } | |
590 |