Skip to content

Commit

Permalink
Fix-for-multiple-thread-detection-in-AIX.
Browse files Browse the repository at this point in the history
In AIX multiple threads were not added. This patch is a fix for the same

When we create a pthread debug session we have callbacks to read
symbols and memory.  One of those call backs is pdc_read_data.

Before we come into aix-thread wait() we switch to no thread and
therefore the current thread is null.

When we get into pdc_read_data we have a dependency that we need to
be in the correct current thread that has caused an event of new
thread, inorder to read memory.

Hence we switch to the correct thread.

This is done by passing the pid in the pthdb_user_t user_current_pid
parameter in every call back.
  • Loading branch information
KamathForAIX authored and uweigand committed Aug 9, 2022
1 parent a8a8829 commit 80d3624
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions gdb/aix-thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ static bool debug_aix_thread;

#define PD_TID(ptid) (pd_active && ptid.tid () != 0)

/* pthdb_user_t value that we pass to pthdb functions. 0 causes
PTHDB_BAD_USER errors, so use 1. */

#define PD_USER 1

/* Success and failure values returned by pthdb callbacks. */

#define PDC_SUCCESS PTHDB_SUCCESS
Expand Down Expand Up @@ -331,16 +326,16 @@ pid_to_prc (ptid_t *ptidp)
the address of SYMBOLS[<i>].name. */

static int
pdc_symbol_addrs (pthdb_user_t user, pthdb_symbol_t *symbols, int count)
pdc_symbol_addrs (pthdb_user_t user_current_pid, pthdb_symbol_t *symbols, int count)
{
struct bound_minimal_symbol ms;
int i;
char *name;

if (debug_aix_thread)
gdb_printf (gdb_stdlog,
"pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
user, (long) symbols, count);
"pdc_symbol_addrs (user_current_pid = %ld, symbols = 0x%lx, count = %d)\n",
user_current_pid, (long) symbols, count);

for (i = 0; i < count; i++)
{
Expand Down Expand Up @@ -378,7 +373,7 @@ pdc_symbol_addrs (pthdb_user_t user, pthdb_symbol_t *symbols, int count)
If successful return 0, else non-zero is returned. */

static int
pdc_read_regs (pthdb_user_t user,
pdc_read_regs (pthdb_user_t user_current_pid,
pthdb_tid_t tid,
unsigned long long flags,
pthdb_context_t *context)
Expand Down Expand Up @@ -450,7 +445,7 @@ pdc_read_regs (pthdb_user_t user,
If successful return 0, else non-zero is returned. */

static int
pdc_write_regs (pthdb_user_t user,
pdc_write_regs (pthdb_user_t user_current_pid,
pthdb_tid_t tid,
unsigned long long flags,
pthdb_context_t *context)
Expand Down Expand Up @@ -500,17 +495,29 @@ pdc_write_regs (pthdb_user_t user,
/* pthdb callback: read LEN bytes from process ADDR into BUF. */

static int
pdc_read_data (pthdb_user_t user, void *buf,
pdc_read_data (pthdb_user_t user_current_pid, void *buf,
pthdb_addr_t addr, size_t len)
{
int status, ret;

if (debug_aix_thread)
gdb_printf (gdb_stdlog,
"pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
user, (long) buf, hex_string (addr), len);
"pdc_read_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
user_current_pid, (long) buf, hex_string (addr), len);

status = target_read_memory (addr, (gdb_byte *) buf, len);
/* This is needed to eliminate the dependency of current thread
which is null so that thread reads the correct target memory. */
{
scoped_restore_current_thread restore_current_thread;
/* Before the first inferior is added, we pass inferior_ptid.pid ()
from pd_enable () which is 0. There is no need to switch threads
during first initialisation. In the rest of the callbacks the
current thread needs to be correct. */
if (user_current_pid != 0)
switch_to_thread (current_inferior ()->process_target (),
ptid_t (user_current_pid));
status = target_read_memory (addr, (gdb_byte *) buf, len);
}
ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;

if (debug_aix_thread)
Expand All @@ -522,15 +529,15 @@ pdc_read_data (pthdb_user_t user, void *buf,
/* pthdb callback: write LEN bytes from BUF to process ADDR. */

static int
pdc_write_data (pthdb_user_t user, void *buf,
pdc_write_data (pthdb_user_t user_current_pid, void *buf,
pthdb_addr_t addr, size_t len)
{
int status, ret;

if (debug_aix_thread)
gdb_printf (gdb_stdlog,
"pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
user, (long) buf, hex_string (addr), len);
"pdc_write_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
user_current_pid, (long) buf, hex_string (addr), len);

status = target_write_memory (addr, (gdb_byte *) buf, len);
ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
Expand All @@ -545,12 +552,12 @@ pdc_write_data (pthdb_user_t user, void *buf,
in BUFP. */

static int
pdc_alloc (pthdb_user_t user, size_t len, void **bufp)
pdc_alloc (pthdb_user_t user_current_pid, size_t len, void **bufp)
{
if (debug_aix_thread)
gdb_printf (gdb_stdlog,
"pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
user, len, (long) bufp);
"pdc_alloc (user_current_pid = %ld, len = %ld, bufp = 0x%lx)\n",
user_current_pid, len, (long) bufp);
*bufp = xmalloc (len);
if (debug_aix_thread)
gdb_printf (gdb_stdlog,
Expand All @@ -567,12 +574,12 @@ pdc_alloc (pthdb_user_t user, size_t len, void **bufp)
pointer to the result in BUFP. */

static int
pdc_realloc (pthdb_user_t user, void *buf, size_t len, void **bufp)
pdc_realloc (pthdb_user_t user_current_pid, void *buf, size_t len, void **bufp)
{
if (debug_aix_thread)
gdb_printf (gdb_stdlog,
"pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
user, (long) buf, len, (long) bufp);
"pdc_realloc (user_current_pid = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
user_current_pid, (long) buf, len, (long) bufp);
*bufp = xrealloc (buf, len);
if (debug_aix_thread)
gdb_printf (gdb_stdlog,
Expand All @@ -584,11 +591,11 @@ pdc_realloc (pthdb_user_t user, void *buf, size_t len, void **bufp)
realloc callback. */

static int
pdc_dealloc (pthdb_user_t user, void *buf)
pdc_dealloc (pthdb_user_t user_current_pid, void *buf)
{
if (debug_aix_thread)
gdb_printf (gdb_stdlog,
"pdc_free (user = %ld, buf = 0x%lx)\n", user,
"pdc_free (user_current_pid = %ld, buf = 0x%lx)\n", user_current_pid,
(long) buf);
xfree (buf);
return PDC_SUCCESS;
Expand Down Expand Up @@ -912,7 +919,7 @@ pd_activate (int pid)
{
int status;

status = pthdb_session_init (PD_USER, arch64 ? PEM_64BIT : PEM_32BIT,
status = pthdb_session_init (pid, arch64 ? PEM_64BIT : PEM_32BIT,
PTHDB_FLAG_REGS, &pd_callbacks,
&pd_session);
if (status != PTHDB_SUCCESS)
Expand Down Expand Up @@ -955,7 +962,7 @@ pd_enable (void)

/* Check whether the application is pthreaded. */
stub_name = NULL;
status = pthdb_session_pthreaded (PD_USER, PTHDB_FLAG_REGS,
status = pthdb_session_pthreaded (inferior_ptid.pid (), PTHDB_FLAG_REGS,
&pd_callbacks, &stub_name);
if ((status != PTHDB_SUCCESS
&& status != PTHDB_NOT_PTHREADED) || !stub_name)
Expand All @@ -976,7 +983,7 @@ pd_enable (void)
/* If we're debugging a core file or an attached inferior, the
pthread library may already have been initialized, so try to
activate thread debugging. */
pd_activate (1);
pd_activate (inferior_ptid.pid ());
}

/* Undo the effects of pd_enable(). */
Expand Down

0 comments on commit 80d3624

Please sign in to comment.