Move dwarf2_frame_state_reg.exp_len to union .loc
[deliverable/binutils-gdb.git] / gdb / nat / linux-osdata.c
index 0ef7fa368c8ba852f20d42c0ba4a099d5d79c420..4b40a4db0e07de94e5376bbbc342712645d5515e 100644 (file)
@@ -1,6 +1,6 @@
 /* Linux-specific functions to retrieve OS data.
    
-   Copyright (C) 2009-2014 Free Software Foundation, Inc.
+   Copyright (C) 2009-2017 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-#ifdef GDBSERVER
-#include "server.h"
-#else
-#include "defs.h"
-#endif
-
+#include "common-defs.h"
 #include "linux-osdata.h"
 
 #include <sys/types.h>
 #include <sys/sysinfo.h>
 #include <ctype.h>
-#include <string.h>
 #include <utmp.h>
 #include <time.h>
 #include <unistd.h>
@@ -67,7 +61,6 @@ int
 linux_common_core_of_thread (ptid_t ptid)
 {
   char filename[sizeof ("/proc//task//stat") + 2 * MAX_PID_T_STRLEN];
-  FILE *f;
   char *content = NULL;
   char *p;
   char *ts = 0;
@@ -77,15 +70,15 @@ linux_common_core_of_thread (ptid_t ptid)
 
   sprintf (filename, "/proc/%lld/task/%lld/stat",
           (PID_T) ptid_get_pid (ptid), (PID_T) ptid_get_lwp (ptid));
-  f = gdb_fopen_cloexec (filename, "r");
+  gdb_file_up f = gdb_fopen_cloexec (filename, "r");
   if (!f)
     return -1;
 
   for (;;)
     {
       int n;
-      content = xrealloc (content, content_read + 1024);
-      n = fread (content + content_read, 1, 1024, f);
+      content = (char *) xrealloc (content, content_read + 1024);
+      n = fread (content + content_read, 1, 1024, f.get ());
       content_read += n;
       if (n < 1024)
        {
@@ -110,7 +103,6 @@ linux_common_core_of_thread (ptid_t ptid)
     core = -1;
 
   xfree (content);
-  fclose (f);
 
   return core;
 }
@@ -123,7 +115,7 @@ static void
 command_from_pid (char *command, int maxlen, PID_T pid)
 {
   char *stat_path = xstrprintf ("/proc/%lld/stat", pid); 
-  FILE *fp = gdb_fopen_cloexec (stat_path, "r");
+  gdb_file_up fp = gdb_fopen_cloexec (stat_path, "r");
   
   command[0] = '\0';
  
@@ -134,15 +126,13 @@ command_from_pid (char *command, int maxlen, PID_T pid)
         (for the brackets).  */
       char cmd[18];
       PID_T stat_pid;
-      int items_read = fscanf (fp, "%lld %17s", &stat_pid, cmd);
+      int items_read = fscanf (fp.get (), "%lld %17s", &stat_pid, cmd);
          
       if (items_read == 2 && pid == stat_pid)
        {
          cmd[strlen (cmd) - 1] = '\0'; /* Remove trailing parenthesis.  */
          strncpy (command, cmd + 1, maxlen); /* Ignore leading parenthesis.  */
        }
-
-      fclose (fp);
     }
   else
     {
@@ -163,16 +153,16 @@ commandline_from_pid (PID_T pid)
 {
   char *pathname = xstrprintf ("/proc/%lld/cmdline", pid);
   char *commandline = NULL;
-  FILE *f = gdb_fopen_cloexec (pathname, "r");
+  gdb_file_up f = gdb_fopen_cloexec (pathname, "r");
 
   if (f)
     {
       size_t len = 0;
 
-      while (!feof (f))
+      while (!feof (f.get ()))
        {
          char buf[1024];
-         size_t read_bytes = fread (buf, 1, sizeof (buf), f);
+         size_t read_bytes = fread (buf, 1, sizeof (buf), f.get ());
      
          if (read_bytes)
            {
@@ -182,8 +172,6 @@ commandline_from_pid (PID_T pid)
            }
        }
 
-      fclose (f);
-
       if (commandline)
        {
          size_t i;
@@ -343,7 +331,7 @@ linux_xfer_osdata_processes (gdb_byte *readbuf,
                strcpy (user, "?");
 
              /* Find CPU cores used by the process.  */
-             cores = (int *) xcalloc (num_cores, sizeof (int));
+             cores = XCNEWVEC (int, num_cores);
              task_count = get_cores_used_by_process (pid, cores, num_cores);
              cores_str = (char *) xcalloc (task_count, sizeof ("4294967295") + 1);
 
@@ -466,7 +454,7 @@ linux_xfer_osdata_processgroups (gdb_byte *readbuf,
        {
          struct dirent *dp;
          const size_t list_block_size = 512;
-         PID_T *process_list = (PID_T *) xmalloc (list_block_size * 2 * sizeof (PID_T));
+         PID_T *process_list = XNEWVEC (PID_T, list_block_size * 2);
          size_t process_count = 0;
          size_t i;
 
@@ -591,7 +579,8 @@ linux_xfer_osdata_threads (gdb_byte *readbuf,
                  || NAMELEN (dp) > sizeof ("4294967295") - 1)
                continue;
 
-             sprintf (procentry, "/proc/%s", dp->d_name);
+             xsnprintf (procentry, sizeof (procentry), "/proc/%s",
+                        dp->d_name);
              if (stat (procentry, &statbuf) == 0
                  && S_ISDIR (statbuf.st_mode))
                {
@@ -668,6 +657,101 @@ linux_xfer_osdata_threads (gdb_byte *readbuf,
   return len;
 }
 
+/* Collect data about the cpus/cores on the system */
+
+static LONGEST
+linux_xfer_osdata_cpus (gdb_byte *readbuf,
+                          ULONGEST offset, ULONGEST len)
+{
+  static const char *buf;
+  static LONGEST len_avail = -1;
+  static struct buffer buffer;
+
+  if (offset == 0)
+    {
+      int first_item = 1;
+
+      if (len_avail != -1 && len_avail != 0)
+       buffer_free (&buffer);
+      len_avail = 0;
+      buf = NULL;
+      buffer_init (&buffer);
+      buffer_grow_str (&buffer, "<osdata type=\"cpus\">\n");
+
+      gdb_file_up fp = gdb_fopen_cloexec ("/proc/cpuinfo", "r");
+      if (fp != NULL)
+       {
+         char buf[8192];
+
+         do
+           {
+             if (fgets (buf, sizeof (buf), fp.get ()))
+               {
+                 char *key, *value;
+                 int i = 0;
+
+                 key = strtok (buf, ":");
+                 if (key == NULL)
+                   continue;
+
+                 value = strtok (NULL, ":");
+                 if (value == NULL)
+                   continue;
+
+                 while (key[i] != '\t' && key[i] != '\0')
+                   i++;
+
+                 key[i] = '\0';
+
+                 i = 0;
+                 while (value[i] != '\t' && value[i] != '\0')
+                   i++;
+
+                 value[i] = '\0';
+
+                 if (strcmp (key, "processor") == 0)
+                   {
+                     if (first_item)
+                       buffer_grow_str (&buffer, "<item>");
+                     else
+                       buffer_grow_str (&buffer, "</item><item>");
+
+                     first_item = 0;
+                   }
+
+                 buffer_xml_printf (&buffer,
+                                    "<column name=\"%s\">%s</column>",
+                                    key,
+                                    value);
+               }
+           }
+         while (!feof (fp.get ()));
+
+         if (first_item == 0)
+           buffer_grow_str (&buffer, "</item>");
+       }
+
+      buffer_grow_str0 (&buffer, "</osdata>\n");
+      buf = buffer_finish (&buffer);
+      len_avail = strlen (buf);
+    }
+
+  if (offset >= len_avail)
+    {
+      /* Done.  Get rid of the buffer.  */
+      buffer_free (&buffer);
+      buf = NULL;
+      len_avail = 0;
+      return 0;
+    }
+
+  if (len > len_avail - offset)
+    len = len_avail - offset;
+  memcpy (readbuf, buf + offset, len);
+
+  return len;
+}
+
 /* Collect all the open file descriptors found in /proc and put the details
    found about them into READBUF.  */
 
@@ -705,7 +789,8 @@ linux_xfer_osdata_fds (gdb_byte *readbuf,
                  || NAMELEN (dp) > sizeof ("4294967295") - 1)
                continue;
 
-             sprintf (procentry, "/proc/%s", dp->d_name);
+             xsnprintf (procentry, sizeof (procentry), "/proc/%s",
+                        dp->d_name);
              if (stat (procentry, &statbuf) == 0
                  && S_ISDIR (statbuf.st_mode))
                {
@@ -848,7 +933,6 @@ static void
 print_sockets (unsigned short family, int tcp, struct buffer *buffer)
 {
   const char *proc_file;
-  FILE *fp;
 
   if (family == AF_INET)
     proc_file = tcp ? "/proc/net/tcp" : "/proc/net/udp";
@@ -857,14 +941,14 @@ print_sockets (unsigned short family, int tcp, struct buffer *buffer)
   else
     return;
 
-  fp = gdb_fopen_cloexec (proc_file, "r");
+  gdb_file_up fp = gdb_fopen_cloexec (proc_file, "r");
   if (fp)
     {
       char buf[8192];
 
       do
        {
-         if (fgets (buf, sizeof (buf), fp))
+         if (fgets (buf, sizeof (buf), fp.get ()))
            {
              uid_t uid;
              unsigned int local_port, remote_port, state;
@@ -970,9 +1054,7 @@ print_sockets (unsigned short family, int tcp, struct buffer *buffer)
                }
            }
        }
-      while (!feof (fp));
-
-      fclose (fp);
+      while (!feof (fp.get ()));
     }
 }
 
@@ -1069,8 +1151,6 @@ linux_xfer_osdata_shm (gdb_byte *readbuf,
 
   if (offset == 0)
     {
-      FILE *fp;
-
       if (len_avail != -1 && len_avail != 0)
        buffer_free (&buffer);
       len_avail = 0;
@@ -1078,14 +1158,14 @@ linux_xfer_osdata_shm (gdb_byte *readbuf,
       buffer_init (&buffer);
       buffer_grow_str (&buffer, "<osdata type=\"shared memory\">\n");
 
-      fp = gdb_fopen_cloexec ("/proc/sysvipc/shm", "r");
+      gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/shm", "r");
       if (fp)
        {
          char buf[8192];
 
          do
            {
-             if (fgets (buf, sizeof (buf), fp))
+             if (fgets (buf, sizeof (buf), fp.get ()))
                {
                  key_t key;
                  uid_t uid, cuid;
@@ -1158,9 +1238,7 @@ linux_xfer_osdata_shm (gdb_byte *readbuf,
                    }
                }
            }
-         while (!feof (fp));
-
-         fclose (fp);
+         while (!feof (fp.get ()));
        }
       
       buffer_grow_str0 (&buffer, "</osdata>\n");
@@ -1197,8 +1275,6 @@ linux_xfer_osdata_sem (gdb_byte *readbuf,
 
   if (offset == 0)
     {
-      FILE *fp;
-      
       if (len_avail != -1 && len_avail != 0)
        buffer_free (&buffer);
       len_avail = 0;
@@ -1206,14 +1282,14 @@ linux_xfer_osdata_sem (gdb_byte *readbuf,
       buffer_init (&buffer);
       buffer_grow_str (&buffer, "<osdata type=\"semaphores\">\n");
 
-      fp = gdb_fopen_cloexec ("/proc/sysvipc/sem", "r");
+      gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/sem", "r");
       if (fp)
        {
          char buf[8192];
          
          do
            {
-             if (fgets (buf, sizeof (buf), fp))
+             if (fgets (buf, sizeof (buf), fp.get ()))
                {
                  key_t key;
                  uid_t uid, cuid;
@@ -1270,9 +1346,7 @@ linux_xfer_osdata_sem (gdb_byte *readbuf,
                    }
                }
            }
-         while (!feof (fp));
-
-         fclose (fp);
+         while (!feof (fp.get ()));
        }
 
       buffer_grow_str0 (&buffer, "</osdata>\n");
@@ -1309,8 +1383,6 @@ linux_xfer_osdata_msg (gdb_byte *readbuf,
 
   if (offset == 0)
     {
-      FILE *fp;
-      
       if (len_avail != -1 && len_avail != 0)
        buffer_free (&buffer);
       len_avail = 0;
@@ -1318,14 +1390,14 @@ linux_xfer_osdata_msg (gdb_byte *readbuf,
       buffer_init (&buffer);
       buffer_grow_str (&buffer, "<osdata type=\"message queues\">\n");
       
-      fp = gdb_fopen_cloexec ("/proc/sysvipc/msg", "r");
+      gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/msg", "r");
       if (fp)
        {
          char buf[8192];
          
          do
            {
-             if (fgets (buf, sizeof (buf), fp))
+             if (fgets (buf, sizeof (buf), fp.get ()))
                {
                  key_t key;
                  PID_T lspid, lrpid;
@@ -1396,9 +1468,7 @@ linux_xfer_osdata_msg (gdb_byte *readbuf,
                    }
                }
            }
-         while (!feof (fp));
-
-         fclose (fp);
+         while (!feof (fp.get ()));
        }
 
       buffer_grow_str0 (&buffer, "</osdata>\n");
@@ -1435,8 +1505,6 @@ linux_xfer_osdata_modules (gdb_byte *readbuf,
 
   if (offset == 0)
     {
-      FILE *fp;
-
       if (len_avail != -1 && len_avail != 0)
        buffer_free (&buffer);
       len_avail = 0;
@@ -1444,14 +1512,14 @@ linux_xfer_osdata_modules (gdb_byte *readbuf,
       buffer_init (&buffer);
       buffer_grow_str (&buffer, "<osdata type=\"modules\">\n");
 
-      fp = gdb_fopen_cloexec ("/proc/modules", "r");
+      gdb_file_up fp = gdb_fopen_cloexec ("/proc/modules", "r");
       if (fp)
        {
          char buf[8192];
          
          do
            {
-             if (fgets (buf, sizeof (buf), fp))
+             if (fgets (buf, sizeof (buf), fp.get ()))
                {
                  char *name, *dependencies, *status, *tmp;
                  unsigned int size;
@@ -1506,9 +1574,7 @@ linux_xfer_osdata_modules (gdb_byte *readbuf,
                        address);
                }
            }
-         while (!feof (fp));
-
-         fclose (fp);
+         while (!feof (fp.get ()));
        }
 
       buffer_grow_str0 (&buffer, "</osdata>\n");
@@ -1533,29 +1599,31 @@ linux_xfer_osdata_modules (gdb_byte *readbuf,
 }
 
 struct osdata_type {
-  char *type;
-  char *title;
-  char *description;
+  const char *type;
+  const char *title;
+  const char *description;
   LONGEST (*getter) (gdb_byte *readbuf, ULONGEST offset, ULONGEST len);
 } osdata_table[] = {
+  { "cpus", "CPUs", "Listing of all cpus/cores on the system",
+    linux_xfer_osdata_cpus },
+  { "files", "File descriptors", "Listing of all file descriptors",
+    linux_xfer_osdata_fds },
+  { "modules", "Kernel modules", "Listing of all loaded kernel modules",
+    linux_xfer_osdata_modules },
+  { "msg", "Message queues", "Listing of all message queues",
+    linux_xfer_osdata_msg },
   { "processes", "Processes", "Listing of all processes",
     linux_xfer_osdata_processes },
   { "procgroups", "Process groups", "Listing of all process groups",
     linux_xfer_osdata_processgroups },
-  { "threads", "Threads", "Listing of all threads",
-    linux_xfer_osdata_threads },
-  { "files", "File descriptors", "Listing of all file descriptors",
-    linux_xfer_osdata_fds },
-  { "sockets", "Sockets", "Listing of all internet-domain sockets",
-    linux_xfer_osdata_isockets },
-  { "shm", "Shared-memory regions", "Listing of all shared-memory regions",
-    linux_xfer_osdata_shm },
   { "semaphores", "Semaphores", "Listing of all semaphores",
     linux_xfer_osdata_sem },
-  { "msg", "Message queues", "Listing of all message queues",
-    linux_xfer_osdata_msg },
-  { "modules", "Kernel modules", "Listing of all loaded kernel modules",
-    linux_xfer_osdata_modules },
+  { "shm", "Shared-memory regions", "Listing of all shared-memory regions",
+    linux_xfer_osdata_shm },
+  { "sockets", "Sockets", "Listing of all internet-domain sockets",
+    linux_xfer_osdata_isockets },
+  { "threads", "Threads", "Listing of all threads",
+    linux_xfer_osdata_threads },
   { NULL, NULL, NULL }
 };
 
This page took 0.031371 seconds and 4 git commands to generate.