public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Kamil Rytarowski <krytarowski@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] Implement "info proc cwd" for NetBSD
Date: Sun, 12 Apr 2020 18:15:17 +0000 (GMT)	[thread overview]
Message-ID: <20200412181517.BC927385BF83@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b4848d2a7b63d6001051f39d50ca965cb4210951

commit b4848d2a7b63d6001051f39d50ca965cb4210951
Author: Kamil Rytarowski <n54@gmx.com>
Date:   Sun Apr 12 19:24:01 2020 +0200

    Implement "info proc cwd" for NetBSD
    
    Add nbsd_pid_to_cwd() to query the program current directory.
    
    gdb/ChangeLog:
    
            * nbsd-nat.c (nbsd_pid_to_cwd): Add.
            (nbsd_nat_target::info_proc): Add do_cwd.

Diff:
---
 gdb/ChangeLog  |  5 +++++
 gdb/nbsd-nat.c | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 3bbd5c3129d..9e49cf9cf97 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2020-04-12  Kamil Rytarowski  <n54@gmx.com>
+
+	* nbsd-nat.c (nbsd_pid_to_cwd): Add.
+        (nbsd_nat_target::info_proc): Add do_cwd.
+
 2020-04-12  Kamil Rytarowski  <n54@gmx.com>
 
 	* nbsd-nat.c (nbsd_nat_target::info_proc): Add do_exe.
diff --git a/gdb/nbsd-nat.c b/gdb/nbsd-nat.c
index 05aedf8e3f1..1bb35f82c18 100644
--- a/gdb/nbsd-nat.c
+++ b/gdb/nbsd-nat.c
@@ -44,6 +44,20 @@ nbsd_nat_target::pid_to_exec_file (int pid)
   return buf;
 }
 
+/* Return the current directory for the process identified by PID.  */
+
+static std::string
+nbsd_pid_to_cwd (int pid)
+{
+  char buf[PATH_MAX];
+  size_t buflen;
+  int mib[4] = {CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_CWD};
+  buflen = sizeof (buf);
+  if (sysctl (mib, ARRAY_SIZE (mib), buf, &buflen, NULL, 0))
+    return "";
+  return buf;
+}
+
 /* Generic thread (LWP) lister within a specified process.  The callback
    parameters is a C++ function that is called for each detected thread.  */
 
@@ -299,6 +313,7 @@ bool
 nbsd_nat_target::info_proc (const char *args, enum info_proc_what what)
 {
   pid_t pid;
+  bool do_cwd = false;
   bool do_exe = false;
   bool do_mappings = false;
 
@@ -310,6 +325,9 @@ nbsd_nat_target::info_proc (const char *args, enum info_proc_what what)
     case IP_EXE:
       do_exe = true;
       break;
+    case IP_CWD:
+      do_cwd = true;
+      break;
     default:
       error (_("Not supported on this target."));
     }
@@ -328,6 +346,14 @@ nbsd_nat_target::info_proc (const char *args, enum info_proc_what what)
 
   printf_filtered (_("process %d\n"), pid);
 
+  if (do_cwd)
+    {
+      std::string cwd = nbsd_pid_to_cwd (pid);
+      if (cwd != "")
+	printf_filtered ("cwd = '%s'\n", cwd.c_str ());
+      else
+	warning (_("unable to fetch current working directory"));
+    }
   if (do_exe)
     {
       const char *exe = pid_to_exec_file (pid);


                 reply	other threads:[~2020-04-12 18:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200412181517.BC927385BF83@sourceware.org \
    --to=krytarowski@sourceware.org \
    --cc=gdb-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).