Bug 33366 - Cannot cast gdb.Value to 128bit python integer in gdb 16.3
Summary: Cannot cast gdb.Value to 128bit python integer in gdb 16.3
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: 16.3
: P2 normal
Target Milestone: 17.1
Assignee: Tom Tromey
URL:
Keywords:
: 33365 (view as bug list)
Depends on:
Blocks: 20991
  Show dependency treegraph
 
Reported: 2025-09-03 17:40 UTC by Vacha Bhavsar
Modified: 2025-09-05 17:40 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2025-09-04 00:00:00
Project(s) to access:
ssh public key:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vacha Bhavsar 2025-09-03 17:40:25 UTC
Overview:

When attempting to cast a gdb.Value object to a 128bit python integer, the following error is seen:

Python Exception <class 'gdb.error'>: Value cannot be represented as integer of 8 bytes.

This issue is encountered with gdb 16.3 only when used without client/server mode. This error has been encountered on arm64.

NOTE: This bug may be related to a similar bug I filed here: https://sourceware.org/bugzilla/show_bug.cgi?id=33365

Steps to reproduce:

Below are the steps to reproduce this issue with gdb 16.3:

1) Begin a gdb debugging session (without client server mode) with any test binary using the above specified version of gdb.
2) Add a breakpoint.
3) Run the program.
4) Once the breakpoint is hit, enter the following commands:

python
reg = "$v0.q.u[0]"
v = gdb.parse_and_eval(reg)
int(v)
end

Actual Results:
Upon running the above commands, gdb printed out the following error message:

Python Exception <class 'gdb.error'>: Value cannot be represented as integer of 8 bytes.
Error occurred in Python: Value cannot be represented as integer of 8 bytes.

Expected Results:
The expected result is for gdb to successfully perform the cast and return the gdb prompt.

Build Date and Hardware:
Release 16.3 on Ubuntu 22.04.5 LTS
Comment 1 Tom Tromey 2025-09-04 00:54:04 UTC
*** Bug 33365 has been marked as a duplicate of this bug. ***
Comment 2 Tom Tromey 2025-09-04 00:57:06 UTC
Confirmed.

The fix is probably https://docs.python.org/3/c-api/long.html#c.PyLong_FromNativeBytes.  (There's also an unsigned variant.)

However that's from 3.13 so we need the workaround mentioned here:
https://docs.python.org/3/c-api/long.html#c.PyLong_FromString

... probably conditionally so that someday we can remove the
old style.
Comment 3 Tom Tromey 2025-09-04 00:58:12 UTC
Meant to say, the fix would be in python/py-value.c:valpy_long
Comment 5 Sourceware Commits 2025-09-05 17:40:02 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=27697103012d289fe279e46a5222308959fe4290

commit 27697103012d289fe279e46a5222308959fe4290
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Sep 5 05:31:34 2025 -0600

    Allow conversion of 128-bit integers to Python
    
    Currently, trying to convert a 128-bit integer from a gdb.Value to a
    Python integer will fail.  This is surprising because Python uses
    bigints internally.
    
    The bug here is that valpy_long uses value_as_long, which fails for
    anything wider than LONGEST.  This patch fixes the problem by using
    the recommended Python API.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33366
    Approved-By: Simon Marchi <simon.marchi@efficios.com>
Comment 6 Tom Tromey 2025-09-05 17:40:56 UTC
Fixed.