binutils-gdb/gdb/darwin-nat.h
Simon Marchi 7aabaf9d4a Create private_thread_info hierarchy
There are multiple definitions of the private_thread_info structure
compiled in the same GDB build.  Because of the one definition rule, we
need to change this if we want to be able to make them non-POD (e.g. use
std::vector fields).  This patch creates a class hierarchy, with
private_thread_info being an abstract base class, and all the specific
implementations inheriting from it.

In order to poison XNEW/xfree for non-POD types, it is also needed to
get rid of the xfree in thread_info::~thread_info, which operates on an
opaque type.  This is replaced by thread_info::priv now being a
unique_ptr, which calls the destructor of the private_thread_info
subclass when the thread is being destroyed.

Including gdbthread.h from darwin-nat.h gave these errors:

/Users/simark/src/binutils-gdb/gdb/gdbthread.h:609:3: error: must use 'class' tag to refer to type 'thread_info' in this scope
  thread_info *m_thread;
  ^
  class
/usr/include/mach/thread_act.h:240:15: note: class 'thread_info' is hidden by a non-type declaration of 'thread_info' here
kern_return_t thread_info
              ^

It turns out that there is a thread_info function in the Darwin/XNU/mach API:

  http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_info.html

Therefore, I had to add the class keyword at a couple of places in gdbthread.h,
I don't really see a way around it.

gdb/ChangeLog:

	* gdbthread.h (private_thread_info): Define structure type, add
	virtual pure destructor.
	(thread_info) <priv>: Change type to unique_ptr.
	<private_dtor>: Remove.
	* thread.c (add_thread_with_info): Adjust to use of unique_ptr.
	(private_thread_info::~private_thread_info): Provide default
	implementation.
	(thread_info::~thread_info): Don't call private_dtor nor
	manually free priv.
	* aix-thread.c (private_thread_info): Rename to ...
	(aix_thread_info): ... this.
	(get_aix_thread_info): New.
	(sync_threadlists): Adjust.
	(iter_tid): Adjust.
	(aix_thread_resume): Adjust.
	(aix_thread_fetch_registers): Adjust.
	(aix_thread_store_registers): Adjust.
	(aix_thread_extra_thread_info): Adjust.
	* darwin-nat.h (private_thread_info): Rename to ...
	(darwin_thread_info): ... this.
	(get_darwin_thread_info): New.
	* darwin-nat.c (darwin_init_thread_list): Adjust.
	(darwin_check_new_threads): Adjust.
	(thread_info_from_private_thread_info): Adjust.
	* linux-thread-db.c (private_thread_info): Rename to ...
	(thread_db_thread_info): ... this, initialize fields.
	(get_thread_db_thread_info): New.
	<dying>: Change type to bool.
	(update_thread_state): Adjust to type rename.
	(record_thread): Adjust to type rename an use of unique_ptr.
	(thread_db_pid_to_str): Likewise.
	(thread_db_extra_thread_info): Likewise.
	(thread_db_thread_handle_to_thread_info): Likewise.
	(thread_db_get_thread_local_address): Likewise.
	* nto-tdep.h (private_thread_info): Rename to ...
	(nto_thread_info): ... this, initialize fields.
	(get_nto_thread_info): New.
	<name>: Change type to std::string.
	* nto-tdep.c (nto_extra_thread_info): Adjust to type rename and
	use of unique_ptr.
	* nto-procfs.c (update_thread_private_data_name): Adjust to
	std::string change, allocate nto_private_thread_info with new.
	(update_thread_private_data): Adjust to unique_ptr.
	* remote.c (private_thread_info): Rename to ...
	(remote_thread_info): ... this, initialize data members with
	default values.
	<extra, name>: Change type to std::string.
	<thread_handle>: Change type to non-pointer.
	(free_private_thread_info): Remove.
	(get_private_info_thread): Rename to...
	(get_remote_thread_info): ... this, change return type, adjust to
	use of unique_ptr, use remote_thread_info constructor.
	(remote_add_thread): Adjust.
	(get_private_info_ptid): Rename to...
	(get_remote_thread_info): ...this, change return type.
	(remote_thread_name): Use get_remote_thread_info, adjust to
	change to std::string.
	(struct thread_item) <~thread_item>: Remove.
	<thread_handle>: Make non pointer.
	(start_thread): Adjust to thread_item::thread_handle type
	change.
	(remote_update_thread_list): Adjust to type name change, move
	strings from temporary to long-lived object instead of
	duplicating.
	(remote_threads_extra_info): Use get_remote_thread_info.
	(process_initial_stop_replies): Likewise.
	(resume_clear_thread_private_info): Likewise.
	(remote_resume): Adjust to type name change.
	(remote_commit_resume): Use get_remote_thread_info.
	(process_stop_reply): Adjust to type name change.
	(remote_stopped_by_sw_breakpoint): Use get_remote_thread_info.
	(remote_stopped_by_hw_breakpoint): Likewise.
	(remote_stopped_by_watchpoint): Likewise.
	(remote_stopped_data_address): Likewise.
	(remote_core_of_thread): Likewise.
	(remote_thread_handle_to_thread_info): Use
	get_private_info_thread, adjust to thread_handle field type
	change.
2017-11-24 10:40:31 -05:00

169 lines
4.7 KiB
C++

/* Common things used by the various darwin files
Copyright (C) 1995-2017 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef __DARWIN_NAT_H__
#define __DARWIN_NAT_H__
#include <mach/mach.h>
#include "gdbthread.h"
/* Describe the mach exception handling state for a task. This state is saved
before being changed and restored when a process is detached.
For more information on these fields see task_get_exception_ports manual
page. */
struct darwin_exception_info
{
/* Exceptions handled by the port. */
exception_mask_t masks[EXC_TYPES_COUNT] {};
/* Ports receiving exception messages. */
mach_port_t ports[EXC_TYPES_COUNT] {};
/* Type of messages sent. */
exception_behavior_t behaviors[EXC_TYPES_COUNT] {};
/* Type of state to be sent. */
thread_state_flavor_t flavors[EXC_TYPES_COUNT] {};
/* Number of elements set. */
mach_msg_type_number_t count = 0;
};
struct darwin_exception_msg
{
mach_msg_header_t header;
/* Thread and task taking the exception. */
mach_port_t thread_port;
mach_port_t task_port;
/* Type of the exception. */
exception_type_t ex_type;
/* Machine dependent details. */
mach_msg_type_number_t data_count;
integer_t ex_data[2];
};
enum darwin_msg_state
{
/* The thread is running. */
DARWIN_RUNNING,
/* The thread is stopped. */
DARWIN_STOPPED,
/* The thread has sent a message and waits for a reply. */
DARWIN_MESSAGE
};
struct darwin_thread_info : public private_thread_info
{
/* The thread port from a GDB point of view. */
thread_t gdb_port;
/* The thread port from the inferior point of view. Not to be used inside
gdb except for get_ada_task_ptid. */
thread_t inf_port;
/* Current message state.
If the kernel has sent a message it expects a reply and the inferior
can't be killed before. */
enum darwin_msg_state msg_state;
/* True if this thread is single-stepped. */
unsigned char single_step;
/* True if a signal was manually sent to the thread. */
unsigned char signaled;
/* The last exception received. */
struct darwin_exception_msg event;
};
typedef struct darwin_thread_info darwin_thread_t;
static inline darwin_thread_info *
get_darwin_thread_info (class thread_info *thread)
{
return static_cast<darwin_thread_info *> (thread->priv.get ());
}
/* Describe an inferior. */
struct darwin_inferior : public private_inferior
{
/* Corresponding task port. */
task_t task = 0;
/* Port which will receive the dead-name notification for the task port.
This is used to detect the death of the task. */
mach_port_t notify_port = 0;
/* Initial exception handling. */
darwin_exception_info exception_info;
/* Number of messages that have been received but not yet replied. */
unsigned int pending_messages = 0;
/* Set if inferior is not controlled by ptrace(2) but through Mach. */
bool no_ptrace = false;
/* True if this task is suspended. */
bool suspended = false;
/* Sorted vector of known threads. */
std::vector<darwin_thread_t *> threads;
};
/* Return the darwin_inferior attached to INF. */
static inline darwin_inferior *
get_darwin_inferior (inferior *inf)
{
return static_cast<darwin_inferior *> (inf->priv.get ());
}
/* Exception port. */
extern mach_port_t darwin_ex_port;
/* Port set. */
extern mach_port_t darwin_port_set;
/* A copy of mach_host_self (). */
extern mach_port_t darwin_host_self;
/* FUNCTION_NAME is defined in common-utils.h (or not). */
#ifdef FUNCTION_NAME
#define MACH_CHECK_ERROR(ret) \
mach_check_error (ret, __FILE__, __LINE__, FUNCTION_NAME)
#else
#define MACH_CHECK_ERROR(ret) \
mach_check_error (ret, __FILE__, __LINE__, "??")
#endif
extern void mach_check_error (kern_return_t ret, const char *file,
unsigned int line, const char *func);
void darwin_set_sstep (thread_t thread, int enable);
/* This one is called in darwin-nat.c, but needs to be provided by the
platform specific nat code. It allows each platform to add platform specific
stuff to the darwin_ops. */
extern void darwin_complete_target (struct target_ops *target);
void darwin_check_osabi (darwin_inferior *inf, thread_t thread);
#endif /* __DARWIN_NAT_H__ */