[1/3] sim: callback: add a getpid interface

Message ID 20210621035505.29431-1-vapier@gentoo.org
State New
Headers show
Series
  • [1/3] sim: callback: add a getpid interface
Related show

Commit Message

Eli Zaretskii via Gdb-patches June 21, 2021, 3:55 a.m.
Rather than hit the OS interface directly, use the existing callback
layer so the instantiator can decide behavior.
---
 include/sim/callback.h |  1 +
 sim/common/callback.c  | 13 +++++++++++++
 sim/common/syscall.c   |  3 ++-
 3 files changed, 16 insertions(+), 1 deletion(-)

-- 
2.31.1

Comments

Eli Zaretskii via Gdb-patches June 21, 2021, 10:58 p.m. | #1
On Sun, Jun 20, 2021 at 10:55 PM Mike Frysinger via Gdb-patches
<gdb-patches@sourceware.org> wrote:
> --- a/sim/common/callback.c

> +++ b/sim/common/callback.c

> @@ -556,6 +556,17 @@ os_truncate (host_callback *p, const char *file, int64_t len)

>  #endif

>  }

>

> +static int

> +os_getpid (host_callback *p)

> +{

> +  int result;

> +

> +  result = getpid ();


sim is C11, right? Why not write this as "int result = getpid ();"?

Christian
Eli Zaretskii via Gdb-patches June 21, 2021, 11:50 p.m. | #2
On 21 Jun 2021 17:58, Christian Biesinger wrote:
> On Sun, Jun 20, 2021 at 10:55 PM Mike Frysinger wrote:

> > --- a/sim/common/callback.c

> > +++ b/sim/common/callback.c

> > @@ -556,6 +556,17 @@ os_truncate (host_callback *p, const char *file, int64_t len)

> >  #endif

> >  }

> >

> > +static int

> > +os_getpid (host_callback *p)

> > +{

> > +  int result;

> > +

> > +  result = getpid ();

> 

> sim is C11, right? Why not write this as "int result = getpid ();"?


just maintaining copy & paste style in this module, and because (iiuc), GNU
style wants a blank line after decls.  so imo it looks nicer to have business
logic grouped together.

{
  int result = getpid ();

  p->last_errno = errno;
  return result;
}

{
  int result;
  
  result = getpid ();
  p->last_errno = errno;
  return result;
}
-mike

Patch

diff --git a/include/sim/callback.h b/include/sim/callback.h
index 4c162bc145ea..a6c536b1be1c 100644
--- a/include/sim/callback.h
+++ b/include/sim/callback.h
@@ -91,6 +91,7 @@  struct host_callback_struct
   int (*to_lstat) (host_callback *, const char *, struct stat *);
   int (*ftruncate) (host_callback *, int, int64_t);
   int (*truncate) (host_callback *, const char *, int64_t);
+  int (*getpid) (host_callback *);
   int (*pipe) (host_callback *, int *);
 
   /* Called by the framework when a read call has emptied a pipe buffer.  */
diff --git a/sim/common/callback.c b/sim/common/callback.c
index f2587a452540..071e7b149b97 100644
--- a/sim/common/callback.c
+++ b/sim/common/callback.c
@@ -556,6 +556,17 @@  os_truncate (host_callback *p, const char *file, int64_t len)
 #endif
 }
 
+static int
+os_getpid (host_callback *p)
+{
+  int result;
+
+  result = getpid ();
+  /* POSIX says getpid always succeeds.  */
+  p->last_errno = 0;
+  return result;
+}
+
 static int
 os_pipe (host_callback *p, int *filedes)
 {
@@ -737,6 +748,8 @@  host_callback default_callback =
   os_ftruncate,
   os_truncate,
 
+  os_getpid,
+
   os_pipe,
   os_pipe_empty,
   os_pipe_nonempty,
diff --git a/sim/common/syscall.c b/sim/common/syscall.c
index 4e76d2008a30..7ef34b95e9cf 100644
--- a/sim/common/syscall.c
+++ b/sim/common/syscall.c
@@ -579,7 +579,8 @@  cb_syscall (host_callback *cb, CB_SYSCALL *sc)
       break;
 
     case CB_SYS_getpid:
-      result = getpid ();
+      /* POSIX says getpid always succeeds.  */
+      result = (*cb->getpid) (cb);
       break;
 
     case CB_SYS_time :