public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/4] [nto] Nto fixes.
@ 2015-10-13 16:01 Aleksandar Ristovski
  2015-10-13 16:01 ` [PATCH 4/4] [nto] Setup signals Aleksandar Ristovski
                   ` (3 more replies)
  0 siblings, 4 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-13 16:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hello,

  The short series of patches fixes nto build and fixes basic functionality.

Thanks,

Aleksandar Ristovski
QNX Software Systems

^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH 2/4] [nto] Fixes for nto procfs.
  2015-10-13 16:01 [PATCH 0/4] [nto] Nto fixes Aleksandar Ristovski
  2015-10-13 16:01 ` [PATCH 4/4] [nto] Setup signals Aleksandar Ristovski
  2015-10-13 16:01 ` [PATCH 3/4] [nto] Fix nto target stopped by watchpoint Aleksandar Ristovski
@ 2015-10-13 16:01 ` Aleksandar Ristovski
  2015-10-15 17:41   ` Pedro Alves
  2015-10-13 16:01 ` [PATCH 1/4] [nto] Fix nto build Aleksandar Ristovski
  3 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-13 16:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

ChangeLog:

        * nto-procfs.c (sys/auxv.h): Include.
        (nto_procfs_path): Rename to...
        (nodestr): ... this, and change type.
        (nto_node): Use new variable and logic accordingly.
        (procfs_open_1): Use new variable name. Use local buffer to construct
        procfs path.
        (procfs_pidlist): Use NODESTR to construct procfs path.
        (procfs_files_info): Use NODESTR to output meaningful text.
        (procfs_pid_to_exec_file): New target function.
        (do_attach): Construct procfs using NODESTR.
        (procfs_xfer_partial): Logic for reading TARGET_OBJECT_AUXV.
        (init_procfs_targets): Wire procfs_pid_to_exec_file.
        * nto-tdep.c (symfile.h): Include.
        (nto_read_auxv_from_initial_stack): New function.
        * nto-tdep.h (nto_read_auxv_from_initial_stack): New function
        declaration.
---
 gdb/ChangeLog    |  19 +++++++++
 gdb/nto-procfs.c | 119 +++++++++++++++++++++++++++++++++++++++++++++----------
 gdb/nto-tdep.c   |  87 ++++++++++++++++++++++++++++++++++++++++
 gdb/nto-tdep.h   |   4 ++
 4 files changed, 209 insertions(+), 20 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 3bd7d7d..04c9f0a 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,22 @@
+<2015-2>  Aleksandar Ristovski  <aristovski@qnx.com>
+
+	* nto-procfs.c (sys/auxv.h): Include.
+	(nto_procfs_path): Rename to...
+	(nodestr): ... this, and change type.
+	(nto_node): Use new variable and logic accordingly.
+	(procfs_open_1): Use new variable name. Use local buffer to construct
+	procfs path.
+	(procfs_pidlist): Use NODESTR to construct procfs path. 
+	(procfs_files_info): Use NODESTR to output meaningful text.
+	(procfs_pid_to_exec_file): New target function.
+	(do_attach): Construct procfs using NODESTR.
+	(procfs_xfer_partial): Logic for reading TARGET_OBJECT_AUXV.
+	(init_procfs_targets): Wire procfs_pid_to_exec_file.
+	* nto-tdep.c (symfile.h): Include.
+	(nto_read_auxv_from_initial_stack): New function.
+	* nto-tdep.h (nto_read_auxv_from_initial_stack): New function
+	declaration.
+
 <2015-1>  Aleksandar Ristovski  <aristovski@qnx.com>
 
 	* nto-procfs.c (common/filestuff.h): Include.
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index d659f79..a4b5ce8 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -30,6 +30,8 @@
 #include <sys/syspage.h>
 #include <dirent.h>
 #include <sys/netmgr.h>
+#include <sys/auxv.h>
+
 #include "gdbcore.h"
 #include "inferior.h"
 #include "target.h"
@@ -73,7 +75,7 @@ static int procfs_stopped_by_watchpoint (struct target_ops *ops);
    referenced elsewhere.  'nto_procfs_node' is a flag used to say
    whether we are local, or we should get the current node descriptor
    for the remote QNX node.  */
-static char nto_procfs_path[PATH_MAX] = { "/proc" };
+static char *nodestr;
 static unsigned nto_procfs_node = ND_LOCAL_NODE;
 
 /* Return the current QNX Node, or error out.  This is a simple
@@ -85,10 +87,11 @@ nto_node (void)
 {
   unsigned node;
 
-  if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0)
+  if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0
+      || nodestr == NULL)
     return ND_LOCAL_NODE;
 
-  node = netmgr_strtond (nto_procfs_path, 0);
+  node = netmgr_strtond (nodestr, 0);
   if (node == -1)
     error (_("Lost the QNX node.  Debug session probably over."));
 
@@ -108,12 +111,12 @@ procfs_is_nto_target (bfd *abfd)
 static void
 procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
 {
-  char *nodestr;
   char *endstr;
   char buffer[50];
   int fd, total_size;
   procfs_sysinfo *sysinfo;
   struct cleanup *cleanups;
+  char nto_procfs_path[PATH_MAX];
 
   /* Offer to kill previous inferiors before opening this target.  */
   target_preopen (from_tty);
@@ -123,6 +126,9 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
   /* Set the default node used for spawning to this one,
      and only override it if there is a valid arg.  */
 
+  xfree (nodestr);
+  nodestr = NULL;
+
   nto_procfs_node = ND_LOCAL_NODE;
   nodestr = arg ? xstrdup (arg) : NULL;
 
@@ -151,8 +157,6 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
     }
   snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s", nodestr ? nodestr : "",
 	    "/proc");
-  if (nodestr)
-    xfree (nodestr);
 
   fd = open (nto_procfs_path, O_RDONLY);
   if (fd == -1)
@@ -359,7 +363,7 @@ procfs_pidlist (char *args, int from_tty)
 {
   DIR *dp = NULL;
   struct dirent *dirp = NULL;
-  char buf[512];
+  char buf[PATH_MAX];
   procfs_info *pidinfo = NULL;
   procfs_debuginfo *info = NULL;
   procfs_status *status = NULL;
@@ -367,12 +371,16 @@ procfs_pidlist (char *args, int from_tty)
   pid_t pid;
   char name[512];
   struct cleanup *cleanups;
+  char procfs_dir[PATH_MAX];
 
-  dp = opendir (nto_procfs_path);
+  snprintf (procfs_dir, sizeof (procfs_dir), "%s%s", nodestr ? nodestr : "",
+	    "/proc");
+
+  dp = opendir (procfs_dir);
   if (dp == NULL)
     {
       fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
-			  nto_procfs_path, errno, safe_strerror (errno));
+			  procfs_dir, errno, safe_strerror (errno));
       return;
     }
 
@@ -395,7 +403,8 @@ procfs_pidlist (char *args, int from_tty)
 	      do_cleanups (cleanups);
 	      return;
 	    }
-	  snprintf (buf, 511, "%s/%s/as", nto_procfs_path, dirp->d_name);
+	  snprintf (buf, sizeof (buf), "%s%s/%s/as", nodestr ? nodestr : "",
+		    "/proc", dirp->d_name);
 	  pid = atoi (dirp->d_name);
 	}
       while (pid == 0);
@@ -406,8 +415,7 @@ procfs_pidlist (char *args, int from_tty)
 	{
 	  fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
 			      buf, errno, safe_strerror (errno));
-	  do_cleanups (cleanups);
-	  return;
+	  continue;
 	}
       inner_cleanup = make_cleanup_close (fd);
 
@@ -431,11 +439,16 @@ procfs_pidlist (char *args, int from_tty)
       status = (procfs_status *) buf;
       for (status->tid = 1; status->tid <= num_threads; status->tid++)
 	{
-	  if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
-	      && status->tid != 0)
-	    break;
-	  if (status->tid != 0)
-	    printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
+	  const int err
+	    = devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0);
+	  printf_filtered ("%s - %d", name, pid);
+	  if (err == EOK && status->tid != 0)
+	    printf_filtered ("/%d\n", status->tid);
+	  else
+	    {
+	      printf_filtered ("\n");
+	      break;
+	    }
 	}
 
       do_cleanups (inner_cleanup);
@@ -599,9 +612,40 @@ procfs_files_info (struct target_ops *ignore)
 
   printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
 		     inf->attach_flag ? "attached" : "child",
-		     target_pid_to_str (inferior_ptid), nto_procfs_path);
+		     target_pid_to_str (inferior_ptid),
+		     nodestr ? nodestr : "local node");
+}
+
+/* Read executable file name for the given PID.  */
+
+static char *
+procfs_pid_to_exec_file (struct target_ops *ops, const int pid)
+{
+  int proc_fd;
+  static char proc_path[PATH_MAX];
+  ssize_t rd;
+
+  /* Read exe file name.  */
+  snprintf (proc_path, sizeof (proc_path), "%s/proc/%d/exefile",
+	    nodestr ? nodestr : "", pid);
+  proc_fd = open (proc_path, O_RDONLY);
+  if (proc_fd == -1)
+    return NULL;
+
+  rd = read (proc_fd, proc_path, sizeof (proc_path) - 1);
+  close (proc_fd);
+  if (rd <= 0)
+    {
+      proc_path[0] = '\0';
+      return NULL;
+    }
+  else
+    proc_path[rd] = '\0';
+
+  return proc_path;
 }
 
+
 /* Attach to process PID, then initialize for debugging it.  */
 static void
 procfs_attach (struct target_ops *ops, const char *args, int from_tty)
@@ -653,8 +697,8 @@ do_attach (ptid_t ptid)
   struct sigevent event;
   char path[PATH_MAX];
 
-  snprintf (path, PATH_MAX - 1, "%s/%d/as", nto_procfs_path,
-	    ptid_get_pid (ptid));
+  snprintf (path, PATH_MAX - 1, "%s%s/%d/as", nodestr ? nodestr : "",
+	    "/proc", ptid_get_pid (ptid));
   ctl_fd = open (path, O_RDWR);
   if (ctl_fd == -1)
     error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
@@ -872,6 +916,40 @@ procfs_xfer_partial (struct target_ops *ops, enum target_object object,
     {
     case TARGET_OBJECT_MEMORY:
       return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
+    case TARGET_OBJECT_AUXV:
+      if (readbuf != NULL)
+	{
+	  int err;
+	  CORE_ADDR initial_stack;
+	  debug_process_t procinfo;
+	  /* For 32-bit architecture, size of auxv_t is 8 bytes.  */
+	  const unsigned int sizeof_auxv_t = sizeof (auxv_t);
+	  const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
+	  int tempread;
+	  gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
+
+	  if (!tempbuf)
+	    return TARGET_XFER_E_IO;
+
+	  err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
+		        sizeof procinfo, 0);
+	  if (err != EOK)
+	    return TARGET_XFER_E_IO;
+
+	  /* Similar as in the case of a core file, we read auxv from
+	     initial_stack.  */
+	  initial_stack = procinfo.initial_stack;
+
+	  /* procfs is always 'self-hosted', no byte-order manipulation. */
+	  tempread = nto_read_auxv_from_initial_stack (initial_stack, tempbuf,
+						       sizeof_tempbuf,
+						       sizeof (auxv_t));
+	  tempread = min (tempread, len) - offset;
+	  memcpy (readbuf, tempbuf + offset, tempread);
+	  *xfered_len = tempread;
+	  return tempread ? TARGET_XFER_OK : TARGET_XFER_EOF;
+	}
+      /* Fallthru */
     default:
       return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
 					    readbuf, writebuf, offset, len,
@@ -1444,6 +1522,7 @@ init_procfs_targets (void)
   t->to_interrupt = procfs_interrupt;
   t->to_have_continuable_watchpoint = 1;
   t->to_extra_thread_info = nto_extra_thread_info;
+  t->to_pid_to_exec_file = procfs_pid_to_exec_file;
 
   nto_native_ops = t;
 
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 81ee7fb..63094e8 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -31,6 +31,7 @@
 #include "solib-svr4.h"
 #include "gdbcore.h"
 #include "objfiles.h"
+#include "symfile.h"
 
 #ifdef __CYGWIN__
 #include <sys/cygwin.h>
@@ -394,3 +395,89 @@ nto_initialize_signals (void)
   signal_pass_update (SIGPHOTON, 1);
 #endif
 }
+
+
+/* Read AUXV from initial_stack.  */
+LONGEST
+nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack, gdb_byte *readbuf,
+                                  LONGEST len, size_t sizeof_auxv_t)
+{
+  gdb_byte targ32[4]; /* For 32 bit target values.  */
+  gdb_byte targ64[8]; /* For 64 bit target values.  */
+  CORE_ADDR data_ofs = 0;
+  ULONGEST anint;
+  LONGEST len_read = 0;
+  gdb_byte *buff;
+  enum bfd_endian byte_order;
+  int ptr_size;
+
+  if (sizeof_auxv_t == 16)
+    ptr_size = 8;
+  else
+    ptr_size = 4;
+
+  /* Skip over argc, argv and envp... Comment from ldd.c:
+
+     The startup frame is set-up so that we have:
+     auxv
+     NULL
+     ...
+     envp2
+     envp1 <----- void *frame + (argc + 2) * sizeof(char *)
+     NULL
+     ...
+     argv2
+     argv1
+     argc  <------ void * frame
+
+     On entry to ldd, frame gives the address of argc on the stack.  */
+  /* Read argc. 4 bytes on both 64 and 32 bit arches and luckily little
+   * endian. So we just read first 4 bytes.  */
+  if (target_read_memory (initial_stack + data_ofs, targ32, 4) != 0)
+    return 0;
+
+  byte_order = gdbarch_byte_order (target_gdbarch ());
+
+  anint = extract_unsigned_integer (targ32, sizeof (targ32), byte_order);
+
+  /* Size of pointer is assumed to be 4 bytes (32 bit arch.) */
+  data_ofs += (anint + 2) * ptr_size; /* + 2 comes from argc itself and
+                                                NULL terminating pointer in
+                                                argv.  */
+
+  /* Now loop over env table:  */
+  anint = 0;
+  while (target_read_memory (initial_stack + data_ofs, targ64, ptr_size)
+         == 0)
+    {
+      if (extract_unsigned_integer (targ64, ptr_size, byte_order) == 0)
+	anint = 1; /* Keep looping until non-null entry is found.  */
+      else if (anint)
+	break;
+      data_ofs += ptr_size;
+    }
+  initial_stack += data_ofs;
+
+  memset (readbuf, 0, len);
+  buff = readbuf;
+  while (len_read <= len-sizeof_auxv_t)
+    {
+      if (target_read_memory (initial_stack + len_read, buff, sizeof_auxv_t)
+	  == 0)
+        {
+	  /* Both 32 and 64 bit structures have int as the first field.  */
+          const ULONGEST a_type
+	    = extract_unsigned_integer (buff, sizeof (targ32), byte_order);
+
+          if (a_type == AT_NULL)
+	    break;
+	  buff += sizeof_auxv_t;
+	  len_read += sizeof_auxv_t;
+        }
+      else
+        break;
+    }
+  return len_read;
+}
+
+
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index bd85d2a..7089a12 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -168,4 +168,8 @@ int nto_in_dynsym_resolve_code (CORE_ADDR pc);
 
 char *nto_extra_thread_info (struct target_ops *self, struct thread_info *);
 
+LONGEST nto_read_auxv_from_initial_stack (CORE_ADDR inital_stack,
+					  gdb_byte *readbuf,
+					  LONGEST len, size_t sizeof_auxv_t);
+
 #endif
-- 
1.9.1

^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH 1/4] [nto] Fix nto build.
  2015-10-13 16:01 [PATCH 0/4] [nto] Nto fixes Aleksandar Ristovski
                   ` (2 preceding siblings ...)
  2015-10-13 16:01 ` [PATCH 2/4] [nto] Fixes for nto procfs Aleksandar Ristovski
@ 2015-10-13 16:01 ` Aleksandar Ristovski
  2015-10-15 17:34   ` Pedro Alves
  3 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-13 16:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

gdb/gdbserver/ChangeLog:

* gdbserver/nto-low.c (nto_insert_point, nto_remove_point): Fix
variable name.

gdb/ChangeLog:

* nto-procfs.c (common/filestuff.h): Include.
(procfs_can_use_hw_breakpoint): Fix enum name.
(procfs_open_1): Fix compiler warning.
(procfs_pidlist): Make static.
(procfs_meminfo): Make static, fix type name, add missing argument.
(procfs_store_registers): Make static.
(procfs_thread_info): Remove unused function.
(_initialize_procfs): Forward declare.
---
 gdb/ChangeLog           | 11 +++++++++++
 gdb/gdbserver/ChangeLog |  5 +++++
 gdb/gdbserver/nto-low.c |  4 ++--
 gdb/nto-procfs.c        | 25 +++++++++++--------------
 4 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index dca7f28..3bd7d7d 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,14 @@
+<2015-1>  Aleksandar Ristovski  <aristovski@qnx.com>
+
+	* nto-procfs.c (common/filestuff.h): Include.
+	(procfs_can_use_hw_breakpoint): Fix enum name.
+	(procfs_open_1): Fix compiler warning.
+	(procfs_pidlist): Make static.
+	(procfs_meminfo): Make static, fix type name, add missing argument.
+	(procfs_store_registers): Make static.
+	(procfs_thread_info): Remove unused function.
+	(_initialize_procfs): Forward declare.
+
 <date>  Aleksandar Ristovski  <aristovski@qnx.com>
 
 	* solib-svr4.c (init_lmo): New function.
diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog
index b631894..195dbda 100644
--- a/gdb/gdbserver/ChangeLog
+++ b/gdb/gdbserver/ChangeLog
@@ -1,3 +1,8 @@
+<date>  Aleksandar Ristovski  <aristovski@qnx.com>
+
+	* gdbserver/nto-low.c (nto_insert_point, nto_remove_point): Fix
+	variable name.
+
 2015-09-30  Yao Qi  <yao.qi@linaro.org>
 
 	* linux-aarch64-low.c (emit_movk): Shorten a long line.
diff --git a/gdb/gdbserver/nto-low.c b/gdb/gdbserver/nto-low.c
index fa216a9..97bd1e9 100644
--- a/gdb/gdbserver/nto-low.c
+++ b/gdb/gdbserver/nto-low.c
@@ -794,7 +794,7 @@ nto_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
 {
   int wtype = _DEBUG_BREAK_HW; /* Always request HW.  */
 
-  TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, len);
+  TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, size);
   switch (type)
     {
     case raw_bkpt_type_sw:
@@ -826,7 +826,7 @@ nto_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
 {
   int wtype = _DEBUG_BREAK_HW; /* Always request HW.  */
 
-  TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, len);
+  TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, size);
   switch (type)
     {
     case raw_bkpt_type_sw:
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index 20b05be..d659f79 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -40,6 +40,7 @@
 #include "regcache.h"
 #include "solib.h"
 #include "inf-child.h"
+#include "common/filestuff.h"
 
 #define NULL_PID		0
 #define _DEBUG_FLAG_TRACE	(_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
@@ -54,7 +55,7 @@ static procfs_run run;
 static ptid_t do_attach (ptid_t ptid);
 
 static int procfs_can_use_hw_breakpoint (struct target_ops *self,
-					 enum target_hw_bp_type, int, int);
+					 enum bptype, int, int);
 
 static int procfs_insert_hw_watchpoint (struct target_ops *self,
 					CORE_ADDR addr, int len,
@@ -123,7 +124,7 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
      and only override it if there is a valid arg.  */
 
   nto_procfs_node = ND_LOCAL_NODE;
-  nodestr = arg ? xstrdup (arg) : arg;
+  nodestr = arg ? xstrdup (arg) : NULL;
 
   init_thread_list ();
 
@@ -353,7 +354,7 @@ do_closedir_cleanup (void *dir)
   closedir (dir);
 }
 
-void
+static void
 procfs_pidlist (char *args, int from_tty)
 {
   DIR *dp = NULL;
@@ -445,7 +446,7 @@ procfs_pidlist (char *args, int from_tty)
   return;
 }
 
-void
+static void
 procfs_meminfo (char *args, int from_tty)
 {
   procfs_mapinfo *mapinfos = NULL;
@@ -486,7 +487,7 @@ procfs_meminfo (char *args, int from_tty)
       return;
     }
 
-  mapinfos = XNEWVEC (procfs_mapping, num);
+  mapinfos = XNEWVEC (procfs_mapinfo, num);
 
   num_mapinfos = num;
   mapinfo_p = mapinfos;
@@ -873,7 +874,8 @@ procfs_xfer_partial (struct target_ops *ops, enum target_object object,
       return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
     default:
       return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
-					    readbuf, writebuf, offset, len);
+					    readbuf, writebuf, offset, len,
+					    xfered_len);
     }
 }
 
@@ -1267,7 +1269,7 @@ get_regset (int regset, char *buf, int bufsize, int *regsize)
   return dev_set;
 }
 
-void
+static void
 procfs_store_registers (struct target_ops *ops,
 			struct regcache *regcache, int regno)
 {
@@ -1349,13 +1351,6 @@ procfs_pass_signals (struct target_ops *self,
     }
 }
 
-static struct tidinfo *
-procfs_thread_info (pid_t pid, short tid)
-{
-/* NYI */
-  return NULL;
-}
-
 static char *
 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
 {
@@ -1468,6 +1463,8 @@ init_procfs_targets (void)
 
 #define OSTYPE_NTO 1
 
+extern initialize_file_ftype _initialize_procfs;
+
 void
 _initialize_procfs (void)
 {
-- 
1.9.1

^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH 4/4] [nto] Setup signals.
  2015-10-13 16:01 [PATCH 0/4] [nto] Nto fixes Aleksandar Ristovski
@ 2015-10-13 16:01 ` Aleksandar Ristovski
  2015-10-16 16:16   ` Pedro Alves
  2015-10-13 16:01 ` [PATCH 3/4] [nto] Fix nto target stopped by watchpoint Aleksandar Ristovski
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-13 16:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Add new file with neutrino signal numerical values.

        * i386-nto-tdep.c (i386nto_init_abi): Setup new
        nto_gdb_signal_from_target and nto_gdb_signal_to_target.
        * nto-tdep.c (signals): New definition.
        (nto_gdb_signal_to_target, nto_gdb_signal_from_target): New functions.
        * nto-tdep.h (nto_gdb_signal_to_target, nto_gdb_signal_from_target):
        New declarations.
        * nto_signals.def: New file.
        * include/gdb/signals.def (GDB_SIGNAL_SELECT): New gdb signal enum.
        (GDB_SIGNAL_LAST): Bump numeric value up.
---
 gdb/ChangeLog           | 12 +++++++++
 gdb/i386-nto-tdep.c     |  3 +++
 gdb/nto-tdep.c          | 45 ++++++++++++++++++++++++++++++++
 gdb/nto-tdep.h          |  3 +++
 gdb/nto_signals.def     | 68 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/gdb/signals.def |  5 +++-
 6 files changed, 135 insertions(+), 1 deletion(-)
 create mode 100644 gdb/nto_signals.def

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 1aa8a68..24a5483 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,15 @@
+<2015-4>  Aleksandar Ristovski  <aristovski@qnx.com>
+
+	* i386-nto-tdep.c (i386nto_init_abi): Setup new
+	nto_gdb_signal_from_target and nto_gdb_signal_to_target.
+	* nto-tdep.c (signals): New definition.
+	(nto_gdb_signal_to_target, nto_gdb_signal_from_target): New functions.
+	* nto-tdep.h (nto_gdb_signal_to_target, nto_gdb_signal_from_target):
+	New declarations.
+	* nto_signals.def: New file.
+	* include/gdb/signals.def (GDB_SIGNAL_SELECT): New gdb signal enum.
+	(GDB_SIGNAL_LAST): Bump numeric value up.
+
 <2015-3>  Aleksandar Ristovski  <aristovski@qnx.com>
 
 	* nto-procfs.c (procfs_wait): Set stopped_flags.
diff --git a/gdb/i386-nto-tdep.c b/gdb/i386-nto-tdep.c
index 818c408..af64a67 100644
--- a/gdb/i386-nto-tdep.c
+++ b/gdb/i386-nto-tdep.c
@@ -362,6 +362,9 @@ i386nto_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
         = nto_in_dynsym_resolve_code;
     }
   set_solib_ops (gdbarch, &nto_svr4_so_ops);
+
+  set_gdbarch_gdb_signal_from_target (gdbarch, nto_gdb_signal_from_target);
+  set_gdbarch_gdb_signal_to_target (gdbarch, nto_gdb_signal_to_target);
 }
 
 /* Provide a prototype to silence -Wmissing-prototypes.  */
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 62dbf8b..da5423f 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -556,6 +556,51 @@ nto_inferior_data (struct inferior *const inferior)
   return inf_data;
 }
 
+/* This table must match in order and size the signals in enum
+   gdb_signal.  */
+
+static const struct {
+  const enum gdb_signal gdb_signal;
+  const int nto_signo;
+  } signals [] =
+{
+#define SET(symbol, constant, name, string) { symbol, constant },
+#include "nto_signals.def"
+#undef SET
+};
+
+
+int
+nto_gdb_signal_to_target (struct gdbarch *gdbarch,
+			  enum gdb_signal const signal)
+{
+  unsigned sig;
+  const unsigned signalsz = sizeof (signals) / sizeof (signals[0]);
+
+  for (sig = 0; sig != signalsz; ++sig)
+    if (signals[sig].gdb_signal == signal)
+      return signals[sig].nto_signo;
+
+  warning (_("GDB signal %d (%s) can not be mapped to a Neutrino signal\n"),
+	   (int) signal, gdb_signal_to_name (signal));
+  return 0;
+}
+
+enum gdb_signal
+nto_gdb_signal_from_target (struct gdbarch *gdbarch, int const signo)
+{
+  unsigned sig;
+  const unsigned signalsz = sizeof (signals) / sizeof (signals[0]);
+
+  for (sig = 0; sig != signalsz; ++sig)
+    if (signals[sig].nto_signo == signo)
+      return signals[sig].gdb_signal;
+
+  warning (_("Neutrino signal %d can not be mapped to gdb signal\n"),
+	   signo);
+  return GDB_SIGNAL_0;
+}
+
 /* Provide a prototype to silence -Wmissing-prototypes.  */
 extern initialize_file_ftype _initialize_nto_tdep;
 
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index 6ed9da0..5cdd4f4 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -184,4 +184,7 @@ LONGEST nto_read_auxv_from_initial_stack (CORE_ADDR inital_stack,
 
 struct nto_inferior_data *nto_inferior_data (struct inferior *inf);
 
+gdbarch_gdb_signal_to_target_ftype nto_gdb_signal_to_target;
+gdbarch_gdb_signal_from_target_ftype nto_gdb_signal_from_target;
+
 #endif
diff --git a/gdb/nto_signals.def b/gdb/nto_signals.def
new file mode 100644
index 0000000..2fee39f
--- /dev/null
+++ b/gdb/nto_signals.def
@@ -0,0 +1,68 @@
+SET (GDB_SIGNAL_HUP, 1, "SIGHUP", "Hangup")
+SET (GDB_SIGNAL_INT, 2, "SIGINT", "Interrupt")
+SET (GDB_SIGNAL_QUIT, 3, "SIGQUIT", "Quit")
+SET (GDB_SIGNAL_ILL, 4, "SIGILL", "Illegal instruction")
+SET (GDB_SIGNAL_TRAP, 5, "SIGTRAP", "Trace/breakpoint trap")
+SET (GDB_SIGNAL_ABRT, 6, "SIGABRT", "Aborted")
+SET (GDB_SIGNAL_EMT, 7, "SIGDEADLK", "Mutex deadlock")
+SET (GDB_SIGNAL_FPE, 8, "SIGFPE", "Arithmetic exception")
+SET (GDB_SIGNAL_KILL, 9, "SIGKILL", "Killed")
+SET (GDB_SIGNAL_BUS, 10, "SIGBUS", "Bus error")
+SET (GDB_SIGNAL_SEGV, 11, "SIGSEGV", "Segmentation fault")
+SET (GDB_SIGNAL_SYS, 12, "SIGSYS", "Bad system call")
+SET (GDB_SIGNAL_PIPE, 13, "SIGPIPE", "Broken pipe")
+SET (GDB_SIGNAL_ALRM, 14, "SIGALRM", "Alarm clock")
+SET (GDB_SIGNAL_TERM, 15, "SIGTERM", "Terminated")
+SET (GDB_SIGNAL_USR1, 16, "SIGUSR1", "User defined signal 1")
+SET (GDB_SIGNAL_USR2, 17, "SIGUSR2", "User defined signal 2")
+SET (GDB_SIGNAL_CHLD, 18, "SIGCHLD", "Death of child process")
+SET (GDB_SIGNAL_PWR, 19, "SIGPWR", "Power fail/restart")
+SET (GDB_SIGNAL_WINCH, 20, "SIGWINCH", "Window change")
+SET (GDB_SIGNAL_URG, 21, "SIGURG", "urgent condition on I/O channel")
+SET (GDB_SIGNAL_POLL, 22, "SIGPOLL",  "System V name for SIGIO")
+SET (GDB_SIGNAL_STOP, 23, "SIGSTOP",  "sendable stop signal not from tty")
+SET (GDB_SIGNAL_TSTP, 24, "SIGTSTP", "stop signal from tty")
+SET (GDB_SIGNAL_CONT, 25, "SIGCONT", "continue a stopped process")
+SET (GDB_SIGNAL_TTIN, 26, "SIGTTIN", "attempted background tty read")
+SET (GDB_SIGNAL_TTOU, 27, "SIGTTOU", "attempted background tty write")
+SET (GDB_SIGNAL_VTALRM, 28, "SIGVTALRM", "virtual timer expired")
+SET (GDB_SIGNAL_PROF, 29, "SIGPROF", "profileing timer expired")
+SET (GDB_SIGNAL_XCPU, 30, "SIGXCPU",  "exceded cpu limit")
+SET (GDB_SIGNAL_XFSZ, 31, "SIGXFSZ", "exceded file size limit")
+SET (GDB_SIGNAL_REALTIME_32, 32, "SIG32", "Real-time event 32")
+SET (GDB_SIGNAL_REALTIME_33, 33, "SIG33", "Real-time event 33")
+SET (GDB_SIGNAL_REALTIME_34, 34, "SIG34", "Real-time event 34")
+SET (GDB_SIGNAL_REALTIME_35, 35, "SIG35", "Real-time event 35")
+SET (GDB_SIGNAL_REALTIME_36, 36, "SIG36", "Real-time event 36")
+SET (GDB_SIGNAL_REALTIME_37, 37, "SIG37", "Real-time event 37")
+SET (GDB_SIGNAL_REALTIME_38, 38, "SIG38", "Real-time event 38")
+SET (GDB_SIGNAL_REALTIME_39, 39, "SIG39", "Real-time event 39")
+SET (GDB_SIGNAL_REALTIME_40, 40, "SIG40", "Real-time event 40")
+
+SET (GDB_SIGNAL_REALTIME_41, 41, "SIG41", "Real-time event 41")
+SET (GDB_SIGNAL_REALTIME_42, 42, "SIG42", "Real-time event 42")
+SET (GDB_SIGNAL_REALTIME_43, 43, "SIG43", "Real-time event 43")
+SET (GDB_SIGNAL_REALTIME_44, 44, "SIG44", "Real-time event 44")
+SET (GDB_SIGNAL_REALTIME_45, 45, "SIG45", "Real-time event 45")
+SET (GDB_SIGNAL_REALTIME_46, 46, "SIG46", "Real-time event 46")
+SET (GDB_SIGNAL_REALTIME_47, 47, "SIG47", "Real-time event 47")
+SET (GDB_SIGNAL_REALTIME_48, 48, "SIG48", "Real-time event 48")
+SET (GDB_SIGNAL_REALTIME_49, 49, "SIG49", "Real-time event 49")
+SET (GDB_SIGNAL_REALTIME_50, 50, "SIG50", "Real-time event 50")
+SET (GDB_SIGNAL_REALTIME_51, 51, "SIG51", "Real-time event 51")
+SET (GDB_SIGNAL_REALTIME_52, 52, "SIG52", "Real-time event 52")
+SET (GDB_SIGNAL_REALTIME_53, 53, "SIG53", "Real-time event 53")
+SET (GDB_SIGNAL_REALTIME_54, 54, "SIG54", "Real-time event 54")
+SET (GDB_SIGNAL_REALTIME_55, 55, "SIG55", "Real-time event 55")
+SET (GDB_SIGNAL_REALTIME_56, 56, "SIG56", "Real-time event 56")
+
+SET (GDB_SIGNAL_SELECT, 57, "SIGSELECT", "SIGSELECT") 
+
+/* Use whatever signal we use when one is not specifically specified
+   (for passing to proceed and so on).  */
+SET (GDB_SIGNAL_DEFAULT, 60, NULL,
+     "Internal error: printing GDB_SIGNAL_DEFAULT")
+
+/* Last and unused enum value, for sizing arrays, etc.  */
+SET (GDB_SIGNAL_LAST, 61, NULL, "GDB_SIGNAL_MAGIC")
+
diff --git a/include/gdb/signals.def b/include/gdb/signals.def
index 3f49980..98645ba 100644
--- a/include/gdb/signals.def
+++ b/include/gdb/signals.def
@@ -194,7 +194,10 @@ SET (GDB_EXC_EMULATION, 148, "EXC_EMULATION", "Emulation instruction")
 SET (GDB_EXC_SOFTWARE, 149, "EXC_SOFTWARE", "Software generated exception")
 SET (GDB_EXC_BREAKPOINT, 150, "EXC_BREAKPOINT", "Breakpoint")
 
+/* Special Neutrino signal. */
+SET (GDB_SIGNAL_SELECT, 151, "SIGSELECT", "SIGSELECT")
+
 /* If you are adding a new signal, add it just above this comment.  */
 
 /* Last and unused enum value, for sizing arrays, etc.  */
-SET (GDB_SIGNAL_LAST, 151, NULL, "GDB_SIGNAL_LAST")
+SET (GDB_SIGNAL_LAST, 152, NULL, "GDB_SIGNAL_LAST")
-- 
1.9.1

^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH 3/4] [nto] Fix nto target stopped by watchpoint.
  2015-10-13 16:01 [PATCH 0/4] [nto] Nto fixes Aleksandar Ristovski
  2015-10-13 16:01 ` [PATCH 4/4] [nto] Setup signals Aleksandar Ristovski
@ 2015-10-13 16:01 ` Aleksandar Ristovski
  2015-10-16 16:10   ` Pedro Alves
  2015-10-13 16:01 ` [PATCH 2/4] [nto] Fixes for nto procfs Aleksandar Ristovski
  2015-10-13 16:01 ` [PATCH 1/4] [nto] Fix nto build Aleksandar Ristovski
  3 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-13 16:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Fix stopped by watchpoint detection: add inferior data, use inferior data
for storing last stopped flags needed for detection.
Add binary sniffing for QNX osabi.

        * nto-procfs.c (procfs_wait): Set stopped_flags.
        (procfs_stopped_by_watchpoint): Use flags stored in inferior data.
        * nto-tdep.c (QNX_NOTE_NAME, QNX_INFO_SECT_NAME): New defines.
        (nto_inferior_data_reg): New definition.
        (nto_sniff_abi_note_section): New function.
        (nto_elf_osabi_sniffer): Use nto_sniff_abi_note_section.
        (nto_new_inferior_data, nto_inferior_data_cleanup, nto_inferior_data):
        New functions.
        (_initialize_nto_tdep): New forward declaration, new function.
        * nto-tdep.h (nto_inferior_data): New struct.
        (nto_inferior_data): New function declaration.
---
 gdb/ChangeLog    | 14 +++++++++
 gdb/nto-procfs.c | 21 ++++++++++++-
 gdb/nto-tdep.c   | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 gdb/nto-tdep.h   | 12 ++++++++
 4 files changed, 133 insertions(+), 4 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 04c9f0a..1aa8a68 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,17 @@
+<2015-3>  Aleksandar Ristovski  <aristovski@qnx.com>
+
+	* nto-procfs.c (procfs_wait): Set stopped_flags.
+	(procfs_stopped_by_watchpoint): Use flags stored in inferior data.
+	* nto-tdep.c (QNX_NOTE_NAME, QNX_INFO_SECT_NAME): New defines.
+	(nto_inferior_data_reg): New definition.
+	(nto_sniff_abi_note_section): New function.
+	(nto_elf_osabi_sniffer): Use nto_sniff_abi_note_section.
+	(nto_new_inferior_data, nto_inferior_data_cleanup, nto_inferior_data):
+	New functions.
+	(_initialize_nto_tdep): New forward declaration, new function.
+	* nto-tdep.h (nto_inferior_data): New struct.
+	(nto_inferior_data): New function declaration.
+
 <2015-2>  Aleksandar Ristovski  <aristovski@qnx.com>
 
 	* nto-procfs.c (sys/auxv.h): Include.
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index a4b5ce8..ae7329c 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -786,6 +786,9 @@ procfs_wait (struct target_ops *ops,
       devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
     }
 
+  nto_inferior_data (NULL)->stopped_flags = status.flags;
+  nto_inferior_data (NULL)->stopped_pc = status.ip;
+
   if (status.flags & _DEBUG_FLAG_SSTEP)
     {
       ourstatus->kind = TARGET_WAITKIND_STOPPED;
@@ -1630,5 +1633,21 @@ procfs_insert_hw_watchpoint (struct target_ops *self,
 static int
 procfs_stopped_by_watchpoint (struct target_ops *ops)
 {
-  return 0;
+  /* NOTE: nto_stopped_by_watchpoint will be called ONLY while we are 
+     stopped due to a SIGTRAP.  This assumes gdb works in 'all-stop' mode;
+     future gdb versions will likely run in 'non-stop' mode in which case 
+     we will have to store/examine statuses per thread in question.  
+     Until then, this will work fine.  */
+
+  struct inferior *inf = current_inferior ();
+  struct nto_inferior_data *inf_data;
+
+  gdb_assert (inf != NULL);
+
+  inf_data = nto_inferior_data (inf);
+
+  return inf_data->stopped_flags
+	 & (_DEBUG_FLAG_TRACE_RD
+	    | _DEBUG_FLAG_TRACE_WR
+	    | _DEBUG_FLAG_TRACE_MODIFY);
 }
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 63094e8..62dbf8b 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -33,6 +33,9 @@
 #include "objfiles.h"
 #include "symfile.h"
 
+#define QNX_NOTE_NAME	"QNX"
+#define QNX_INFO_SECT_NAME "QNX_info"
+
 #ifdef __CYGWIN__
 #include <sys/cygwin.h>
 #endif
@@ -47,6 +50,8 @@ static char default_nto_target[] = "";
 
 struct nto_target_ops current_nto_target;
 
+static const struct inferior_data *nto_inferior_data_reg;
+
 static char *
 nto_target (void)
 {
@@ -331,12 +336,51 @@ nto_dummy_supply_regset (struct regcache *regcache, char *regs)
   /* Do nothing.  */
 }
 
+static void
+nto_sniff_abi_note_section (bfd *abfd, asection *sect, void *obj)
+{
+  const char *sectname;
+  unsigned int sectsize;
+  char *note; // buffer holding the section contents
+  unsigned int namelen;
+  const char *name;
+
+  sectname = bfd_get_section_name (abfd, sect);
+  sectsize = bfd_section_size (abfd, sect);
+
+  /* TODO: limit the note size here, for now limit is 128 bytes
+     (enough to check the name and type).  */
+  if (sectsize > 128)
+    sectsize = 128;
+
+  if (sectname && strstr (sectname, QNX_INFO_SECT_NAME) != NULL)
+    *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+
+  if (sectname && strstr (sectname, "note") != NULL)
+    {
+      note = alloca (sectsize);
+      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
+      namelen = (unsigned int) bfd_h_get_32 (abfd, note);
+      name = note + 12;
+
+      if (namelen > 0
+	  && (0 == strcmp (name, QNX_NOTE_NAME)))
+        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+    }
+}
+
 enum gdb_osabi
 nto_elf_osabi_sniffer (bfd *abfd)
 {
-  if (nto_is_nto_target)
+  enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
+
+  bfd_map_over_sections (abfd,
+			 nto_sniff_abi_note_section,
+			 &osabi);
+
+  if (osabi == GDB_OSABI_UNKNOWN && nto_is_nto_target)
     return nto_is_nto_target (abfd);
-  return GDB_OSABI_UNKNOWN;
+  return osabi;
 }
 
 static const char *nto_thread_state_str[] =
@@ -396,7 +440,6 @@ nto_initialize_signals (void)
 #endif
 }
 
-
 /* Read AUXV from initial_stack.  */
 LONGEST
 nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack, gdb_byte *readbuf,
@@ -480,4 +523,45 @@ nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack, gdb_byte *readbuf,
   return len_read;
 }
 
+static struct nto_inferior_data *
+nto_new_inferior_data (void)
+{
+  struct nto_inferior_data *const inf_data
+    = XCNEW (struct nto_inferior_data);
+
+  return inf_data;
+}
+
+static void
+nto_inferior_data_cleanup (struct inferior *const inf, void *const dat)
+{
+  xfree (dat);
+}
+
+struct nto_inferior_data *
+nto_inferior_data (struct inferior *const inferior)
+{
+  struct inferior *const inf = inferior ? inferior : current_inferior ();
+  struct nto_inferior_data *inf_data;
+
+  gdb_assert (inf != NULL);
+
+  inf_data = inferior_data (inf, nto_inferior_data_reg);
+  if (inf_data == NULL)
+    {
+      set_inferior_data (inf, nto_inferior_data_reg,
+			 (inf_data = nto_new_inferior_data ()));
+    }
+
+  return inf_data;
+}
+
+/* Provide a prototype to silence -Wmissing-prototypes.  */
+extern initialize_file_ftype _initialize_nto_tdep;
 
+void
+_initialize_nto_tdep (void)
+{
+  nto_inferior_data_reg
+    = register_inferior_data_with_cleanup (NULL, nto_inferior_data_cleanup);
+}
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index 7089a12..6ed9da0 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -142,6 +142,16 @@ struct private_thread_info
   char name[1];
 };
 
+/* Per-inferior data, common for both procfs and remote.  */
+struct nto_inferior_data
+{
+  /* Last stopped flags result from wait function */
+  unsigned int stopped_flags;
+
+  /* Last known stopped PC */
+  CORE_ADDR stopped_pc;
+};
+
 /* Generic functions in nto-tdep.c.  */
 
 void nto_init_solib_absolute_prefix (void);
@@ -172,4 +182,6 @@ LONGEST nto_read_auxv_from_initial_stack (CORE_ADDR inital_stack,
 					  gdb_byte *readbuf,
 					  LONGEST len, size_t sizeof_auxv_t);
 
+struct nto_inferior_data *nto_inferior_data (struct inferior *inf);
+
 #endif
-- 
1.9.1

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 1/4] [nto] Fix nto build.
  2015-10-13 16:01 ` [PATCH 1/4] [nto] Fix nto build Aleksandar Ristovski
@ 2015-10-15 17:34   ` Pedro Alves
  0 siblings, 0 replies; 47+ messages in thread
From: Pedro Alves @ 2015-10-15 17:34 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/13/2015 05:01 PM, Aleksandar Ristovski wrote:
> gdb/gdbserver/ChangeLog:
> 
> * gdbserver/nto-low.c (nto_insert_point, nto_remove_point): Fix
> variable name.
> 
> gdb/ChangeLog:
> 
> * nto-procfs.c (common/filestuff.h): Include.
> (procfs_can_use_hw_breakpoint): Fix enum name.
> (procfs_open_1): Fix compiler warning.
> (procfs_pidlist): Make static.
> (procfs_meminfo): Make static, fix type name, add missing argument.
> (procfs_store_registers): Make static.
> (procfs_thread_info): Remove unused function.
> (_initialize_procfs): Forward declare.

LGTM.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/4] [nto] Fixes for nto procfs.
  2015-10-13 16:01 ` [PATCH 2/4] [nto] Fixes for nto procfs Aleksandar Ristovski
@ 2015-10-15 17:41   ` Pedro Alves
  2015-10-20 12:43     ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-15 17:41 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

It would have been nicer to see this split into a fix/theme
per patch, and add something to the commit log about each
fix.  E.g., the aux bits could easily be a separate patch.

Anyway, this is pretty isolated to NTO bits.

LGTM with the nits below addressed.

On 10/13/2015 05:01 PM, Aleksandar Ristovski wrote:

>  	}
>  
>        do_cleanups (inner_cleanup);
> @@ -599,9 +612,40 @@ procfs_files_info (struct target_ops *ignore)
>  
>    printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
>  		     inf->attach_flag ? "attached" : "child",
> -		     target_pid_to_str (inferior_ptid), nto_procfs_path);
> +		     target_pid_to_str (inferior_ptid),
> +		     nodestr ? nodestr : "local node");

Write 'nodestr != NULL'.

> +}
> +
> +/* Read executable file name for the given PID.  */
> +
> +static char *
> +procfs_pid_to_exec_file (struct target_ops *ops, const int pid)
> +{
> +  int proc_fd;
> +  static char proc_path[PATH_MAX];
> +  ssize_t rd;
> +
> +  /* Read exe file name.  */
> +  snprintf (proc_path, sizeof (proc_path), "%s/proc/%d/exefile",
> +	    nodestr ? nodestr : "", pid);

Ditto.

> +  proc_fd = open (proc_path, O_RDONLY);
> +  if (proc_fd == -1)
> +    return NULL;
> +
> +  rd = read (proc_fd, proc_path, sizeof (proc_path) - 1);
> +  close (proc_fd);
> +  if (rd <= 0)
> +    {
> +      proc_path[0] = '\0';
> +      return NULL;
> +    }
> +  else
> +    proc_path[rd] = '\0';
> +
> +  return proc_path;

Either write:

  else
    {
      proc_path[rd] = '\0';
      return proc_path;
    }

Or drop the "else".

>  }
>  
> +
>  /* Attach to process PID, then initialize for debugging it.  */
>  static void
>  procfs_attach (struct target_ops *ops, const char *args, int from_tty)
> @@ -653,8 +697,8 @@ do_attach (ptid_t ptid)
>    struct sigevent event;
>    char path[PATH_MAX];
>  
> -  snprintf (path, PATH_MAX - 1, "%s/%d/as", nto_procfs_path,
> -	    ptid_get_pid (ptid));
> +  snprintf (path, PATH_MAX - 1, "%s%s/%d/as", nodestr ? nodestr : "",
> +	    "/proc", ptid_get_pid (ptid));

nodestr != NULL

>    ctl_fd = open (path, O_RDWR);
>    if (ctl_fd == -1)
>      error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
> @@ -872,6 +916,40 @@ procfs_xfer_partial (struct target_ops *ops, enum target_object object,
>      {
>      case TARGET_OBJECT_MEMORY:
>        return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
> +    case TARGET_OBJECT_AUXV:
> +      if (readbuf != NULL)
> +	{
> +	  int err;
> +	  CORE_ADDR initial_stack;
> +	  debug_process_t procinfo;
> +	  /* For 32-bit architecture, size of auxv_t is 8 bytes.  */
> +	  const unsigned int sizeof_auxv_t = sizeof (auxv_t);
> +	  const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
> +	  int tempread;
> +	  gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
> +
> +	  if (!tempbuf)
> +	    return TARGET_XFER_E_IO;

 if (tempbuf == NULL)

Can NTO's alloca really return NULL?

> +
> +	  err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
> +		        sizeof procinfo, 0);
> +	  if (err != EOK)
> +	    return TARGET_XFER_E_IO;

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 3/4] [nto] Fix nto target stopped by watchpoint.
  2015-10-13 16:01 ` [PATCH 3/4] [nto] Fix nto target stopped by watchpoint Aleksandar Ristovski
@ 2015-10-16 16:10   ` Pedro Alves
  2015-10-20 18:42     ` [PATCH 0/2] (patch 3/4 v2) Broken down patch 3/4 Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-16 16:10 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/13/2015 05:01 PM, Aleksandar Ristovski wrote:
> Fix stopped by watchpoint detection: add inferior data, use inferior data
> for storing last stopped flags needed for detection.

> Add binary sniffing for QNX osabi.

Is the binary sniffing issue any way related to the watchpoint issue?

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 4/4] [nto] Setup signals.
  2015-10-13 16:01 ` [PATCH 4/4] [nto] Setup signals Aleksandar Ristovski
@ 2015-10-16 16:16   ` Pedro Alves
  2015-10-22 15:57     ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-16 16:16 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/13/2015 05:01 PM, Aleksandar Ristovski wrote:
> Add new file with neutrino signal numerical values.
> 
>         * i386-nto-tdep.c (i386nto_init_abi): Setup new
>         nto_gdb_signal_from_target and nto_gdb_signal_to_target.
>         * nto-tdep.c (signals): New definition.
>         (nto_gdb_signal_to_target, nto_gdb_signal_from_target): New functions.
>         * nto-tdep.h (nto_gdb_signal_to_target, nto_gdb_signal_from_target):
>         New declarations.
>         * nto_signals.def: New file.
>         * include/gdb/signals.def (GDB_SIGNAL_SELECT): New gdb signal enum.
>         (GDB_SIGNAL_LAST): Bump numeric value up.

What does this fix?  I assume more than just adding the SIGSELECT signal?

Please extend the commit log to be a bit more descriptive.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/4] [nto] Fixes for nto procfs.
  2015-10-15 17:41   ` Pedro Alves
@ 2015-10-20 12:43     ` Aleksandar Ristovski
  2015-10-20 13:21       ` Aleksandar Ristovski
  2015-10-20 14:28       ` Pedro Alves
  0 siblings, 2 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 12:43 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves

[-- Attachment #1: Type: text/plain, Size: 1663 bytes --]

On 15-10-15 01:41 PM, Pedro Alves wrote:
> It would have been nicer to see this split into a fix/theme
> per patch, and add something to the commit log about each
> fix.  E.g., the aux bits could easily be a separate patch.
> 
> Anyway, this is pretty isolated to NTO bits.

I'm trying to catch up and submit local changes for previous ports.

While patches may not be minimalistic, I am trying to at least bring
certain rounded-up improvement (e.g. having a debug session).

But I will try to make more granulated patches.

> 
> LGTM with the nits below addressed.
> 
> On 10/13/2015 05:01 PM, Aleksandar Ristovski wrote:
> 
>>  	}
>>  
>>        do_cleanups (inner_cleanup);
>> @@ -599,9 +612,40 @@ procfs_files_info (struct target_ops *ignore)
>>  
>>    printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
>>  		     inf->attach_flag ? "attached" : "child",
>> -		     target_pid_to_str (inferior_ptid), nto_procfs_path);
>> +		     target_pid_to_str (inferior_ptid),
>> +		     nodestr ? nodestr : "local node");
> 
> Write 'nodestr != NULL'.

Done. Here and other places where pointer is used as a logical expression.
...
>> +  if (rd <= 0)
>> +    {
>> +      proc_path[0] = '\0';
>> +      return NULL;
>> +    }
>> +  else
>> +    proc_path[rd] = '\0';
>> +
>> +  return proc_path;
> 
> Either write:
> 
>   else
>     {
>       proc_path[rd] = '\0';
>       return proc_path;
>     }
> 
> Or drop the "else".

Dropped 'else'.

...
>> +
>> +	  if (!tempbuf)
>> +	    return TARGET_XFER_E_IO;
> 
>  if (tempbuf == NULL)
> 
> Can NTO's alloca really return NULL?

Yes.


Attached fixed version of the patch.


Thanks,

Aleksandar Ristovski


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-nto-Fixes-for-nto-procfs.patch --]
[-- Type: text/x-patch; name="0001-nto-Fixes-for-nto-procfs.patch", Size: 13407 bytes --]

From 62774c1deef7eb01108ac65636e2ccf974992d85 Mon Sep 17 00:00:00 2001
From: Aleksandar Ristovski <aristovski@qnx.com>
Date: Mon, 5 Oct 2015 10:32:16 -0400
Subject: [PATCH] [nto] Fixes for nto procfs.

gdb/ChangeLog:

	* nto-procfs.c (sys/auxv.h): Include.
	(nto_procfs_path): Rename to...
	(nodestr): ... this, and change type.
	(nto_node): Use new variable and logic accordingly.
	(procfs_open_1): Use new variable name. Use local buffer to construct
	procfs path.
	(procfs_pidlist): Use NODESTR to construct procfs path.
	(procfs_files_info): Use NODESTR to output meaningful text.
	(procfs_pid_to_exec_file): New target function.
	(do_attach): Construct procfs using NODESTR.
	(procfs_xfer_partial): Logic for reading TARGET_OBJECT_AUXV.
	(procfs_create_inferior): Compare pointer to NULL.
	(init_procfs_targets): Wire procfs_pid_to_exec_file.
	* nto-tdep.c (symfile.h): Include.
	(nto_read_auxv_from_initial_stack): New function.
	* nto-tdep.h (nto_read_auxv_from_initial_stack): New function
	declaration.
---
 gdb/nto-procfs.c | 128 ++++++++++++++++++++++++++++++++++++++++++++-----------
 gdb/nto-tdep.c   |  87 +++++++++++++++++++++++++++++++++++++
 gdb/nto-tdep.h   |   4 ++
 3 files changed, 194 insertions(+), 25 deletions(-)

diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index d659f79..176ceea 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -30,6 +30,8 @@
 #include <sys/syspage.h>
 #include <dirent.h>
 #include <sys/netmgr.h>
+#include <sys/auxv.h>
+
 #include "gdbcore.h"
 #include "inferior.h"
 #include "target.h"
@@ -73,7 +75,7 @@ static int procfs_stopped_by_watchpoint (struct target_ops *ops);
    referenced elsewhere.  'nto_procfs_node' is a flag used to say
    whether we are local, or we should get the current node descriptor
    for the remote QNX node.  */
-static char nto_procfs_path[PATH_MAX] = { "/proc" };
+static char *nodestr;
 static unsigned nto_procfs_node = ND_LOCAL_NODE;
 
 /* Return the current QNX Node, or error out.  This is a simple
@@ -85,10 +87,11 @@ nto_node (void)
 {
   unsigned node;
 
-  if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0)
+  if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0
+      || nodestr == NULL)
     return ND_LOCAL_NODE;
 
-  node = netmgr_strtond (nto_procfs_path, 0);
+  node = netmgr_strtond (nodestr, 0);
   if (node == -1)
     error (_("Lost the QNX node.  Debug session probably over."));
 
@@ -108,12 +111,12 @@ procfs_is_nto_target (bfd *abfd)
 static void
 procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
 {
-  char *nodestr;
   char *endstr;
   char buffer[50];
   int fd, total_size;
   procfs_sysinfo *sysinfo;
   struct cleanup *cleanups;
+  char nto_procfs_path[PATH_MAX];
 
   /* Offer to kill previous inferiors before opening this target.  */
   target_preopen (from_tty);
@@ -123,8 +126,11 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
   /* Set the default node used for spawning to this one,
      and only override it if there is a valid arg.  */
 
+  xfree (nodestr);
+  nodestr = NULL;
+
   nto_procfs_node = ND_LOCAL_NODE;
-  nodestr = arg ? xstrdup (arg) : NULL;
+  nodestr = (arg != NULL) ? xstrdup (arg) : NULL;
 
   init_thread_list ();
 
@@ -149,10 +155,8 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
 	    *endstr = 0;
 	}
     }
-  snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s", nodestr ? nodestr : "",
-	    "/proc");
-  if (nodestr)
-    xfree (nodestr);
+  snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s",
+	    (nodestr != NULL) ? nodestr : "", "/proc");
 
   fd = open (nto_procfs_path, O_RDONLY);
   if (fd == -1)
@@ -174,7 +178,7 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
     {
       total_size = sysinfo->total_size;
       sysinfo = alloca (total_size);
-      if (!sysinfo)
+      if (sysinfo == NULL)
 	{
 	  printf_filtered ("Memory error: %d (%s)\n", errno,
 			   safe_strerror (errno));
@@ -359,7 +363,7 @@ procfs_pidlist (char *args, int from_tty)
 {
   DIR *dp = NULL;
   struct dirent *dirp = NULL;
-  char buf[512];
+  char buf[PATH_MAX];
   procfs_info *pidinfo = NULL;
   procfs_debuginfo *info = NULL;
   procfs_status *status = NULL;
@@ -367,12 +371,16 @@ procfs_pidlist (char *args, int from_tty)
   pid_t pid;
   char name[512];
   struct cleanup *cleanups;
+  char procfs_dir[PATH_MAX];
 
-  dp = opendir (nto_procfs_path);
+  snprintf (procfs_dir, sizeof (procfs_dir), "%s%s",
+	    (nodestr != NULL) ? nodestr : "", "/proc");
+
+  dp = opendir (procfs_dir);
   if (dp == NULL)
     {
       fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
-			  nto_procfs_path, errno, safe_strerror (errno));
+			  procfs_dir, errno, safe_strerror (errno));
       return;
     }
 
@@ -395,7 +403,9 @@ procfs_pidlist (char *args, int from_tty)
 	      do_cleanups (cleanups);
 	      return;
 	    }
-	  snprintf (buf, 511, "%s/%s/as", nto_procfs_path, dirp->d_name);
+	  snprintf (buf, sizeof (buf), "%s%s/%s/as",
+		    (nodestr != NULL) ? nodestr : "",
+		    "/proc", dirp->d_name);
 	  pid = atoi (dirp->d_name);
 	}
       while (pid == 0);
@@ -406,8 +416,7 @@ procfs_pidlist (char *args, int from_tty)
 	{
 	  fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
 			      buf, errno, safe_strerror (errno));
-	  do_cleanups (cleanups);
-	  return;
+	  continue;
 	}
       inner_cleanup = make_cleanup_close (fd);
 
@@ -431,11 +440,16 @@ procfs_pidlist (char *args, int from_tty)
       status = (procfs_status *) buf;
       for (status->tid = 1; status->tid <= num_threads; status->tid++)
 	{
-	  if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
-	      && status->tid != 0)
-	    break;
-	  if (status->tid != 0)
-	    printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
+	  const int err
+	    = devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0);
+	  printf_filtered ("%s - %d", name, pid);
+	  if (err == EOK && status->tid != 0)
+	    printf_filtered ("/%d\n", status->tid);
+	  else
+	    {
+	      printf_filtered ("\n");
+	      break;
+	    }
 	}
 
       do_cleanups (inner_cleanup);
@@ -599,9 +613,38 @@ procfs_files_info (struct target_ops *ignore)
 
   printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
 		     inf->attach_flag ? "attached" : "child",
-		     target_pid_to_str (inferior_ptid), nto_procfs_path);
+		     target_pid_to_str (inferior_ptid),
+		     (nodestr != NULL) ? nodestr : "local node");
 }
 
+/* Read executable file name for the given PID.  */
+
+static char *
+procfs_pid_to_exec_file (struct target_ops *ops, const int pid)
+{
+  int proc_fd;
+  static char proc_path[PATH_MAX];
+  ssize_t rd;
+
+  /* Read exe file name.  */
+  snprintf (proc_path, sizeof (proc_path), "%s/proc/%d/exefile",
+	    (nodestr != NULL) ? nodestr : "", pid);
+  proc_fd = open (proc_path, O_RDONLY);
+  if (proc_fd == -1)
+    return NULL;
+
+  rd = read (proc_fd, proc_path, sizeof (proc_path) - 1);
+  close (proc_fd);
+  if (rd <= 0)
+    {
+      proc_path[0] = '\0';
+      return NULL;
+    }
+  proc_path[rd] = '\0';
+  return proc_path;
+}
+
+
 /* Attach to process PID, then initialize for debugging it.  */
 static void
 procfs_attach (struct target_ops *ops, const char *args, int from_tty)
@@ -653,8 +696,8 @@ do_attach (ptid_t ptid)
   struct sigevent event;
   char path[PATH_MAX];
 
-  snprintf (path, PATH_MAX - 1, "%s/%d/as", nto_procfs_path,
-	    ptid_get_pid (ptid));
+  snprintf (path, PATH_MAX - 1, "%s%s/%d/as",
+	    (nodestr != NULL) ? nodestr : "", "/proc", ptid_get_pid (ptid));
   ctl_fd = open (path, O_RDWR);
   if (ctl_fd == -1)
     error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
@@ -872,6 +915,40 @@ procfs_xfer_partial (struct target_ops *ops, enum target_object object,
     {
     case TARGET_OBJECT_MEMORY:
       return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
+    case TARGET_OBJECT_AUXV:
+      if (readbuf != NULL)
+	{
+	  int err;
+	  CORE_ADDR initial_stack;
+	  debug_process_t procinfo;
+	  /* For 32-bit architecture, size of auxv_t is 8 bytes.  */
+	  const unsigned int sizeof_auxv_t = sizeof (auxv_t);
+	  const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
+	  int tempread;
+	  gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
+
+	  if (tempbuf == NULL)
+	    return TARGET_XFER_E_IO;
+
+	  err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
+		        sizeof procinfo, 0);
+	  if (err != EOK)
+	    return TARGET_XFER_E_IO;
+
+	  /* Similar as in the case of a core file, we read auxv from
+	     initial_stack.  */
+	  initial_stack = procinfo.initial_stack;
+
+	  /* procfs is always 'self-hosted', no byte-order manipulation. */
+	  tempread = nto_read_auxv_from_initial_stack (initial_stack, tempbuf,
+						       sizeof_tempbuf,
+						       sizeof (auxv_t));
+	  tempread = min (tempread, len) - offset;
+	  memcpy (readbuf, tempbuf + offset, tempread);
+	  *xfered_len = tempread;
+	  return tempread ? TARGET_XFER_OK : TARGET_XFER_EOF;
+	}
+      /* Fallthru */
     default:
       return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
 					    readbuf, writebuf, offset, len,
@@ -1121,7 +1198,7 @@ procfs_create_inferior (struct target_ops *ops, char *exec_file,
     }
 
   args = xstrdup (allargs);
-  breakup_args (args, exec_file ? &argv[1] : &argv[0]);
+  breakup_args (args, (exec_file != NULL) ? &argv[1] : &argv[0]);
 
   argv = nto_parse_redirection (argv, &in, &out, &err);
 
@@ -1444,6 +1521,7 @@ init_procfs_targets (void)
   t->to_interrupt = procfs_interrupt;
   t->to_have_continuable_watchpoint = 1;
   t->to_extra_thread_info = nto_extra_thread_info;
+  t->to_pid_to_exec_file = procfs_pid_to_exec_file;
 
   nto_native_ops = t;
 
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 81ee7fb..63094e8 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -31,6 +31,7 @@
 #include "solib-svr4.h"
 #include "gdbcore.h"
 #include "objfiles.h"
+#include "symfile.h"
 
 #ifdef __CYGWIN__
 #include <sys/cygwin.h>
@@ -394,3 +395,89 @@ nto_initialize_signals (void)
   signal_pass_update (SIGPHOTON, 1);
 #endif
 }
+
+
+/* Read AUXV from initial_stack.  */
+LONGEST
+nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack, gdb_byte *readbuf,
+                                  LONGEST len, size_t sizeof_auxv_t)
+{
+  gdb_byte targ32[4]; /* For 32 bit target values.  */
+  gdb_byte targ64[8]; /* For 64 bit target values.  */
+  CORE_ADDR data_ofs = 0;
+  ULONGEST anint;
+  LONGEST len_read = 0;
+  gdb_byte *buff;
+  enum bfd_endian byte_order;
+  int ptr_size;
+
+  if (sizeof_auxv_t == 16)
+    ptr_size = 8;
+  else
+    ptr_size = 4;
+
+  /* Skip over argc, argv and envp... Comment from ldd.c:
+
+     The startup frame is set-up so that we have:
+     auxv
+     NULL
+     ...
+     envp2
+     envp1 <----- void *frame + (argc + 2) * sizeof(char *)
+     NULL
+     ...
+     argv2
+     argv1
+     argc  <------ void * frame
+
+     On entry to ldd, frame gives the address of argc on the stack.  */
+  /* Read argc. 4 bytes on both 64 and 32 bit arches and luckily little
+   * endian. So we just read first 4 bytes.  */
+  if (target_read_memory (initial_stack + data_ofs, targ32, 4) != 0)
+    return 0;
+
+  byte_order = gdbarch_byte_order (target_gdbarch ());
+
+  anint = extract_unsigned_integer (targ32, sizeof (targ32), byte_order);
+
+  /* Size of pointer is assumed to be 4 bytes (32 bit arch.) */
+  data_ofs += (anint + 2) * ptr_size; /* + 2 comes from argc itself and
+                                                NULL terminating pointer in
+                                                argv.  */
+
+  /* Now loop over env table:  */
+  anint = 0;
+  while (target_read_memory (initial_stack + data_ofs, targ64, ptr_size)
+         == 0)
+    {
+      if (extract_unsigned_integer (targ64, ptr_size, byte_order) == 0)
+	anint = 1; /* Keep looping until non-null entry is found.  */
+      else if (anint)
+	break;
+      data_ofs += ptr_size;
+    }
+  initial_stack += data_ofs;
+
+  memset (readbuf, 0, len);
+  buff = readbuf;
+  while (len_read <= len-sizeof_auxv_t)
+    {
+      if (target_read_memory (initial_stack + len_read, buff, sizeof_auxv_t)
+	  == 0)
+        {
+	  /* Both 32 and 64 bit structures have int as the first field.  */
+          const ULONGEST a_type
+	    = extract_unsigned_integer (buff, sizeof (targ32), byte_order);
+
+          if (a_type == AT_NULL)
+	    break;
+	  buff += sizeof_auxv_t;
+	  len_read += sizeof_auxv_t;
+        }
+      else
+        break;
+    }
+  return len_read;
+}
+
+
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index bd85d2a..7089a12 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -168,4 +168,8 @@ int nto_in_dynsym_resolve_code (CORE_ADDR pc);
 
 char *nto_extra_thread_info (struct target_ops *self, struct thread_info *);
 
+LONGEST nto_read_auxv_from_initial_stack (CORE_ADDR inital_stack,
+					  gdb_byte *readbuf,
+					  LONGEST len, size_t sizeof_auxv_t);
+
 #endif
-- 
1.9.1


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/4] [nto] Fixes for nto procfs.
  2015-10-20 12:43     ` Aleksandar Ristovski
@ 2015-10-20 13:21       ` Aleksandar Ristovski
  2015-10-20 14:28       ` Pedro Alves
  1 sibling, 0 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 13:21 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves

[-- Attachment #1: Type: text/plain, Size: 1663 bytes --]

On 15-10-15 01:41 PM, Pedro Alves wrote:
> It would have been nicer to see this split into a fix/theme
> per patch, and add something to the commit log about each
> fix.  E.g., the aux bits could easily be a separate patch.
> 
> Anyway, this is pretty isolated to NTO bits.

I'm trying to catch up and submit local changes for previous ports.

While patches may not be minimalistic, I am trying to at least bring
certain rounded-up improvement (e.g. having a debug session).

But I will try to make more granulated patches.

> 
> LGTM with the nits below addressed.
> 
> On 10/13/2015 05:01 PM, Aleksandar Ristovski wrote:
> 
>>  	}
>>  
>>        do_cleanups (inner_cleanup);
>> @@ -599,9 +612,40 @@ procfs_files_info (struct target_ops *ignore)
>>  
>>    printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
>>  		     inf->attach_flag ? "attached" : "child",
>> -		     target_pid_to_str (inferior_ptid), nto_procfs_path);
>> +		     target_pid_to_str (inferior_ptid),
>> +		     nodestr ? nodestr : "local node");
> 
> Write 'nodestr != NULL'.

Done. Here and other places where pointer is used as a logical expression.
...
>> +  if (rd <= 0)
>> +    {
>> +      proc_path[0] = '\0';
>> +      return NULL;
>> +    }
>> +  else
>> +    proc_path[rd] = '\0';
>> +
>> +  return proc_path;
> 
> Either write:
> 
>   else
>     {
>       proc_path[rd] = '\0';
>       return proc_path;
>     }
> 
> Or drop the "else".

Dropped 'else'.

...
>> +
>> +	  if (!tempbuf)
>> +	    return TARGET_XFER_E_IO;
> 
>  if (tempbuf == NULL)
> 
> Can NTO's alloca really return NULL?

Yes.


Attached fixed version of the patch.


Thanks,

Aleksandar Ristovski


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-nto-Fixes-for-nto-procfs.patch --]
[-- Type: text/x-patch; name="0001-nto-Fixes-for-nto-procfs.patch", Size: 13407 bytes --]

From 62774c1deef7eb01108ac65636e2ccf974992d85 Mon Sep 17 00:00:00 2001
From: Aleksandar Ristovski <aristovski@qnx.com>
Date: Mon, 5 Oct 2015 10:32:16 -0400
Subject: [PATCH] [nto] Fixes for nto procfs.

gdb/ChangeLog:

	* nto-procfs.c (sys/auxv.h): Include.
	(nto_procfs_path): Rename to...
	(nodestr): ... this, and change type.
	(nto_node): Use new variable and logic accordingly.
	(procfs_open_1): Use new variable name. Use local buffer to construct
	procfs path.
	(procfs_pidlist): Use NODESTR to construct procfs path.
	(procfs_files_info): Use NODESTR to output meaningful text.
	(procfs_pid_to_exec_file): New target function.
	(do_attach): Construct procfs using NODESTR.
	(procfs_xfer_partial): Logic for reading TARGET_OBJECT_AUXV.
	(procfs_create_inferior): Compare pointer to NULL.
	(init_procfs_targets): Wire procfs_pid_to_exec_file.
	* nto-tdep.c (symfile.h): Include.
	(nto_read_auxv_from_initial_stack): New function.
	* nto-tdep.h (nto_read_auxv_from_initial_stack): New function
	declaration.
---
 gdb/nto-procfs.c | 128 ++++++++++++++++++++++++++++++++++++++++++++-----------
 gdb/nto-tdep.c   |  87 +++++++++++++++++++++++++++++++++++++
 gdb/nto-tdep.h   |   4 ++
 3 files changed, 194 insertions(+), 25 deletions(-)

diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index d659f79..176ceea 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -30,6 +30,8 @@
 #include <sys/syspage.h>
 #include <dirent.h>
 #include <sys/netmgr.h>
+#include <sys/auxv.h>
+
 #include "gdbcore.h"
 #include "inferior.h"
 #include "target.h"
@@ -73,7 +75,7 @@ static int procfs_stopped_by_watchpoint (struct target_ops *ops);
    referenced elsewhere.  'nto_procfs_node' is a flag used to say
    whether we are local, or we should get the current node descriptor
    for the remote QNX node.  */
-static char nto_procfs_path[PATH_MAX] = { "/proc" };
+static char *nodestr;
 static unsigned nto_procfs_node = ND_LOCAL_NODE;
 
 /* Return the current QNX Node, or error out.  This is a simple
@@ -85,10 +87,11 @@ nto_node (void)
 {
   unsigned node;
 
-  if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0)
+  if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0
+      || nodestr == NULL)
     return ND_LOCAL_NODE;
 
-  node = netmgr_strtond (nto_procfs_path, 0);
+  node = netmgr_strtond (nodestr, 0);
   if (node == -1)
     error (_("Lost the QNX node.  Debug session probably over."));
 
@@ -108,12 +111,12 @@ procfs_is_nto_target (bfd *abfd)
 static void
 procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
 {
-  char *nodestr;
   char *endstr;
   char buffer[50];
   int fd, total_size;
   procfs_sysinfo *sysinfo;
   struct cleanup *cleanups;
+  char nto_procfs_path[PATH_MAX];
 
   /* Offer to kill previous inferiors before opening this target.  */
   target_preopen (from_tty);
@@ -123,8 +126,11 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
   /* Set the default node used for spawning to this one,
      and only override it if there is a valid arg.  */
 
+  xfree (nodestr);
+  nodestr = NULL;
+
   nto_procfs_node = ND_LOCAL_NODE;
-  nodestr = arg ? xstrdup (arg) : NULL;
+  nodestr = (arg != NULL) ? xstrdup (arg) : NULL;
 
   init_thread_list ();
 
@@ -149,10 +155,8 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
 	    *endstr = 0;
 	}
     }
-  snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s", nodestr ? nodestr : "",
-	    "/proc");
-  if (nodestr)
-    xfree (nodestr);
+  snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s",
+	    (nodestr != NULL) ? nodestr : "", "/proc");
 
   fd = open (nto_procfs_path, O_RDONLY);
   if (fd == -1)
@@ -174,7 +178,7 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
     {
       total_size = sysinfo->total_size;
       sysinfo = alloca (total_size);
-      if (!sysinfo)
+      if (sysinfo == NULL)
 	{
 	  printf_filtered ("Memory error: %d (%s)\n", errno,
 			   safe_strerror (errno));
@@ -359,7 +363,7 @@ procfs_pidlist (char *args, int from_tty)
 {
   DIR *dp = NULL;
   struct dirent *dirp = NULL;
-  char buf[512];
+  char buf[PATH_MAX];
   procfs_info *pidinfo = NULL;
   procfs_debuginfo *info = NULL;
   procfs_status *status = NULL;
@@ -367,12 +371,16 @@ procfs_pidlist (char *args, int from_tty)
   pid_t pid;
   char name[512];
   struct cleanup *cleanups;
+  char procfs_dir[PATH_MAX];
 
-  dp = opendir (nto_procfs_path);
+  snprintf (procfs_dir, sizeof (procfs_dir), "%s%s",
+	    (nodestr != NULL) ? nodestr : "", "/proc");
+
+  dp = opendir (procfs_dir);
   if (dp == NULL)
     {
       fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
-			  nto_procfs_path, errno, safe_strerror (errno));
+			  procfs_dir, errno, safe_strerror (errno));
       return;
     }
 
@@ -395,7 +403,9 @@ procfs_pidlist (char *args, int from_tty)
 	      do_cleanups (cleanups);
 	      return;
 	    }
-	  snprintf (buf, 511, "%s/%s/as", nto_procfs_path, dirp->d_name);
+	  snprintf (buf, sizeof (buf), "%s%s/%s/as",
+		    (nodestr != NULL) ? nodestr : "",
+		    "/proc", dirp->d_name);
 	  pid = atoi (dirp->d_name);
 	}
       while (pid == 0);
@@ -406,8 +416,7 @@ procfs_pidlist (char *args, int from_tty)
 	{
 	  fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
 			      buf, errno, safe_strerror (errno));
-	  do_cleanups (cleanups);
-	  return;
+	  continue;
 	}
       inner_cleanup = make_cleanup_close (fd);
 
@@ -431,11 +440,16 @@ procfs_pidlist (char *args, int from_tty)
       status = (procfs_status *) buf;
       for (status->tid = 1; status->tid <= num_threads; status->tid++)
 	{
-	  if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
-	      && status->tid != 0)
-	    break;
-	  if (status->tid != 0)
-	    printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
+	  const int err
+	    = devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0);
+	  printf_filtered ("%s - %d", name, pid);
+	  if (err == EOK && status->tid != 0)
+	    printf_filtered ("/%d\n", status->tid);
+	  else
+	    {
+	      printf_filtered ("\n");
+	      break;
+	    }
 	}
 
       do_cleanups (inner_cleanup);
@@ -599,9 +613,38 @@ procfs_files_info (struct target_ops *ignore)
 
   printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
 		     inf->attach_flag ? "attached" : "child",
-		     target_pid_to_str (inferior_ptid), nto_procfs_path);
+		     target_pid_to_str (inferior_ptid),
+		     (nodestr != NULL) ? nodestr : "local node");
 }
 
+/* Read executable file name for the given PID.  */
+
+static char *
+procfs_pid_to_exec_file (struct target_ops *ops, const int pid)
+{
+  int proc_fd;
+  static char proc_path[PATH_MAX];
+  ssize_t rd;
+
+  /* Read exe file name.  */
+  snprintf (proc_path, sizeof (proc_path), "%s/proc/%d/exefile",
+	    (nodestr != NULL) ? nodestr : "", pid);
+  proc_fd = open (proc_path, O_RDONLY);
+  if (proc_fd == -1)
+    return NULL;
+
+  rd = read (proc_fd, proc_path, sizeof (proc_path) - 1);
+  close (proc_fd);
+  if (rd <= 0)
+    {
+      proc_path[0] = '\0';
+      return NULL;
+    }
+  proc_path[rd] = '\0';
+  return proc_path;
+}
+
+
 /* Attach to process PID, then initialize for debugging it.  */
 static void
 procfs_attach (struct target_ops *ops, const char *args, int from_tty)
@@ -653,8 +696,8 @@ do_attach (ptid_t ptid)
   struct sigevent event;
   char path[PATH_MAX];
 
-  snprintf (path, PATH_MAX - 1, "%s/%d/as", nto_procfs_path,
-	    ptid_get_pid (ptid));
+  snprintf (path, PATH_MAX - 1, "%s%s/%d/as",
+	    (nodestr != NULL) ? nodestr : "", "/proc", ptid_get_pid (ptid));
   ctl_fd = open (path, O_RDWR);
   if (ctl_fd == -1)
     error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
@@ -872,6 +915,40 @@ procfs_xfer_partial (struct target_ops *ops, enum target_object object,
     {
     case TARGET_OBJECT_MEMORY:
       return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
+    case TARGET_OBJECT_AUXV:
+      if (readbuf != NULL)
+	{
+	  int err;
+	  CORE_ADDR initial_stack;
+	  debug_process_t procinfo;
+	  /* For 32-bit architecture, size of auxv_t is 8 bytes.  */
+	  const unsigned int sizeof_auxv_t = sizeof (auxv_t);
+	  const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
+	  int tempread;
+	  gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
+
+	  if (tempbuf == NULL)
+	    return TARGET_XFER_E_IO;
+
+	  err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
+		        sizeof procinfo, 0);
+	  if (err != EOK)
+	    return TARGET_XFER_E_IO;
+
+	  /* Similar as in the case of a core file, we read auxv from
+	     initial_stack.  */
+	  initial_stack = procinfo.initial_stack;
+
+	  /* procfs is always 'self-hosted', no byte-order manipulation. */
+	  tempread = nto_read_auxv_from_initial_stack (initial_stack, tempbuf,
+						       sizeof_tempbuf,
+						       sizeof (auxv_t));
+	  tempread = min (tempread, len) - offset;
+	  memcpy (readbuf, tempbuf + offset, tempread);
+	  *xfered_len = tempread;
+	  return tempread ? TARGET_XFER_OK : TARGET_XFER_EOF;
+	}
+      /* Fallthru */
     default:
       return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
 					    readbuf, writebuf, offset, len,
@@ -1121,7 +1198,7 @@ procfs_create_inferior (struct target_ops *ops, char *exec_file,
     }
 
   args = xstrdup (allargs);
-  breakup_args (args, exec_file ? &argv[1] : &argv[0]);
+  breakup_args (args, (exec_file != NULL) ? &argv[1] : &argv[0]);
 
   argv = nto_parse_redirection (argv, &in, &out, &err);
 
@@ -1444,6 +1521,7 @@ init_procfs_targets (void)
   t->to_interrupt = procfs_interrupt;
   t->to_have_continuable_watchpoint = 1;
   t->to_extra_thread_info = nto_extra_thread_info;
+  t->to_pid_to_exec_file = procfs_pid_to_exec_file;
 
   nto_native_ops = t;
 
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 81ee7fb..63094e8 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -31,6 +31,7 @@
 #include "solib-svr4.h"
 #include "gdbcore.h"
 #include "objfiles.h"
+#include "symfile.h"
 
 #ifdef __CYGWIN__
 #include <sys/cygwin.h>
@@ -394,3 +395,89 @@ nto_initialize_signals (void)
   signal_pass_update (SIGPHOTON, 1);
 #endif
 }
+
+
+/* Read AUXV from initial_stack.  */
+LONGEST
+nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack, gdb_byte *readbuf,
+                                  LONGEST len, size_t sizeof_auxv_t)
+{
+  gdb_byte targ32[4]; /* For 32 bit target values.  */
+  gdb_byte targ64[8]; /* For 64 bit target values.  */
+  CORE_ADDR data_ofs = 0;
+  ULONGEST anint;
+  LONGEST len_read = 0;
+  gdb_byte *buff;
+  enum bfd_endian byte_order;
+  int ptr_size;
+
+  if (sizeof_auxv_t == 16)
+    ptr_size = 8;
+  else
+    ptr_size = 4;
+
+  /* Skip over argc, argv and envp... Comment from ldd.c:
+
+     The startup frame is set-up so that we have:
+     auxv
+     NULL
+     ...
+     envp2
+     envp1 <----- void *frame + (argc + 2) * sizeof(char *)
+     NULL
+     ...
+     argv2
+     argv1
+     argc  <------ void * frame
+
+     On entry to ldd, frame gives the address of argc on the stack.  */
+  /* Read argc. 4 bytes on both 64 and 32 bit arches and luckily little
+   * endian. So we just read first 4 bytes.  */
+  if (target_read_memory (initial_stack + data_ofs, targ32, 4) != 0)
+    return 0;
+
+  byte_order = gdbarch_byte_order (target_gdbarch ());
+
+  anint = extract_unsigned_integer (targ32, sizeof (targ32), byte_order);
+
+  /* Size of pointer is assumed to be 4 bytes (32 bit arch.) */
+  data_ofs += (anint + 2) * ptr_size; /* + 2 comes from argc itself and
+                                                NULL terminating pointer in
+                                                argv.  */
+
+  /* Now loop over env table:  */
+  anint = 0;
+  while (target_read_memory (initial_stack + data_ofs, targ64, ptr_size)
+         == 0)
+    {
+      if (extract_unsigned_integer (targ64, ptr_size, byte_order) == 0)
+	anint = 1; /* Keep looping until non-null entry is found.  */
+      else if (anint)
+	break;
+      data_ofs += ptr_size;
+    }
+  initial_stack += data_ofs;
+
+  memset (readbuf, 0, len);
+  buff = readbuf;
+  while (len_read <= len-sizeof_auxv_t)
+    {
+      if (target_read_memory (initial_stack + len_read, buff, sizeof_auxv_t)
+	  == 0)
+        {
+	  /* Both 32 and 64 bit structures have int as the first field.  */
+          const ULONGEST a_type
+	    = extract_unsigned_integer (buff, sizeof (targ32), byte_order);
+
+          if (a_type == AT_NULL)
+	    break;
+	  buff += sizeof_auxv_t;
+	  len_read += sizeof_auxv_t;
+        }
+      else
+        break;
+    }
+  return len_read;
+}
+
+
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index bd85d2a..7089a12 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -168,4 +168,8 @@ int nto_in_dynsym_resolve_code (CORE_ADDR pc);
 
 char *nto_extra_thread_info (struct target_ops *self, struct thread_info *);
 
+LONGEST nto_read_auxv_from_initial_stack (CORE_ADDR inital_stack,
+					  gdb_byte *readbuf,
+					  LONGEST len, size_t sizeof_auxv_t);
+
 #endif
-- 
1.9.1


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/4] [nto] Fixes for nto procfs.
  2015-10-20 12:43     ` Aleksandar Ristovski
  2015-10-20 13:21       ` Aleksandar Ristovski
@ 2015-10-20 14:28       ` Pedro Alves
  2015-10-20 14:28         ` [PATCH 0/3] (patch 2/4, v2) Break patch 2/4 into 3 Aleksandar Ristovski
  1 sibling, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-20 14:28 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/20/2015 01:43 PM, Aleksandar Ristovski wrote:
> On 15-10-15 01:41 PM, Pedro Alves wrote:
>> It would have been nicer to see this split into a fix/theme
>> per patch, and add something to the commit log about each
>> fix.  E.g., the aux bits could easily be a separate patch.
>>
>> Anyway, this is pretty isolated to NTO bits.
> 
> I'm trying to catch up and submit local changes for previous ports.

That shouldn't prevent splitting up changes per logical units.

> 
> While patches may not be minimalistic, I am trying to at least bring
> certain rounded-up improvement (e.g. having a debug session).

The procfs_pid_to_exec_file change here doesn't look like the sort
that would be necessary for plainly having a debug session,
for instance.

AFAICS, there are a few different changes here:

 - Implementing procfs_pid_to_exec_file, so that "attach" can
   work without having the user specify an executable.

 - Some accommodations for different procfs paths.

 - Reading TARGET_OBJECT_AUXV off the stack.

 - Some minor fixes here and there.

My trouble, along with having these all mixed up in a single
patch, is that as is there no clue on why these changes
are necessary at all (either in comments or in the commit log).

> 
> But I will try to make more granulated patches.
> 

Thanks.

>>
>> LGTM with the nits below addressed.
>>
>> On 10/13/2015 05:01 PM, Aleksandar Ristovski wrote:
>>
>>>  	}
>>>  
>>>        do_cleanups (inner_cleanup);
>>> @@ -599,9 +612,40 @@ procfs_files_info (struct target_ops *ignore)
>>>  
>>>    printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
>>>  		     inf->attach_flag ? "attached" : "child",
>>> -		     target_pid_to_str (inferior_ptid), nto_procfs_path);
>>> +		     target_pid_to_str (inferior_ptid),
>>> +		     nodestr ? nodestr : "local node");
>>
>> Write 'nodestr != NULL'.
> 
> Done. Here and other places where pointer is used as a logical expression.
> ...
>>> +  if (rd <= 0)
>>> +    {
>>> +      proc_path[0] = '\0';
>>> +      return NULL;
>>> +    }
>>> +  else
>>> +    proc_path[rd] = '\0';
>>> +
>>> +  return proc_path;
>>
>> Either write:
>>
>>   else
>>     {
>>       proc_path[rd] = '\0';
>>       return proc_path;
>>     }
>>
>> Or drop the "else".
> 
> Dropped 'else'.
> 
> ...
>>> +
>>> +	  if (!tempbuf)
>>> +	    return TARGET_XFER_E_IO;
>>
>>  if (tempbuf == NULL)
>>
>> Can NTO's alloca really return NULL?
> 
> Yes.
> 
> 
> Attached fixed version of the patch.

This looks better, thanks.

-- 
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH 0/3] (patch 2/4, v2) Break patch 2/4 into 3.
  2015-10-20 14:28       ` Pedro Alves
@ 2015-10-20 14:28         ` Aleksandar Ristovski
  2015-10-20 14:28           ` [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV Aleksandar Ristovski
                             ` (2 more replies)
  0 siblings, 3 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 14:28 UTC (permalink / raw)
  To: gdb-patches; +Cc: palves, Aleksandar Ristovski


This patch breaks the patch 2/4 into 3:

  [nto] Fixes for nto procfs.
  [nto] Implement TARGET_OBJECT_AUXV.
  [nto] Implement procfs_pid_to_exec_file.

Thank you,
Aleksandar Ristovski


^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV.
  2015-10-20 14:28         ` [PATCH 0/3] (patch 2/4, v2) Break patch 2/4 into 3 Aleksandar Ristovski
@ 2015-10-20 14:28           ` Aleksandar Ristovski
  2015-10-20 15:24             ` Pedro Alves
  2015-10-20 14:29           ` [PATCH 1/3] (patch 2/4, v2) [nto] Fixes for nto procfs Aleksandar Ristovski
  2015-10-20 15:03           ` [PATCH 3/3] (patch 2/4, v2) [nto] Implement procfs_pid_to_exec_file Aleksandar Ristovski
  2 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 14:28 UTC (permalink / raw)
  To: gdb-patches; +Cc: palves, Aleksandar Ristovski

gdb/ChangeLog:

	* nto-procfs.c (sys/auxv.h): Include.
	(procfs_xfer_partial): Implement TARGET_OBJECT_AUXV.
	* gdb/nto-tdep.c (nto_read_auxv_from_initial_stack): New function.
	* gdb/nto-tdep.h (nto_read_auxv_from_initial_stack): New declaration.
---
 gdb/nto-procfs.c | 36 ++++++++++++++++++++++++
 gdb/nto-tdep.c   | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/nto-tdep.h   |  3 ++
 3 files changed, 122 insertions(+)

diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index 264d88b..ac54c32 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -30,6 +30,8 @@
 #include <sys/syspage.h>
 #include <dirent.h>
 #include <sys/netmgr.h>
+#include <sys/auxv.h>
+
 #include "gdbcore.h"
 #include "inferior.h"
 #include "target.h"
@@ -885,6 +887,40 @@ procfs_xfer_partial (struct target_ops *ops, enum target_object object,
     {
     case TARGET_OBJECT_MEMORY:
       return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
+    case TARGET_OBJECT_AUXV:
+      if (readbuf != NULL)
+	{
+	  int err;
+	  CORE_ADDR initial_stack;
+	  debug_process_t procinfo;
+	  /* For 32-bit architecture, size of auxv_t is 8 bytes.  */
+	  const unsigned int sizeof_auxv_t = sizeof (auxv_t);
+	  const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
+	  int tempread;
+	  gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
+
+	  if (tempbuf == NULL)
+	    return TARGET_XFER_E_IO;
+
+	  err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
+		        sizeof procinfo, 0);
+	  if (err != EOK)
+	    return TARGET_XFER_E_IO;
+
+	  /* Similar as in the case of a core file, we read auxv from
+	     initial_stack.  */
+	  initial_stack = procinfo.initial_stack;
+
+	  /* procfs is always 'self-hosted', no byte-order manipulation. */
+	  tempread = nto_read_auxv_from_initial_stack (initial_stack, tempbuf,
+						       sizeof_tempbuf,
+						       sizeof (auxv_t));
+	  tempread = min (tempread, len) - offset;
+	  memcpy (readbuf, tempbuf + offset, tempread);
+	  *xfered_len = tempread;
+	  return tempread ? TARGET_XFER_OK : TARGET_XFER_EOF;
+	}
+	/* Fallthru */
     default:
       return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
 					    readbuf, writebuf, offset, len,
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 81ee7fb..62eb88a 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -394,3 +394,86 @@ nto_initialize_signals (void)
   signal_pass_update (SIGPHOTON, 1);
 #endif
 }
+
+/* Read AUXV from initial_stack.  */
+LONGEST
+nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack, gdb_byte *readbuf,
+                                  LONGEST len, size_t sizeof_auxv_t)
+{
+  gdb_byte targ32[4]; /* For 32 bit target values.  */
+  gdb_byte targ64[8]; /* For 64 bit target values.  */
+  CORE_ADDR data_ofs = 0;
+  ULONGEST anint;
+  LONGEST len_read = 0;
+  gdb_byte *buff;
+  enum bfd_endian byte_order;
+  int ptr_size;
+
+  if (sizeof_auxv_t == 16)
+    ptr_size = 8;
+  else
+    ptr_size = 4;
+
+  /* Skip over argc, argv and envp... Comment from ldd.c:
+
+     The startup frame is set-up so that we have:
+     auxv
+     NULL
+     ...
+     envp2
+     envp1 <----- void *frame + (argc + 2) * sizeof(char *)
+     NULL
+     ...
+     argv2
+     argv1
+     argc  <------ void * frame
+
+     On entry to ldd, frame gives the address of argc on the stack.  */
+  /* Read argc. 4 bytes on both 64 and 32 bit arches and luckily little
+   * endian. So we just read first 4 bytes.  */
+  if (target_read_memory (initial_stack + data_ofs, targ32, 4) != 0)
+    return 0;
+
+  byte_order = gdbarch_byte_order (target_gdbarch ());
+
+  anint = extract_unsigned_integer (targ32, sizeof (targ32), byte_order);
+
+  /* Size of pointer is assumed to be 4 bytes (32 bit arch.) */
+  data_ofs += (anint + 2) * ptr_size; /* + 2 comes from argc itself and
+                                                NULL terminating pointer in
+                                                argv.  */
+
+  /* Now loop over env table:  */
+  anint = 0;
+  while (target_read_memory (initial_stack + data_ofs, targ64, ptr_size)
+         == 0)
+    {
+      if (extract_unsigned_integer (targ64, ptr_size, byte_order) == 0)
+	anint = 1; /* Keep looping until non-null entry is found.  */
+      else if (anint)
+	break;
+      data_ofs += ptr_size;
+    }
+  initial_stack += data_ofs;
+
+  memset (readbuf, 0, len);
+  buff = readbuf;
+  while (len_read <= len-sizeof_auxv_t)
+    {
+      if (target_read_memory (initial_stack + len_read, buff, sizeof_auxv_t)
+	  == 0)
+        {
+	  /* Both 32 and 64 bit structures have int as the first field.  */
+          const ULONGEST a_type
+	    = extract_unsigned_integer (buff, sizeof (targ32), byte_order);
+
+          if (a_type == AT_NULL)
+	    break;
+	  buff += sizeof_auxv_t;
+	  len_read += sizeof_auxv_t;
+        }
+      else
+        break;
+    }
+  return len_read;
+}
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index bd85d2a..d029f07 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -168,4 +168,7 @@ int nto_in_dynsym_resolve_code (CORE_ADDR pc);
 
 char *nto_extra_thread_info (struct target_ops *self, struct thread_info *);
 
+LONGEST nto_read_auxv_from_initial_stack (CORE_ADDR inital_stack,
+					  gdb_byte *readbuf,
+					  LONGEST len, size_t sizeof_auxv_t);
 #endif
-- 
1.9.1

^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH 1/3] (patch 2/4, v2) [nto] Fixes for nto procfs.
  2015-10-20 14:28         ` [PATCH 0/3] (patch 2/4, v2) Break patch 2/4 into 3 Aleksandar Ristovski
  2015-10-20 14:28           ` [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV Aleksandar Ristovski
@ 2015-10-20 14:29           ` Aleksandar Ristovski
  2015-10-20 15:20             ` Pedro Alves
  2015-10-20 15:03           ` [PATCH 3/3] (patch 2/4, v2) [nto] Implement procfs_pid_to_exec_file Aleksandar Ristovski
  2 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 14:29 UTC (permalink / raw)
  To: gdb-patches; +Cc: palves, Aleksandar Ristovski

Fix errnoeus construction of procfs path. The issue is, after the first
info pidlist or first run, the path for local node (the most common node)
will be reset to empty which makes subsequent queries and runs impossible.

gdb/ChangeLog:

	* nto-procfs.c (nto_procfs_path): Rename to...
	(nodestr): ... this, and change type.
	(nto_node): Use new variable and logic accordingly.
	(procfs_open_1): Use new variable name. Use local buffer to construct
	procfrs path.
	(procfs_pidlist): Use NODESTR to construct procfs path.
	(procfs_files_info): Use NODESTR to output meaningful text.
	(do_attach): Construct procfs using NODESTR.
	(procfs_create_inferior): Compare pointer to NULL.
---
 gdb/nto-procfs.c | 63 ++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 25 deletions(-)

diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index d659f79..264d88b 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -73,7 +73,7 @@ static int procfs_stopped_by_watchpoint (struct target_ops *ops);
    referenced elsewhere.  'nto_procfs_node' is a flag used to say
    whether we are local, or we should get the current node descriptor
    for the remote QNX node.  */
-static char nto_procfs_path[PATH_MAX] = { "/proc" };
+static char *nodestr;
 static unsigned nto_procfs_node = ND_LOCAL_NODE;
 
 /* Return the current QNX Node, or error out.  This is a simple
@@ -85,10 +85,11 @@ nto_node (void)
 {
   unsigned node;
 
-  if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0)
+  if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0
+      || nodestr == NULL)
     return ND_LOCAL_NODE;
 
-  node = netmgr_strtond (nto_procfs_path, 0);
+  node = netmgr_strtond (nodestr, 0);
   if (node == -1)
     error (_("Lost the QNX node.  Debug session probably over."));
 
@@ -108,12 +109,12 @@ procfs_is_nto_target (bfd *abfd)
 static void
 procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
 {
-  char *nodestr;
   char *endstr;
   char buffer[50];
   int fd, total_size;
   procfs_sysinfo *sysinfo;
   struct cleanup *cleanups;
+  char nto_procfs_path[PATH_MAX];
 
   /* Offer to kill previous inferiors before opening this target.  */
   target_preopen (from_tty);
@@ -123,8 +124,11 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
   /* Set the default node used for spawning to this one,
      and only override it if there is a valid arg.  */
 
+  xfree (nodestr);
+  nodestr = NULL;
+
   nto_procfs_node = ND_LOCAL_NODE;
-  nodestr = arg ? xstrdup (arg) : NULL;
+  nodestr = (arg != NULL) ? xstrdup (arg) : NULL;
 
   init_thread_list ();
 
@@ -149,10 +153,8 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
 	    *endstr = 0;
 	}
     }
-  snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s", nodestr ? nodestr : "",
-	    "/proc");
-  if (nodestr)
-    xfree (nodestr);
+  snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s",
+	    (nodestr != NULL) ? nodestr : "", "/proc");
 
   fd = open (nto_procfs_path, O_RDONLY);
   if (fd == -1)
@@ -174,7 +176,7 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
     {
       total_size = sysinfo->total_size;
       sysinfo = alloca (total_size);
-      if (!sysinfo)
+      if (sysinfo == NULL)
 	{
 	  printf_filtered ("Memory error: %d (%s)\n", errno,
 			   safe_strerror (errno));
@@ -359,7 +361,7 @@ procfs_pidlist (char *args, int from_tty)
 {
   DIR *dp = NULL;
   struct dirent *dirp = NULL;
-  char buf[512];
+  char buf[PATH_MAX];
   procfs_info *pidinfo = NULL;
   procfs_debuginfo *info = NULL;
   procfs_status *status = NULL;
@@ -367,12 +369,16 @@ procfs_pidlist (char *args, int from_tty)
   pid_t pid;
   char name[512];
   struct cleanup *cleanups;
+  char procfs_dir[PATH_MAX];
 
-  dp = opendir (nto_procfs_path);
+  snprintf (procfs_dir, sizeof (procfs_dir), "%s%s",
+	    (nodestr != NULL) ? nodestr : "", "/proc");
+
+  dp = opendir (procfs_dir);
   if (dp == NULL)
     {
       fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
-			  nto_procfs_path, errno, safe_strerror (errno));
+			  procfs_dir, errno, safe_strerror (errno));
       return;
     }
 
@@ -395,7 +401,9 @@ procfs_pidlist (char *args, int from_tty)
 	      do_cleanups (cleanups);
 	      return;
 	    }
-	  snprintf (buf, 511, "%s/%s/as", nto_procfs_path, dirp->d_name);
+	  snprintf (buf, sizeof (buf), "%s%s/%s/as",
+		    (nodestr != NULL) ? nodestr : "",
+		    "/proc", dirp->d_name);
 	  pid = atoi (dirp->d_name);
 	}
       while (pid == 0);
@@ -406,8 +414,7 @@ procfs_pidlist (char *args, int from_tty)
 	{
 	  fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
 			      buf, errno, safe_strerror (errno));
-	  do_cleanups (cleanups);
-	  return;
+	  continue;
 	}
       inner_cleanup = make_cleanup_close (fd);
 
@@ -431,11 +438,16 @@ procfs_pidlist (char *args, int from_tty)
       status = (procfs_status *) buf;
       for (status->tid = 1; status->tid <= num_threads; status->tid++)
 	{
-	  if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
-	      && status->tid != 0)
-	    break;
-	  if (status->tid != 0)
-	    printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
+	  const int err
+	    = devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0);
+	  printf_filtered ("%s - %d", name, pid);
+	  if (err == EOK && status->tid != 0)
+	    printf_filtered ("/%d\n", status->tid);
+	  else
+	    {
+	      printf_filtered ("\n");
+	      break;
+	    }
 	}
 
       do_cleanups (inner_cleanup);
@@ -599,7 +611,8 @@ procfs_files_info (struct target_ops *ignore)
 
   printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
 		     inf->attach_flag ? "attached" : "child",
-		     target_pid_to_str (inferior_ptid), nto_procfs_path);
+		     target_pid_to_str (inferior_ptid),
+		     (nodestr != NULL) ? nodestr : "local node");
 }
 
 /* Attach to process PID, then initialize for debugging it.  */
@@ -653,8 +666,8 @@ do_attach (ptid_t ptid)
   struct sigevent event;
   char path[PATH_MAX];
 
-  snprintf (path, PATH_MAX - 1, "%s/%d/as", nto_procfs_path,
-	    ptid_get_pid (ptid));
+  snprintf (path, PATH_MAX - 1, "%s%s/%d/as",
+	    (nodestr != NULL) ? nodestr : "", "/proc", ptid_get_pid (ptid));
   ctl_fd = open (path, O_RDWR);
   if (ctl_fd == -1)
     error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
@@ -1121,7 +1134,7 @@ procfs_create_inferior (struct target_ops *ops, char *exec_file,
     }
 
   args = xstrdup (allargs);
-  breakup_args (args, exec_file ? &argv[1] : &argv[0]);
+  breakup_args (args, (exec_file != NULL) ? &argv[1] : &argv[0]);
 
   argv = nto_parse_redirection (argv, &in, &out, &err);
 
-- 
1.9.1

^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH 3/3] (patch 2/4, v2) [nto] Implement procfs_pid_to_exec_file.
  2015-10-20 14:28         ` [PATCH 0/3] (patch 2/4, v2) Break patch 2/4 into 3 Aleksandar Ristovski
  2015-10-20 14:28           ` [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV Aleksandar Ristovski
  2015-10-20 14:29           ` [PATCH 1/3] (patch 2/4, v2) [nto] Fixes for nto procfs Aleksandar Ristovski
@ 2015-10-20 15:03           ` Aleksandar Ristovski
  2015-10-20 15:25             ` Pedro Alves
  2 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 15:03 UTC (permalink / raw)
  To: gdb-patches; +Cc: palves, Aleksandar Ristovski

gdb/ChangeLog:

	* gdb/nto-procfs.c (procfs_pid_to_exec_file): New function.
	(init_procfs_targets): Wire new function.
---
 gdb/nto-procfs.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index ac54c32..e7882ba 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -617,6 +617,33 @@ procfs_files_info (struct target_ops *ignore)
 		     (nodestr != NULL) ? nodestr : "local node");
 }
 
+/* Read executable file name for the given PID.  */
+
+static char *
+procfs_pid_to_exec_file (struct target_ops *ops, const int pid)
+{
+  int proc_fd;
+  static char proc_path[PATH_MAX];
+  ssize_t rd;
+
+  /* Read exe file name.  */
+  snprintf (proc_path, sizeof (proc_path), "%s/proc/%d/exefile",
+	    (nodestr != NULL) ? nodestr : "", pid);
+  proc_fd = open (proc_path, O_RDONLY);
+  if (proc_fd == -1)
+    return NULL;
+
+  rd = read (proc_fd, proc_path, sizeof (proc_path) - 1);
+  close (proc_fd);
+  if (rd <= 0)
+    {
+      proc_path[0] = '\0';
+      return NULL;
+    }
+  proc_path[rd] = '\0';
+  return proc_path;
+}
+
 /* Attach to process PID, then initialize for debugging it.  */
 static void
 procfs_attach (struct target_ops *ops, const char *args, int from_tty)
@@ -1493,6 +1520,7 @@ init_procfs_targets (void)
   t->to_interrupt = procfs_interrupt;
   t->to_have_continuable_watchpoint = 1;
   t->to_extra_thread_info = nto_extra_thread_info;
+  t->to_pid_to_exec_file = procfs_pid_to_exec_file;
 
   nto_native_ops = t;
 
-- 
1.9.1

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 1/3] (patch 2/4, v2) [nto] Fixes for nto procfs.
  2015-10-20 14:29           ` [PATCH 1/3] (patch 2/4, v2) [nto] Fixes for nto procfs Aleksandar Ristovski
@ 2015-10-20 15:20             ` Pedro Alves
  2015-10-20 17:13               ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-20 15:20 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/20/2015 03:28 PM, Aleksandar Ristovski wrote:
> Fix errnoeus construction of procfs path. The issue is, after the first
> info pidlist or first run, the path for local node (the most common node)
> will be reset to empty which makes subsequent queries and runs impossible.

Thanks.

> 
> gdb/ChangeLog:
> 
> 	* nto-procfs.c (nto_procfs_path): Rename to...
> 	(nodestr): ... this, and change type.
> 	(nto_node): Use new variable and logic accordingly.
> 	(procfs_open_1): Use new variable name. Use local buffer to construct
> 	procfrs path.
> 	(procfs_pidlist): Use NODESTR to construct procfs path.
> 	(procfs_files_info): Use NODESTR to output meaningful text.
> 	(do_attach): Construct procfs using NODESTR.
> 	(procfs_create_inferior): Compare pointer to NULL.

This is OK.

There are some bits here that seem a bit unrelated, but we're
hitting the point of diminishing returns.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV.
  2015-10-20 14:28           ` [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV Aleksandar Ristovski
@ 2015-10-20 15:24             ` Pedro Alves
  2015-10-20 16:03               ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-20 15:24 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

Does this result in any visible improvement?  I assume that
at least, "info auxv" now works [1] [2].  It'd be really nice to have a
blurb in the commit log mentioning what motivated this.

[1] - BTW, if you enable gdb.base/auxv.exp on NTO, does it pass?

On 10/20/2015 03:28 PM, Aleksandar Ristovski wrote:
> gdb/ChangeLog:
> 
> 	* nto-procfs.c (sys/auxv.h): Include.
> 	(procfs_xfer_partial): Implement TARGET_OBJECT_AUXV.
> 	* gdb/nto-tdep.c (nto_read_auxv_from_initial_stack): New function.
> 	* gdb/nto-tdep.h (nto_read_auxv_from_initial_stack): New declaration.

Drop the "gdb/" in the file paths.

> @@ -885,6 +887,40 @@ procfs_xfer_partial (struct target_ops *ops, enum target_object object,
>      {
>      case TARGET_OBJECT_MEMORY:
>        return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
> +    case TARGET_OBJECT_AUXV:
> +      if (readbuf != NULL)
> +	{
> +	  int err;
> +	  CORE_ADDR initial_stack;
> +	  debug_process_t procinfo;
> +	  /* For 32-bit architecture, size of auxv_t is 8 bytes.  */
> +	  const unsigned int sizeof_auxv_t = sizeof (auxv_t);
> +	  const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
> +	  int tempread;
> +	  gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
> +
> +	  if (tempbuf == NULL)
> +	    return TARGET_XFER_E_IO;
> +
> +	  err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
> +		        sizeof procinfo, 0);
> +	  if (err != EOK)
> +	    return TARGET_XFER_E_IO;
> +
> +	  /* Similar as in the case of a core file, we read auxv from
> +	     initial_stack.  */

Hmm, where's this "similar" you refer to?  AFAICS, for cores, BFD
extracts it from the NT_AUVX note.

> +	  initial_stack = procinfo.initial_stack;
> +
> +	  /* procfs is always 'self-hosted', no byte-order manipulation. */

Double-space after period.

Otherwise looks OK.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 3/3] (patch 2/4, v2) [nto] Implement procfs_pid_to_exec_file.
  2015-10-20 15:03           ` [PATCH 3/3] (patch 2/4, v2) [nto] Implement procfs_pid_to_exec_file Aleksandar Ristovski
@ 2015-10-20 15:25             ` Pedro Alves
  2015-10-20 18:11               ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-20 15:25 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/20/2015 03:28 PM, Aleksandar Ristovski wrote:
> gdb/ChangeLog:
> 
> 	* gdb/nto-procfs.c (procfs_pid_to_exec_file): New function.
> 	(init_procfs_targets): Wire new function.
> ---
>  gdb/nto-procfs.c | 28 ++++++++++++++++++++++++++++
>  1 file changed, 28 insertions(+)
> 
> diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
> index ac54c32..e7882ba 100644
> --- a/gdb/nto-procfs.c
> +++ b/gdb/nto-procfs.c
> @@ -617,6 +617,33 @@ procfs_files_info (struct target_ops *ignore)
>  		     (nodestr != NULL) ? nodestr : "local node");
>  }
>  
> +/* Read executable file name for the given PID.  */
> +

Please write instead:

/* Target to_pid_to_exec_file implementation.  */

> +static char *
> +procfs_pid_to_exec_file (struct target_ops *ops, const int pid)
> +{

OK with that change.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV.
  2015-10-20 15:24             ` Pedro Alves
@ 2015-10-20 16:03               ` Aleksandar Ristovski
  2015-10-20 16:48                 ` Pedro Alves
  0 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 16:03 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves

[-- Attachment #1: Type: text/plain, Size: 2697 bytes --]

On 15-10-20 11:20 AM, Pedro Alves wrote:
> Does this result in any visible improvement?  I assume that
> at least, "info auxv" now works [1] [2].  It'd be really nice to have a
> blurb in the commit log mentioning what motivated this.

Yes, info auxv works on a live process. For the core I have other
patches that need to go in first, but the mechanism of getting auxv
remains the same; only determining initial stack changes.

I will add something to the commit log.
"Fix 'info auxv' for nto."


> 
> [1] - BTW, if you enable gdb.base/auxv.exp on NTO, does it pass?
> 

It fails since we have AT_* entries that are specific to nto, and get
printed as ??? which causes regex to not match. I have it patched
internally and print them out, but didn't think it would be acceptable
upstream.

(gdb) PASS: gdb.base/auxv.exp: continue
info auxv^M
3    AT_PHDR              Program headers for program    0x8048034^M
4    AT_PHENT             Size of program header entry   32^M
5    AT_PHNUM             Number of program headers      8^M
9    AT_ENTRY             Entry point of program         0x8048668^M
6    AT_PAGESZ            System page size               4096^M
7    AT_BASE              Base address of interpreter    0xb0300000^M
12   AT_EUID              Effective user ID              2050^M
13   AT_GID               Real group ID                  3221225482^M
47   ???                                                 0x8047f7d^M
WARNING: Unrecognized tag value: 47   ???
                  0x8047f7d^M

(gdb) FAIL: gdb.base/auxv.exp: info auxv on live process


> On 10/20/2015 03:28 PM, Aleksandar Ristovski wrote:
>> gdb/ChangeLog:
>>
>> 	* nto-procfs.c (sys/auxv.h): Include.
>> 	(procfs_xfer_partial): Implement TARGET_OBJECT_AUXV.
>> 	* gdb/nto-tdep.c (nto_read_auxv_from_initial_stack): New function.
>> 	* gdb/nto-tdep.h (nto_read_auxv_from_initial_stack): New declaration.
> 
> Drop the "gdb/" in the file paths.
> 

Ok, sorry. Used 'mklog' script and didn't remove gdb/.

>> @@ -885,6 +887,40 @@ procfs_xfer_partial (struct target_ops *ops, enum target_object object,
>>      {
...
>> +	  /* Similar as in the case of a core file, we read auxv from
>> +	     initial_stack.  */
> 
> Hmm, where's this "similar" you refer to?  AFAICS, for cores, BFD
> extracts it from the NT_AUVX note.

We don't dump it in the note, we dump initial stack page containing it.

> 
>> +	  initial_stack = procinfo.initial_stack;
>> +
>> +	  /* procfs is always 'self-hosted', no byte-order manipulation. */
> 
> Double-space after period.
> 

Ok.

> Otherwise looks OK.
> 

Attaching the patch with double-space fix and added "what was fixed" in
commit message.


Thank you,

Aleksandar Ristovski

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0002-nto-Implement-TARGET_OBJECT_AUXV.patch --]
[-- Type: text/x-patch; name="0002-nto-Implement-TARGET_OBJECT_AUXV.patch", Size: 5710 bytes --]

From 37c288bf9c2c945c63ab89eff6da459ee274c083 Mon Sep 17 00:00:00 2001
From: Aleksandar Ristovski <aristovski@qnx.com>
Date: Tue, 20 Oct 2015 10:01:04 -0400
Subject: [PATCH] [nto] Implement TARGET_OBJECT_AUXV.

Fix 'info auxv' for nto.

gdb/ChangeLog:

	* nto-procfs.c (sys/auxv.h): Include.
	(procfs_xfer_partial): Implement TARGET_OBJECT_AUXV.
	* nto-tdep.c (nto_read_auxv_from_initial_stack): New function.
	* nto-tdep.h (nto_read_auxv_from_initial_stack): New declaration.
---
 gdb/nto-procfs.c | 36 ++++++++++++++++++++++++
 gdb/nto-tdep.c   | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/nto-tdep.h   |  3 ++
 3 files changed, 122 insertions(+)

diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index 264d88b..df8a344 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -30,6 +30,8 @@
 #include <sys/syspage.h>
 #include <dirent.h>
 #include <sys/netmgr.h>
+#include <sys/auxv.h>
+
 #include "gdbcore.h"
 #include "inferior.h"
 #include "target.h"
@@ -885,6 +887,40 @@ procfs_xfer_partial (struct target_ops *ops, enum target_object object,
     {
     case TARGET_OBJECT_MEMORY:
       return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
+    case TARGET_OBJECT_AUXV:
+      if (readbuf != NULL)
+	{
+	  int err;
+	  CORE_ADDR initial_stack;
+	  debug_process_t procinfo;
+	  /* For 32-bit architecture, size of auxv_t is 8 bytes.  */
+	  const unsigned int sizeof_auxv_t = sizeof (auxv_t);
+	  const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
+	  int tempread;
+	  gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
+
+	  if (tempbuf == NULL)
+	    return TARGET_XFER_E_IO;
+
+	  err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
+		        sizeof procinfo, 0);
+	  if (err != EOK)
+	    return TARGET_XFER_E_IO;
+
+	  /* Similar as in the case of a core file, we read auxv from
+	     initial_stack.  */
+	  initial_stack = procinfo.initial_stack;
+
+	  /* procfs is always 'self-hosted', no byte-order manipulation.  */
+	  tempread = nto_read_auxv_from_initial_stack (initial_stack, tempbuf,
+						       sizeof_tempbuf,
+						       sizeof (auxv_t));
+	  tempread = min (tempread, len) - offset;
+	  memcpy (readbuf, tempbuf + offset, tempread);
+	  *xfered_len = tempread;
+	  return tempread ? TARGET_XFER_OK : TARGET_XFER_EOF;
+	}
+	/* Fallthru */
     default:
       return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
 					    readbuf, writebuf, offset, len,
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 81ee7fb..62eb88a 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -394,3 +394,86 @@ nto_initialize_signals (void)
   signal_pass_update (SIGPHOTON, 1);
 #endif
 }
+
+/* Read AUXV from initial_stack.  */
+LONGEST
+nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack, gdb_byte *readbuf,
+                                  LONGEST len, size_t sizeof_auxv_t)
+{
+  gdb_byte targ32[4]; /* For 32 bit target values.  */
+  gdb_byte targ64[8]; /* For 64 bit target values.  */
+  CORE_ADDR data_ofs = 0;
+  ULONGEST anint;
+  LONGEST len_read = 0;
+  gdb_byte *buff;
+  enum bfd_endian byte_order;
+  int ptr_size;
+
+  if (sizeof_auxv_t == 16)
+    ptr_size = 8;
+  else
+    ptr_size = 4;
+
+  /* Skip over argc, argv and envp... Comment from ldd.c:
+
+     The startup frame is set-up so that we have:
+     auxv
+     NULL
+     ...
+     envp2
+     envp1 <----- void *frame + (argc + 2) * sizeof(char *)
+     NULL
+     ...
+     argv2
+     argv1
+     argc  <------ void * frame
+
+     On entry to ldd, frame gives the address of argc on the stack.  */
+  /* Read argc. 4 bytes on both 64 and 32 bit arches and luckily little
+   * endian. So we just read first 4 bytes.  */
+  if (target_read_memory (initial_stack + data_ofs, targ32, 4) != 0)
+    return 0;
+
+  byte_order = gdbarch_byte_order (target_gdbarch ());
+
+  anint = extract_unsigned_integer (targ32, sizeof (targ32), byte_order);
+
+  /* Size of pointer is assumed to be 4 bytes (32 bit arch.) */
+  data_ofs += (anint + 2) * ptr_size; /* + 2 comes from argc itself and
+                                                NULL terminating pointer in
+                                                argv.  */
+
+  /* Now loop over env table:  */
+  anint = 0;
+  while (target_read_memory (initial_stack + data_ofs, targ64, ptr_size)
+         == 0)
+    {
+      if (extract_unsigned_integer (targ64, ptr_size, byte_order) == 0)
+	anint = 1; /* Keep looping until non-null entry is found.  */
+      else if (anint)
+	break;
+      data_ofs += ptr_size;
+    }
+  initial_stack += data_ofs;
+
+  memset (readbuf, 0, len);
+  buff = readbuf;
+  while (len_read <= len-sizeof_auxv_t)
+    {
+      if (target_read_memory (initial_stack + len_read, buff, sizeof_auxv_t)
+	  == 0)
+        {
+	  /* Both 32 and 64 bit structures have int as the first field.  */
+          const ULONGEST a_type
+	    = extract_unsigned_integer (buff, sizeof (targ32), byte_order);
+
+          if (a_type == AT_NULL)
+	    break;
+	  buff += sizeof_auxv_t;
+	  len_read += sizeof_auxv_t;
+        }
+      else
+        break;
+    }
+  return len_read;
+}
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index bd85d2a..d029f07 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -168,4 +168,7 @@ int nto_in_dynsym_resolve_code (CORE_ADDR pc);
 
 char *nto_extra_thread_info (struct target_ops *self, struct thread_info *);
 
+LONGEST nto_read_auxv_from_initial_stack (CORE_ADDR inital_stack,
+					  gdb_byte *readbuf,
+					  LONGEST len, size_t sizeof_auxv_t);
 #endif
-- 
1.9.1


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV.
  2015-10-20 16:03               ` Aleksandar Ristovski
@ 2015-10-20 16:48                 ` Pedro Alves
  2015-10-20 17:08                   ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-20 16:48 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/20/2015 04:42 PM, Aleksandar Ristovski wrote:
> On 15-10-20 11:20 AM, Pedro Alves wrote:
>> Does this result in any visible improvement?  I assume that
>> at least, "info auxv" now works [1] [2].  It'd be really nice to have a
>> blurb in the commit log mentioning what motivated this.
> 
> Yes, info auxv works on a live process. For the core I have other
> patches that need to go in first, but the mechanism of getting auxv
> remains the same; only determining initial stack changes.

OK, but please clarify or drop the misleading comment until
those patches go in then.  Please push with that fixed.

> 
> I will add something to the commit log.
> "Fix 'info auxv' for nto."

Thanks.

>> [1] - BTW, if you enable gdb.base/auxv.exp on NTO, does it pass?
>>
> 
> It fails since we have AT_* entries that are specific to nto, and get
> printed as ??? which causes regex to not match. I have it patched
> internally and print them out, but didn't think it would be acceptable
> upstream.

Why wouldn't it?  If the issue is that the numbers conflict with other
ports, then it can be handled with a gdbarch method.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV.
  2015-10-20 16:48                 ` Pedro Alves
@ 2015-10-20 17:08                   ` Aleksandar Ristovski
  2015-10-20 17:13                     ` Aleksandar Ristovski
                                       ` (2 more replies)
  0 siblings, 3 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 17:08 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 15-10-20 12:03 PM, Pedro Alves wrote:
> On 10/20/2015 04:42 PM, Aleksandar Ristovski wrote:
>> On 15-10-20 11:20 AM, Pedro Alves wrote:
>>> Does this result in any visible improvement?  I assume that
>>> at least, "info auxv" now works [1] [2].  It'd be really nice to have a
>>> blurb in the commit log mentioning what motivated this.
>>
>> Yes, info auxv works on a live process. For the core I have other
>> patches that need to go in first, but the mechanism of getting auxv
>> remains the same; only determining initial stack changes.
> 
> OK, but please clarify or drop the misleading comment until
> those patches go in then.  Please push with that fixed.

Comment dropped.
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -934,8 +934,6 @@ procfs_xfer_partial (struct target_ops *ops, enum
target_object object,
          if (err != EOK)
            return TARGET_XFER_E_IO;

-         /* Similar as in the case of a core file, we read auxv from
-            initial_stack.  */
          initial_stack = procinfo.initial_stack;

          /* procfs is always 'self-hosted', no byte-order manipulation.  */


> 
>>
>> I will add something to the commit log.
>> "Fix 'info auxv' for nto."
> 
> Thanks.
> 
>>> [1] - BTW, if you enable gdb.base/auxv.exp on NTO, does it pass?
>>>
>>
>> It fails since we have AT_* entries that are specific to nto, and get
>> printed as ??? which causes regex to not match. I have it patched
>> internally and print them out, but didn't think it would be acceptable
>> upstream.
> 
> Why wouldn't it?  If the issue is that the numbers conflict with other
> ports, then it can be handled with a gdbarch method.

We have added tags with neutrino specific meanings. I'll address that
later if that's ok with you.

Thanks,

Aleksandar Ristovski

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV.
  2015-10-20 17:08                   ` Aleksandar Ristovski
@ 2015-10-20 17:13                     ` Aleksandar Ristovski
  2015-10-20 18:11                     ` Pedro Alves
  2015-10-20 18:11                     ` Aleksandar Ristovski
  2 siblings, 0 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 17:13 UTC (permalink / raw)
  To: gdb-patches

On 15-10-20 12:03 PM, Pedro Alves wrote:
> On 10/20/2015 04:42 PM, Aleksandar Ristovski wrote:
>> On 15-10-20 11:20 AM, Pedro Alves wrote:
>>> Does this result in any visible improvement?  I assume that
>>> at least, "info auxv" now works [1] [2].  It'd be really nice to have a
>>> blurb in the commit log mentioning what motivated this.
>>
>> Yes, info auxv works on a live process. For the core I have other
>> patches that need to go in first, but the mechanism of getting auxv
>> remains the same; only determining initial stack changes.
> 
> OK, but please clarify or drop the misleading comment until
> those patches go in then.  Please push with that fixed.

Comment dropped.
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -934,8 +934,6 @@ procfs_xfer_partial (struct target_ops *ops, enum
target_object object,
          if (err != EOK)
            return TARGET_XFER_E_IO;

-         /* Similar as in the case of a core file, we read auxv from
-            initial_stack.  */
          initial_stack = procinfo.initial_stack;

          /* procfs is always 'self-hosted', no byte-order manipulation.  */


> 
>>
>> I will add something to the commit log.
>> "Fix 'info auxv' for nto."
> 
> Thanks.
> 
>>> [1] - BTW, if you enable gdb.base/auxv.exp on NTO, does it pass?
>>>
>>
>> It fails since we have AT_* entries that are specific to nto, and get
>> printed as ??? which causes regex to not match. I have it patched
>> internally and print them out, but didn't think it would be acceptable
>> upstream.
> 
> Why wouldn't it?  If the issue is that the numbers conflict with other
> ports, then it can be handled with a gdbarch method.

We have added tags with neutrino specific meanings. I'll address that
later if that's ok with you.

Thanks,

Aleksandar Ristovski

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 1/3] (patch 2/4, v2) [nto] Fixes for nto procfs.
  2015-10-20 15:20             ` Pedro Alves
@ 2015-10-20 17:13               ` Aleksandar Ristovski
  2015-10-20 17:14                 ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 17:13 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 15-10-20 11:02 AM, Pedro Alves wrote:
> On 10/20/2015 03:28 PM, Aleksandar Ristovski wrote:
>> Fix errnoeus construction of procfs path. The issue is, after the first
>> info pidlist or first run, the path for local node (the most common node)
>> will be reset to empty which makes subsequent queries and runs impossible.
> 
> Thanks.
> 
>>
>> gdb/ChangeLog:
>>
>> 	* nto-procfs.c (nto_procfs_path): Rename to...
>> 	(nodestr): ... this, and change type.
>> 	(nto_node): Use new variable and logic accordingly.
>> 	(procfs_open_1): Use new variable name. Use local buffer to construct
>> 	procfrs path.
>> 	(procfs_pidlist): Use NODESTR to construct procfs path.
>> 	(procfs_files_info): Use NODESTR to output meaningful text.
>> 	(do_attach): Construct procfs using NODESTR.
>> 	(procfs_create_inferior): Compare pointer to NULL.
> 
> This is OK.
> 
> There are some bits here that seem a bit unrelated, but we're
> hitting the point of diminishing returns.
> 
> Thanks,
> Pedro Alves


Pushed, thank you,

Aleksandar Ristovski

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 1/3] (patch 2/4, v2) [nto] Fixes for nto procfs.
  2015-10-20 17:13               ` Aleksandar Ristovski
@ 2015-10-20 17:14                 ` Aleksandar Ristovski
  0 siblings, 0 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 17:14 UTC (permalink / raw)
  To: gdb-patches

On 15-10-20 11:02 AM, Pedro Alves wrote:
> On 10/20/2015 03:28 PM, Aleksandar Ristovski wrote:
>> Fix errnoeus construction of procfs path. The issue is, after the first
>> info pidlist or first run, the path for local node (the most common node)
>> will be reset to empty which makes subsequent queries and runs impossible.
> 
> Thanks.
> 
>>
>> gdb/ChangeLog:
>>
>> 	* nto-procfs.c (nto_procfs_path): Rename to...
>> 	(nodestr): ... this, and change type.
>> 	(nto_node): Use new variable and logic accordingly.
>> 	(procfs_open_1): Use new variable name. Use local buffer to construct
>> 	procfrs path.
>> 	(procfs_pidlist): Use NODESTR to construct procfs path.
>> 	(procfs_files_info): Use NODESTR to output meaningful text.
>> 	(do_attach): Construct procfs using NODESTR.
>> 	(procfs_create_inferior): Compare pointer to NULL.
> 
> This is OK.
> 
> There are some bits here that seem a bit unrelated, but we're
> hitting the point of diminishing returns.
> 
> Thanks,
> Pedro Alves


Pushed, thank you,

Aleksandar Ristovski


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV.
  2015-10-20 17:08                   ` Aleksandar Ristovski
  2015-10-20 17:13                     ` Aleksandar Ristovski
  2015-10-20 18:11                     ` Pedro Alves
@ 2015-10-20 18:11                     ` Aleksandar Ristovski
  2015-10-20 18:39                       ` Aleksandar Ristovski
  2 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 18:11 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 15-10-20 12:55 PM, Aleksandar Ristovski wrote:
> On 15-10-20 12:03 PM, Pedro Alves wrote:
>> On 10/20/2015 04:42 PM, Aleksandar Ristovski wrote:
>>> On 15-10-20 11:20 AM, Pedro Alves wrote:
>>>> Does this result in any visible improvement?  I assume that
>>>> at least, "info auxv" now works [1] [2].  It'd be really nice to have a
>>>> blurb in the commit log mentioning what motivated this.
>>>
>>> Yes, info auxv works on a live process. For the core I have other
>>> patches that need to go in first, but the mechanism of getting auxv
>>> remains the same; only determining initial stack changes.
>>
>> OK, but please clarify or drop the misleading comment until
>> those patches go in then.  Please push with that fixed.
>

Pushed now.

Thank you,

Aleksandar Ristovski


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV.
  2015-10-20 17:08                   ` Aleksandar Ristovski
  2015-10-20 17:13                     ` Aleksandar Ristovski
@ 2015-10-20 18:11                     ` Pedro Alves
  2015-10-20 18:11                     ` Aleksandar Ristovski
  2 siblings, 0 replies; 47+ messages in thread
From: Pedro Alves @ 2015-10-20 18:11 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/20/2015 05:55 PM, Aleksandar Ristovski wrote:

>>>> [1] - BTW, if you enable gdb.base/auxv.exp on NTO, does it pass?
>>>>
>>>
>>> It fails since we have AT_* entries that are specific to nto, and get
>>> printed as ??? which causes regex to not match. I have it patched
>>> internally and print them out, but didn't think it would be acceptable
>>> upstream.
>>
>> Why wouldn't it?  If the issue is that the numbers conflict with other
>> ports, then it can be handled with a gdbarch method.
> 
> We have added tags with neutrino specific meanings. I'll address that
> later if that's ok with you.

Certainly, that's fine.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 3/3] (patch 2/4, v2) [nto] Implement procfs_pid_to_exec_file.
  2015-10-20 15:25             ` Pedro Alves
@ 2015-10-20 18:11               ` Aleksandar Ristovski
  2015-10-20 18:19                 ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 18:11 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 15-10-20 11:24 AM, Pedro Alves wrote:
> On 10/20/2015 03:28 PM, Aleksandar Ristovski wrote:
>> gdb/ChangeLog:
>>
>> 	* gdb/nto-procfs.c (procfs_pid_to_exec_file): New function.
>> 	(init_procfs_targets): Wire new function.
>> ---
>>  gdb/nto-procfs.c | 28 ++++++++++++++++++++++++++++
>>  1 file changed, 28 insertions(+)
>>
>> diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
>> index ac54c32..e7882ba 100644
>> --- a/gdb/nto-procfs.c
>> +++ b/gdb/nto-procfs.c
>> @@ -617,6 +617,33 @@ procfs_files_info (struct target_ops *ignore)
>>  		     (nodestr != NULL) ? nodestr : "local node");
>>  }
>>  
>> +/* Read executable file name for the given PID.  */
>> +
> 
> Please write instead:
> 
> /* Target to_pid_to_exec_file implementation.  */
> 
>> +static char *
>> +procfs_pid_to_exec_file (struct target_ops *ops, const int pid)
>> +{
> 
> OK with that change.
> 
> Thanks,
> Pedro Alves
> 
> 

Pushed with:

+/* Target to_pid_to_exec_file implementation.  */
+
+static char *
+procfs_pid_to_exec_file (struct target_ops *ops, const int pid)


Thank you,

Aleksandar Ristovski

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 3/3] (patch 2/4, v2) [nto] Implement procfs_pid_to_exec_file.
  2015-10-20 18:11               ` Aleksandar Ristovski
@ 2015-10-20 18:19                 ` Aleksandar Ristovski
  0 siblings, 0 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 18:19 UTC (permalink / raw)
  To: gdb-patches

On 15-10-20 11:24 AM, Pedro Alves wrote:
> On 10/20/2015 03:28 PM, Aleksandar Ristovski wrote:
>> gdb/ChangeLog:
>>
>> 	* gdb/nto-procfs.c (procfs_pid_to_exec_file): New function.
>> 	(init_procfs_targets): Wire new function.
>> ---
>>  gdb/nto-procfs.c | 28 ++++++++++++++++++++++++++++
>>  1 file changed, 28 insertions(+)
>>
>> diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
>> index ac54c32..e7882ba 100644
>> --- a/gdb/nto-procfs.c
>> +++ b/gdb/nto-procfs.c
>> @@ -617,6 +617,33 @@ procfs_files_info (struct target_ops *ignore)
>>  		     (nodestr != NULL) ? nodestr : "local node");
>>  }
>>  
>> +/* Read executable file name for the given PID.  */
>> +
> 
> Please write instead:
> 
> /* Target to_pid_to_exec_file implementation.  */
> 
>> +static char *
>> +procfs_pid_to_exec_file (struct target_ops *ops, const int pid)
>> +{
> 
> OK with that change.
> 
> Thanks,
> Pedro Alves
> 
> 

Pushed with:

+/* Target to_pid_to_exec_file implementation.  */
+
+static char *
+procfs_pid_to_exec_file (struct target_ops *ops, const int pid)


Thank you,

Aleksandar Ristovski

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV.
  2015-10-20 18:11                     ` Aleksandar Ristovski
@ 2015-10-20 18:39                       ` Aleksandar Ristovski
  0 siblings, 0 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 18:39 UTC (permalink / raw)
  To: gdb-patches

On 15-10-20 12:55 PM, Aleksandar Ristovski wrote:
> On 15-10-20 12:03 PM, Pedro Alves wrote:
>> On 10/20/2015 04:42 PM, Aleksandar Ristovski wrote:
>>> On 15-10-20 11:20 AM, Pedro Alves wrote:
>>>> Does this result in any visible improvement?  I assume that
>>>> at least, "info auxv" now works [1] [2].  It'd be really nice to have a
>>>> blurb in the commit log mentioning what motivated this.
>>>
>>> Yes, info auxv works on a live process. For the core I have other
>>> patches that need to go in first, but the mechanism of getting auxv
>>> remains the same; only determining initial stack changes.
>>
>> OK, but please clarify or drop the misleading comment until
>> those patches go in then.  Please push with that fixed.
>

Pushed now.

Thank you,

Aleksandar Ristovski



^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH 0/2] (patch 3/4 v2) Broken down patch 3/4.
  2015-10-16 16:10   ` Pedro Alves
@ 2015-10-20 18:42     ` Aleksandar Ristovski
  2015-10-20 19:24       ` [PATCH 2/2] [nto] Improve ABI sniffing Aleksandar Ristovski
  2015-10-21  8:18       ` [PATCH 1/2] [nto] Fix nto target stopped by watchpoint Aleksandar Ristovski
  0 siblings, 2 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 18:42 UTC (permalink / raw)
  To: gdb-patches; +Cc: palves, Aleksandar Ristovski

The patch "Fix nto target stopped by watchpoint" is broken into two:
  [nto] Fix nto target stopped by watchpoint.
  [nto] Improve ABI sniffing.

^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH 2/2] [nto] Improve ABI sniffing.
  2015-10-20 18:42     ` [PATCH 0/2] (patch 3/4 v2) Broken down patch 3/4 Aleksandar Ristovski
@ 2015-10-20 19:24       ` Aleksandar Ristovski
  2015-10-21 10:39         ` Pedro Alves
  2015-10-21  8:18       ` [PATCH 1/2] [nto] Fix nto target stopped by watchpoint Aleksandar Ristovski
  1 sibling, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-20 19:24 UTC (permalink / raw)
  To: gdb-patches; +Cc: palves, Aleksandar Ristovski

Use qnx specific notes to figure out the OS.

gdb/ChangeLog:
	* gdb/nto-tdep.c (QNX_NOTE_NAME, QNX_INFO_SECT_NAME): New defines.
	(nto_sniff_abi_note_section): New function.
	(nto_elf_osabi_sniffer): Use new function to recognize nto specific
	binary.
---
 gdb/nto-tdep.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)

diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index e50d302..48826cb 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -32,6 +32,9 @@
 #include "gdbcore.h"
 #include "objfiles.h"
 
+#define QNX_NOTE_NAME	"QNX"
+#define QNX_INFO_SECT_NAME "QNX_info"
+
 #ifdef __CYGWIN__
 #include <sys/cygwin.h>
 #endif
@@ -332,12 +335,51 @@ nto_dummy_supply_regset (struct regcache *regcache, char *regs)
   /* Do nothing.  */
 }
 
+static void
+nto_sniff_abi_note_section (bfd *abfd, asection *sect, void *obj)
+{
+  const char *sectname;
+  unsigned int sectsize;
+  char *note; // buffer holding the section contents
+  unsigned int namelen;
+  const char *name;
+
+  sectname = bfd_get_section_name (abfd, sect);
+  sectsize = bfd_section_size (abfd, sect);
+
+  /* TODO: limit the note size here, for now limit is 128 bytes
+     (enough to check the name and type).  */
+  if (sectsize > 128)
+    sectsize = 128;
+
+  if (sectname && strstr (sectname, QNX_INFO_SECT_NAME) != NULL)
+    *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+
+  if (sectname && strstr (sectname, "note") != NULL)
+    {
+      note = alloca (sectsize);
+      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
+      namelen = (unsigned int) bfd_h_get_32 (abfd, note);
+      name = note + 12;
+
+      if (namelen > 0
+	  && (0 == strcmp (name, QNX_NOTE_NAME)))
+        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+    }
+}
+
 enum gdb_osabi
 nto_elf_osabi_sniffer (bfd *abfd)
 {
-  if (nto_is_nto_target)
+  enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
+
+  bfd_map_over_sections (abfd,
+			 nto_sniff_abi_note_section,
+			 &osabi);
+
+  if (osabi == GDB_OSABI_UNKNOWN && nto_is_nto_target)
     return nto_is_nto_target (abfd);
-  return GDB_OSABI_UNKNOWN;
+  return osabi;
 }
 
 static const char *nto_thread_state_str[] =
-- 
1.9.1

^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH 1/2] [nto] Fix nto target stopped by watchpoint.
  2015-10-20 18:42     ` [PATCH 0/2] (patch 3/4 v2) Broken down patch 3/4 Aleksandar Ristovski
  2015-10-20 19:24       ` [PATCH 2/2] [nto] Improve ABI sniffing Aleksandar Ristovski
@ 2015-10-21  8:18       ` Aleksandar Ristovski
  2015-10-21 10:39         ` Pedro Alves
  1 sibling, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-21  8:18 UTC (permalink / raw)
  To: gdb-patches; +Cc: palves, Aleksandar Ristovski

Fix 'stopped by watchpoint' detection: add inferior data, use inferior data
for storing last stopped flags needed for detection.

gdb/ChangeLog:

	* nto-procfs.c (procfs_wait): Set stopped_flags nad stopped_pc.
	(procfs_stopped_by_watchpoint): Use flags stored in inferior data.
	* nto-tdep.c (nto_new_inferior_data_reg): New definition.
	(nto_new_inferior_data, nto_inferior_data_cleanup, nto_inferior_data):
	New functions.
	(_initialize_nto_tdep): New forward declaration, new function.
	* nto-tdep.h (struct nto_inferior_data): New struct.
	(nto_inferior_data): New function declaration.
---
 gdb/nto-procfs.c | 21 ++++++++++++++++++++-
 gdb/nto-tdep.c   | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/nto-tdep.h   | 13 +++++++++++++
 3 files changed, 85 insertions(+), 1 deletion(-)

diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index 82b428c..6ab78e3 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -784,6 +784,9 @@ procfs_wait (struct target_ops *ops,
       devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
     }
 
+  nto_inferior_data (NULL)->stopped_flags = status.flags;
+  nto_inferior_data (NULL)->stopped_pc = status.ip;
+
   if (status.flags & _DEBUG_FLAG_SSTEP)
     {
       ourstatus->kind = TARGET_WAITKIND_STOPPED;
@@ -1626,5 +1629,21 @@ procfs_insert_hw_watchpoint (struct target_ops *self,
 static int
 procfs_stopped_by_watchpoint (struct target_ops *ops)
 {
-  return 0;
+  /* NOTE: nto_stopped_by_watchpoint will be called ONLY while we are
+     stopped due to a SIGTRAP.  This assumes gdb works in 'all-stop' mode;
+     future gdb versions will likely run in 'non-stop' mode in which case
+     we will have to store/examine statuses per thread in question.
+     Until then, this will work fine.  */
+
+  struct inferior *inf = current_inferior ();
+  struct nto_inferior_data *inf_data;
+
+  gdb_assert (inf != NULL);
+
+  inf_data = nto_inferior_data (inf);
+
+  return inf_data->stopped_flags
+	 & (_DEBUG_FLAG_TRACE_RD
+	    | _DEBUG_FLAG_TRACE_WR
+	    | _DEBUG_FLAG_TRACE_MODIFY);
 }
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 62eb88a..e50d302 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -46,6 +46,8 @@ static char default_nto_target[] = "";
 
 struct nto_target_ops current_nto_target;
 
+static const struct inferior_data *nto_inferior_data_reg;
+
 static char *
 nto_target (void)
 {
@@ -477,3 +479,53 @@ nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack, gdb_byte *readbuf,
     }
   return len_read;
 }
+
+/* Allocate new nto_inferior_data object.  */
+
+static struct nto_inferior_data *
+nto_new_inferior_data (void)
+{
+  struct nto_inferior_data *const inf_data
+    = XCNEW (struct nto_inferior_data);
+
+  return inf_data;
+}
+
+/* Free inferior data.  */
+
+static void
+nto_inferior_data_cleanup (struct inferior *const inf, void *const dat)
+{
+  xfree (dat);
+}
+
+/* Return nto_inferior_data for the given INFERIOR.  If not yet created,
+   construct it.  */
+
+struct nto_inferior_data *
+nto_inferior_data (struct inferior *const inferior)
+{
+  struct inferior *const inf = inferior ? inferior : current_inferior ();
+  struct nto_inferior_data *inf_data;
+
+  gdb_assert (inf != NULL);
+
+  inf_data = inferior_data (inf, nto_inferior_data_reg);
+  if (inf_data == NULL)
+    {
+      set_inferior_data (inf, nto_inferior_data_reg,
+			 (inf_data = nto_new_inferior_data ()));
+    }
+
+  return inf_data;
+}
+
+/* Provide a prototype to silence -Wmissing-prototypes.  */
+extern initialize_file_ftype _initialize_nto_tdep;
+
+void
+_initialize_nto_tdep (void)
+{
+  nto_inferior_data_reg
+    = register_inferior_data_with_cleanup (NULL, nto_inferior_data_cleanup);
+}
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index d029f07..6ed9da0 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -142,6 +142,16 @@ struct private_thread_info
   char name[1];
 };
 
+/* Per-inferior data, common for both procfs and remote.  */
+struct nto_inferior_data
+{
+  /* Last stopped flags result from wait function */
+  unsigned int stopped_flags;
+
+  /* Last known stopped PC */
+  CORE_ADDR stopped_pc;
+};
+
 /* Generic functions in nto-tdep.c.  */
 
 void nto_init_solib_absolute_prefix (void);
@@ -171,4 +181,7 @@ char *nto_extra_thread_info (struct target_ops *self, struct thread_info *);
 LONGEST nto_read_auxv_from_initial_stack (CORE_ADDR inital_stack,
 					  gdb_byte *readbuf,
 					  LONGEST len, size_t sizeof_auxv_t);
+
+struct nto_inferior_data *nto_inferior_data (struct inferior *inf);
+
 #endif
-- 
1.9.1

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/2] [nto] Improve ABI sniffing.
  2015-10-20 19:24       ` [PATCH 2/2] [nto] Improve ABI sniffing Aleksandar Ristovski
@ 2015-10-21 10:39         ` Pedro Alves
  2015-10-21 14:42           ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-21 10:39 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/20/2015 07:10 PM, Aleksandar Ristovski wrote:
> Use qnx specific notes to figure out the OS.
> 
> gdb/ChangeLog:
> 	* gdb/nto-tdep.c (QNX_NOTE_NAME, QNX_INFO_SECT_NAME): New defines.
> 	(nto_sniff_abi_note_section): New function.
> 	(nto_elf_osabi_sniffer): Use new function to recognize nto specific
> 	binary.
> ---
>  gdb/nto-tdep.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 44 insertions(+), 2 deletions(-)
> 
> diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
> index e50d302..48826cb 100644
> --- a/gdb/nto-tdep.c
> +++ b/gdb/nto-tdep.c
> @@ -32,6 +32,9 @@
>  #include "gdbcore.h"
>  #include "objfiles.h"
>  
> +#define QNX_NOTE_NAME	"QNX"
> +#define QNX_INFO_SECT_NAME "QNX_info"
> +
>  #ifdef __CYGWIN__
>  #include <sys/cygwin.h>
>  #endif
> @@ -332,12 +335,51 @@ nto_dummy_supply_regset (struct regcache *regcache, char *regs)
>    /* Do nothing.  */
>  }
>  
> +static void
> +nto_sniff_abi_note_section (bfd *abfd, asection *sect, void *obj)
> +{
> +  const char *sectname;
> +  unsigned int sectsize;
> +  char *note; // buffer holding the section contents

Please use /**/ format for comments, and write full sentences - start
with uppercase, period at end.  Usually that leads to putting the comment
above, e.g.:

 +  /* Buffer holding the section contents.  */
 +  char *note;

> +  unsigned int namelen;
> +  const char *name;
> +
> +  sectname = bfd_get_section_name (abfd, sect);
> +  sectsize = bfd_section_size (abfd, sect);
> +
> +  /* TODO: limit the note size here, for now limit is 128 bytes
> +     (enough to check the name and type).  */

This reads like limiting is left to do, but then it does
implement a limit.  So this TODO comment is confusing.

You should also make sure the section is the at least
the minimum size you expect though.

> +  if (sectsize > 128)
> +    sectsize = 128;
> +
> +  if (sectname && strstr (sectname, QNX_INFO_SECT_NAME) != NULL)
> +    *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
> +

sectname != NULL

> +  if (sectname && strstr (sectname, "note") != NULL)

sectname != NULL


> +    {
> +      note = alloca (sectsize);

For C++, write:

      note = (char *) alloca (sectsize);


> +      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
> +      namelen = (unsigned int) bfd_h_get_32 (abfd, note);
> +      name = note + 12;
> +
> +      if (namelen > 0
> +	  && (0 == strcmp (name, QNX_NOTE_NAME)))
> +        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
> +    }
> +}
> +
>  enum gdb_osabi
>  nto_elf_osabi_sniffer (bfd *abfd)
>  {
> -  if (nto_is_nto_target)
> +  enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
> +
> +  bfd_map_over_sections (abfd,
> +			 nto_sniff_abi_note_section,
> +			 &osabi);
> +
> +  if (osabi == GDB_OSABI_UNKNOWN && nto_is_nto_target)
>      return nto_is_nto_target (abfd);

...
nto_is_nto_target = procfs_is_nto_target;
...

static enum gdb_osabi
procfs_is_nto_target (bfd *abfd)
{
  return GDB_OSABI_QNXNTO;
}

So that basically could be rewritten as:

  if (osabi == GDB_OSABI_UNKNOWN && nto_is_nto_target)
     return GDB_OSABI_QNXNTO;


But, do we still need this nto_is_nto_target hack here?
Now with proper sniffing, can't we just remove it altogether?

> -  return GDB_OSABI_UNKNOWN;
> +  return osabi;
>  }
>  
>  static const char *nto_thread_state_str[] =
> 


Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 1/2] [nto] Fix nto target stopped by watchpoint.
  2015-10-21  8:18       ` [PATCH 1/2] [nto] Fix nto target stopped by watchpoint Aleksandar Ristovski
@ 2015-10-21 10:39         ` Pedro Alves
  2015-10-21 17:51           ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-21 10:39 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/20/2015 07:10 PM, Aleksandar Ristovski wrote:
> Fix 'stopped by watchpoint' detection: add inferior data, use inferior data
> for storing last stopped flags needed for detection.
> 

OK.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/2] [nto] Improve ABI sniffing.
  2015-10-21 10:39         ` Pedro Alves
@ 2015-10-21 14:42           ` Aleksandar Ristovski
  2015-10-21 14:47             ` Aleksandar Ristovski
  2015-10-21 15:17             ` Pedro Alves
  0 siblings, 2 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-21 14:42 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

[-- Attachment #1: Type: text/plain, Size: 2393 bytes --]

On 15-10-21 05:49 AM, Pedro Alves wrote:
...
>> +  char *note; // buffer holding the section contents
> 
> Please use /**/ format for comments, and write full sentences - start
> with uppercase, period at end.  Usually that leads to putting the comment
> above, e.g.:
> 
>  +  /* Buffer holding the section contents.  */
>  +  char *note;

Done.

> 
>> +  unsigned int namelen;
>> +  const char *name;
>> +
>> +  sectname = bfd_get_section_name (abfd, sect);
>> +  sectsize = bfd_section_size (abfd, sect);
>> +
>> +  /* TODO: limit the note size here, for now limit is 128 bytes
>> +     (enough to check the name and type).  */
> 
> This reads like limiting is left to do, but then it does
> implement a limit.  So this TODO comment is confusing.

Comment removed.

> 
> You should also make sure the section is the at least
> the minimum size you expect though.

Done.

...
>> +    {
>> +      note = alloca (sectsize);
> 
> For C++, write:
> 
>       note = (char *) alloca (sectsize);

Used XNEWVEC/XDELETEVEC instead.

> 
> 
>> +      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
>> +      namelen = (unsigned int) bfd_h_get_32 (abfd, note);
>> +      name = note + 12;
>> +
>> +      if (namelen > 0
>> +	  && (0 == strcmp (name, QNX_NOTE_NAME)))
>> +        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
>> +    }
>> +}
>> +
>>  enum gdb_osabi
>>  nto_elf_osabi_sniffer (bfd *abfd)
>>  {
>> -  if (nto_is_nto_target)
>> +  enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
>> +
>> +  bfd_map_over_sections (abfd,
>> +			 nto_sniff_abi_note_section,
>> +			 &osabi);
>> +
>> +  if (osabi == GDB_OSABI_UNKNOWN && nto_is_nto_target)
>>      return nto_is_nto_target (abfd);
> 
> ...
> nto_is_nto_target = procfs_is_nto_target;
> ...
> 
> static enum gdb_osabi
> procfs_is_nto_target (bfd *abfd)
> {
>   return GDB_OSABI_QNXNTO;
> }
> 
> So that basically could be rewritten as:
> 
>   if (osabi == GDB_OSABI_UNKNOWN && nto_is_nto_target)
>      return GDB_OSABI_QNXNTO;
> 
> 
> But, do we still need this nto_is_nto_target hack here?
> Now with proper sniffing, can't we just remove it altogether?

Removed. Rely on new sniffing only, no hard coded fallback.


> 
>> -  return GDB_OSABI_UNKNOWN;
>> +  return osabi;
>>  }
>>  
>>  static const char *nto_thread_state_str[] =
>>
> 
> 
> Thanks,
> Pedro Alves
> 
> 


New version of the patch attached.

Thank you,

Aleksandar Ristovski

[-- Attachment #2: 0002-nto-Improve-ABI-sniffing.patch --]
[-- Type: text/x-patch, Size: 2513 bytes --]

From 6df97e71696e47d85000413bf4a97cf5d43b11f1 Mon Sep 17 00:00:00 2001
From: Aleksandar Ristovski <aristovski@qnx.com>
Date: Wed, 21 Oct 2015 09:29:54 -0400
Subject: [PATCH 2/2] [nto] Improve ABI sniffing.

Use qnx specific notes to figure out the OS.

gdb/ChangeLog:
	* gdb/nto-tdep.c (QNX_NOTE_NAME, QNX_INFO_SECT_NAME): New defines.
	(nto_sniff_abi_note_section): New function.
	(nto_elf_osabi_sniffer): Use new function to recognize nto specific
	binary.
---
 gdb/nto-tdep.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 52 insertions(+), 3 deletions(-)

diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index e50d302..4e0cc84 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -32,6 +32,9 @@
 #include "gdbcore.h"
 #include "objfiles.h"
 
+#define QNX_NOTE_NAME	"QNX"
+#define QNX_INFO_SECT_NAME "QNX_info"
+
 #ifdef __CYGWIN__
 #include <sys/cygwin.h>
 #endif
@@ -332,12 +335,58 @@ nto_dummy_supply_regset (struct regcache *regcache, char *regs)
   /* Do nothing.  */
 }
 
+static void
+nto_sniff_abi_note_section (bfd *abfd, asection *sect, void *obj)
+{
+  const char *sectname;
+  unsigned int sectsize;
+  /* Buffer holding the section contents.  */
+  char *note;
+  unsigned int namelen;
+  const char *name;
+
+  sectname = bfd_get_section_name (abfd, sect);
+  sectsize = bfd_section_size (abfd, sect);
+
+  if (sectsize > 128)
+    sectsize = 128;
+
+  if (sectname != NULL && strstr (sectname, QNX_INFO_SECT_NAME) != NULL)
+    *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+
+  if (sectname != NULL && strstr (sectname, "note") != NULL)
+    {
+      const unsigned sizeof_Elf_Nhdr = 12;
+
+      note = XNEWVEC (char, sectsize);
+      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
+      namelen = (unsigned int) bfd_h_get_32 (abfd, note);
+      name = note + sizeof_Elf_Nhdr;
+      if (sectsize < namelen + sizeof_Elf_Nhdr
+	  || namelen > sizeof (QNX_NOTE_NAME) + 1)
+	{
+	  /* Can not be QNX note.  */
+	  XDELETEVEC (note);
+	  return;
+	}
+
+      if (namelen > 0 && 0 == strcmp (name, QNX_NOTE_NAME))
+        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+
+      XDELETEVEC (note);
+    }
+}
+
 enum gdb_osabi
 nto_elf_osabi_sniffer (bfd *abfd)
 {
-  if (nto_is_nto_target)
-    return nto_is_nto_target (abfd);
-  return GDB_OSABI_UNKNOWN;
+  enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
+
+  bfd_map_over_sections (abfd,
+			 nto_sniff_abi_note_section,
+			 &osabi);
+
+  return osabi;
 }
 
 static const char *nto_thread_state_str[] =
-- 
1.9.1


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/2] [nto] Improve ABI sniffing.
  2015-10-21 14:42           ` Aleksandar Ristovski
@ 2015-10-21 14:47             ` Aleksandar Ristovski
  2015-10-21 15:17             ` Pedro Alves
  1 sibling, 0 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-21 14:47 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 2393 bytes --]

On 15-10-21 05:49 AM, Pedro Alves wrote:
...
>> +  char *note; // buffer holding the section contents
> 
> Please use /**/ format for comments, and write full sentences - start
> with uppercase, period at end.  Usually that leads to putting the comment
> above, e.g.:
> 
>  +  /* Buffer holding the section contents.  */
>  +  char *note;

Done.

> 
>> +  unsigned int namelen;
>> +  const char *name;
>> +
>> +  sectname = bfd_get_section_name (abfd, sect);
>> +  sectsize = bfd_section_size (abfd, sect);
>> +
>> +  /* TODO: limit the note size here, for now limit is 128 bytes
>> +     (enough to check the name and type).  */
> 
> This reads like limiting is left to do, but then it does
> implement a limit.  So this TODO comment is confusing.

Comment removed.

> 
> You should also make sure the section is the at least
> the minimum size you expect though.

Done.

...
>> +    {
>> +      note = alloca (sectsize);
> 
> For C++, write:
> 
>       note = (char *) alloca (sectsize);

Used XNEWVEC/XDELETEVEC instead.

> 
> 
>> +      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
>> +      namelen = (unsigned int) bfd_h_get_32 (abfd, note);
>> +      name = note + 12;
>> +
>> +      if (namelen > 0
>> +	  && (0 == strcmp (name, QNX_NOTE_NAME)))
>> +        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
>> +    }
>> +}
>> +
>>  enum gdb_osabi
>>  nto_elf_osabi_sniffer (bfd *abfd)
>>  {
>> -  if (nto_is_nto_target)
>> +  enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
>> +
>> +  bfd_map_over_sections (abfd,
>> +			 nto_sniff_abi_note_section,
>> +			 &osabi);
>> +
>> +  if (osabi == GDB_OSABI_UNKNOWN && nto_is_nto_target)
>>      return nto_is_nto_target (abfd);
> 
> ...
> nto_is_nto_target = procfs_is_nto_target;
> ...
> 
> static enum gdb_osabi
> procfs_is_nto_target (bfd *abfd)
> {
>   return GDB_OSABI_QNXNTO;
> }
> 
> So that basically could be rewritten as:
> 
>   if (osabi == GDB_OSABI_UNKNOWN && nto_is_nto_target)
>      return GDB_OSABI_QNXNTO;
> 
> 
> But, do we still need this nto_is_nto_target hack here?
> Now with proper sniffing, can't we just remove it altogether?

Removed. Rely on new sniffing only, no hard coded fallback.


> 
>> -  return GDB_OSABI_UNKNOWN;
>> +  return osabi;
>>  }
>>  
>>  static const char *nto_thread_state_str[] =
>>
> 
> 
> Thanks,
> Pedro Alves
> 
> 


New version of the patch attached.

Thank you,

Aleksandar Ristovski

[-- Attachment #2: 0002-nto-Improve-ABI-sniffing.patch --]
[-- Type: text/x-patch, Size: 2513 bytes --]

From 6df97e71696e47d85000413bf4a97cf5d43b11f1 Mon Sep 17 00:00:00 2001
From: Aleksandar Ristovski <aristovski@qnx.com>
Date: Wed, 21 Oct 2015 09:29:54 -0400
Subject: [PATCH 2/2] [nto] Improve ABI sniffing.

Use qnx specific notes to figure out the OS.

gdb/ChangeLog:
	* gdb/nto-tdep.c (QNX_NOTE_NAME, QNX_INFO_SECT_NAME): New defines.
	(nto_sniff_abi_note_section): New function.
	(nto_elf_osabi_sniffer): Use new function to recognize nto specific
	binary.
---
 gdb/nto-tdep.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 52 insertions(+), 3 deletions(-)

diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index e50d302..4e0cc84 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -32,6 +32,9 @@
 #include "gdbcore.h"
 #include "objfiles.h"
 
+#define QNX_NOTE_NAME	"QNX"
+#define QNX_INFO_SECT_NAME "QNX_info"
+
 #ifdef __CYGWIN__
 #include <sys/cygwin.h>
 #endif
@@ -332,12 +335,58 @@ nto_dummy_supply_regset (struct regcache *regcache, char *regs)
   /* Do nothing.  */
 }
 
+static void
+nto_sniff_abi_note_section (bfd *abfd, asection *sect, void *obj)
+{
+  const char *sectname;
+  unsigned int sectsize;
+  /* Buffer holding the section contents.  */
+  char *note;
+  unsigned int namelen;
+  const char *name;
+
+  sectname = bfd_get_section_name (abfd, sect);
+  sectsize = bfd_section_size (abfd, sect);
+
+  if (sectsize > 128)
+    sectsize = 128;
+
+  if (sectname != NULL && strstr (sectname, QNX_INFO_SECT_NAME) != NULL)
+    *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+
+  if (sectname != NULL && strstr (sectname, "note") != NULL)
+    {
+      const unsigned sizeof_Elf_Nhdr = 12;
+
+      note = XNEWVEC (char, sectsize);
+      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
+      namelen = (unsigned int) bfd_h_get_32 (abfd, note);
+      name = note + sizeof_Elf_Nhdr;
+      if (sectsize < namelen + sizeof_Elf_Nhdr
+	  || namelen > sizeof (QNX_NOTE_NAME) + 1)
+	{
+	  /* Can not be QNX note.  */
+	  XDELETEVEC (note);
+	  return;
+	}
+
+      if (namelen > 0 && 0 == strcmp (name, QNX_NOTE_NAME))
+        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+
+      XDELETEVEC (note);
+    }
+}
+
 enum gdb_osabi
 nto_elf_osabi_sniffer (bfd *abfd)
 {
-  if (nto_is_nto_target)
-    return nto_is_nto_target (abfd);
-  return GDB_OSABI_UNKNOWN;
+  enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
+
+  bfd_map_over_sections (abfd,
+			 nto_sniff_abi_note_section,
+			 &osabi);
+
+  return osabi;
 }
 
 static const char *nto_thread_state_str[] =
-- 
1.9.1


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/2] [nto] Improve ABI sniffing.
  2015-10-21 14:42           ` Aleksandar Ristovski
  2015-10-21 14:47             ` Aleksandar Ristovski
@ 2015-10-21 15:17             ` Pedro Alves
  2015-10-21 15:37               ` Aleksandar Ristovski
  1 sibling, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-21 15:17 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/21/2015 02:34 PM, Aleksandar Ristovski wrote:

> +static void
> +nto_sniff_abi_note_section (bfd *abfd, asection *sect, void *obj)
> +{
> +  const char *sectname;
> +  unsigned int sectsize;
> +  /* Buffer holding the section contents.  */
> +  char *note;
> +  unsigned int namelen;
> +  const char *name;
> +
> +  sectname = bfd_get_section_name (abfd, sect);
> +  sectsize = bfd_section_size (abfd, sect);
> +
> +  if (sectsize > 128)
> +    sectsize = 128;
> +
> +  if (sectname != NULL && strstr (sectname, QNX_INFO_SECT_NAME) != NULL)
> +    *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
> +
> +  if (sectname != NULL && strstr (sectname, "note") != NULL)

This can be "else if".

> +    {
> +      const unsigned sizeof_Elf_Nhdr = 12;
> +
> +      note = XNEWVEC (char, sectsize);
> +      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
> +      namelen = (unsigned int) bfd_h_get_32 (abfd, note);

You also need to check that the section's size
is enough to contain 'namelen', _before_ extracting it, otherwise
you may be reading garbage.


> +      name = note + sizeof_Elf_Nhdr;
> +      if (sectsize < namelen + sizeof_Elf_Nhdr

> +	  || namelen > sizeof (QNX_NOTE_NAME) + 1)
> +	{
> +	  /* Can not be QNX note.  */
> +	  XDELETEVEC (note);
> +	  return;

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/2] [nto] Improve ABI sniffing.
  2015-10-21 15:17             ` Pedro Alves
@ 2015-10-21 15:37               ` Aleksandar Ristovski
  2015-10-21 16:13                 ` Aleksandar Ristovski
  2015-10-21 16:39                 ` Pedro Alves
  0 siblings, 2 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-21 15:37 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

[-- Attachment #1: Type: text/plain, Size: 1700 bytes --]

[repeat e-mail to include mailing list]

On 15-10-21 09:47 AM, Pedro Alves wrote:
> On 10/21/2015 02:34 PM, Aleksandar Ristovski wrote:
> 
>> +static void
>> +nto_sniff_abi_note_section (bfd *abfd, asection *sect, void *obj)
>> +{
>> +  const char *sectname;
>> +  unsigned int sectsize;
>> +  /* Buffer holding the section contents.  */
>> +  char *note;
>> +  unsigned int namelen;
>> +  const char *name;
>> +
>> +  sectname = bfd_get_section_name (abfd, sect);
>> +  sectsize = bfd_section_size (abfd, sect);
>> +
>> +  if (sectsize > 128)
>> +    sectsize = 128;
>> +
>> +  if (sectname != NULL && strstr (sectname, QNX_INFO_SECT_NAME) != NULL)
>> +    *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
>> +
>> +  if (sectname != NULL && strstr (sectname, "note") != NULL)
> 
> This can be "else if".

Ok.

> 
>> +    {
>> +      const unsigned sizeof_Elf_Nhdr = 12;
>> +
>> +      note = XNEWVEC (char, sectsize);
>> +      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
>> +      namelen = (unsigned int) bfd_h_get_32 (abfd, note);
> 
> You also need to check that the section's size
> is enough to contain 'namelen', _before_ extracting it, otherwise
> you may be reading garbage.
> 

Done.

> 
>> +      name = note + sizeof_Elf_Nhdr;
>> +      if (sectsize < namelen + sizeof_Elf_Nhdr
> 
>> +	  || namelen > sizeof (QNX_NOTE_NAME) + 1)

Removed "+ 1" here.

>> +	{
>> +	  /* Can not be QNX note.  */
>> +	  XDELETEVEC (note);
>> +	  return;
> 
> Thanks,
> Pedro Alves
> 
> 

And compare to exact expected length of the qnx name.
+      if (namelen == sizeof (QNX_NOTE_NAME)

+         && 0 == strcmp (name, QNX_NOTE_NAME))


Attached the latest version.

Thanks,

Aleksandar Ristovski

[-- Attachment #2: 0002-nto-Improve-ABI-sniffing.patch --]
[-- Type: text/x-patch, Size: 2567 bytes --]

From 5b3605345909dccecaeded1c48a1844ec192fa0d Mon Sep 17 00:00:00 2001
From: Aleksandar Ristovski <aristovski@qnx.com>
Date: Wed, 21 Oct 2015 09:29:54 -0400
Subject: [PATCH 2/2] [nto] Improve ABI sniffing.

Use qnx specific notes to figure out the OS.

gdb/ChangeLog:
	* gdb/nto-tdep.c (QNX_NOTE_NAME, QNX_INFO_SECT_NAME): New defines.
	(nto_sniff_abi_note_section): New function.
	(nto_elf_osabi_sniffer): Use new function to recognize nto specific
	binary.
---
 gdb/nto-tdep.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 52 insertions(+), 3 deletions(-)

diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index e50d302..cc3e94e 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -32,6 +32,9 @@
 #include "gdbcore.h"
 #include "objfiles.h"
 
+#define QNX_NOTE_NAME	"QNX"
+#define QNX_INFO_SECT_NAME "QNX_info"
+
 #ifdef __CYGWIN__
 #include <sys/cygwin.h>
 #endif
@@ -332,12 +335,58 @@ nto_dummy_supply_regset (struct regcache *regcache, char *regs)
   /* Do nothing.  */
 }
 
+static void
+nto_sniff_abi_note_section (bfd *abfd, asection *sect, void *obj)
+{
+  const char *sectname;
+  unsigned int sectsize;
+  /* Buffer holding the section contents.  */
+  char *note;
+  unsigned int namelen;
+  const char *name;
+  const unsigned sizeof_Elf_Nhdr = 12;
+
+  sectname = bfd_get_section_name (abfd, sect);
+  sectsize = bfd_section_size (abfd, sect);
+
+  if (sectsize > 128)
+    sectsize = 128;
+
+  if (sectname != NULL && strstr (sectname, QNX_INFO_SECT_NAME) != NULL)
+    *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+  else if (sectname != NULL && strstr (sectname, "note") != NULL
+	   && sectsize > sizeof_Elf_Nhdr)
+    {
+      note = XNEWVEC (char, sectsize);
+      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
+      namelen = (unsigned int) bfd_h_get_32 (abfd, note);
+      name = note + sizeof_Elf_Nhdr;
+      if (sectsize < namelen + sizeof_Elf_Nhdr
+	  || namelen > sizeof (QNX_NOTE_NAME))
+	{
+	  /* Can not be QNX note.  */
+	  XDELETEVEC (note);
+	  return;
+	}
+
+      if (namelen == sizeof (QNX_NOTE_NAME)
+	  && 0 == strcmp (name, QNX_NOTE_NAME))
+        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+
+      XDELETEVEC (note);
+    }
+}
+
 enum gdb_osabi
 nto_elf_osabi_sniffer (bfd *abfd)
 {
-  if (nto_is_nto_target)
-    return nto_is_nto_target (abfd);
-  return GDB_OSABI_UNKNOWN;
+  enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
+
+  bfd_map_over_sections (abfd,
+			 nto_sniff_abi_note_section,
+			 &osabi);
+
+  return osabi;
 }
 
 static const char *nto_thread_state_str[] =
-- 
1.9.1


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/2] [nto] Improve ABI sniffing.
  2015-10-21 15:37               ` Aleksandar Ristovski
@ 2015-10-21 16:13                 ` Aleksandar Ristovski
  2015-10-21 16:39                 ` Pedro Alves
  1 sibling, 0 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-21 16:13 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 1700 bytes --]

[repeat e-mail to include mailing list]

On 15-10-21 09:47 AM, Pedro Alves wrote:
> On 10/21/2015 02:34 PM, Aleksandar Ristovski wrote:
> 
>> +static void
>> +nto_sniff_abi_note_section (bfd *abfd, asection *sect, void *obj)
>> +{
>> +  const char *sectname;
>> +  unsigned int sectsize;
>> +  /* Buffer holding the section contents.  */
>> +  char *note;
>> +  unsigned int namelen;
>> +  const char *name;
>> +
>> +  sectname = bfd_get_section_name (abfd, sect);
>> +  sectsize = bfd_section_size (abfd, sect);
>> +
>> +  if (sectsize > 128)
>> +    sectsize = 128;
>> +
>> +  if (sectname != NULL && strstr (sectname, QNX_INFO_SECT_NAME) != NULL)
>> +    *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
>> +
>> +  if (sectname != NULL && strstr (sectname, "note") != NULL)
> 
> This can be "else if".

Ok.

> 
>> +    {
>> +      const unsigned sizeof_Elf_Nhdr = 12;
>> +
>> +      note = XNEWVEC (char, sectsize);
>> +      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
>> +      namelen = (unsigned int) bfd_h_get_32 (abfd, note);
> 
> You also need to check that the section's size
> is enough to contain 'namelen', _before_ extracting it, otherwise
> you may be reading garbage.
> 

Done.

> 
>> +      name = note + sizeof_Elf_Nhdr;
>> +      if (sectsize < namelen + sizeof_Elf_Nhdr
> 
>> +	  || namelen > sizeof (QNX_NOTE_NAME) + 1)

Removed "+ 1" here.

>> +	{
>> +	  /* Can not be QNX note.  */
>> +	  XDELETEVEC (note);
>> +	  return;
> 
> Thanks,
> Pedro Alves
> 
> 

And compare to exact expected length of the qnx name.
+      if (namelen == sizeof (QNX_NOTE_NAME)

+         && 0 == strcmp (name, QNX_NOTE_NAME))


Attached the latest version.

Thanks,

Aleksandar Ristovski

[-- Attachment #2: 0002-nto-Improve-ABI-sniffing.patch --]
[-- Type: text/x-patch, Size: 2567 bytes --]

From 5b3605345909dccecaeded1c48a1844ec192fa0d Mon Sep 17 00:00:00 2001
From: Aleksandar Ristovski <aristovski@qnx.com>
Date: Wed, 21 Oct 2015 09:29:54 -0400
Subject: [PATCH 2/2] [nto] Improve ABI sniffing.

Use qnx specific notes to figure out the OS.

gdb/ChangeLog:
	* gdb/nto-tdep.c (QNX_NOTE_NAME, QNX_INFO_SECT_NAME): New defines.
	(nto_sniff_abi_note_section): New function.
	(nto_elf_osabi_sniffer): Use new function to recognize nto specific
	binary.
---
 gdb/nto-tdep.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 52 insertions(+), 3 deletions(-)

diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index e50d302..cc3e94e 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -32,6 +32,9 @@
 #include "gdbcore.h"
 #include "objfiles.h"
 
+#define QNX_NOTE_NAME	"QNX"
+#define QNX_INFO_SECT_NAME "QNX_info"
+
 #ifdef __CYGWIN__
 #include <sys/cygwin.h>
 #endif
@@ -332,12 +335,58 @@ nto_dummy_supply_regset (struct regcache *regcache, char *regs)
   /* Do nothing.  */
 }
 
+static void
+nto_sniff_abi_note_section (bfd *abfd, asection *sect, void *obj)
+{
+  const char *sectname;
+  unsigned int sectsize;
+  /* Buffer holding the section contents.  */
+  char *note;
+  unsigned int namelen;
+  const char *name;
+  const unsigned sizeof_Elf_Nhdr = 12;
+
+  sectname = bfd_get_section_name (abfd, sect);
+  sectsize = bfd_section_size (abfd, sect);
+
+  if (sectsize > 128)
+    sectsize = 128;
+
+  if (sectname != NULL && strstr (sectname, QNX_INFO_SECT_NAME) != NULL)
+    *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+  else if (sectname != NULL && strstr (sectname, "note") != NULL
+	   && sectsize > sizeof_Elf_Nhdr)
+    {
+      note = XNEWVEC (char, sectsize);
+      bfd_get_section_contents (abfd, sect, note, 0, sectsize);
+      namelen = (unsigned int) bfd_h_get_32 (abfd, note);
+      name = note + sizeof_Elf_Nhdr;
+      if (sectsize < namelen + sizeof_Elf_Nhdr
+	  || namelen > sizeof (QNX_NOTE_NAME))
+	{
+	  /* Can not be QNX note.  */
+	  XDELETEVEC (note);
+	  return;
+	}
+
+      if (namelen == sizeof (QNX_NOTE_NAME)
+	  && 0 == strcmp (name, QNX_NOTE_NAME))
+        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
+
+      XDELETEVEC (note);
+    }
+}
+
 enum gdb_osabi
 nto_elf_osabi_sniffer (bfd *abfd)
 {
-  if (nto_is_nto_target)
-    return nto_is_nto_target (abfd);
-  return GDB_OSABI_UNKNOWN;
+  enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
+
+  bfd_map_over_sections (abfd,
+			 nto_sniff_abi_note_section,
+			 &osabi);
+
+  return osabi;
 }
 
 static const char *nto_thread_state_str[] =
-- 
1.9.1


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/2] [nto] Improve ABI sniffing.
  2015-10-21 15:37               ` Aleksandar Ristovski
  2015-10-21 16:13                 ` Aleksandar Ristovski
@ 2015-10-21 16:39                 ` Pedro Alves
  2015-10-21 18:10                   ` Aleksandar Ristovski
  1 sibling, 1 reply; 47+ messages in thread
From: Pedro Alves @ 2015-10-21 16:39 UTC (permalink / raw)
  To: Aleksandar Ristovski, gdb-patches

On 10/21/2015 03:14 PM, Aleksandar Ristovski wrote:
> +      name = note + sizeof_Elf_Nhdr;
> +      if (sectsize < namelen + sizeof_Elf_Nhdr
> +	  || namelen > sizeof (QNX_NOTE_NAME))
> +	{
> +	  /* Can not be QNX note.  */
> +	  XDELETEVEC (note);
> +	  return;
> +	}
> +
> +      if (namelen == sizeof (QNX_NOTE_NAME)
> +	  && 0 == strcmp (name, QNX_NOTE_NAME))
> +        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
> +
> +      XDELETEVEC (note);

Looks like these two ifs are now the same as the simpler:

      name = note + sizeof_Elf_Nhdr;
      if (sectsize >= namelen + sizeof_Elf_Nhdr
          && namelen == sizeof (QNX_NOTE_NAME)
	  && 0 == strcmp (name, QNX_NOTE_NAME))
        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;

      XDELETEVEC (note);

OK with that change.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 1/2] [nto] Fix nto target stopped by watchpoint.
  2015-10-21 10:39         ` Pedro Alves
@ 2015-10-21 17:51           ` Aleksandar Ristovski
  2015-10-21 18:00             ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-21 17:51 UTC (permalink / raw)
  To: gdb-patches

On 15-10-21 05:28 AM, Pedro Alves wrote:
> On 10/20/2015 07:10 PM, Aleksandar Ristovski wrote:
>> Fix 'stopped by watchpoint' detection: add inferior data, use inferior data
>> for storing last stopped flags needed for detection.
>>
> 
> OK.
> 
> Thanks,
> Pedro Alves
> 
> 

Pushed a9889169e5b21efb8c42105fc62461be43968d64

Thank you,

Aleksandar Ristovski

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 1/2] [nto] Fix nto target stopped by watchpoint.
  2015-10-21 17:51           ` Aleksandar Ristovski
@ 2015-10-21 18:00             ` Aleksandar Ristovski
  0 siblings, 0 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-21 18:00 UTC (permalink / raw)
  To: gdb-patches

On 15-10-21 05:28 AM, Pedro Alves wrote:
> On 10/20/2015 07:10 PM, Aleksandar Ristovski wrote:
>> Fix 'stopped by watchpoint' detection: add inferior data, use inferior data
>> for storing last stopped flags needed for detection.
>>
> 
> OK.
> 
> Thanks,
> Pedro Alves
> 
> 

Pushed a9889169e5b21efb8c42105fc62461be43968d64

Thank you,

Aleksandar Ristovski


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/2] [nto] Improve ABI sniffing.
  2015-10-21 16:39                 ` Pedro Alves
@ 2015-10-21 18:10                   ` Aleksandar Ristovski
  2015-10-21 18:23                     ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-21 18:10 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 15-10-21 10:23 AM, Pedro Alves wrote:
> On 10/21/2015 03:14 PM, Aleksandar Ristovski wrote:
>> +      name = note + sizeof_Elf_Nhdr;
>> +      if (sectsize < namelen + sizeof_Elf_Nhdr
>> +	  || namelen > sizeof (QNX_NOTE_NAME))
>> +	{
>> +	  /* Can not be QNX note.  */
>> +	  XDELETEVEC (note);
>> +	  return;
>> +	}
>> +
>> +      if (namelen == sizeof (QNX_NOTE_NAME)
>> +	  && 0 == strcmp (name, QNX_NOTE_NAME))
>> +        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
>> +
>> +      XDELETEVEC (note);
> 
> Looks like these two ifs are now the same as the simpler:
> 
>       name = note + sizeof_Elf_Nhdr;
>       if (sectsize >= namelen + sizeof_Elf_Nhdr
>           && namelen == sizeof (QNX_NOTE_NAME)
> 	  && 0 == strcmp (name, QNX_NOTE_NAME))
>         *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
> 
>       XDELETEVEC (note);
> 
> OK with that change.
> 
> Thanks,
> Pedro Alves
> 

Pushed  d7161de46af80e460d432d8dbb1c42f8cbacf6dc

With the following fixup:
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -361,15 +361,8 @@ nto_sniff_abi_note_section (bfd *abfd, asection
*sect, void *obj)
       bfd_get_section_contents (abfd, sect, note, 0, sectsize);
       namelen = (unsigned int) bfd_h_get_32 (abfd, note);
       name = note + sizeof_Elf_Nhdr;
-      if (sectsize < namelen + sizeof_Elf_Nhdr
-         || namelen > sizeof (QNX_NOTE_NAME))
-       {
-         /* Can not be QNX note.  */
-         XDELETEVEC (note);
-         return;
-       }
-
-      if (namelen == sizeof (QNX_NOTE_NAME)
+      if (sectsize >= namelen + sizeof_Elf_Nhdr
+         && namelen == sizeof (QNX_NOTE_NAME)
          && 0 == strcmp (name, QNX_NOTE_NAME))
         *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;


Thank you,

Aleksandar Ristovski

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 2/2] [nto] Improve ABI sniffing.
  2015-10-21 18:10                   ` Aleksandar Ristovski
@ 2015-10-21 18:23                     ` Aleksandar Ristovski
  0 siblings, 0 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-21 18:23 UTC (permalink / raw)
  To: gdb-patches

On 15-10-21 10:23 AM, Pedro Alves wrote:
> On 10/21/2015 03:14 PM, Aleksandar Ristovski wrote:
>> +      name = note + sizeof_Elf_Nhdr;
>> +      if (sectsize < namelen + sizeof_Elf_Nhdr
>> +	  || namelen > sizeof (QNX_NOTE_NAME))
>> +	{
>> +	  /* Can not be QNX note.  */
>> +	  XDELETEVEC (note);
>> +	  return;
>> +	}
>> +
>> +      if (namelen == sizeof (QNX_NOTE_NAME)
>> +	  && 0 == strcmp (name, QNX_NOTE_NAME))
>> +        *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
>> +
>> +      XDELETEVEC (note);
> 
> Looks like these two ifs are now the same as the simpler:
> 
>       name = note + sizeof_Elf_Nhdr;
>       if (sectsize >= namelen + sizeof_Elf_Nhdr
>           && namelen == sizeof (QNX_NOTE_NAME)
> 	  && 0 == strcmp (name, QNX_NOTE_NAME))
>         *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
> 
>       XDELETEVEC (note);
> 
> OK with that change.
> 
> Thanks,
> Pedro Alves
> 

Pushed  d7161de46af80e460d432d8dbb1c42f8cbacf6dc

With the following fixup:
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -361,15 +361,8 @@ nto_sniff_abi_note_section (bfd *abfd, asection
*sect, void *obj)
       bfd_get_section_contents (abfd, sect, note, 0, sectsize);
       namelen = (unsigned int) bfd_h_get_32 (abfd, note);
       name = note + sizeof_Elf_Nhdr;
-      if (sectsize < namelen + sizeof_Elf_Nhdr
-         || namelen > sizeof (QNX_NOTE_NAME))
-       {
-         /* Can not be QNX note.  */
-         XDELETEVEC (note);
-         return;
-       }
-
-      if (namelen == sizeof (QNX_NOTE_NAME)
+      if (sectsize >= namelen + sizeof_Elf_Nhdr
+         && namelen == sizeof (QNX_NOTE_NAME)
          && 0 == strcmp (name, QNX_NOTE_NAME))
         *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;


Thank you,

Aleksandar Ristovski

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 4/4] [nto] Setup signals.
  2015-10-16 16:16   ` Pedro Alves
@ 2015-10-22 15:57     ` Aleksandar Ristovski
  2015-10-22 15:58       ` Aleksandar Ristovski
  0 siblings, 1 reply; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-22 15:57 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 15-10-16 12:16 PM, Pedro Alves wrote:
> On 10/13/2015 05:01 PM, Aleksandar Ristovski wrote:
>> Add new file with neutrino signal numerical values.
>>
>>         * i386-nto-tdep.c (i386nto_init_abi): Setup new
>>         nto_gdb_signal_from_target and nto_gdb_signal_to_target.
>>         * nto-tdep.c (signals): New definition.
>>         (nto_gdb_signal_to_target, nto_gdb_signal_from_target): New functions.
>>         * nto-tdep.h (nto_gdb_signal_to_target, nto_gdb_signal_from_target):
>>         New declarations.
>>         * nto_signals.def: New file.
>>         * include/gdb/signals.def (GDB_SIGNAL_SELECT): New gdb signal enum.
>>         (GDB_SIGNAL_LAST): Bump numeric value up.
> 
> What does this fix?  I assume more than just adding the SIGSELECT signal?
> 


I have had similar hack for a long time. The purpose was addressing core
signal translation, and more recently I figured I'd need it for
gdbserver. However, it is not needed for gdbserver at all, and I'm not
ready to submit patches for nto/core support, so dropping this patch.

Thank you,

Aleksandar Ristovski

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH 4/4] [nto] Setup signals.
  2015-10-22 15:57     ` Aleksandar Ristovski
@ 2015-10-22 15:58       ` Aleksandar Ristovski
  0 siblings, 0 replies; 47+ messages in thread
From: Aleksandar Ristovski @ 2015-10-22 15:58 UTC (permalink / raw)
  To: gdb-patches

On 15-10-16 12:16 PM, Pedro Alves wrote:
> On 10/13/2015 05:01 PM, Aleksandar Ristovski wrote:
>> Add new file with neutrino signal numerical values.
>>
>>         * i386-nto-tdep.c (i386nto_init_abi): Setup new
>>         nto_gdb_signal_from_target and nto_gdb_signal_to_target.
>>         * nto-tdep.c (signals): New definition.
>>         (nto_gdb_signal_to_target, nto_gdb_signal_from_target): New functions.
>>         * nto-tdep.h (nto_gdb_signal_to_target, nto_gdb_signal_from_target):
>>         New declarations.
>>         * nto_signals.def: New file.
>>         * include/gdb/signals.def (GDB_SIGNAL_SELECT): New gdb signal enum.
>>         (GDB_SIGNAL_LAST): Bump numeric value up.
> 
> What does this fix?  I assume more than just adding the SIGSELECT signal?
> 


I have had similar hack for a long time. The purpose was addressing core
signal translation, and more recently I figured I'd need it for
gdbserver. However, it is not needed for gdbserver at all, and I'm not
ready to submit patches for nto/core support, so dropping this patch.

Thank you,

Aleksandar Ristovski


^ permalink raw reply	[flat|nested] 47+ messages in thread

end of thread, other threads:[~2015-10-22 14:33 UTC | newest]

Thread overview: 47+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-13 16:01 [PATCH 0/4] [nto] Nto fixes Aleksandar Ristovski
2015-10-13 16:01 ` [PATCH 4/4] [nto] Setup signals Aleksandar Ristovski
2015-10-16 16:16   ` Pedro Alves
2015-10-22 15:57     ` Aleksandar Ristovski
2015-10-22 15:58       ` Aleksandar Ristovski
2015-10-13 16:01 ` [PATCH 3/4] [nto] Fix nto target stopped by watchpoint Aleksandar Ristovski
2015-10-16 16:10   ` Pedro Alves
2015-10-20 18:42     ` [PATCH 0/2] (patch 3/4 v2) Broken down patch 3/4 Aleksandar Ristovski
2015-10-20 19:24       ` [PATCH 2/2] [nto] Improve ABI sniffing Aleksandar Ristovski
2015-10-21 10:39         ` Pedro Alves
2015-10-21 14:42           ` Aleksandar Ristovski
2015-10-21 14:47             ` Aleksandar Ristovski
2015-10-21 15:17             ` Pedro Alves
2015-10-21 15:37               ` Aleksandar Ristovski
2015-10-21 16:13                 ` Aleksandar Ristovski
2015-10-21 16:39                 ` Pedro Alves
2015-10-21 18:10                   ` Aleksandar Ristovski
2015-10-21 18:23                     ` Aleksandar Ristovski
2015-10-21  8:18       ` [PATCH 1/2] [nto] Fix nto target stopped by watchpoint Aleksandar Ristovski
2015-10-21 10:39         ` Pedro Alves
2015-10-21 17:51           ` Aleksandar Ristovski
2015-10-21 18:00             ` Aleksandar Ristovski
2015-10-13 16:01 ` [PATCH 2/4] [nto] Fixes for nto procfs Aleksandar Ristovski
2015-10-15 17:41   ` Pedro Alves
2015-10-20 12:43     ` Aleksandar Ristovski
2015-10-20 13:21       ` Aleksandar Ristovski
2015-10-20 14:28       ` Pedro Alves
2015-10-20 14:28         ` [PATCH 0/3] (patch 2/4, v2) Break patch 2/4 into 3 Aleksandar Ristovski
2015-10-20 14:28           ` [PATCH 2/3] (patch 2/4, v2) [nto] Implement TARGET_OBJECT_AUXV Aleksandar Ristovski
2015-10-20 15:24             ` Pedro Alves
2015-10-20 16:03               ` Aleksandar Ristovski
2015-10-20 16:48                 ` Pedro Alves
2015-10-20 17:08                   ` Aleksandar Ristovski
2015-10-20 17:13                     ` Aleksandar Ristovski
2015-10-20 18:11                     ` Pedro Alves
2015-10-20 18:11                     ` Aleksandar Ristovski
2015-10-20 18:39                       ` Aleksandar Ristovski
2015-10-20 14:29           ` [PATCH 1/3] (patch 2/4, v2) [nto] Fixes for nto procfs Aleksandar Ristovski
2015-10-20 15:20             ` Pedro Alves
2015-10-20 17:13               ` Aleksandar Ristovski
2015-10-20 17:14                 ` Aleksandar Ristovski
2015-10-20 15:03           ` [PATCH 3/3] (patch 2/4, v2) [nto] Implement procfs_pid_to_exec_file Aleksandar Ristovski
2015-10-20 15:25             ` Pedro Alves
2015-10-20 18:11               ` Aleksandar Ristovski
2015-10-20 18:19                 ` Aleksandar Ristovski
2015-10-13 16:01 ` [PATCH 1/4] [nto] Fix nto build Aleksandar Ristovski
2015-10-15 17:34   ` Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).