public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3] Cast pid_t to long long when printing
@ 2017-08-18 15:20 Ulf Hermann
  2017-08-19  8:11 ` Mark Wielaard
  0 siblings, 1 reply; 2+ messages in thread
From: Ulf Hermann @ 2017-08-18 15:20 UTC (permalink / raw)
  To: elfutils-devel

We don't know sizeof(pid_t) as it's not specified in any standard. In
order to still print it, we cast to long long, the largest integer type
we can easily print.

(Rebased, and rephrased message: This is not a windows quirk, but
rather a standards problem.)

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
---
 src/ChangeLog |  4 ++++
 src/stack.c   | 22 +++++++++++-----------
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 8aa57051..daedfca6 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* stack.c: Print pid_t using %lld.
+
 2017-08-18  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* readelf.c: Hardcode the signal numbers for non-linux systems.
diff --git a/src/stack.c b/src/stack.c
index 6f2ff69f..392db2a6 100644
--- a/src/stack.c
+++ b/src/stack.c
@@ -362,7 +362,7 @@ print_frames (struct frames *frames, pid_t tid, int dwflerr, const char *what)
   if (frames->frames > 0)
     frames_shown = true;
 
-  printf ("TID %d:\n", tid);
+  printf ("TID %lld:\n", (long long)tid);
   int frame_nr = 0;
   for (int nr = 0; nr < frames->frames && (maxframes == 0
 					   || frame_nr < maxframes); nr++)
@@ -419,8 +419,8 @@ print_frames (struct frames *frames, pid_t tid, int dwflerr, const char *what)
     }
 
   if (frames->frames > 0 && frame_nr == maxframes)
-    error (0, 0, "tid %d: shown max number of frames "
-	   "(%d, use -n 0 for unlimited)", tid, maxframes);
+    error (0, 0, "tid %lld: shown max number of frames "
+	   "(%d, use -n 0 for unlimited)", (long long)tid, maxframes);
   else if (dwflerr != 0)
     {
       if (frames->frames > 0)
@@ -440,11 +440,11 @@ print_frames (struct frames *frames, pid_t tid, int dwflerr, const char *what)
 	      else
 		modname = "<unknown>";
 	    }
-	  error (0, 0, "%s tid %d at 0x%" PRIx64 " in %s: %s", what, tid,
+	  error (0, 0, "%s tid %lld at 0x%" PRIx64 " in %s: %s", what, (long long)tid,
 		 pc_adjusted, modname, dwfl_errmsg (dwflerr));
 	}
       else
-	error (0, 0, "%s tid %d: %s", what, tid, dwfl_errmsg (dwflerr));
+	error (0, 0, "%s tid %lld: %s", what, (long long)tid, dwfl_errmsg (dwflerr));
     }
 }
 
@@ -575,10 +575,10 @@ parse_opt (int key, char *arg __attribute__ ((unused)),
 
 	  int err = dwfl_linux_proc_report (dwfl, pid);
 	  if (err < 0)
-	    error (EXIT_BAD, 0, "dwfl_linux_proc_report pid %d: %s", pid,
+	    error (EXIT_BAD, 0, "dwfl_linux_proc_report pid %lld: %s", (long long)pid,
 		   dwfl_errmsg (-1));
 	  else if (err > 0)
-	    error (EXIT_BAD, err, "dwfl_linux_proc_report pid %d", pid);
+	    error (EXIT_BAD, err, "dwfl_linux_proc_report pid %lld", (long long)pid);
 	}
 
       if (core != NULL)
@@ -597,10 +597,10 @@ parse_opt (int key, char *arg __attribute__ ((unused)),
 	{
 	  int err = dwfl_linux_proc_attach (dwfl, pid, false);
 	  if (err < 0)
-	    error (EXIT_BAD, 0, "dwfl_linux_proc_attach pid %d: %s", pid,
+	    error (EXIT_BAD, 0, "dwfl_linux_proc_attach pid %lld: %s", (long long)pid,
 		   dwfl_errmsg (-1));
 	  else if (err > 0)
-	    error (EXIT_BAD, err, "dwfl_linux_proc_attach pid %d", pid);
+	    error (EXIT_BAD, err, "dwfl_linux_proc_attach pid %lld", (long long)pid);
 	}
 
       if (core != NULL)
@@ -688,7 +688,7 @@ invoked with bad or missing arguments it will exit with return code 64.")
 
   if (show_modules)
     {
-      printf ("PID %d - %s module memory map\n", dwfl_pid (dwfl),
+      printf ("PID %lld - %s module memory map\n", (long long)dwfl_pid (dwfl),
 	      pid != 0 ? "process" : "core");
       if (dwfl_getmodules (dwfl, module_callback, NULL, 0) != 0)
 	error (EXIT_BAD, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
@@ -721,7 +721,7 @@ invoked with bad or missing arguments it will exit with return code 64.")
     }
   else
     {
-      printf ("PID %d - %s\n", dwfl_pid (dwfl), pid != 0 ? "process" : "core");
+      printf ("PID %lld - %s\n", (long long)dwfl_pid (dwfl), pid != 0 ? "process" : "core");
       switch (dwfl_getthreads (dwfl, thread_callback, &frames))
 	{
 	case DWARF_CB_OK:
-- 
2.11.0

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

* Re: [PATCH v3] Cast pid_t to long long when printing
  2017-08-18 15:20 [PATCH v3] Cast pid_t to long long when printing Ulf Hermann
@ 2017-08-19  8:11 ` Mark Wielaard
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Wielaard @ 2017-08-19  8:11 UTC (permalink / raw)
  To: Ulf Hermann; +Cc: elfutils-devel

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

On Fri, Aug 18, 2017 at 05:20:02PM +0200, Ulf Hermann wrote:
> We don't know sizeof(pid_t) as it's not specified in any standard. In
> order to still print it, we cast to long long, the largest integer type
> we can easily print.

That is surprising. But then printing it as long long is as good as
any other choice. I did tweak the patch a little so there is always
a space after the cast and no line gets longer than 80 chars.

Thanks,

Mark

[-- Attachment #2: 0001-Cast-pid_t-to-long-long-when-printing.patch --]
[-- Type: text/plain, Size: 4384 bytes --]

From 760925bc7b43814d30ee4e0396709fe8a3f66cd6 Mon Sep 17 00:00:00 2001
From: Ulf Hermann <ulf.hermann@qt.io>
Date: Fri, 18 Aug 2017 17:20:02 +0200
Subject: [PATCH] Cast pid_t to long long when printing

We don't know sizeof(pid_t) as it's not specified in any standard. In
order to still print it, we cast to long long, the largest integer type
we can easily print.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ChangeLog |  4 ++++
 src/stack.c   | 32 ++++++++++++++++++--------------
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 8aa57051..daedfca6 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* stack.c: Print pid_t using %lld.
+
 2017-08-18  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* readelf.c: Hardcode the signal numbers for non-linux systems.
diff --git a/src/stack.c b/src/stack.c
index 6f2ff69f..7c180b79 100644
--- a/src/stack.c
+++ b/src/stack.c
@@ -362,7 +362,7 @@ print_frames (struct frames *frames, pid_t tid, int dwflerr, const char *what)
   if (frames->frames > 0)
     frames_shown = true;
 
-  printf ("TID %d:\n", tid);
+  printf ("TID %lld:\n", (long long) tid);
   int frame_nr = 0;
   for (int nr = 0; nr < frames->frames && (maxframes == 0
 					   || frame_nr < maxframes); nr++)
@@ -419,8 +419,8 @@ print_frames (struct frames *frames, pid_t tid, int dwflerr, const char *what)
     }
 
   if (frames->frames > 0 && frame_nr == maxframes)
-    error (0, 0, "tid %d: shown max number of frames "
-	   "(%d, use -n 0 for unlimited)", tid, maxframes);
+    error (0, 0, "tid %lld: shown max number of frames "
+	   "(%d, use -n 0 for unlimited)", (long long) tid, maxframes);
   else if (dwflerr != 0)
     {
       if (frames->frames > 0)
@@ -440,11 +440,12 @@ print_frames (struct frames *frames, pid_t tid, int dwflerr, const char *what)
 	      else
 		modname = "<unknown>";
 	    }
-	  error (0, 0, "%s tid %d at 0x%" PRIx64 " in %s: %s", what, tid,
-		 pc_adjusted, modname, dwfl_errmsg (dwflerr));
+	  error (0, 0, "%s tid %lld at 0x%" PRIx64 " in %s: %s", what,
+		 (long long) tid, pc_adjusted, modname, dwfl_errmsg (dwflerr));
 	}
       else
-	error (0, 0, "%s tid %d: %s", what, tid, dwfl_errmsg (dwflerr));
+	error (0, 0, "%s tid %lld: %s", what, (long long) tid,
+	       dwfl_errmsg (dwflerr));
     }
 }
 
@@ -575,10 +576,11 @@ parse_opt (int key, char *arg __attribute__ ((unused)),
 
 	  int err = dwfl_linux_proc_report (dwfl, pid);
 	  if (err < 0)
-	    error (EXIT_BAD, 0, "dwfl_linux_proc_report pid %d: %s", pid,
-		   dwfl_errmsg (-1));
+	    error (EXIT_BAD, 0, "dwfl_linux_proc_report pid %lld: %s",
+		   (long long) pid, dwfl_errmsg (-1));
 	  else if (err > 0)
-	    error (EXIT_BAD, err, "dwfl_linux_proc_report pid %d", pid);
+	    error (EXIT_BAD, err, "dwfl_linux_proc_report pid %lld",
+		   (long long) pid);
 	}
 
       if (core != NULL)
@@ -597,10 +599,11 @@ parse_opt (int key, char *arg __attribute__ ((unused)),
 	{
 	  int err = dwfl_linux_proc_attach (dwfl, pid, false);
 	  if (err < 0)
-	    error (EXIT_BAD, 0, "dwfl_linux_proc_attach pid %d: %s", pid,
-		   dwfl_errmsg (-1));
+	    error (EXIT_BAD, 0, "dwfl_linux_proc_attach pid %lld: %s",
+		   (long long) pid, dwfl_errmsg (-1));
 	  else if (err > 0)
-	    error (EXIT_BAD, err, "dwfl_linux_proc_attach pid %d", pid);
+	    error (EXIT_BAD, err, "dwfl_linux_proc_attach pid %lld",
+		   (long long) pid);
 	}
 
       if (core != NULL)
@@ -688,7 +691,7 @@ invoked with bad or missing arguments it will exit with return code 64.")
 
   if (show_modules)
     {
-      printf ("PID %d - %s module memory map\n", dwfl_pid (dwfl),
+      printf ("PID %lld - %s module memory map\n", (long long) dwfl_pid (dwfl),
 	      pid != 0 ? "process" : "core");
       if (dwfl_getmodules (dwfl, module_callback, NULL, 0) != 0)
 	error (EXIT_BAD, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
@@ -721,7 +724,8 @@ invoked with bad or missing arguments it will exit with return code 64.")
     }
   else
     {
-      printf ("PID %d - %s\n", dwfl_pid (dwfl), pid != 0 ? "process" : "core");
+      printf ("PID %lld - %s\n", (long long) dwfl_pid (dwfl),
+	      pid != 0 ? "process" : "core");
       switch (dwfl_getthreads (dwfl, thread_callback, &frames))
 	{
 	case DWARF_CB_OK:
-- 
2.13.5


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

end of thread, other threads:[~2017-08-19  8:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-18 15:20 [PATCH v3] Cast pid_t to long long when printing Ulf Hermann
2017-08-19  8:11 ` Mark Wielaard

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).