[Pdbg] [PATCH v4] stack: guess endian for stack frame walking

Alistair Popple alistair at popple.id.au
Fri Nov 2 11:16:34 AEDT 2018


Thanks Nick!

On Wednesday, 31 October 2018 9:58:03 AM AEDT Nicholas Piggin wrote:
> +		/*
> +		 * Basic endian detection.
> +		 * Stack grows down, so as we unwind it we expect to see
> +		 * increasing addresses without huge jumps.  The stack may
> +		 * switch endian-ness across frames in some cases (e.g., LE
> +		 * kernel calling BE OPAL).
> +		 */
> +
> +		/* Check for OPAL stack -> Linux stack */
> +		if ((sp >= 0x30000000UL && sp < 0x40000000UL) &&
> +				!(tmp >= 0x30000000UL && tmp < 0x40000000UL)) {
> +			if (tmp >> 60 == 0xc)
> +				goto no_flip;
> +			if (tmp2 >> 60 == 0xc)
> +				goto do_flip;
> +		}
> +
> +		/* Check for Linux -> userspace */
> +		if ((sp >> 60 == 0xc) && !(tmp >> 60 == 0xc)) {
> +			finished = true; /* Don't decode userspace */
> +			if (tmp >> 60 == 0)
> +				goto no_flip;
> +			if (tmp2 >> 60 == 0)
> +				goto do_flip;
> +		}
> +
> +		/* Otherwise try to ensure sane stack */
> +		if (tmp < sp || (tmp - sp > 0xffffffffUL)) {
> +			if (tmp2 < sp || (tmp2 - sp > 0xffffffffUL)) {
> +				finished = true;
> +				goto no_flip;
> +			}
> +do_flip:
> +			next_sp = tmp2;
> +			flip = true;
> +			be = !be;
> +		} else {
> +no_flip:
> +			next_sp = tmp;

I'd struggle to call this particular bit of code elegant but it seems to get 
the job done :-)

I'm not sure it's possible but I wouldn't be upset if someone submitted a 
clean-up patch in future to simplify the control flow a little bit. However 
it's quite a useful feature and I had to do a custom build last week for 
someone wanting to use it so I'll merge it now.

Reviewed-by: Alistair Popple <alistair at popple.id.au>

> +		}
> +
> +		if (flip)
> +			pc = flip_endian(pc);
> +
> +		printf(" 0x%016" PRIx64 " 0x%016" PRIx64 " (%s)\n",
> +			sp, pc, be ? "big-endian" : "little-endian");
>  	}
> +	printf(" 0x%016" PRIx64 "\n", next_sp);
> 
>  	return 0;
>  }




More information about the Pdbg mailing list