[U-Boot] [PATCH 08/21] sandbox: Allow running from valgrind

Alexander Graf agraf at suse.de
Sat Nov 24 21:31:19 UTC 2018



On 24.11.18 05:29, Simon Glass wrote:
> It is useful to run sandbox from valgrind to find memory errors, etc. At
> present this works OK until U-Boot jumps into the next phase (e.g. from
> SPL to U-Boot). Update os_jump_to_file() to use valgrind for each
> subsequent phase also.
> 
> Signed-off-by: Simon Glass <sjg at chromium.org>
> ---
> 
>  arch/sandbox/cpu/os.c | 37 +++++++++++++++++++++++++------------
>  1 file changed, 25 insertions(+), 12 deletions(-)
> 
> diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
> index 58d9a46263e..fd0c688edfb 100644
> --- a/arch/sandbox/cpu/os.c
> +++ b/arch/sandbox/cpu/os.c
> @@ -572,9 +572,11 @@ static int make_exec(char *fname, const void *data, int size)
>   * @argvp:  Returns newly allocated args list
>   * @add_args: Arguments to add, each a string
>   * @count: Number of arguments in @add_args
> + * @use_valgrind: Run the program with valgrind
>   * @return 0 if OK, -ENOMEM if out of memory
>   */
> -static int add_args(char ***argvp, char *add_args[], int count)
> +static int add_args(char ***argvp, char *add_args[], int count,
> +		    bool use_valgrind)
>  {
>  	char **argv, **ap;
>  	int argc;
> @@ -582,12 +584,15 @@ static int add_args(char ***argvp, char *add_args[], int count)
>  	for (argc = 0; (*argvp)[argc]; argc++)
>  		;
>  
> -	argv = os_malloc((argc + count + 1) * sizeof(char *));
> +	argv = os_malloc((argc + count + 2) * sizeof(char *));
>  	if (!argv) {
>  		printf("Out of memory for %d argv\n", count);
>  		return -ENOMEM;
>  	}
> -	for (ap = *argvp, argc = 0; *ap; ap++) {
> +	argc = 0;
> +	if (use_valgrind)
> +		argv[argc++] = "valgrind";
> +	for (ap = *argvp; *ap; ap++) {
>  		char *arg = *ap;
>  
>  		/* Drop args that we don't want to propagate */
> @@ -624,15 +629,18 @@ static int add_args(char ***argvp, char *add_args[], int count)
>  static int os_jump_to_file(const char *fname)
>  {
>  	struct sandbox_state *state = state_get_current();
> +	bool use_valgrind;
>  	char mem_fname[30];
>  	int fd, err;
> -	char *extra_args[5];
> +	char *extra_args[6];
>  	char **argv = state->argv;
>  	int argc;
>  #ifdef DEBUG
>  	int i;
>  #endif
>  
> +	use_valgrind = strlen(argv[0]) >= 8 &&
> +		!strcmp(argv[0] + strlen(argv[0]) - 8, "valgrind");
>  	strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
>  	fd = mkstemp(mem_fname);
>  	if (fd < 0)
> @@ -644,17 +652,22 @@ static int os_jump_to_file(const char *fname)
>  
>  	os_fd_restore();
>  
> -	extra_args[0] = "-j";
> -	extra_args[1] = (char *)fname;
> -	extra_args[2] = "-m";
> -	extra_args[3] = mem_fname;
> -	argc = 4;
> +	argc = 0;
> +	extra_args[argc++] = "-j";
> +	extra_args[argc++] = (char *)fname;
> +	extra_args[argc++] = "-m";
> +	extra_args[argc++] = mem_fname;
>  	if (state->ram_buf_rm)
>  		extra_args[argc++] = "--rm_memory";
> -	err = add_args(&argv, extra_args, argc);
> +	err = add_args(&argv, extra_args, argc, use_valgrind);
>  	if (err)
>  		return err;
> -	argv[0] = (char *)fname;
> +	if (use_valgrind) {
> +		argv[0] = "/usr/bin/valgrind";

Is there a better way to exec into another program but preserve its
valgrindness than to explicitly call valgrind - and worse - hard code
the path to valgrind and its invocation type?

I would've expected valgrind has some way to make it inherit into child
processes?

(I've added Christian to CC - he knows his way around valgrind quite a
bit too)


Alex


More information about the U-Boot mailing list