public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Aditya Kamath1 <Aditya.Kamath1@ibm.com>
To: Ulrich Weigand <Ulrich.Weigand@de.ibm.com>,
	"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>,
	"simon.marchi@efficios.com" <simon.marchi@efficios.com>
Cc: Sangamesh Mallayya <sangamesh.swamy@in.ibm.com>
Subject: Re: [PATCH] Fix call functions command bug in 64-bit programs for AIX
Date: Fri, 11 Nov 2022 17:53:01 +0000	[thread overview]
Message-ID: <BY5PR15MB3540CDC6D947954016FB791CD6009@BY5PR15MB3540.namprd15.prod.outlook.com> (raw)
In-Reply-To: <cd6f52b3394d83dc552e91ba7e2c99000c5f49d2.camel@de.ibm.com>

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

Hi Ulrich,

> This is not how the GDB register cache is working.  Can you explain in
> more detail what exactly you're refering to here?

Let us say we have a simple function, like print_Num (int a, long b) which prints these numbers a and b and the program is in 64-bit mode. If the programs are debugged normally, things work fine. But if a user attempts to see what the function does with the command call print_Num (int a, long b) then in the register assigned for function parameters the value 0 is getting dumped. What is happening is the pointer that is pointing to the input numbers a and b in the regcache->raw_supply which was addr previously is not aliging our values. It dumps the higher 32 bits in lower 32 bits and lower 32 bits to the higher which I figured out when I extracted it byte by byte using long pointer [long addr64 variable in the patch]. If int pointer is used in 64-bit mode in raw_supply code's second parameter, then memcpy () before rs6000_ptrace64 () fails to copy the right 8 bytes in buffer as int addr[64] is pointing to the wrong information. Overall, we were not pointing to the right place with the pointers passed to these functions.

>- what does ARCH64() return?
True in 64 bit mode and false in 32 bit mode

> - what does register_size(...) return for those registers?
8 in 64 bit mode, 4 in 32 bit mode
  (for GPRs with a 64-bit inferior it should return 8, if
  it doesn't that's probably the root cause of the problem)
> - what value does ptrace return in your case?
It returns the data parameter value that is passed as the calls are successful.

Consider this code below:-


#include "stdio.h"

int num2print(long num, float num2, int num3, double num4) {

  if (num == 0) {

    printf("R0\n");

    return 0;

  }


  if (num == 1) {

    printf("R1\n");

    return 1;

  }

  printf("R%d\n",num);

  printf("R%f\n",num2);

  printf("R%d\n",num3);

  printf("R%lf\n",num4);

  return num;

}


int main(int argc, char** argv)


{

  printf("Hi Bangalore %x\n",num2print(27, 16, 13, 9.9));

  return 0;

}


The output before patch in 64 bit mode is:-


Reading symbols from /home/XYZ...

(gdb) b main

Breakpoint 1 at 0x100007dc: file /home/XYZ.c, line 22.

(gdb) r

Starting program: /home/XYZ

BFD: /usr/lib/libc.a(/usr/lib/libc.a(shr_64.o)): wrong auxtype 0xff for storage class 0x2

BFD: /usr/lib/libc.a(/usr/lib/libc.a(shr_64.o)): wrong auxtype 0xff for storage class 0x6b


Breakpoint 1, main (argc=1, argv=0xffffffffffffad0) at /home/XYZ.c:22

22        printf("Hi Bangalore %x\n",num2print(27, 16, 13, 9.9));

(gdb) call num2print

$1 = {int (long, float, int, double)} 0x1000006a0 <num2print>

(gdb) call num2print (2, 3, 4, 5)

R2

R3.000000

R0

R5.000000

$2 = 2

(gdb)



The output highlighted in bold should have been R4 but is R0.. The datatype of the variable is long and if you print the output byte by byte before memcpy and if you have taken a long pointer to raw_supply, one realises 4 which is lower 32 bits is in higher bits and 0 in higher bits is in lower bits which was the pointer will be holding. The pointer getting the value in raw_supply was pointing to elsewhere instead of 4 [ the lower bits] if it is an int pointer.


Kindly let me know if you need more details. I am only trying to re align to correct the things my pointer in raw_supply function has messed up. Let me know if there can be a better way you have in mind.

Have a nice day.

Thanks and regards,
Aditya.




________________________________
From: Ulrich Weigand <Ulrich.Weigand@de.ibm.com>
Sent: 08 November 2022 19:00
To: gdb-patches@sourceware.org <gdb-patches@sourceware.org>; Aditya Kamath1 <Aditya.Kamath1@ibm.com>; simon.marchi@efficios.com <simon.marchi@efficios.com>
Cc: Sangamesh Mallayya <sangamesh.swamy@in.ibm.com>
Subject: Re: [PATCH] Fix call functions command bug in 64-bit programs for AIX

Aditya Kamath1 <Aditya.Kamath1@ibm.com> wrote:

>The issue is that when a user attempts to test the return type or print
>statements via the call "FUNC_NAME (parameter A, parameter B)" command,
any input be it parameter A or B is taken in little endian format from
>the GDB cache. But AIX is using Big endian format.

>For example, if we have a value 21 as type long then the higher 32 bits
>[ which is 0 in number 21] were stored in lower 32 bits and lower 32 bits
>[ represent 21 in the number 21] is stored in higher 32 bits.

This is not how the GDB register cache is working.  Can you explain in
more detail what exactly you're refering to here?

The register cache holds a sequence of bytes for each register.  The
number of bytes in the cache depends on the size of that register as
defined by the architecture.  The contents of those bytes are *always*
assumed to be in big-endian byte order on AIX.

For 64-bit inferior processes, GDB assumes that the ptrace call
always uses a 64-bit buffer, even if the register size is only 4.
To handle these cases, the current code will convert between the
two formats, which looks correct to me: in the fetch_register
case, it will retrieve an 8-byte value from ptrace, convert that
value (correctly, also for big-endian) to an 4-byte value, and
pass that 4-byte value to raw_supply, which should expect 4 bytes
in this case because the register_size is 4.

To understand what's going on exactly I need more details:
- what does ARCH64() return?
- what does register_size(...) return for those registers?
  (for GPRs with a 64-bit inferior it should return 8, if
  it doesn't that's probably the root cause of the problem)
- what value does ptrace return in your case?


In any case, explicitly listing specific register numbers by ABI
is definitely wrong here.  This routine must correctly handle
any register, it does not matter what the ABI usage is.


Bye,
Ulrich


  reply	other threads:[~2022-11-11 17:53 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-07 11:00 Aditya Kamath1
2022-11-08 13:30 ` Ulrich Weigand
2022-11-11 17:53   ` Aditya Kamath1 [this message]
2022-11-14 15:54     ` Ulrich Weigand
2022-11-14 17:32       ` Aditya Kamath1
2022-11-14 18:19         ` Ulrich Weigand
2022-11-14 18:28           ` Aditya Kamath1
2022-11-14 18:43             ` Ulrich Weigand
2022-11-14 18:52               ` Aditya Kamath1
2022-11-14 19:10                 ` Ulrich Weigand
2022-11-16 11:27                   ` Aditya Kamath1
2022-11-16 15:15                     ` Ulrich Weigand
2022-11-16 18:07                       ` Aditya Kamath1
2022-11-16 18:30                         ` Tom Tromey
2022-11-17 12:54                         ` Ulrich Weigand
2022-11-24 17:56                           ` Aditya Kamath1
2022-11-24 18:15                             ` Tom Tromey
2023-04-14  7:38                               ` [PATCH] Fix call functions command bug in 64-bit programs for AIX and PC read in psymtab-symtab warning Aditya Kamath1
2023-04-14 14:45                                 ` Tom Tromey
2023-04-17 13:08                                   ` Aditya Kamath1
2023-04-17 13:16                                     ` Aditya Kamath1
2023-04-18 10:12                                       ` Ulrich Weigand
2023-04-21 13:00                                         ` Aditya Kamath1
2023-04-24 15:44                                           ` Ulrich Weigand
2023-04-27 10:13                                             ` Aditya Kamath1
2023-04-27 12:23                                               ` Ulrich Weigand
2023-04-27 10:14                                   ` Aditya Kamath1

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BY5PR15MB3540CDC6D947954016FB791CD6009@BY5PR15MB3540.namprd15.prod.outlook.com \
    --to=aditya.kamath1@ibm.com \
    --cc=Ulrich.Weigand@de.ibm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=sangamesh.swamy@in.ibm.com \
    --cc=simon.marchi@efficios.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).