[Mesa-dev] [PATCH 1/3] gallium/os: add os_get_process_cmd_line

Brian Paul brianp at vmware.com
Mon Aug 1 14:39:26 UTC 2016


Coincidentally, I wrote a very similar function a couple months ago (in 
our in-house tree).  My version uses open("/proc/self/cmdline", 
O_RDONLY) and has a Windows implementation too.

I'll post it shortly...

-Brian


On 07/30/2016 09:22 AM, Marek Olšák wrote:
> From: Marek Olšák <marek.olsak at amd.com>
>
> for debugging
> ---
>   src/gallium/auxiliary/os/os_process.c | 37 +++++++++++++++++++++++++++++++++++
>   src/gallium/auxiliary/os/os_process.h |  2 ++
>   2 files changed, 39 insertions(+)
>
> diff --git a/src/gallium/auxiliary/os/os_process.c b/src/gallium/auxiliary/os/os_process.c
> index 332e195..3ee30b1 100644
> --- a/src/gallium/auxiliary/os/os_process.c
> +++ b/src/gallium/auxiliary/os/os_process.c
> @@ -30,6 +30,10 @@
>   #include "os/os_process.h"
>   #include "util/u_memory.h"
>
> +#if defined(PIPE_OS_UNIX)
> +#include <stdio.h>
> +#endif
> +
>   #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
>   #  include <windows.h>
>   #elif defined(__GLIBC__) || defined(__CYGWIN__)
> @@ -108,3 +112,36 @@ os_get_process_name(char *procname, size_t size)
>         return FALSE;
>      }
>   }
> +
> +bool
> +os_get_process_cmd_line(char *result, size_t size)
> +{
> +#if defined(PIPE_OS_UNIX)
> +   char ps[256];
> +   int len;
> +   FILE *p;
> +
> +   /* Execute the ps command. */
> +   snprintf(ps, sizeof(ps), "ps --pid %i -o args --no-headers", getpid());
> +
> +   p = popen(ps, "r");
> +   if (!p)
> +      return false;
> +
> +   result = fgets(result, size, p);
> +   pclose(p);
> +
> +   if (!result)
> +      return false;
> +
> +   /* Remove the newline character. */
> +   len = strlen(result);
> +   if (result[len - 1] == '\n')
> +      result[len - 1] = 0;
> +
> +   return true;
> +
> +#else
> +   return false;
> +#endif
> +}
> diff --git a/src/gallium/auxiliary/os/os_process.h b/src/gallium/auxiliary/os/os_process.h
> index 0d50ddc..3965a8b 100644
> --- a/src/gallium/auxiliary/os/os_process.h
> +++ b/src/gallium/auxiliary/os/os_process.h
> @@ -36,5 +36,7 @@
>   extern boolean
>   os_get_process_name(char *str, size_t size);
>
> +extern bool
> +os_get_process_cmd_line(char *result, size_t size);
>
>   #endif /* OS_PROCESS_H */
>



More information about the mesa-dev mailing list