23 #include <sys/types.h>
24 #include <sys/sysinfo.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
41 #define NAMELEN(dirent) strlen ((dirent)->d_name)
54 #define MAX_PID_T_STRLEN (sizeof ("-9223372036854775808") - 1)
72 sprintf (filename,
"/proc/%lld/task/%lld/stat",
81 content =
xrealloc (content, content_read + 1024);
82 n = fread (content + content_read, 1, 1024, f);
86 content[content_read] =
'\0';
92 p = strrchr (content,
')');
99 p = strtok_r (p,
" ", &ts);
100 for (i = 0; p != NULL && i != 36; ++i)
101 p = strtok_r (NULL,
" ", &ts);
103 if (p == NULL || sscanf (p,
"%d", &core) == 0)
119 char *stat_path =
xstrprintf (
"/proc/%lld/stat", pid);
131 int items_read = fscanf (fp,
"%lld %17s", &stat_pid, cmd);
133 if (items_read == 2 && pid == stat_pid)
135 cmd[strlen (cmd) - 1] =
'\0';
136 strncpy (command, cmd + 1, maxlen);
144 snprintf (command, maxlen,
"%lld", pid);
147 command[maxlen - 1] =
'\0';
158 char *pathname =
xstrprintf (
"/proc/%lld/cmdline", pid);
159 char *commandline = NULL;
169 size_t read_bytes = fread (buf, 1,
sizeof (buf), f);
173 commandline = (
char *)
xrealloc (commandline, len + read_bytes + 1);
174 memcpy (commandline + len, buf, read_bytes);
186 for (i = 0; i <
len; ++i)
187 if (commandline[i] ==
'\0')
188 commandline[i] =
' ';
190 commandline[
len] =
'\0';
196 commandline = (
char *)
xmalloc (32);
197 commandline[0] =
'[';
200 len = strlen (commandline);
202 strcat (commandline,
"]");
217 struct passwd *pwentry = getpwuid (uid);
221 strncpy (user, pwentry->pw_name, maxlen);
223 user[maxlen - 1] =
'\0';
238 sprintf (procentry,
"/proc/%lld", pid);
240 if (stat (procentry, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
242 *owner = statbuf.st_uid;
260 sprintf (taskdir,
"/proc/%lld/task", pid);
261 dir = opendir (taskdir);
264 while ((dp = readdir (dir)) != NULL)
269 if (!isdigit (dp->d_name[0])
273 sscanf (dp->d_name,
"%lld", &tid);
277 if (core >= 0 && core < num_cores)
295 static const char *buf;
297 static struct buffer buffer;
303 if (len_avail != -1 && len_avail != 0)
310 dirp = opendir (
"/proc");
313 const int num_cores = sysconf (_SC_NPROCESSORS_ONLN);
316 while ((dp = readdir (dirp)) != NULL)
320 char user[UT_NAMESIZE];
327 if (!isdigit (dp->d_name[0])
331 sscanf (dp->d_name,
"%lld", &pid);
340 cores = (
int *)
xcalloc (num_cores,
sizeof (
int));
342 cores_str = (
char *)
xcalloc (task_count,
sizeof (
"4294967295") + 1);
344 for (i = 0; i < num_cores && task_count > 0; ++i)
347 char core_str[
sizeof (
"4294967295")];
349 sprintf (core_str,
"%d", i);
350 strcat (cores_str, core_str);
352 task_count -= cores[i];
354 strcat (cores_str,
",");
362 "<column name=\"pid\">%lld</column>"
363 "<column name=\"user\">%s</column>"
364 "<column name=\"command\">%s</column>"
365 "<column name=\"cores\">%s</column>"
369 command_line ? command_line :
"",
372 xfree (command_line);
381 len_avail = strlen (buf);
384 if (offset >= len_avail)
393 if (len > len_avail - offset)
395 memcpy (readbuf, buf + offset, len);
418 else if (pgid1 > pgid2)
425 else if (pid2 == pgid2)
427 else if (pid1 < pid2)
429 else if (pid1 > pid2)
443 static const char *buf;
445 static struct buffer buffer;
451 if (len_avail != -1 && len_avail != 0)
458 dirp = opendir (
"/proc");
462 const size_t list_block_size = 512;
464 size_t process_count = 0;
469 while ((dp = readdir (dirp)) != NULL)
473 if (!isdigit (dp->d_name[0])
477 sscanf (dp->d_name,
"%lld", &pid);
478 pgid = getpgid (pid);
482 process_list[2 * process_count] =
pid;
483 process_list[2 * process_count + 1] = pgid;
487 if (process_count % list_block_size == 0)
490 (process_count + list_block_size)
491 * 2 *
sizeof (
PID_T));
498 qsort (process_list, process_count, 2 *
sizeof (
PID_T),
501 for (i = 0; i < process_count; ++i)
504 PID_T pgid = process_list[2 * i + 1];
505 char leader_command[32];
514 "<column name=\"pgid\">%lld</column>"
515 "<column name=\"leader command\">%s</column>"
516 "<column name=\"pid\">%lld</column>"
517 "<column name=\"command line\">%s</column>"
522 command_line ? command_line :
"");
524 xfree (command_line);
527 xfree (process_list);
532 len_avail = strlen (buf);
535 if (offset >= len_avail)
544 if (len > len_avail - offset)
546 memcpy (readbuf, buf + offset, len);
559 static const char *buf;
561 static struct buffer buffer;
567 if (len_avail != -1 && len_avail != 0)
574 dirp = opendir (
"/proc");
579 while ((dp = readdir (dirp)) != NULL)
582 char procentry[
sizeof (
"/proc/4294967295")];
584 if (!isdigit (dp->d_name[0])
585 ||
NAMELEN (dp) >
sizeof (
"4294967295") - 1)
588 sprintf (procentry,
"/proc/%s", dp->d_name);
589 if (stat (procentry, &statbuf) == 0
590 && S_ISDIR (statbuf.st_mode))
597 pathname =
xstrprintf (
"/proc/%s/task", dp->d_name);
599 pid = atoi (dp->d_name);
602 dirp2 = opendir (pathname);
608 while ((dp2 = readdir (dirp2)) != NULL)
613 if (!isdigit (dp2->d_name[0])
614 ||
NAMELEN (dp2) >
sizeof (
"4294967295") - 1)
617 tid = atoi (dp2->d_name);
623 "<column name=\"pid\">%lld</column>"
624 "<column name=\"command\">%s</column>"
625 "<column name=\"tid\">%lld</column>"
626 "<column name=\"core\">%d</column>"
646 len_avail = strlen (buf);
649 if (offset >= len_avail)
658 if (len > len_avail - offset)
660 memcpy (readbuf, buf + offset, len);
671 static const char *buf;
673 static struct buffer buffer;
680 if (len_avail != -1 && len_avail != 0)
694 if (fgets (buf,
sizeof (buf), fp))
699 key = strtok (buf,
":");
703 value = strtok (NULL,
":");
707 while (key[i] !=
'\t' && key[i] !=
'\0')
713 while (value[i] !=
'\t' && value[i] !=
'\0')
718 if (strcmp (key,
"processor") == 0)
729 "<column name=\"%s\">%s</column>",
744 len_avail = strlen (buf);
747 if (offset >= len_avail)
756 if (len > len_avail - offset)
758 memcpy (readbuf, buf + offset, len);
771 static const char *buf;
773 static struct buffer buffer;
779 if (len_avail != -1 && len_avail != 0)
786 dirp = opendir (
"/proc");
791 while ((dp = readdir (dirp)) != NULL)
794 char procentry[
sizeof (
"/proc/4294967295")];
796 if (!isdigit (dp->d_name[0])
797 ||
NAMELEN (dp) >
sizeof (
"4294967295") - 1)
800 sprintf (procentry,
"/proc/%s", dp->d_name);
801 if (stat (procentry, &statbuf) == 0
802 && S_ISDIR (statbuf.st_mode))
809 pid = atoi (dp->d_name);
812 pathname =
xstrprintf (
"/proc/%s/fd", dp->d_name);
813 dirp2 = opendir (pathname);
819 while ((dp2 = readdir (dirp2)) != NULL)
825 if (!isdigit (dp2->d_name[0]))
828 fdname =
xstrprintf (
"%s/%s", pathname, dp2->d_name);
829 rslt = readlink (fdname, buf,
sizeof (buf) - 1);
836 "<column name=\"pid\">%s</column>"
837 "<column name=\"command\">%s</column>"
838 "<column name=\"file descriptor\">%s</column>"
839 "<column name=\"name\">%s</column>"
844 (rslt >= 0 ? buf : dp2->d_name));
859 len_avail = strlen (buf);
862 if (offset >= len_avail)
871 if (len > len_avail - offset)
873 memcpy (readbuf, buf + offset, len);
900 case TCP_ESTABLISHED:
901 return "ESTABLISHED";
942 const char *proc_file;
945 if (family == AF_INET)
946 proc_file = tcp ?
"/proc/net/tcp" :
"/proc/net/udp";
947 else if (family == AF_INET6)
948 proc_file = tcp ?
"/proc/net/tcp6" :
"/proc/net/udp6";
959 if (fgets (buf,
sizeof (buf), fp))
962 unsigned int local_port, remote_port, state;
963 char local_address[NI_MAXHOST], remote_address[NI_MAXHOST];
967 #error "local_address and remote_address buffers too small"
970 result = sscanf (buf,
971 "%*d: %32[0-9A-F]:%X %32[0-9A-F]:%X %X %*X:%*X %*X:%*X %*X %d %*d %*u %*s\n",
972 local_address, &local_port,
973 remote_address, &remote_port,
981 char user[UT_NAMESIZE];
982 char local_service[NI_MAXSERV], remote_service[NI_MAXSERV];
984 if (family == AF_INET)
986 sscanf (local_address,
"%X",
987 &locaddr.
sin.sin_addr.s_addr);
988 sscanf (remote_address,
"%X",
989 &remaddr.
sin.sin_addr.s_addr);
991 locaddr.
sin.sin_port = htons (local_port);
992 remaddr.
sin.sin_port = htons (remote_port);
994 addr_size =
sizeof (
struct sockaddr_in);
998 sscanf (local_address,
"%8X%8X%8X%8X",
999 locaddr.
sin6.sin6_addr.s6_addr32,
1000 locaddr.
sin6.sin6_addr.s6_addr32 + 1,
1001 locaddr.
sin6.sin6_addr.s6_addr32 + 2,
1002 locaddr.
sin6.sin6_addr.s6_addr32 + 3);
1003 sscanf (remote_address,
"%8X%8X%8X%8X",
1004 remaddr.
sin6.sin6_addr.s6_addr32,
1005 remaddr.
sin6.sin6_addr.s6_addr32 + 1,
1006 remaddr.
sin6.sin6_addr.s6_addr32 + 2,
1007 remaddr.
sin6.sin6_addr.s6_addr32 + 3);
1009 locaddr.
sin6.sin6_port = htons (local_port);
1010 remaddr.
sin6.sin6_port = htons (remote_port);
1012 locaddr.
sin6.sin6_flowinfo = 0;
1013 remaddr.
sin6.sin6_flowinfo = 0;
1014 locaddr.
sin6.sin6_scope_id = 0;
1015 remaddr.
sin6.sin6_scope_id = 0;
1017 addr_size =
sizeof (
struct sockaddr_in6);
1020 locaddr.
sa.sa_family = remaddr.
sa.sa_family = family;
1022 result = getnameinfo (&locaddr.
sa, addr_size,
1023 local_address, sizeof (local_address),
1024 local_service,
sizeof (local_service),
1025 NI_NUMERICHOST | NI_NUMERICSERV
1026 | (tcp ? 0 : NI_DGRAM));
1030 result = getnameinfo (&remaddr.
sa, addr_size,
1032 sizeof (remote_address),
1034 sizeof (remote_service),
1035 NI_NUMERICHOST | NI_NUMERICSERV
1036 | (tcp ? 0 : NI_DGRAM));
1045 "<column name=\"local address\">%s</column>"
1046 "<column name=\"local port\">%s</column>"
1047 "<column name=\"remote address\">%s</column>"
1048 "<column name=\"remote port\">%s</column>"
1049 "<column name=\"state\">%s</column>"
1050 "<column name=\"user\">%s</column>"
1051 "<column name=\"family\">%s</column>"
1052 "<column name=\"protocol\">%s</column>"
1060 (family == AF_INET) ?
"INET" :
"INET6",
1061 tcp ?
"STREAM" :
"DGRAM");
1077 static const char *buf;
1078 static LONGEST len_avail = -1;
1079 static struct buffer buffer;
1083 if (len_avail != -1 && len_avail != 0)
1097 len_avail = strlen (buf);
1100 if (offset >= len_avail)
1109 if (len > len_avail - offset)
1110 len = len_avail -
offset;
1111 memcpy (readbuf, buf + offset, len);
1126 time_t t = (time_t) seconds;
1128 strncpy (time, ctime (&t), maxlen);
1129 time[maxlen - 1] =
'\0';
1139 struct group *grentry = getgrgid (gid);
1143 strncpy (group, grentry->gr_name, maxlen);
1145 group[maxlen - 1] =
'\0';
1158 static const char *buf;
1159 static LONGEST len_avail = -1;
1160 static struct buffer buffer;
1166 if (len_avail != -1 && len_avail != 0)
1180 if (fgets (buf,
sizeof (buf), fp))
1186 int shmid,
size, nattch;
1187 TIME_T atime, dtime, ctime;
1191 items_read = sscanf (buf,
1192 "%d %d %o %d %lld %lld %d %u %u %u %u %lld %lld %lld",
1193 &key, &shmid, &perms, &size,
1196 &uid, &gid, &cuid, &cgid,
1197 &atime, &dtime, &ctime);
1199 if (items_read == 14)
1201 char user[UT_NAMESIZE], group[UT_NAMESIZE];
1202 char cuser[UT_NAMESIZE], cgroup[UT_NAMESIZE];
1203 char ccmd[32], lcmd[32];
1204 char atime_str[32], dtime_str[32], ctime_str[32];
1221 "<column name=\"key\">%d</column>"
1222 "<column name=\"shmid\">%d</column>"
1223 "<column name=\"permissions\">%o</column>"
1224 "<column name=\"size\">%d</column>"
1225 "<column name=\"creator command\">%s</column>"
1226 "<column name=\"last op. command\">%s</column>"
1227 "<column name=\"num attached\">%d</column>"
1228 "<column name=\"user\">%s</column>"
1229 "<column name=\"group\">%s</column>"
1230 "<column name=\"creator user\">%s</column>"
1231 "<column name=\"creator group\">%s</column>"
1232 "<column name=\"last shmat() time\">%s</column>"
1233 "<column name=\"last shmdt() time\">%s</column>"
1234 "<column name=\"last shmctl() time\">%s</column>"
1260 len_avail = strlen (buf);
1263 if (offset >= len_avail)
1272 if (len > len_avail - offset)
1273 len = len_avail -
offset;
1274 memcpy (readbuf, buf + offset, len);
1286 static const char *buf;
1287 static LONGEST len_avail = -1;
1288 static struct buffer buffer;
1294 if (len_avail != -1 && len_avail != 0)
1308 if (fgets (buf,
sizeof (buf), fp))
1313 unsigned int perms, nsems;
1318 items_read = sscanf (buf,
1319 "%d %d %o %u %d %d %d %d %lld %lld",
1320 &key, &semid, &perms, &nsems,
1321 &uid, &gid, &cuid, &cgid,
1324 if (items_read == 10)
1326 char user[UT_NAMESIZE], group[UT_NAMESIZE];
1327 char cuser[UT_NAMESIZE], cgroup[UT_NAMESIZE];
1328 char otime_str[32], ctime_str[32];
1341 "<column name=\"key\">%d</column>"
1342 "<column name=\"semid\">%d</column>"
1343 "<column name=\"permissions\">%o</column>"
1344 "<column name=\"num semaphores\">%u</column>"
1345 "<column name=\"user\">%s</column>"
1346 "<column name=\"group\">%s</column>"
1347 "<column name=\"creator user\">%s</column>"
1348 "<column name=\"creator group\">%s</column>"
1349 "<column name=\"last semop() time\">%s</column>"
1350 "<column name=\"last semctl() time\">%s</column>"
1372 len_avail = strlen (buf);
1375 if (offset >= len_avail)
1384 if (len > len_avail - offset)
1385 len = len_avail -
offset;
1386 memcpy (readbuf, buf + offset, len);
1398 static const char *buf;
1399 static LONGEST len_avail = -1;
1400 static struct buffer buffer;
1406 if (len_avail != -1 && len_avail != 0)
1420 if (fgets (buf,
sizeof (buf), fp))
1426 unsigned int perms, cbytes, qnum;
1428 TIME_T stime, rtime, ctime;
1431 items_read = sscanf (buf,
1432 "%d %d %o %u %u %lld %lld %d %d %d %d %lld %lld %lld",
1433 &key, &msqid, &perms, &cbytes, &qnum,
1434 &lspid, &lrpid, &uid, &gid, &cuid, &cgid,
1435 &stime, &rtime, &ctime);
1437 if (items_read == 14)
1439 char user[UT_NAMESIZE], group[UT_NAMESIZE];
1440 char cuser[UT_NAMESIZE], cgroup[UT_NAMESIZE];
1441 char lscmd[32], lrcmd[32];
1442 char stime_str[32], rtime_str[32], ctime_str[32];
1459 "<column name=\"key\">%d</column>"
1460 "<column name=\"msqid\">%d</column>"
1461 "<column name=\"permissions\">%o</column>"
1462 "<column name=\"num used bytes\">%u</column>"
1463 "<column name=\"num messages\">%u</column>"
1464 "<column name=\"last msgsnd() command\">%s</column>"
1465 "<column name=\"last msgrcv() command\">%s</column>"
1466 "<column name=\"user\">%s</column>"
1467 "<column name=\"group\">%s</column>"
1468 "<column name=\"creator user\">%s</column>"
1469 "<column name=\"creator group\">%s</column>"
1470 "<column name=\"last msgsnd() time\">%s</column>"
1471 "<column name=\"last msgrcv() time\">%s</column>"
1472 "<column name=\"last msgctl() time\">%s</column>"
1498 len_avail = strlen (buf);
1501 if (offset >= len_avail)
1510 if (len > len_avail - offset)
1511 len = len_avail -
offset;
1512 memcpy (readbuf, buf + offset, len);
1524 static const char *buf;
1525 static LONGEST len_avail = -1;
1526 static struct buffer buffer;
1532 if (len_avail != -1 && len_avail != 0)
1546 if (fgets (buf,
sizeof (buf), fp))
1550 unsigned long long address;
1553 name = strtok (buf,
" ");
1557 tmp = strtok (NULL,
" ");
1560 if (sscanf (tmp,
"%u", &size) != 1)
1563 tmp = strtok (NULL,
" ");
1566 if (sscanf (tmp,
"%d", &uses) != 1)
1569 dependencies = strtok (NULL,
" ");
1570 if (dependencies == NULL)
1573 status = strtok (NULL,
" ");
1577 tmp = strtok (NULL,
"\n");
1580 if (sscanf (tmp,
"%llx", &address) != 1)
1586 "<column name=\"name\">%s</column>"
1587 "<column name=\"size\">%u</column>"
1588 "<column name=\"num uses\">%d</column>"
1589 "<column name=\"dependencies\">%s</column>"
1590 "<column name=\"status\">%s</column>"
1591 "<column name=\"address\">%llx</column>"
1608 len_avail = strlen (buf);
1611 if (offset >= len_avail)
1620 if (len > len_avail - offset)
1621 len = len_avail -
offset;
1622 memcpy (readbuf, buf + offset, len);
1633 {
"cpus",
"CPUs",
"Listing of all cpus/cores on the system",
1635 {
"files",
"File descriptors",
"Listing of all file descriptors",
1637 {
"modules",
"Kernel modules",
"Listing of all loaded kernel modules",
1639 {
"msg",
"Message queues",
"Listing of all message queues",
1641 {
"processes",
"Processes",
"Listing of all processes",
1643 {
"procgroups",
"Process groups",
"Listing of all process groups",
1645 {
"semaphores",
"Semaphores",
"Listing of all semaphores",
1647 {
"shm",
"Shared-memory regions",
"Listing of all shared-memory regions",
1649 {
"sockets",
"Sockets",
"Listing of all internet-domain sockets",
1651 {
"threads",
"Threads",
"Listing of all threads",
1653 { NULL, NULL, NULL }
1660 if (!annex || *annex ==
'\0')
1662 static const char *buf;
1663 static LONGEST len_avail = -1;
1664 static struct buffer buffer;
1670 if (len_avail != -1 && len_avail != 0)
1681 "<column name=\"Type\">%s</column>"
1682 "<column name=\"Description\">%s</column>"
1683 "<column name=\"Title\">%s</column>"
1691 len_avail = strlen (buf);
1694 if (offset >= len_avail)
1703 if (len > len_avail - offset)
1704 len = len_avail -
offset;
1705 memcpy (readbuf, buf + offset, len);
static int compare_processes(const void *process1, const void *process2)
static void group_from_gid(char *group, int maxlen, gid_t gid)
static LONGEST linux_xfer_osdata_modules(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
static LONGEST linux_xfer_osdata_isockets(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
static LONGEST linux_xfer_osdata_shm(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
static LONGEST linux_xfer_osdata_fds(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
void buffer_free(struct buffer *buffer)
static LONGEST linux_xfer_osdata_cpus(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
static void user_from_uid(char *user, int maxlen, uid_t uid)
static LONGEST linux_xfer_osdata_msg(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
FILE * gdb_fopen_cloexec(const char *filename, const char *opentype)
ptid_t ptid_build(int pid, long lwp, long tid)
static LONGEST linux_xfer_osdata_processgroups(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
struct osdata_type osdata_table[]
static void print_sockets(unsigned short family, int tcp, struct buffer *buffer)
void buffer_xml_printf(struct buffer *buffer, const char *format,...)
static int get_process_owner(uid_t *owner, PID_T pid)
static void command_from_pid(char *command, int maxlen, PID_T pid)
mach_port_t mach_port_t name mach_port_t mach_port_t name error_t int status
LONGEST(* getter)(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
int linux_common_core_of_thread(ptid_t ptid)
char * xstrprintf(const char *format,...)
#define buffer_grow_str(BUFFER, STRING)
static char * commandline_from_pid(PID_T pid)
static LONGEST linux_xfer_osdata_processes(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
LONGEST linux_common_xfer_osdata(const char *annex, gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
int ptid_get_pid(ptid_t ptid)
PTR xrealloc(PTR ptr, size_t size)
#define buffer_grow_str0(BUFFER, STRING)
void buffer_init(struct buffer *buffer)
static void time_from_time_t(char *time, int maxlen, TIME_T seconds)
unsigned long long ULONGEST
long ptid_get_lwp(ptid_t ptid)
static LONGEST linux_xfer_osdata_threads(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
static LONGEST linux_xfer_osdata_sem(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
static int get_cores_used_by_process(PID_T pid, int *cores, const int num_cores)
PTR xcalloc(size_t number, size_t size)
char * buffer_finish(struct buffer *buffer)
mach_port_t mach_port_t name mach_port_t mach_port_t name error_t int int rusage_t pid_t pid
static const char * format_socket_state(unsigned char state)
const ULONGEST const LONGEST len