aboutsummaryrefslogtreecommitdiff
path: root/gdb/nat
diff options
context:
space:
mode:
authorGary Benson <gbenson@redhat.com>2014-06-19 14:46:38 +0100
committerGary Benson <gbenson@redhat.com>2014-06-20 14:06:48 +0100
commit125f8a3ddedd413a2290dae011f0bed9ffc78278 (patch)
treed9755566364119d825f6ee87229983c7a8d68e46 /gdb/nat
parent42995dbda646ff0291a36f83a7f1a9f45e3fda8a (diff)
downloadgdb-125f8a3ddedd413a2290dae011f0bed9ffc78278.zip
gdb-125f8a3ddedd413a2290dae011f0bed9ffc78278.tar.gz
gdb-125f8a3ddedd413a2290dae011f0bed9ffc78278.tar.bz2
Move shared native target specific code to gdb/nat
https://sourceware.org/gdb/wiki/Common describes the following directory structure: gdb/nat/ Native target backend files. Code that interfaces with the host debug API. E.g., ptrace code, Windows debug API code, procfs code should go here. gdb/target/ Host-independent, target vector specific code (target_ops). gdb/common/ All other shared code. This commit moves all native target backend files currently in gdb/common to gdb/nat. gdb/ 2014-06-20 Gary Benson <gbenson@redhat.com> * common/gdb_thread_db.h: Moved to nat. All includes updated. * common/glibc_thread_db.h: Likewise. * common/i386-cpuid.h: Likewise. * common/i386-gcc-cpuid.h: Likewise. * common/linux-btrace.h: Likewise. * common/linux-osdata.h: Likewise. * common/linux-procfs.h: Likewise. * common/linux-ptrace.h: Likewise. * common/mips-linux-watch.h: Likewise. * common/linux-btrace.c: Moved to nat. * common/linux-osdata.c: Likewise. * common/linux-procfs.c: Likewise. * common/linux-ptrace.c: Likewise. * common/mips-linux-watch.c: Likewise. * nat/gdb_thread_db.h: Moved from common. * nat/glibc_thread_db.h: Likewise. * nat/i386-cpuid.h: Likewise. * nat/i386-gcc-cpuid.h: Likewise. * nat/linux-btrace.c: Likewise. * nat/linux-btrace.h: Likewise. * nat/linux-osdata.c: Likewise. * nat/linux-osdata.h: Likewise. * nat/linux-procfs.c: Likewise. * nat/linux-procfs.h: Likewise. * nat/linux-ptrace.c: Likewise. * nat/linux-ptrace.h: Likewise. * nat/mips-linux-watch.c: Likewise. * nat/mips-linux-watch.h: Likewise. * Makefile.in (HFILES_NO_SRCDIR): Reflect new locations. (object file files): Reordered. * gdb/copyright.py (EXCLUDE_LIST): Reflect new location of glibc_thread_db.h. gdb/gdbserver/ 2014-06-20 Gary Benson <gbenson@redhat.com> * Makefile.in (SFILES): Update locations for files moved from common to nat. (object file files): Reordered. gdb/testsuite/ 2014-06-20 Gary Benson <gbenson@redhat.com> * gdb.arch/i386-avx.exp: Fix include file location. * gdb.arch/i386-sse.exp: Likewise.
Diffstat (limited to 'gdb/nat')
-rw-r--r--gdb/nat/gdb_thread_db.h16
-rw-r--r--gdb/nat/glibc_thread_db.h458
-rw-r--r--gdb/nat/i386-cpuid.h63
-rw-r--r--gdb/nat/i386-dregs.c2
-rw-r--r--gdb/nat/i386-gcc-cpuid.h278
-rw-r--r--gdb/nat/linux-btrace.c634
-rw-r--r--gdb/nat/linux-btrace.h78
-rw-r--r--gdb/nat/linux-osdata.c1634
-rw-r--r--gdb/nat/linux-osdata.h29
-rw-r--r--gdb/nat/linux-procfs.c121
-rw-r--r--gdb/nat/linux-procfs.h43
-rw-r--r--gdb/nat/linux-ptrace.c553
-rw-r--r--gdb/nat/linux-ptrace.h95
-rw-r--r--gdb/nat/linux-waitpid.c4
-rw-r--r--gdb/nat/mips-linux-watch.c349
-rw-r--r--gdb/nat/mips-linux-watch.h126
16 files changed, 4480 insertions, 3 deletions
diff --git a/gdb/nat/gdb_thread_db.h b/gdb/nat/gdb_thread_db.h
new file mode 100644
index 0000000..a1d9473
--- /dev/null
+++ b/gdb/nat/gdb_thread_db.h
@@ -0,0 +1,16 @@
+#ifdef HAVE_THREAD_DB_H
+#include <thread_db.h>
+#else
+#include "glibc_thread_db.h"
+#endif
+
+#ifndef LIBTHREAD_DB_SO
+#define LIBTHREAD_DB_SO "libthread_db.so.1"
+#endif
+
+#ifndef LIBTHREAD_DB_SEARCH_PATH
+/* $sdir appears before $pdir for some minimal security protection:
+ we trust the system libthread_db.so a bit more than some random
+ libthread_db associated with whatever libpthread the app is using. */
+#define LIBTHREAD_DB_SEARCH_PATH "$sdir:$pdir"
+#endif
diff --git a/gdb/nat/glibc_thread_db.h b/gdb/nat/glibc_thread_db.h
new file mode 100644
index 0000000..97423a4
--- /dev/null
+++ b/gdb/nat/glibc_thread_db.h
@@ -0,0 +1,458 @@
+/* thread_db.h -- interface to libthread_db.so library for debugging -lpthread
+ Copyright (C) 1999-2013 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#ifndef _THREAD_DB_H
+#define _THREAD_DB_H 1
+
+/* This is the debugger interface for the NPTL library. It is
+ modelled closely after the interface with same names in Solaris
+ with the goal to share the same code in the debugger. */
+#include <pthread.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/procfs.h>
+
+
+/* Error codes of the library. */
+typedef enum
+{
+ TD_OK, /* No error. */
+ TD_ERR, /* No further specified error. */
+ TD_NOTHR, /* No matching thread found. */
+ TD_NOSV, /* No matching synchronization handle found. */
+ TD_NOLWP, /* No matching light-weighted process found. */
+ TD_BADPH, /* Invalid process handle. */
+ TD_BADTH, /* Invalid thread handle. */
+ TD_BADSH, /* Invalid synchronization handle. */
+ TD_BADTA, /* Invalid thread agent. */
+ TD_BADKEY, /* Invalid key. */
+ TD_NOMSG, /* No event available. */
+ TD_NOFPREGS, /* No floating-point register content available. */
+ TD_NOLIBTHREAD, /* Application not linked with thread library. */
+ TD_NOEVENT, /* Requested event is not supported. */
+ TD_NOCAPAB, /* Capability not available. */
+ TD_DBERR, /* Internal debug library error. */
+ TD_NOAPLIC, /* Operation is not applicable. */
+ TD_NOTSD, /* No thread-specific data available. */
+ TD_MALLOC, /* Out of memory. */
+ TD_PARTIALREG, /* Not entire register set was read or written. */
+ TD_NOXREGS, /* X register set not available for given thread. */
+ TD_TLSDEFER, /* Thread has not yet allocated TLS for given module. */
+ TD_NOTALLOC = TD_TLSDEFER,
+ TD_VERSION, /* Version if libpthread and libthread_db do not match. */
+ TD_NOTLS /* There is no TLS segment in the given module. */
+} td_err_e;
+
+
+/* Possible thread states. TD_THR_ANY_STATE is a pseudo-state used to
+ select threads regardless of state in td_ta_thr_iter(). */
+typedef enum
+{
+ TD_THR_ANY_STATE,
+ TD_THR_UNKNOWN,
+ TD_THR_STOPPED,
+ TD_THR_RUN,
+ TD_THR_ACTIVE,
+ TD_THR_ZOMBIE,
+ TD_THR_SLEEP,
+ TD_THR_STOPPED_ASLEEP
+} td_thr_state_e;
+
+/* Thread type: user or system. TD_THR_ANY_TYPE is a pseudo-type used
+ to select threads regardless of type in td_ta_thr_iter(). */
+typedef enum
+{
+ TD_THR_ANY_TYPE,
+ TD_THR_USER,
+ TD_THR_SYSTEM
+} td_thr_type_e;
+
+
+/* Types of the debugging library. */
+
+/* Handle for a process. This type is opaque. */
+typedef struct td_thragent td_thragent_t;
+
+/* The actual thread handle type. This is also opaque. */
+typedef struct td_thrhandle
+{
+ td_thragent_t *th_ta_p;
+ psaddr_t th_unique;
+} td_thrhandle_t;
+
+
+/* Forward declaration of a type defined by and for the dynamic linker. */
+struct link_map;
+
+
+/* Flags for `td_ta_thr_iter'. */
+#define TD_THR_ANY_USER_FLAGS 0xffffffff
+#define TD_THR_LOWEST_PRIORITY -20
+#define TD_SIGNO_MASK NULL
+
+
+#define TD_EVENTSIZE 2
+#define BT_UISHIFT 5 /* log base 2 of BT_NBIPUI, to extract word index */
+#define BT_NBIPUI (1 << BT_UISHIFT) /* n bits per uint */
+#define BT_UIMASK (BT_NBIPUI - 1) /* to extract bit index */
+
+/* Bitmask of enabled events. */
+typedef struct td_thr_events
+{
+ uint32_t event_bits[TD_EVENTSIZE];
+} td_thr_events_t;
+
+/* Event set manipulation macros. */
+#define __td_eventmask(n) \
+ (UINT32_C (1) << (((n) - 1) & BT_UIMASK))
+#define __td_eventword(n) \
+ ((UINT32_C ((n) - 1)) >> BT_UISHIFT)
+
+#define td_event_emptyset(setp) \
+ do { \
+ int __i; \
+ for (__i = TD_EVENTSIZE; __i > 0; --__i) \
+ (setp)->event_bits[__i - 1] = 0; \
+ } while (0)
+
+#define td_event_fillset(setp) \
+ do { \
+ int __i; \
+ for (__i = TD_EVENTSIZE; __i > 0; --__i) \
+ (setp)->event_bits[__i - 1] = UINT32_C (0xffffffff); \
+ } while (0)
+
+#define td_event_addset(setp, n) \
+ (((setp)->event_bits[__td_eventword (n)]) |= __td_eventmask (n))
+#define td_event_delset(setp, n) \
+ (((setp)->event_bits[__td_eventword (n)]) &= ~__td_eventmask (n))
+#define td_eventismember(setp, n) \
+ (__td_eventmask (n) & ((setp)->event_bits[__td_eventword (n)]))
+#if TD_EVENTSIZE == 2
+# define td_eventisempty(setp) \
+ (!((setp)->event_bits[0]) && !((setp)->event_bits[1]))
+#else
+# error "td_eventisempty must be changed to match TD_EVENTSIZE"
+#endif
+
+/* Events reportable by the thread implementation. */
+typedef enum
+{
+ TD_ALL_EVENTS, /* Pseudo-event number. */
+ TD_EVENT_NONE = TD_ALL_EVENTS, /* Depends on context. */
+ TD_READY, /* Is executable now. */
+ TD_SLEEP, /* Blocked in a synchronization obj. */
+ TD_SWITCHTO, /* Now assigned to a process. */
+ TD_SWITCHFROM, /* Not anymore assigned to a process. */
+ TD_LOCK_TRY, /* Trying to get an unavailable lock. */
+ TD_CATCHSIG, /* Signal posted to the thread. */
+ TD_IDLE, /* Process getting idle. */
+ TD_CREATE, /* New thread created. */
+ TD_DEATH, /* Thread terminated. */
+ TD_PREEMPT, /* Preempted. */
+ TD_PRI_INHERIT, /* Inherited elevated priority. */
+ TD_REAP, /* Reaped. */
+ TD_CONCURRENCY, /* Number of processes changing. */
+ TD_TIMEOUT, /* Conditional variable wait timed out. */
+ TD_MIN_EVENT_NUM = TD_READY,
+ TD_MAX_EVENT_NUM = TD_TIMEOUT,
+ TD_EVENTS_ENABLE = 31 /* Event reporting enabled. */
+} td_event_e;
+
+/* Values representing the different ways events are reported. */
+typedef enum
+{
+ NOTIFY_BPT, /* User must insert breakpoint at u.bptaddr. */
+ NOTIFY_AUTOBPT, /* Breakpoint at u.bptaddr is automatically
+ inserted. */
+ NOTIFY_SYSCALL /* System call u.syscallno will be invoked. */
+} td_notify_e;
+
+/* Description how event type is reported. */
+typedef struct td_notify
+{
+ td_notify_e type; /* Way the event is reported. */
+ union
+ {
+ psaddr_t bptaddr; /* Address of breakpoint. */
+ int syscallno; /* Number of system call used. */
+ } u;
+} td_notify_t;
+
+/* Structure used to report event. */
+typedef struct td_event_msg
+{
+ td_event_e event; /* Event type being reported. */
+ const td_thrhandle_t *th_p; /* Thread reporting the event. */
+ union
+ {
+# if 0
+ td_synchandle_t *sh; /* Handle of synchronization object. */
+#endif
+ uintptr_t data; /* Event specific data. */
+ } msg;
+} td_event_msg_t;
+
+/* Structure containing event data available in each thread structure. */
+typedef struct
+{
+ td_thr_events_t eventmask; /* Mask of enabled events. */
+ td_event_e eventnum; /* Number of last event. */
+ void *eventdata; /* Data associated with event. */
+} td_eventbuf_t;
+
+
+/* Gathered statistics about the process. */
+typedef struct td_ta_stats
+{
+ int nthreads; /* Total number of threads in use. */
+ int r_concurrency; /* Concurrency level requested by user. */
+ int nrunnable_num; /* Average runnable threads, numerator. */
+ int nrunnable_den; /* Average runnable threads, denominator. */
+ int a_concurrency_num; /* Achieved concurrency level, numerator. */
+ int a_concurrency_den; /* Achieved concurrency level, denominator. */
+ int nlwps_num; /* Average number of processes in use,
+ numerator. */
+ int nlwps_den; /* Average number of processes in use,
+ denominator. */
+ int nidle_num; /* Average number of idling processes,
+ numerator. */
+ int nidle_den; /* Average number of idling processes,
+ denominator. */
+} td_ta_stats_t;
+
+
+/* Since Sun's library is based on Solaris threads we have to define a few
+ types to map them to POSIX threads. */
+typedef pthread_t thread_t;
+typedef pthread_key_t thread_key_t;
+
+
+/* Callback for iteration over threads. */
+typedef int td_thr_iter_f (const td_thrhandle_t *, void *);
+
+/* Callback for iteration over thread local data. */
+typedef int td_key_iter_f (thread_key_t, void (*) (void *), void *);
+
+
+
+/* Forward declaration. This has to be defined by the user. */
+struct ps_prochandle;
+
+
+/* Information about the thread. */
+typedef struct td_thrinfo
+{
+ td_thragent_t *ti_ta_p; /* Process handle. */
+ unsigned int ti_user_flags; /* Unused. */
+ thread_t ti_tid; /* Thread ID returned by
+ pthread_create(). */
+ char *ti_tls; /* Pointer to thread-local data. */
+ psaddr_t ti_startfunc; /* Start function passed to
+ pthread_create(). */
+ psaddr_t ti_stkbase; /* Base of thread's stack. */
+ long int ti_stksize; /* Size of thread's stack. */
+ psaddr_t ti_ro_area; /* Unused. */
+ int ti_ro_size; /* Unused. */
+ td_thr_state_e ti_state; /* Thread state. */
+ unsigned char ti_db_suspended; /* Nonzero if suspended by debugger. */
+ td_thr_type_e ti_type; /* Type of the thread (system vs
+ user thread). */
+ intptr_t ti_pc; /* Unused. */
+ intptr_t ti_sp; /* Unused. */
+ short int ti_flags; /* Unused. */
+ int ti_pri; /* Thread priority. */
+ lwpid_t ti_lid; /* Kernel PID for this thread. */
+ sigset_t ti_sigmask; /* Signal mask. */
+ unsigned char ti_traceme; /* Nonzero if event reporting
+ enabled. */
+ unsigned char ti_preemptflag; /* Unused. */
+ unsigned char ti_pirecflag; /* Unused. */
+ sigset_t ti_pending; /* Set of pending signals. */
+ td_thr_events_t ti_events; /* Set of enabled events. */
+} td_thrinfo_t;
+
+
+
+/* Prototypes for exported library functions. */
+
+/* Initialize the thread debug support library. */
+extern td_err_e td_init (void);
+
+/* Historical relict. Should not be used anymore. */
+extern td_err_e td_log (void);
+
+/* Return list of symbols the library can request. */
+extern const char **td_symbol_list (void);
+
+/* Generate new thread debug library handle for process PS. */
+extern td_err_e td_ta_new (struct ps_prochandle *__ps, td_thragent_t **__ta);
+
+/* Free resources allocated for TA. */
+extern td_err_e td_ta_delete (td_thragent_t *__ta);
+
+/* Get number of currently running threads in process associated with TA. */
+extern td_err_e td_ta_get_nthreads (const td_thragent_t *__ta, int *__np);
+
+/* Return process handle passed in `td_ta_new' for process associated with
+ TA. */
+extern td_err_e td_ta_get_ph (const td_thragent_t *__ta,
+ struct ps_prochandle **__ph);
+
+/* Map thread library handle PT to thread debug library handle for process
+ associated with TA and store result in *TH. */
+extern td_err_e td_ta_map_id2thr (const td_thragent_t *__ta, pthread_t __pt,
+ td_thrhandle_t *__th);
+
+/* Map process ID LWPID to thread debug library handle for process
+ associated with TA and store result in *TH. */
+extern td_err_e td_ta_map_lwp2thr (const td_thragent_t *__ta, lwpid_t __lwpid,
+ td_thrhandle_t *__th);
+
+
+/* Call for each thread in a process associated with TA the callback function
+ CALLBACK. */
+extern td_err_e td_ta_thr_iter (const td_thragent_t *__ta,
+ td_thr_iter_f *__callback, void *__cbdata_p,
+ td_thr_state_e __state, int __ti_pri,
+ sigset_t *__ti_sigmask_p,
+ unsigned int __ti_user_flags);
+
+/* Call for each defined thread local data entry the callback function KI. */
+extern td_err_e td_ta_tsd_iter (const td_thragent_t *__ta, td_key_iter_f *__ki,
+ void *__p);
+
+
+/* Get event address for EVENT. */
+extern td_err_e td_ta_event_addr (const td_thragent_t *__ta,
+ td_event_e __event, td_notify_t *__ptr);
+
+/* Enable EVENT in global mask. */
+extern td_err_e td_ta_set_event (const td_thragent_t *__ta,
+ td_thr_events_t *__event);
+
+/* Disable EVENT in global mask. */
+extern td_err_e td_ta_clear_event (const td_thragent_t *__ta,
+ td_thr_events_t *__event);
+
+/* Return information about last event. */
+extern td_err_e td_ta_event_getmsg (const td_thragent_t *__ta,
+ td_event_msg_t *__msg);
+
+
+/* Set suggested concurrency level for process associated with TA. */
+extern td_err_e td_ta_setconcurrency (const td_thragent_t *__ta, int __level);
+
+
+/* Enable collecting statistics for process associated with TA. */
+extern td_err_e td_ta_enable_stats (const td_thragent_t *__ta, int __enable);
+
+/* Reset statistics. */
+extern td_err_e td_ta_reset_stats (const td_thragent_t *__ta);
+
+/* Retrieve statistics from process associated with TA. */
+extern td_err_e td_ta_get_stats (const td_thragent_t *__ta,
+ td_ta_stats_t *__statsp);
+
+
+/* Validate that TH is a thread handle. */
+extern td_err_e td_thr_validate (const td_thrhandle_t *__th);
+
+/* Return information about thread TH. */
+extern td_err_e td_thr_get_info (const td_thrhandle_t *__th,
+ td_thrinfo_t *__infop);
+
+/* Retrieve floating-point register contents of process running thread TH. */
+extern td_err_e td_thr_getfpregs (const td_thrhandle_t *__th,
+ prfpregset_t *__regset);
+
+/* Retrieve general register contents of process running thread TH. */
+extern td_err_e td_thr_getgregs (const td_thrhandle_t *__th,
+ prgregset_t __gregs);
+
+/* Retrieve extended register contents of process running thread TH. */
+extern td_err_e td_thr_getxregs (const td_thrhandle_t *__th, void *__xregs);
+
+/* Get size of extended register set of process running thread TH. */
+extern td_err_e td_thr_getxregsize (const td_thrhandle_t *__th, int *__sizep);
+
+/* Set floating-point register contents of process running thread TH. */
+extern td_err_e td_thr_setfpregs (const td_thrhandle_t *__th,
+ const prfpregset_t *__fpregs);
+
+/* Set general register contents of process running thread TH. */
+extern td_err_e td_thr_setgregs (const td_thrhandle_t *__th,
+ prgregset_t __gregs);
+
+/* Set extended register contents of process running thread TH. */
+extern td_err_e td_thr_setxregs (const td_thrhandle_t *__th,
+ const void *__addr);
+
+
+/* Get address of the given module's TLS storage area for the given thread. */
+extern td_err_e td_thr_tlsbase (const td_thrhandle_t *__th,
+ unsigned long int __modid,
+ psaddr_t *__base);
+
+/* Get address of thread local variable. */
+extern td_err_e td_thr_tls_get_addr (const td_thrhandle_t *__th,
+ psaddr_t __map_address, size_t __offset,
+ psaddr_t *__address);
+
+
+/* Enable reporting for EVENT for thread TH. */
+extern td_err_e td_thr_event_enable (const td_thrhandle_t *__th, int __event);
+
+/* Enable EVENT for thread TH. */
+extern td_err_e td_thr_set_event (const td_thrhandle_t *__th,
+ td_thr_events_t *__event);
+
+/* Disable EVENT for thread TH. */
+extern td_err_e td_thr_clear_event (const td_thrhandle_t *__th,
+ td_thr_events_t *__event);
+
+/* Get event message for thread TH. */
+extern td_err_e td_thr_event_getmsg (const td_thrhandle_t *__th,
+ td_event_msg_t *__msg);
+
+
+/* Set priority of thread TH. */
+extern td_err_e td_thr_setprio (const td_thrhandle_t *__th, int __prio);
+
+
+/* Set pending signals for thread TH. */
+extern td_err_e td_thr_setsigpending (const td_thrhandle_t *__th,
+ unsigned char __n, const sigset_t *__ss);
+
+/* Set signal mask for thread TH. */
+extern td_err_e td_thr_sigsetmask (const td_thrhandle_t *__th,
+ const sigset_t *__ss);
+
+
+/* Return thread local data associated with key TK in thread TH. */
+extern td_err_e td_thr_tsd (const td_thrhandle_t *__th,
+ const thread_key_t __tk, void **__data);
+
+
+/* Suspend execution of thread TH. */
+extern td_err_e td_thr_dbsuspend (const td_thrhandle_t *__th);
+
+/* Resume execution of thread TH. */
+extern td_err_e td_thr_dbresume (const td_thrhandle_t *__th);
+
+#endif /* thread_db.h */
diff --git a/gdb/nat/i386-cpuid.h b/gdb/nat/i386-cpuid.h
new file mode 100644
index 0000000..9aea054
--- /dev/null
+++ b/gdb/nat/i386-cpuid.h
@@ -0,0 +1,63 @@
+/* C API for x86 cpuid insn.
+ Copyright (C) 2007-2014 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This file 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, 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 I386_CPUID_COMMON_H
+#define I386_CPUID_COMMON_H
+
+/* Always include the header for the cpu bit defines. */
+#include "i386-gcc-cpuid.h"
+
+#if defined(__i386__) || defined(__x86_64__)
+
+/* Return cpuid data for requested cpuid level, as found in returned
+ eax, ebx, ecx and edx registers. The function checks if cpuid is
+ supported and returns 1 for valid cpuid information or 0 for
+ unsupported cpuid level. Pointers may be non-null. */
+
+static __inline int
+i386_cpuid (unsigned int __level,
+ unsigned int *__eax, unsigned int *__ebx,
+ unsigned int *__ecx, unsigned int *__edx)
+{
+ unsigned int __scratch;
+
+ if (!__eax)
+ __eax = &__scratch;
+ if (!__ebx)
+ __ebx = &__scratch;
+ if (!__ecx)
+ __ecx = &__scratch;
+ if (!__edx)
+ __edx = &__scratch;
+
+ return __get_cpuid (__level, __eax, __ebx, __ecx, __edx);
+}
+
+#else
+
+static __inline int
+i386_cpuid (unsigned int __level,
+ unsigned int *__eax, unsigned int *__ebx,
+ unsigned int *__ecx, unsigned int *__edx)
+{
+ return 0;
+}
+
+#endif /* i386 && x86_64 */
+
+#endif /* I386_CPUID_COMMON_H */
diff --git a/gdb/nat/i386-dregs.c b/gdb/nat/i386-dregs.c
index e7d1620..214616c 100644
--- a/gdb/nat/i386-dregs.c
+++ b/gdb/nat/i386-dregs.c
@@ -23,7 +23,7 @@
#include "defs.h"
#include "inferior.h"
#endif
-#include "nat/i386-dregs.h"
+#include "i386-dregs.h"
/* Support for hardware watchpoints and breakpoints using the i386
debug registers.
diff --git a/gdb/nat/i386-gcc-cpuid.h b/gdb/nat/i386-gcc-cpuid.h
new file mode 100644
index 0000000..34ab197
--- /dev/null
+++ b/gdb/nat/i386-gcc-cpuid.h
@@ -0,0 +1,278 @@
+/*
+ * Helper cpuid.h file copied from gcc-4.8.0. Code in gdb should not
+ * include this directly, but pull in i386-cpuid.h and use that func.
+ */
+/*
+ * Copyright (C) 2007-2014 Free Software Foundation, Inc.
+ *
+ * This file 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, or (at your option) any
+ * later version.
+ *
+ * This file 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.
+ *
+ * Under Section 7 of GPL version 3, you are granted additional
+ * permissions described in the GCC Runtime Library Exception, version
+ * 3.1, as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License and
+ * a copy of the GCC Runtime Library Exception along with this program;
+ * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+/* %ecx */
+#define bit_SSE3 (1 << 0)
+#define bit_PCLMUL (1 << 1)
+#define bit_LZCNT (1 << 5)
+#define bit_SSSE3 (1 << 9)
+#define bit_FMA (1 << 12)
+#define bit_CMPXCHG16B (1 << 13)
+#define bit_SSE4_1 (1 << 19)
+#define bit_SSE4_2 (1 << 20)
+#define bit_MOVBE (1 << 22)
+#define bit_POPCNT (1 << 23)
+#define bit_AES (1 << 25)
+#define bit_XSAVE (1 << 26)
+#define bit_OSXSAVE (1 << 27)
+#define bit_AVX (1 << 28)
+#define bit_F16C (1 << 29)
+#define bit_RDRND (1 << 30)
+
+/* %edx */
+#define bit_CMPXCHG8B (1 << 8)
+#define bit_CMOV (1 << 15)
+#define bit_MMX (1 << 23)
+#define bit_FXSAVE (1 << 24)
+#define bit_SSE (1 << 25)
+#define bit_SSE2 (1 << 26)
+
+/* Extended Features */
+/* %ecx */
+#define bit_LAHF_LM (1 << 0)
+#define bit_ABM (1 << 5)
+#define bit_SSE4a (1 << 6)
+#define bit_PRFCHW (1 << 8)
+#define bit_XOP (1 << 11)
+#define bit_LWP (1 << 15)
+#define bit_FMA4 (1 << 16)
+#define bit_TBM (1 << 21)
+
+/* %edx */
+#define bit_MMXEXT (1 << 22)
+#define bit_LM (1 << 29)
+#define bit_3DNOWP (1 << 30)
+#define bit_3DNOW (1 << 31)
+
+/* Extended Features (%eax == 7) */
+#define bit_FSGSBASE (1 << 0)
+#define bit_BMI (1 << 3)
+#define bit_HLE (1 << 4)
+#define bit_AVX2 (1 << 5)
+#define bit_BMI2 (1 << 8)
+#define bit_RTM (1 << 11)
+#define bit_AVX512F (1 << 16)
+#define bit_MPX (1 << 14)
+#define bit_RDSEED (1 << 18)
+#define bit_ADX (1 << 19)
+#define bit_AVX512PF (1 << 26)
+#define bit_AVX512ER (1 << 27)
+#define bit_AVX512CD (1 << 28)
+#define bit_SHA (1 << 29)
+
+/* Extended State Enumeration Sub-leaf (%eax == 13, %ecx == 1) */
+#define bit_XSAVEOPT (1 << 0)
+
+/* Signatures for different CPU implementations as returned in uses
+ of cpuid with level 0. */
+#define signature_AMD_ebx 0x68747541
+#define signature_AMD_ecx 0x444d4163
+#define signature_AMD_edx 0x69746e65
+
+#define signature_CENTAUR_ebx 0x746e6543
+#define signature_CENTAUR_ecx 0x736c7561
+#define signature_CENTAUR_edx 0x48727561
+
+#define signature_CYRIX_ebx 0x69727943
+#define signature_CYRIX_ecx 0x64616574
+#define signature_CYRIX_edx 0x736e4978
+
+#define signature_INTEL_ebx 0x756e6547
+#define signature_INTEL_ecx 0x6c65746e
+#define signature_INTEL_edx 0x49656e69
+
+#define signature_TM1_ebx 0x6e617254
+#define signature_TM1_ecx 0x55504361
+#define signature_TM1_edx 0x74656d73
+
+#define signature_TM2_ebx 0x756e6547
+#define signature_TM2_ecx 0x3638784d
+#define signature_TM2_edx 0x54656e69
+
+#define signature_NSC_ebx 0x646f6547
+#define signature_NSC_ecx 0x43534e20
+#define signature_NSC_edx 0x79622065
+
+#define signature_NEXGEN_ebx 0x4778654e
+#define signature_NEXGEN_ecx 0x6e657669
+#define signature_NEXGEN_edx 0x72446e65
+
+#define signature_RISE_ebx 0x65736952
+#define signature_RISE_ecx 0x65736952
+#define signature_RISE_edx 0x65736952
+
+#define signature_SIS_ebx 0x20536953
+#define signature_SIS_ecx 0x20536953
+#define signature_SIS_edx 0x20536953
+
+#define signature_UMC_ebx 0x20434d55
+#define signature_UMC_ecx 0x20434d55
+#define signature_UMC_edx 0x20434d55
+
+#define signature_VIA_ebx 0x20414956
+#define signature_VIA_ecx 0x20414956
+#define signature_VIA_edx 0x20414956
+
+#define signature_VORTEX_ebx 0x74726f56
+#define signature_VORTEX_ecx 0x436f5320
+#define signature_VORTEX_edx 0x36387865
+
+#if defined(__i386__) && defined(__PIC__)
+/* %ebx may be the PIC register. */
+#if __GNUC__ >= 3
+#define __cpuid(level, a, b, c, d) \
+ __asm__ ("xchg{l}\t{%%}ebx, %k1\n\t" \
+ "cpuid\n\t" \
+ "xchg{l}\t{%%}ebx, %k1\n\t" \
+ : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
+ : "0" (level))
+
+#define __cpuid_count(level, count, a, b, c, d) \
+ __asm__ ("xchg{l}\t{%%}ebx, %k1\n\t" \
+ "cpuid\n\t" \
+ "xchg{l}\t{%%}ebx, %k1\n\t" \
+ : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
+ : "0" (level), "2" (count))
+#else
+/* Host GCCs older than 3.0 weren't supporting Intel asm syntax
+ nor alternatives in i386 code. */
+#define __cpuid(level, a, b, c, d) \
+ __asm__ ("xchgl\t%%ebx, %k1\n\t" \
+ "cpuid\n\t" \
+ "xchgl\t%%ebx, %k1\n\t" \
+ : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
+ : "0" (level))
+
+#define __cpuid_count(level, count, a, b, c, d) \
+ __asm__ ("xchgl\t%%ebx, %k1\n\t" \
+ "cpuid\n\t" \
+ "xchgl\t%%ebx, %k1\n\t" \
+ : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
+ : "0" (level), "2" (count))
+#endif
+#elif defined(__x86_64__) && (defined(__code_model_medium__) || defined(__code_model_large__)) && defined(__PIC__)
+/* %rbx may be the PIC register. */
+#define __cpuid(level, a, b, c, d) \
+ __asm__ ("xchg{q}\t{%%}rbx, %q1\n\t" \
+ "cpuid\n\t" \
+ "xchg{q}\t{%%}rbx, %q1\n\t" \
+ : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
+ : "0" (level))
+
+#define __cpuid_count(level, count, a, b, c, d) \
+ __asm__ ("xchg{q}\t{%%}rbx, %q1\n\t" \
+ "cpuid\n\t" \
+ "xchg{q}\t{%%}rbx, %q1\n\t" \
+ : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
+ : "0" (level), "2" (count))
+#else
+#define __cpuid(level, a, b, c, d) \
+ __asm__ ("cpuid\n\t" \
+ : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
+ : "0" (level))
+
+#define __cpuid_count(level, count, a, b, c, d) \
+ __asm__ ("cpuid\n\t" \
+ : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
+ : "0" (level), "2" (count))
+#endif
+
+/* Return highest supported input value for cpuid instruction. ext can
+ be either 0x0 or 0x8000000 to return highest supported value for
+ basic or extended cpuid information. Function returns 0 if cpuid
+ is not supported or whatever cpuid returns in eax register. If sig
+ pointer is non-null, then first four bytes of the signature
+ (as found in ebx register) are returned in location pointed by sig. */
+
+static __inline unsigned int
+__get_cpuid_max (unsigned int __ext, unsigned int *__sig)
+{
+ unsigned int __eax, __ebx, __ecx, __edx;
+
+#ifndef __x86_64__
+ /* See if we can use cpuid. On AMD64 we always can. */
+#if __GNUC__ >= 3
+ __asm__ ("pushf{l|d}\n\t"
+ "pushf{l|d}\n\t"
+ "pop{l}\t%0\n\t"
+ "mov{l}\t{%0, %1|%1, %0}\n\t"
+ "xor{l}\t{%2, %0|%0, %2}\n\t"
+ "push{l}\t%0\n\t"
+ "popf{l|d}\n\t"
+ "pushf{l|d}\n\t"
+ "pop{l}\t%0\n\t"
+ "popf{l|d}\n\t"
+ : "=&r" (__eax), "=&r" (__ebx)
+ : "i" (0x00200000));
+#else
+/* Host GCCs older than 3.0 weren't supporting Intel asm syntax
+ nor alternatives in i386 code. */
+ __asm__ ("pushfl\n\t"
+ "pushfl\n\t"
+ "popl\t%0\n\t"
+ "movl\t%0, %1\n\t"
+ "xorl\t%2, %0\n\t"
+ "pushl\t%0\n\t"
+ "popfl\n\t"
+ "pushfl\n\t"
+ "popl\t%0\n\t"
+ "popfl\n\t"
+ : "=&r" (__eax), "=&r" (__ebx)
+ : "i" (0x00200000));
+#endif
+
+ if (!((__eax ^ __ebx) & 0x00200000))
+ return 0;
+#endif
+
+ /* Host supports cpuid. Return highest supported cpuid input value. */
+ __cpuid (__ext, __eax, __ebx, __ecx, __edx);
+
+ if (__sig)
+ *__sig = __ebx;
+
+ return __eax;
+}
+
+/* Return cpuid data for requested cpuid level, as found in returned
+ eax, ebx, ecx and edx registers. The function checks if cpuid is
+ supported and returns 1 for valid cpuid information or 0 for
+ unsupported cpuid level. All pointers are required to be non-null. */
+
+static __inline int
+__get_cpuid (unsigned int __level,
+ unsigned int *__eax, unsigned int *__ebx,
+ unsigned int *__ecx, unsigned int *__edx)
+{
+ unsigned int __ext = __level & 0x80000000;
+
+ if (__get_cpuid_max (__ext, 0) < __level)
+ return 0;
+
+ __cpuid (__level, *__eax, *__ebx, *__ecx, *__edx);
+ return 1;
+}
diff --git a/gdb/nat/linux-btrace.c b/gdb/nat/linux-btrace.c
new file mode 100644
index 0000000..188220b
--- /dev/null
+++ b/gdb/nat/linux-btrace.c
@@ -0,0 +1,634 @@
+/* Linux-dependent part of branch trace support for GDB, and GDBserver.
+
+ Copyright (C) 2013-2014 Free Software Foundation, Inc.
+
+ Contributed by Intel Corp. <markus.t.metzger@intel.com>
+
+ This file is part of GDB.
+
+ 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/>. */
+
+#ifdef GDBSERVER
+#include "server.h"
+#else
+#include "defs.h"
+#endif
+
+#include "linux-btrace.h"
+#include "common-utils.h"
+#include "gdb_assert.h"
+#include "regcache.h"
+#include "gdbthread.h"
+#include "gdb_wait.h"
+#include "i386-cpuid.h"
+
+#ifdef HAVE_SYS_SYSCALL_H
+#include <sys/syscall.h>
+#endif
+
+#if HAVE_LINUX_PERF_EVENT_H && defined(SYS_perf_event_open)
+
+#include <errno.h>
+#include <string.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/user.h>
+#include <sys/ptrace.h>
+#include <sys/types.h>
+#include <signal.h>
+
+/* A branch trace record in perf_event. */
+struct perf_event_bts
+{
+ /* The linear address of the branch source. */
+ uint64_t from;
+
+ /* The linear address of the branch destination. */
+ uint64_t to;
+};
+
+/* A perf_event branch trace sample. */
+struct perf_event_sample
+{
+ /* The perf_event sample header. */
+ struct perf_event_header header;
+
+ /* The perf_event branch tracing payload. */
+ struct perf_event_bts bts;
+};
+
+/* Get the perf_event header. */
+
+static inline volatile struct perf_event_mmap_page *
+perf_event_header (struct btrace_target_info* tinfo)
+{
+ return tinfo->buffer;
+}
+
+/* Get the size of the perf_event mmap buffer. */
+
+static inline size_t
+perf_event_mmap_size (const struct btrace_target_info *tinfo)
+{
+ /* The branch trace buffer is preceded by a configuration page. */
+ return (tinfo->size + 1) * PAGE_SIZE;
+}
+
+/* Get the size of the perf_event buffer. */
+
+static inline size_t
+perf_event_buffer_size (struct btrace_target_info* tinfo)
+{
+ return tinfo->size * PAGE_SIZE;
+}
+
+/* Get the start address of the perf_event buffer. */
+
+static inline const uint8_t *
+perf_event_buffer_begin (struct btrace_target_info* tinfo)
+{
+ return ((const uint8_t *) tinfo->buffer) + PAGE_SIZE;
+}
+
+/* Get the end address of the perf_event buffer. */
+
+static inline const uint8_t *
+perf_event_buffer_end (struct btrace_target_info* tinfo)
+{
+ return perf_event_buffer_begin (tinfo) + perf_event_buffer_size (tinfo);
+}
+
+/* Check whether an address is in the kernel. */
+
+static inline int
+perf_event_is_kernel_addr (const struct btrace_target_info *tinfo,
+ uint64_t addr)
+{
+ uint64_t mask;
+
+ /* If we don't know the size of a pointer, we can't check. Let's assume it's
+ not a kernel address in this case. */
+ if (tinfo->ptr_bits == 0)
+ return 0;
+
+ /* A bit mask for the most significant bit in an address. */
+ mask = (uint64_t) 1 << (tinfo->ptr_bits - 1);
+
+ /* Check whether the most significant bit in the address is set. */
+ return (addr & mask) != 0;
+}
+
+/* Check whether a perf event record should be skipped. */
+
+static inline int
+perf_event_skip_record (const struct btrace_target_info *tinfo,
+ const struct perf_event_bts *bts)
+{
+ /* The hardware may report branches from kernel into user space. Branches
+ from user into kernel space will be suppressed. We filter the former to
+ provide a consistent branch trace excluding kernel. */
+ return perf_event_is_kernel_addr (tinfo, bts->from);
+}
+
+/* Perform a few consistency checks on a perf event sample record. This is
+ meant to catch cases when we get out of sync with the perf event stream. */
+
+static inline int
+perf_event_sample_ok (const struct perf_event_sample *sample)
+{
+ if (sample->header.type != PERF_RECORD_SAMPLE)
+ return 0;
+
+ if (sample->header.size != sizeof (*sample))
+ return 0;
+
+ return 1;
+}
+
+/* Branch trace is collected in a circular buffer [begin; end) as pairs of from
+ and to addresses (plus a header).
+
+ Start points into that buffer at the next sample position.
+ We read the collected samples backwards from start.
+
+ While reading the samples, we convert the information into a list of blocks.
+ For two adjacent samples s1 and s2, we form a block b such that b.begin =
+ s1.to and b.end = s2.from.
+
+ In case the buffer overflows during sampling, one sample may have its lower
+ part at the end and its upper part at the beginning of the buffer. */
+
+static VEC (btrace_block_s) *
+perf_event_read_bts (struct btrace_target_info* tinfo, const uint8_t *begin,
+ const uint8_t *end, const uint8_t *start, size_t size)
+{
+ VEC (btrace_block_s) *btrace = NULL;
+ struct perf_event_sample sample;
+ size_t read = 0;
+ struct btrace_block block = { 0, 0 };
+ struct regcache *regcache;
+
+ gdb_assert (begin <= start);
+ gdb_assert (start <= end);
+
+ /* The first block ends at the current pc. */
+#ifdef GDBSERVER
+ regcache = get_thread_regcache (find_thread_ptid (tinfo->ptid), 1);
+#else
+ regcache = get_thread_regcache (tinfo->ptid);
+#endif
+ block.end = regcache_read_pc (regcache);
+
+ /* The buffer may contain a partial record as its last entry (i.e. when the
+ buffer size is not a multiple of the sample size). */
+ read = sizeof (sample) - 1;
+
+ for (; read < size; read += sizeof (sample))
+ {
+ const struct perf_event_sample *psample;
+
+ /* Find the next perf_event sample in a backwards traversal. */
+ start -= sizeof (sample);
+
+ /* If we're still inside the buffer, we're done. */
+ if (begin <= start)
+ psample = (const struct perf_event_sample *) start;
+ else
+ {
+ int missing;
+
+ /* We're to the left of the ring buffer, we will wrap around and
+ reappear at the very right of the ring buffer. */
+
+ missing = (begin - start);
+ start = (end - missing);
+
+ /* If the entire sample is missing, we're done. */
+ if (missing == sizeof (sample))
+ psample = (const struct perf_event_sample *) start;
+ else
+ {
+ uint8_t *stack;
+
+ /* The sample wrapped around. The lower part is at the end and
+ the upper part is at the beginning of the buffer. */
+ stack = (uint8_t *) &sample;
+
+ /* Copy the two parts so we have a contiguous sample. */
+ memcpy (stack, start, missing);
+ memcpy (stack + missing, begin, sizeof (sample) - missing);
+
+ psample = &sample;
+ }
+ }
+
+ if (!perf_event_sample_ok (psample))
+ {
+ warning (_("Branch trace may be incomplete."));
+ break;
+ }
+
+ if (perf_event_skip_record (tinfo, &psample->bts))
+ continue;
+
+ /* We found a valid sample, so we can complete the current block. */
+ block.begin = psample->bts.to;
+
+ VEC_safe_push (btrace_block_s, btrace, &block);
+
+ /* Start the next block. */
+ block.end = psample->bts.from;
+ }
+
+ /* Push the last block (i.e. the first one of inferior execution), as well.
+ We don't know where it ends, but we know where it starts. If we're
+ reading delta trace, we can fill in the start address later on.
+ Otherwise we will prune it. */
+ block.begin = 0;
+ VEC_safe_push (btrace_block_s, btrace, &block);
+
+ return btrace;
+}
+
+/* Check whether the kernel supports branch tracing. */
+
+static int
+kernel_supports_btrace (void)
+{
+ struct perf_event_attr attr;
+ pid_t child, pid;
+ int status, file;
+
+ errno = 0;
+ child = fork ();
+ switch (child)
+ {
+ case -1:
+ warning (_("test branch tracing: cannot fork: %s."), strerror (errno));
+ return 0;
+
+ case 0:
+ status = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
+ if (status != 0)
+ {
+ warning (_("test branch tracing: cannot PTRACE_TRACEME: %s."),
+ strerror (errno));
+ _exit (1);
+ }
+
+ status = raise (SIGTRAP);
+ if (status != 0)
+ {
+ warning (_("test branch tracing: cannot raise SIGTRAP: %s."),
+ strerror (errno));
+ _exit (1);
+ }
+
+ _exit (1);
+
+ default:
+ pid = waitpid (child, &status, 0);
+ if (pid != child)
+ {
+ warning (_("test branch tracing: bad pid %ld, error: %s."),
+ (long) pid, strerror (errno));
+ return 0;
+ }
+
+ if (!WIFSTOPPED (status))
+ {
+ warning (_("test branch tracing: expected stop. status: %d."),
+ status);
+ return 0;
+ }
+
+ memset (&attr, 0, sizeof (attr));
+
+ attr.type = PERF_TYPE_HARDWARE;
+ attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
+ attr.sample_period = 1;
+ attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
+ attr.exclude_kernel = 1;
+ attr.exclude_hv = 1;
+ attr.exclude_idle = 1;
+
+ file = syscall (SYS_perf_event_open, &attr, child, -1, -1, 0);
+ if (file >= 0)
+ close (file);
+
+ kill (child, SIGKILL);
+ ptrace (PTRACE_KILL, child, NULL, NULL);
+
+ pid = waitpid (child, &status, 0);
+ if (pid != child)
+ {
+ warning (_("test branch tracing: bad pid %ld, error: %s."),
+ (long) pid, strerror (errno));
+ if (!WIFSIGNALED (status))
+ warning (_("test branch tracing: expected killed. status: %d."),
+ status);
+ }
+
+ return (file >= 0);
+ }
+}
+
+/* Check whether an Intel cpu supports branch tracing. */
+
+static int
+intel_supports_btrace (void)
+{
+ unsigned int cpuid, model, family;
+
+ if (!i386_cpuid (1, &cpuid, NULL, NULL, NULL))
+ return 0;
+
+ family = (cpuid >> 8) & 0xf;
+ model = (cpuid >> 4) & 0xf;
+
+ switch (family)
+ {
+ case 0x6:
+ model += (cpuid >> 12) & 0xf0;
+
+ switch (model)
+ {
+ case 0x1a: /* Nehalem */
+ case 0x1f:
+ case 0x1e:
+ case 0x2e:
+ case 0x25: /* Westmere */
+ case 0x2c:
+ case 0x2f:
+ case 0x2a: /* Sandy Bridge */
+ case 0x2d:
+ case 0x3a: /* Ivy Bridge */
+
+ /* AAJ122: LBR, BTM, or BTS records may have incorrect branch
+ "from" information afer an EIST transition, T-states, C1E, or
+ Adaptive Thermal Throttling. */
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+/* Check whether the cpu supports branch tracing. */
+
+static int
+cpu_supports_btrace (void)
+{
+ unsigned int ebx, ecx, edx;
+
+ if (!i386_cpuid (0, NULL, &ebx, &ecx, &edx))
+ return 0;
+
+ if (ebx == signature_INTEL_ebx && ecx == signature_INTEL_ecx
+ && edx == signature_INTEL_edx)
+ return intel_supports_btrace ();
+
+ /* Don't know about others. Let's assume they do. */
+ return 1;
+}
+
+/* See linux-btrace.h. */
+
+int
+linux_supports_btrace (struct target_ops *ops)
+{
+ static int cached;
+
+ if (cached == 0)
+ {
+ if (!kernel_supports_btrace ())
+ cached = -1;
+ else if (!cpu_supports_btrace ())
+ cached = -1;
+ else
+ cached = 1;
+ }
+
+ return cached > 0;
+}
+
+/* See linux-btrace.h. */
+
+struct btrace_target_info *
+linux_enable_btrace (ptid_t ptid)
+{
+ struct btrace_target_info *tinfo;
+ int pid, pg;
+
+ tinfo = xzalloc (sizeof (*tinfo));
+ tinfo->ptid = ptid;
+
+ tinfo->attr.size = sizeof (tinfo->attr);
+ tinfo->attr.type = PERF_TYPE_HARDWARE;
+ tinfo->attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
+ tinfo->attr.sample_period = 1;
+
+ /* We sample from and to address. */
+ tinfo->attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
+
+ tinfo->attr.exclude_kernel = 1;
+ tinfo->attr.exclude_hv = 1;
+ tinfo->attr.exclude_idle = 1;
+
+ tinfo->ptr_bits = 0;
+
+ pid = ptid_get_lwp (ptid);
+ if (pid == 0)
+ pid = ptid_get_pid (ptid);
+
+ errno = 0;
+ tinfo->file = syscall (SYS_perf_event_open, &tinfo->attr, pid, -1, -1, 0);
+ if (tinfo->file < 0)
+ goto err;
+
+ /* We try to allocate as much buffer as we can get.
+ We could allow the user to specify the size of the buffer, but then
+ we'd leave this search for the maximum buffer size to him. */
+ for (pg = 4; pg >= 0; --pg)
+ {
+ /* The number of pages we request needs to be a power of two. */
+ tinfo->size = 1 << pg;
+ tinfo->buffer = mmap (NULL, perf_event_mmap_size (tinfo),
+ PROT_READ, MAP_SHARED, tinfo->file, 0);
+ if (tinfo->buffer == MAP_FAILED)
+ continue;
+
+ return tinfo;
+ }
+
+ /* We were not able to allocate any buffer. */
+ close (tinfo->file);
+
+ err:
+ xfree (tinfo);
+ return NULL;
+}
+
+/* See linux-btrace.h. */
+
+enum btrace_error
+linux_disable_btrace (struct btrace_target_info *tinfo)
+{
+ int errcode;
+
+ errno = 0;
+ errcode = munmap (tinfo->buffer, perf_event_mmap_size (tinfo));
+ if (errcode != 0)
+ return BTRACE_ERR_UNKNOWN;
+
+ close (tinfo->file);
+ xfree (tinfo);
+
+ return BTRACE_ERR_NONE;
+}
+
+/* Check whether the branch trace has changed. */
+
+static int
+linux_btrace_has_changed (struct btrace_target_info *tinfo)
+{
+ volatile struct perf_event_mmap_page *header = perf_event_header (tinfo);
+
+ return header->data_head != tinfo->data_head;
+}
+
+/* See linux-btrace.h. */
+
+enum btrace_error
+linux_read_btrace (VEC (btrace_block_s) **btrace,
+ struct btrace_target_info *tinfo,
+ enum btrace_read_type type)
+{
+ volatile struct perf_event_mmap_page *header;
+ const uint8_t *begin, *end, *start;
+ unsigned long data_head, data_tail, retries = 5;
+ size_t buffer_size, size;
+
+ /* For delta reads, we return at least the partial last block containing
+ the current PC. */
+ if (type == BTRACE_READ_NEW && !linux_btrace_has_changed (tinfo))
+ return BTRACE_ERR_NONE;
+
+ header = perf_event_header (tinfo);
+ buffer_size = perf_event_buffer_size (tinfo);
+ data_tail = tinfo->data_head;
+
+ /* We may need to retry reading the trace. See below. */
+ while (retries--)
+ {
+ data_head = header->data_head;
+
+ /* Delete any leftover trace from the previous iteration. */
+ VEC_free (btrace_block_s, *btrace);
+
+ if (type == BTRACE_READ_DELTA)
+ {
+ /* Determine the number of bytes to read and check for buffer
+ overflows. */
+
+ /* Check for data head overflows. We might be able to recover from
+ those but they are very unlikely and it's not really worth the
+ effort, I think. */
+ if (data_head < data_tail)
+ return BTRACE_ERR_OVERFLOW;
+
+ /* If the buffer is smaller than the trace delta, we overflowed. */
+ size = data_head - data_tail;
+ if (buffer_size < size)
+ return BTRACE_ERR_OVERFLOW;
+ }
+ else
+ {
+ /* Read the entire buffer. */
+ size = buffer_size;
+
+ /* Adjust the size if the buffer has not overflowed, yet. */
+ if (data_head < size)
+ size = data_head;
+ }
+
+ /* Data_head keeps growing; the buffer itself is circular. */
+ begin = perf_event_buffer_begin (tinfo);
+ start = begin + data_head % buffer_size;
+
+ if (data_head <= buffer_size)
+ end = start;
+ else
+ end = perf_event_buffer_end (tinfo);
+
+ *btrace = perf_event_read_bts (tinfo, begin, end, start, size);
+
+ /* The stopping thread notifies its ptracer before it is scheduled out.
+ On multi-core systems, the debugger might therefore run while the
+ kernel might be writing the last branch trace records.
+
+ Let's check whether the data head moved while we read the trace. */
+ if (data_head == header->data_head)
+ break;
+ }
+
+ tinfo->data_head = data_head;
+
+ /* Prune the incomplete last block (i.e. the first one of inferior execution)
+ if we're not doing a delta read. There is no way of filling in its zeroed
+ BEGIN element. */
+ if (!VEC_empty (btrace_block_s, *btrace) && type != BTRACE_READ_DELTA)
+ VEC_pop (btrace_block_s, *btrace);
+
+ return BTRACE_ERR_NONE;
+}
+
+#else /* !HAVE_LINUX_PERF_EVENT_H */
+
+/* See linux-btrace.h. */
+
+int
+linux_supports_btrace (struct target_ops *ops)
+{
+ return 0;
+}
+
+/* See linux-btrace.h. */
+
+struct btrace_target_info *
+linux_enable_btrace (ptid_t ptid)
+{
+ return NULL;
+}
+
+/* See linux-btrace.h. */
+
+enum btrace_error
+linux_disable_btrace (struct btrace_target_info *tinfo)
+{
+ return BTRACE_ERR_NOT_SUPPORTED;
+}
+
+/* See linux-btrace.h. */
+
+enum btrace_error
+linux_read_btrace (VEC (btrace_block_s) **btrace,
+ struct btrace_target_info *tinfo,
+ enum btrace_read_type type)
+{
+ return BTRACE_ERR_NOT_SUPPORTED;
+}
+
+#endif /* !HAVE_LINUX_PERF_EVENT_H */
diff --git a/gdb/nat/linux-btrace.h b/gdb/nat/linux-btrace.h
new file mode 100644
index 0000000..12e9b60
--- /dev/null
+++ b/gdb/nat/linux-btrace.h
@@ -0,0 +1,78 @@
+/* Linux-dependent part of branch trace support for GDB, and GDBserver.
+
+ Copyright (C) 2013-2014 Free Software Foundation, Inc.
+
+ Contributed by Intel Corp. <markus.t.metzger@intel.com>
+
+ This file is part of GDB.
+
+ 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 LINUX_BTRACE_H
+#define LINUX_BTRACE_H
+
+#include "btrace-common.h"
+#include "config.h"
+#include "vec.h"
+#include "ptid.h"
+#include <stddef.h>
+#include <stdint.h>
+
+#if HAVE_LINUX_PERF_EVENT_H
+# include <linux/perf_event.h>
+#endif
+
+/* Branch trace target information per thread. */
+struct btrace_target_info
+{
+#if HAVE_LINUX_PERF_EVENT_H
+ /* The Linux perf_event configuration for collecting the branch trace. */
+ struct perf_event_attr attr;
+
+ /* The ptid of this thread. */
+ ptid_t ptid;
+
+ /* The mmap configuration mapping the branch trace perf_event buffer.
+
+ file .. the file descriptor
+ buffer .. the mmapped memory buffer
+ size .. the buffer's size in pages without the configuration page
+ data_head .. the data head from the last read */
+ int file;
+ void *buffer;
+ size_t size;
+ unsigned long data_head;
+#endif /* HAVE_LINUX_PERF_EVENT_H */
+
+ /* The size of a pointer in bits for this thread.
+ The information is used to identify kernel addresses in order to skip
+ records from/to kernel space. */
+ int ptr_bits;
+};
+
+/* See to_supports_btrace in target.h. */
+extern int linux_supports_btrace (struct target_ops *);
+
+/* See to_enable_btrace in target.h. */
+extern struct btrace_target_info *linux_enable_btrace (ptid_t ptid);
+
+/* See to_disable_btrace in target.h. */
+extern enum btrace_error linux_disable_btrace (struct btrace_target_info *ti);
+
+/* See to_read_btrace in target.h. */
+extern enum btrace_error linux_read_btrace (VEC (btrace_block_s) **btrace,
+ struct btrace_target_info *btinfo,
+ enum btrace_read_type type);
+
+#endif /* LINUX_BTRACE_H */
diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c
new file mode 100644
index 0000000..dae637b
--- /dev/null
+++ b/gdb/nat/linux-osdata.c
@@ -0,0 +1,1634 @@
+/* Linux-specific functions to retrieve OS data.
+
+ Copyright (C) 2009-2014 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ 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/>. */
+
+#ifdef GDBSERVER
+#include "server.h"
+#else
+#include "defs.h"
+#endif
+
+#include "linux-osdata.h"
+
+#include <sys/types.h>
+#include <sys/sysinfo.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <utmp.h>
+#include <time.h>
+#include <unistd.h>
+#include <pwd.h>
+#include <grp.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "xml-utils.h"
+#include "buffer.h"
+#include "gdb_assert.h"
+#include <dirent.h>
+#include <sys/stat.h>
+#include "filestuff.h"
+
+#define NAMELEN(dirent) strlen ((dirent)->d_name)
+
+/* Define PID_T to be a fixed size that is at least as large as pid_t,
+ so that reading pid values embedded in /proc works
+ consistently. */
+
+typedef long long PID_T;
+
+/* Define TIME_T to be at least as large as time_t, so that reading
+ time values embedded in /proc works consistently. */
+
+typedef long long TIME_T;
+
+#define MAX_PID_T_STRLEN (sizeof ("-9223372036854775808") - 1)
+
+/* Returns the CPU core that thread PTID is currently running on. */
+
+/* Compute and return the processor core of a given thread. */
+
+int
+linux_common_core_of_thread (ptid_t ptid)
+{
+ char filename[sizeof ("/proc//task//stat") + 2 * MAX_PID_T_STRLEN];
+ FILE *f;
+ char *content = NULL;
+ char *p;
+ char *ts = 0;
+ int content_read = 0;
+ int i;
+ int core;
+
+ sprintf (filename, "/proc/%lld/task/%lld/stat",
+ (PID_T) ptid_get_pid (ptid), (PID_T) ptid_get_lwp (ptid));
+ f = gdb_fopen_cloexec (filename, "r");
+ if (!f)
+ return -1;
+
+ for (;;)
+ {
+ int n;
+ content = xrealloc (content, content_read + 1024);
+ n = fread (content + content_read, 1, 1024, f);
+ content_read += n;
+ if (n < 1024)
+ {
+ content[content_read] = '\0';
+ break;
+ }
+ }
+
+ /* ps command also relies on no trailing fields ever contain ')'. */
+ p = strrchr (content, ')');
+ if (p != NULL)
+ p++;
+
+ /* If the first field after program name has index 0, then core number is
+ the field with index 36. There's no constant for that anywhere. */
+ if (p != NULL)
+ p = strtok_r (p, " ", &ts);
+ for (i = 0; p != NULL && i != 36; ++i)
+ p = strtok_r (NULL, " ", &ts);
+
+ if (p == NULL || sscanf (p, "%d", &core) == 0)
+ core = -1;
+
+ xfree (content);
+ fclose (f);
+
+ return core;
+}
+
+/* Finds the command-line of process PID and copies it into COMMAND.
+ At most MAXLEN characters are copied. If the command-line cannot
+ be found, PID is copied into command in text-form. */
+
+static void
+command_from_pid (char *command, int maxlen, PID_T pid)
+{
+ char *stat_path = xstrprintf ("/proc/%lld/stat", pid);
+ FILE *fp = gdb_fopen_cloexec (stat_path, "r");
+
+ command[0] = '\0';
+
+ if (fp)
+ {
+ /* sizeof (cmd) should be greater or equal to TASK_COMM_LEN (in
+ include/linux/sched.h in the Linux kernel sources) plus two
+ (for the brackets). */
+ char cmd[18];
+ PID_T stat_pid;
+ int items_read = fscanf (fp, "%lld %17s", &stat_pid, cmd);
+
+ if (items_read == 2 && pid == stat_pid)
+ {
+ cmd[strlen (cmd) - 1] = '\0'; /* Remove trailing parenthesis. */
+ strncpy (command, cmd + 1, maxlen); /* Ignore leading parenthesis. */
+ }
+
+ fclose (fp);
+ }
+ else
+ {
+ /* Return the PID if a /proc entry for the process cannot be found. */
+ snprintf (command, maxlen, "%lld", pid);
+ }
+
+ command[maxlen - 1] = '\0'; /* Ensure string is null-terminated. */
+
+ xfree (stat_path);
+}
+
+/* Returns the command-line of the process with the given PID. The
+ returned string needs to be freed using xfree after use. */
+
+static char *
+commandline_from_pid (PID_T pid)
+{
+ char *pathname = xstrprintf ("/proc/%lld/cmdline", pid);
+ char *commandline = NULL;
+ FILE *f = gdb_fopen_cloexec (pathname, "r");
+
+ if (f)
+ {
+ size_t len = 0;
+
+ while (!feof (f))
+ {
+ char buf[1024];
+ size_t read_bytes = fread (buf, 1, sizeof (buf), f);
+
+ if (read_bytes)
+ {
+ commandline = (char *) xrealloc (commandline, len + read_bytes + 1);
+ memcpy (commandline + len, buf, read_bytes);
+ len += read_bytes;
+ }
+ }
+
+ fclose (f);
+
+ if (commandline)
+ {
+ size_t i;
+
+ /* Replace null characters with spaces. */
+ for (i = 0; i < len; ++i)
+ if (commandline[i] == '\0')
+ commandline[i] = ' ';
+
+ commandline[len] = '\0';
+ }
+ else
+ {
+ /* Return the command in square brackets if the command-line
+ is empty. */
+ commandline = (char *) xmalloc (32);
+ commandline[0] = '[';
+ command_from_pid (commandline + 1, 31, pid);
+
+ len = strlen (commandline);
+ if (len < 31)
+ strcat (commandline, "]");
+ }
+ }
+
+ xfree (pathname);
+
+ return commandline;
+}
+
+/* Finds the user name for the user UID and copies it into USER. At
+ most MAXLEN characters are copied. */
+
+static void
+user_from_uid (char *user, int maxlen, uid_t uid)
+{
+ struct passwd *pwentry = getpwuid (uid);
+
+ if (pwentry)
+ {
+ strncpy (user, pwentry->pw_name, maxlen);
+ /* Ensure that the user name is null-terminated. */
+ user[maxlen - 1] = '\0';
+ }
+ else
+ user[0] = '\0';
+}
+
+/* Finds the owner of process PID and returns the user id in OWNER.
+ Returns 0 if the owner was found, -1 otherwise. */
+
+static int
+get_process_owner (uid_t *owner, PID_T pid)
+{
+ struct stat statbuf;
+ char procentry[sizeof ("/proc/") + MAX_PID_T_STRLEN];
+
+ sprintf (procentry, "/proc/%lld", pid);
+
+ if (stat (procentry, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
+ {
+ *owner = statbuf.st_uid;
+ return 0;
+ }
+ else
+ return -1;
+}
+
+/* Find the CPU cores used by process PID and return them in CORES.
+ CORES points to an array of NUM_CORES elements. */
+
+static int
+get_cores_used_by_process (PID_T pid, int *cores, const int num_cores)
+{
+ char taskdir[sizeof ("/proc/") + MAX_PID_T_STRLEN + sizeof ("/task") - 1];
+ DIR *dir;
+ struct dirent *dp;
+ int task_count = 0;
+
+ sprintf (taskdir, "/proc/%lld/task", pid);
+ dir = opendir (taskdir);
+ if (dir)
+ {
+ while ((dp = readdir (dir)) != NULL)
+ {
+ PID_T tid;
+ int core;
+
+ if (!isdigit (dp->d_name[0])
+ || NAMELEN (dp) > MAX_PID_T_STRLEN)
+ continue;
+
+ sscanf (dp->d_name, "%lld", &tid);
+ core = linux_common_core_of_thread (ptid_build ((pid_t) pid,
+ (pid_t) tid, 0));
+
+ if (core >= 0 && core < num_cores)
+ {
+ ++cores[core];
+ ++task_count;
+ }
+ }
+
+ closedir (dir);
+ }
+
+ return task_count;
+}
+
+static LONGEST
+linux_xfer_osdata_processes (gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len)
+{
+ /* We make the process list snapshot when the object starts to be read. */
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct buffer buffer;
+
+ if (offset == 0)
+ {
+ DIR *dirp;
+
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"processes\">\n");
+
+ dirp = opendir ("/proc");
+ if (dirp)
+ {
+ const int num_cores = sysconf (_SC_NPROCESSORS_ONLN);
+ struct dirent *dp;
+
+ while ((dp = readdir (dirp)) != NULL)
+ {
+ PID_T pid;
+ uid_t owner;
+ char user[UT_NAMESIZE];
+ char *command_line;
+ int *cores;
+ int task_count;
+ char *cores_str;
+ int i;
+
+ if (!isdigit (dp->d_name[0])
+ || NAMELEN (dp) > MAX_PID_T_STRLEN)
+ continue;
+
+ sscanf (dp->d_name, "%lld", &pid);
+ command_line = commandline_from_pid (pid);
+
+ if (get_process_owner (&owner, pid) == 0)
+ user_from_uid (user, sizeof (user), owner);
+ else
+ strcpy (user, "?");
+
+ /* Find CPU cores used by the process. */
+ cores = (int *) xcalloc (num_cores, sizeof (int));
+ task_count = get_cores_used_by_process (pid, cores, num_cores);
+ cores_str = (char *) xcalloc (task_count, sizeof ("4294967295") + 1);
+
+ for (i = 0; i < num_cores && task_count > 0; ++i)
+ if (cores[i])
+ {
+ char core_str[sizeof ("4294967295")];
+
+ sprintf (core_str, "%d", i);
+ strcat (cores_str, core_str);
+
+ task_count -= cores[i];
+ if (task_count > 0)
+ strcat (cores_str, ",");
+ }
+
+ xfree (cores);
+
+ buffer_xml_printf (
+ &buffer,
+ "<item>"
+ "<column name=\"pid\">%lld</column>"
+ "<column name=\"user\">%s</column>"
+ "<column name=\"command\">%s</column>"
+ "<column name=\"cores\">%s</column>"
+ "</item>",
+ pid,
+ user,
+ command_line ? command_line : "",
+ cores_str);
+
+ xfree (command_line);
+ xfree (cores_str);
+ }
+
+ closedir (dirp);
+ }
+
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the buffer. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+}
+
+/* Auxiliary function used by qsort to sort processes by process
+ group. Compares two processes with ids PROCESS1 and PROCESS2.
+ PROCESS1 comes before PROCESS2 if it has a lower process group id.
+ If they belong to the same process group, PROCESS1 comes before
+ PROCESS2 if it has a lower process id or is the process group
+ leader. */
+
+static int
+compare_processes (const void *process1, const void *process2)
+{
+ PID_T pid1 = *((PID_T *) process1);
+ PID_T pid2 = *((PID_T *) process2);
+ PID_T pgid1 = *((PID_T *) process1 + 1);
+ PID_T pgid2 = *((PID_T *) process2 + 1);
+
+ /* Sort by PGID. */
+ if (pgid1 < pgid2)
+ return -1;
+ else if (pgid1 > pgid2)
+ return 1;
+ else
+ {
+ /* Process group leaders always come first, else sort by PID. */
+ if (pid1 == pgid1)
+ return -1;
+ else if (pid2 == pgid2)
+ return 1;
+ else if (pid1 < pid2)
+ return -1;
+ else if (pid1 > pid2)
+ return 1;
+ else
+ return 0;
+ }
+}
+
+/* Collect all process groups from /proc. */
+
+static LONGEST
+linux_xfer_osdata_processgroups (gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len)
+{
+ /* We make the process list snapshot when the object starts to be read. */
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct buffer buffer;
+
+ if (offset == 0)
+ {
+ DIR *dirp;
+
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"process groups\">\n");
+
+ dirp = opendir ("/proc");
+ if (dirp)
+ {
+ struct dirent *dp;
+ const size_t list_block_size = 512;
+ PID_T *process_list = (PID_T *) xmalloc (list_block_size * 2 * sizeof (PID_T));
+ size_t process_count = 0;
+ size_t i;
+
+ /* Build list consisting of PIDs followed by their
+ associated PGID. */
+ while ((dp = readdir (dirp)) != NULL)
+ {
+ PID_T pid, pgid;
+
+ if (!isdigit (dp->d_name[0])
+ || NAMELEN (dp) > MAX_PID_T_STRLEN)
+ continue;
+
+ sscanf (dp->d_name, "%lld", &pid);
+ pgid = getpgid (pid);
+
+ if (pgid > 0)
+ {
+ process_list[2 * process_count] = pid;
+ process_list[2 * process_count + 1] = pgid;
+ ++process_count;
+
+ /* Increase the size of the list if necessary. */
+ if (process_count % list_block_size == 0)
+ process_list = (PID_T *) xrealloc (
+ process_list,
+ (process_count + list_block_size)
+ * 2 * sizeof (PID_T));
+ }
+ }
+
+ closedir (dirp);
+
+ /* Sort the process list. */
+ qsort (process_list, process_count, 2 * sizeof (PID_T),
+ compare_processes);
+
+ for (i = 0; i < process_count; ++i)
+ {
+ PID_T pid = process_list[2 * i];
+ PID_T pgid = process_list[2 * i + 1];
+ char leader_command[32];
+ char *command_line;
+
+ command_from_pid (leader_command, sizeof (leader_command), pgid);
+ command_line = commandline_from_pid (pid);
+
+ buffer_xml_printf (
+ &buffer,
+ "<item>"
+ "<column name=\"pgid\">%lld</column>"
+ "<column name=\"leader command\">%s</column>"
+ "<column name=\"pid\">%lld</column>"
+ "<column name=\"command line\">%s</column>"
+ "</item>",
+ pgid,
+ leader_command,
+ pid,
+ command_line ? command_line : "");
+
+ xfree (command_line);
+ }
+
+ xfree (process_list);
+ }
+
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the buffer. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+}
+
+/* Collect all the threads in /proc by iterating through processes and
+ then tasks within each process. */
+
+static LONGEST
+linux_xfer_osdata_threads (gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len)
+{
+ /* We make the process list snapshot when the object starts to be read. */
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct buffer buffer;
+
+ if (offset == 0)
+ {
+ DIR *dirp;
+
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"threads\">\n");
+
+ dirp = opendir ("/proc");
+ if (dirp)
+ {
+ struct dirent *dp;
+
+ while ((dp = readdir (dirp)) != NULL)
+ {
+ struct stat statbuf;
+ char procentry[sizeof ("/proc/4294967295")];
+
+ if (!isdigit (dp->d_name[0])
+ || NAMELEN (dp) > sizeof ("4294967295") - 1)
+ continue;
+
+ sprintf (procentry, "/proc/%s", dp->d_name);
+ if (stat (procentry, &statbuf) == 0
+ && S_ISDIR (statbuf.st_mode))
+ {
+ DIR *dirp2;
+ char *pathname;
+ PID_T pid;
+ char command[32];
+
+ pathname = xstrprintf ("/proc/%s/task", dp->d_name);
+
+ pid = atoi (dp->d_name);
+ command_from_pid (command, sizeof (command), pid);
+
+ dirp2 = opendir (pathname);
+
+ if (dirp2)
+ {
+ struct dirent *dp2;
+
+ while ((dp2 = readdir (dirp2)) != NULL)
+ {
+ PID_T tid;
+ int core;
+
+ if (!isdigit (dp2->d_name[0])
+ || NAMELEN (dp2) > sizeof ("4294967295") - 1)
+ continue;
+
+ tid = atoi (dp2->d_name);
+ core = linux_common_core_of_thread (ptid_build (pid, tid, 0));
+
+ buffer_xml_printf (
+ &buffer,
+ "<item>"
+ "<column name=\"pid\">%lld</column>"
+ "<column name=\"command\">%s</column>"
+ "<column name=\"tid\">%lld</column>"
+ "<column name=\"core\">%d</column>"
+ "</item>",
+ pid,
+ command,
+ tid,
+ core);
+ }
+
+ closedir (dirp2);
+ }
+
+ xfree (pathname);
+ }
+ }
+
+ closedir (dirp);
+ }
+
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the buffer. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+}
+
+/* Collect all the open file descriptors found in /proc and put the details
+ found about them into READBUF. */
+
+static LONGEST
+linux_xfer_osdata_fds (gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len)
+{
+ /* We make the process list snapshot when the object starts to be read. */
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct buffer buffer;
+
+ if (offset == 0)
+ {
+ DIR *dirp;
+
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"files\">\n");
+
+ dirp = opendir ("/proc");
+ if (dirp)
+ {
+ struct dirent *dp;
+
+ while ((dp = readdir (dirp)) != NULL)
+ {
+ struct stat statbuf;
+ char procentry[sizeof ("/proc/4294967295")];
+
+ if (!isdigit (dp->d_name[0])
+ || NAMELEN (dp) > sizeof ("4294967295") - 1)
+ continue;
+
+ sprintf (procentry, "/proc/%s", dp->d_name);
+ if (stat (procentry, &statbuf) == 0
+ && S_ISDIR (statbuf.st_mode))
+ {
+ char *pathname;
+ DIR *dirp2;
+ PID_T pid;
+ char command[32];
+
+ pid = atoi (dp->d_name);
+ command_from_pid (command, sizeof (command), pid);
+
+ pathname = xstrprintf ("/proc/%s/fd", dp->d_name);
+ dirp2 = opendir (pathname);
+
+ if (dirp2)
+ {
+ struct dirent *dp2;
+
+ while ((dp2 = readdir (dirp2)) != NULL)
+ {
+ char *fdname;
+ char buf[1000];
+ ssize_t rslt;
+
+ if (!isdigit (dp2->d_name[0]))
+ continue;
+
+ fdname = xstrprintf ("%s/%s", pathname, dp2->d_name);
+ rslt = readlink (fdname, buf, sizeof (buf) - 1);
+ if (rslt >= 0)
+ buf[rslt] = '\0';
+
+ buffer_xml_printf (
+ &buffer,
+ "<item>"
+ "<column name=\"pid\">%s</column>"
+ "<column name=\"command\">%s</column>"
+ "<column name=\"file descriptor\">%s</column>"
+ "<column name=\"name\">%s</column>"
+ "</item>",
+ dp->d_name,
+ command,
+ dp2->d_name,
+ (rslt >= 0 ? buf : dp2->d_name));
+ }
+
+ closedir (dirp2);
+ }
+
+ xfree (pathname);
+ }
+ }
+
+ closedir (dirp);
+ }
+
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the buffer. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+}
+
+/* Returns the socket state STATE in textual form. */
+
+static const char *
+format_socket_state (unsigned char state)
+{
+ /* Copied from include/net/tcp_states.h in the Linux kernel sources. */
+ enum {
+ TCP_ESTABLISHED = 1,
+ TCP_SYN_SENT,
+ TCP_SYN_RECV,
+ TCP_FIN_WAIT1,
+ TCP_FIN_WAIT2,
+ TCP_TIME_WAIT,
+ TCP_CLOSE,
+ TCP_CLOSE_WAIT,
+ TCP_LAST_ACK,
+ TCP_LISTEN,
+ TCP_CLOSING
+ };
+
+ switch (state)
+ {
+ case TCP_ESTABLISHED:
+ return "ESTABLISHED";
+ case TCP_SYN_SENT:
+ return "SYN_SENT";
+ case TCP_SYN_RECV:
+ return "SYN_RECV";
+ case TCP_FIN_WAIT1:
+ return "FIN_WAIT1";
+ case TCP_FIN_WAIT2:
+ return "FIN_WAIT2";
+ case TCP_TIME_WAIT:
+ return "TIME_WAIT";
+ case TCP_CLOSE:
+ return "CLOSE";
+ case TCP_CLOSE_WAIT:
+ return "CLOSE_WAIT";
+ case TCP_LAST_ACK:
+ return "LAST_ACK";
+ case TCP_LISTEN:
+ return "LISTEN";
+ case TCP_CLOSING:
+ return "CLOSING";
+ default:
+ return "(unknown)";
+ }
+}
+
+union socket_addr
+ {
+ struct sockaddr sa;
+ struct sockaddr_in sin;
+ struct sockaddr_in6 sin6;
+ };
+
+/* Auxiliary function used by linux_xfer_osdata_isocket. Formats
+ information for all open internet sockets of type FAMILY on the
+ system into BUFFER. If TCP is set, only TCP sockets are processed,
+ otherwise only UDP sockets are processed. */
+
+static void
+print_sockets (unsigned short family, int tcp, struct buffer *buffer)
+{
+ const char *proc_file;
+ FILE *fp;
+
+ if (family == AF_INET)
+ proc_file = tcp ? "/proc/net/tcp" : "/proc/net/udp";
+ else if (family == AF_INET6)
+ proc_file = tcp ? "/proc/net/tcp6" : "/proc/net/udp6";
+ else
+ return;
+
+ fp = gdb_fopen_cloexec (proc_file, "r");
+ if (fp)
+ {
+ char buf[8192];
+
+ do
+ {
+ if (fgets (buf, sizeof (buf), fp))
+ {
+ uid_t uid;
+ unsigned int local_port, remote_port, state;
+ char local_address[NI_MAXHOST], remote_address[NI_MAXHOST];
+ int result;
+
+#if NI_MAXHOST <= 32
+#error "local_address and remote_address buffers too small"
+#endif
+
+ result = sscanf (buf,
+ "%*d: %32[0-9A-F]:%X %32[0-9A-F]:%X %X %*X:%*X %*X:%*X %*X %d %*d %*u %*s\n",
+ local_address, &local_port,
+ remote_address, &remote_port,
+ &state,
+ &uid);
+
+ if (result == 6)
+ {
+ union socket_addr locaddr, remaddr;
+ size_t addr_size;
+ char user[UT_NAMESIZE];
+ char local_service[NI_MAXSERV], remote_service[NI_MAXSERV];
+
+ if (family == AF_INET)
+ {
+ sscanf (local_address, "%X",
+ &locaddr.sin.sin_addr.s_addr);
+ sscanf (remote_address, "%X",
+ &remaddr.sin.sin_addr.s_addr);
+
+ locaddr.sin.sin_port = htons (local_port);
+ remaddr.sin.sin_port = htons (remote_port);
+
+ addr_size = sizeof (struct sockaddr_in);
+ }
+ else
+ {
+ sscanf (local_address, "%8X%8X%8X%8X",
+ locaddr.sin6.sin6_addr.s6_addr32,
+ locaddr.sin6.sin6_addr.s6_addr32 + 1,
+ locaddr.sin6.sin6_addr.s6_addr32 + 2,
+ locaddr.sin6.sin6_addr.s6_addr32 + 3);
+ sscanf (remote_address, "%8X%8X%8X%8X",
+ remaddr.sin6.sin6_addr.s6_addr32,
+ remaddr.sin6.sin6_addr.s6_addr32 + 1,
+ remaddr.sin6.sin6_addr.s6_addr32 + 2,
+ remaddr.sin6.sin6_addr.s6_addr32 + 3);
+
+ locaddr.sin6.sin6_port = htons (local_port);
+ remaddr.sin6.sin6_port = htons (remote_port);
+
+ locaddr.sin6.sin6_flowinfo = 0;
+ remaddr.sin6.sin6_flowinfo = 0;
+ locaddr.sin6.sin6_scope_id = 0;
+ remaddr.sin6.sin6_scope_id = 0;
+
+ addr_size = sizeof (struct sockaddr_in6);
+ }
+
+ locaddr.sa.sa_family = remaddr.sa.sa_family = family;
+
+ result = getnameinfo (&locaddr.sa, addr_size,
+ local_address, sizeof (local_address),
+ local_service, sizeof (local_service),
+ NI_NUMERICHOST | NI_NUMERICSERV
+ | (tcp ? 0 : NI_DGRAM));
+ if (result)
+ continue;
+
+ result = getnameinfo (&remaddr.sa, addr_size,
+ remote_address,
+ sizeof (remote_address),
+ remote_service,
+ sizeof (remote_service),
+ NI_NUMERICHOST | NI_NUMERICSERV
+ | (tcp ? 0 : NI_DGRAM));
+ if (result)
+ continue;
+
+ user_from_uid (user, sizeof (user), uid);
+
+ buffer_xml_printf (
+ buffer,
+ "<item>"
+ "<column name=\"local address\">%s</column>"
+ "<column name=\"local port\">%s</column>"
+ "<column name=\"remote address\">%s</column>"
+ "<column name=\"remote port\">%s</column>"
+ "<column name=\"state\">%s</column>"
+ "<column name=\"user\">%s</column>"
+ "<column name=\"family\">%s</column>"
+ "<column name=\"protocol\">%s</column>"
+ "</item>",
+ local_address,
+ local_service,
+ remote_address,
+ remote_service,
+ format_socket_state (state),
+ user,
+ (family == AF_INET) ? "INET" : "INET6",
+ tcp ? "STREAM" : "DGRAM");
+ }
+ }
+ }
+ while (!feof (fp));
+
+ fclose (fp);
+ }
+}
+
+/* Collect data about internet sockets and write it into READBUF. */
+
+static LONGEST
+linux_xfer_osdata_isockets (gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len)
+{
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct buffer buffer;
+
+ if (offset == 0)
+ {
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"I sockets\">\n");
+
+ print_sockets (AF_INET, 1, &buffer);
+ print_sockets (AF_INET, 0, &buffer);
+ print_sockets (AF_INET6, 1, &buffer);
+ print_sockets (AF_INET6, 0, &buffer);
+
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the buffer. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+}
+
+/* Converts the time SECONDS into textual form and copies it into a
+ buffer TIME, with at most MAXLEN characters copied. */
+
+static void
+time_from_time_t (char *time, int maxlen, TIME_T seconds)
+{
+ if (!seconds)
+ time[0] = '\0';
+ else
+ {
+ time_t t = (time_t) seconds;
+
+ strncpy (time, ctime (&t), maxlen);
+ time[maxlen - 1] = '\0';
+ }
+}
+
+/* Finds the group name for the group GID and copies it into GROUP.
+ At most MAXLEN characters are copied. */
+
+static void
+group_from_gid (char *group, int maxlen, gid_t gid)
+{
+ struct group *grentry = getgrgid (gid);
+
+ if (grentry)
+ {
+ strncpy (group, grentry->gr_name, maxlen);
+ /* Ensure that the group name is null-terminated. */
+ group[maxlen - 1] = '\0';
+ }
+ else
+ group[0] = '\0';
+}
+
+/* Collect data about shared memory recorded in /proc and write it
+ into READBUF. */
+
+static LONGEST
+linux_xfer_osdata_shm (gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len)
+{
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct buffer buffer;
+
+ if (offset == 0)
+ {
+ FILE *fp;
+
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"shared memory\">\n");
+
+ fp = gdb_fopen_cloexec ("/proc/sysvipc/shm", "r");
+ if (fp)
+ {
+ char buf[8192];
+
+ do
+ {
+ if (fgets (buf, sizeof (buf), fp))
+ {
+ key_t key;
+ uid_t uid, cuid;
+ gid_t gid, cgid;
+ PID_T cpid, lpid;
+ int shmid, size, nattch;
+ TIME_T atime, dtime, ctime;
+ unsigned int perms;
+ int items_read;
+
+ items_read = sscanf (buf,
+ "%d %d %o %d %lld %lld %d %u %u %u %u %lld %lld %lld",
+ &key, &shmid, &perms, &size,
+ &cpid, &lpid,
+ &nattch,
+ &uid, &gid, &cuid, &cgid,
+ &atime, &dtime, &ctime);
+
+ if (items_read == 14)
+ {
+ char user[UT_NAMESIZE], group[UT_NAMESIZE];
+ char cuser[UT_NAMESIZE], cgroup[UT_NAMESIZE];
+ char ccmd[32], lcmd[32];
+ char atime_str[32], dtime_str[32], ctime_str[32];
+
+ user_from_uid (user, sizeof (user), uid);
+ group_from_gid (group, sizeof (group), gid);
+ user_from_uid (cuser, sizeof (cuser), cuid);
+ group_from_gid (cgroup, sizeof (cgroup), cgid);
+
+ command_from_pid (ccmd, sizeof (ccmd), cpid);
+ command_from_pid (lcmd, sizeof (lcmd), lpid);
+
+ time_from_time_t (atime_str, sizeof (atime_str), atime);
+ time_from_time_t (dtime_str, sizeof (dtime_str), dtime);
+ time_from_time_t (ctime_str, sizeof (ctime_str), ctime);
+
+ buffer_xml_printf (
+ &buffer,
+ "<item>"
+ "<column name=\"key\">%d</column>"
+ "<column name=\"shmid\">%d</column>"
+ "<column name=\"permissions\">%o</column>"
+ "<column name=\"size\">%d</column>"
+ "<column name=\"creator command\">%s</column>"
+ "<column name=\"last op. command\">%s</column>"
+ "<column name=\"num attached\">%d</column>"
+ "<column name=\"user\">%s</column>"
+ "<column name=\"group\">%s</column>"
+ "<column name=\"creator user\">%s</column>"
+ "<column name=\"creator group\">%s</column>"
+ "<column name=\"last shmat() time\">%s</column>"
+ "<column name=\"last shmdt() time\">%s</column>"
+ "<column name=\"last shmctl() time\">%s</column>"
+ "</item>",
+ key,
+ shmid,
+ perms,
+ size,
+ ccmd,
+ lcmd,
+ nattch,
+ user,
+ group,
+ cuser,
+ cgroup,
+ atime_str,
+ dtime_str,
+ ctime_str);
+ }
+ }
+ }
+ while (!feof (fp));
+
+ fclose (fp);
+ }
+
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the buffer. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+}
+
+/* Collect data about semaphores recorded in /proc and write it
+ into READBUF. */
+
+static LONGEST
+linux_xfer_osdata_sem (gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len)
+{
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct buffer buffer;
+
+ if (offset == 0)
+ {
+ FILE *fp;
+
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"semaphores\">\n");
+
+ fp = gdb_fopen_cloexec ("/proc/sysvipc/sem", "r");
+ if (fp)
+ {
+ char buf[8192];
+
+ do
+ {
+ if (fgets (buf, sizeof (buf), fp))
+ {
+ key_t key;
+ uid_t uid, cuid;
+ gid_t gid, cgid;
+ unsigned int perms, nsems;
+ int semid;
+ TIME_T otime, ctime;
+ int items_read;
+
+ items_read = sscanf (buf,
+ "%d %d %o %u %d %d %d %d %lld %lld",
+ &key, &semid, &perms, &nsems,
+ &uid, &gid, &cuid, &cgid,
+ &otime, &ctime);
+
+ if (items_read == 10)
+ {
+ char user[UT_NAMESIZE], group[UT_NAMESIZE];
+ char cuser[UT_NAMESIZE], cgroup[UT_NAMESIZE];
+ char otime_str[32], ctime_str[32];
+
+ user_from_uid (user, sizeof (user), uid);
+ group_from_gid (group, sizeof (group), gid);
+ user_from_uid (cuser, sizeof (cuser), cuid);
+ group_from_gid (cgroup, sizeof (cgroup), cgid);
+
+ time_from_time_t (otime_str, sizeof (otime_str), otime);
+ time_from_time_t (ctime_str, sizeof (ctime_str), ctime);
+
+ buffer_xml_printf (
+ &buffer,
+ "<item>"
+ "<column name=\"key\">%d</column>"
+ "<column name=\"semid\">%d</column>"
+ "<column name=\"permissions\">%o</column>"
+ "<column name=\"num semaphores\">%u</column>"
+ "<column name=\"user\">%s</column>"
+ "<column name=\"group\">%s</column>"
+ "<column name=\"creator user\">%s</column>"
+ "<column name=\"creator group\">%s</column>"
+ "<column name=\"last semop() time\">%s</column>"
+ "<column name=\"last semctl() time\">%s</column>"
+ "</item>",
+ key,
+ semid,
+ perms,
+ nsems,
+ user,
+ group,
+ cuser,
+ cgroup,
+ otime_str,
+ ctime_str);
+ }
+ }
+ }
+ while (!feof (fp));
+
+ fclose (fp);
+ }
+
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the buffer. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+}
+
+/* Collect data about message queues recorded in /proc and write it
+ into READBUF. */
+
+static LONGEST
+linux_xfer_osdata_msg (gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len)
+{
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct buffer buffer;
+
+ if (offset == 0)
+ {
+ FILE *fp;
+
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"message queues\">\n");
+
+ fp = gdb_fopen_cloexec ("/proc/sysvipc/msg", "r");
+ if (fp)
+ {
+ char buf[8192];
+
+ do
+ {
+ if (fgets (buf, sizeof (buf), fp))
+ {
+ key_t key;
+ PID_T lspid, lrpid;
+ uid_t uid, cuid;
+ gid_t gid, cgid;
+ unsigned int perms, cbytes, qnum;
+ int msqid;
+ TIME_T stime, rtime, ctime;
+ int items_read;
+
+ items_read = sscanf (buf,
+ "%d %d %o %u %u %lld %lld %d %d %d %d %lld %lld %lld",
+ &key, &msqid, &perms, &cbytes, &qnum,
+ &lspid, &lrpid, &uid, &gid, &cuid, &cgid,
+ &stime, &rtime, &ctime);
+
+ if (items_read == 14)
+ {
+ char user[UT_NAMESIZE], group[UT_NAMESIZE];
+ char cuser[UT_NAMESIZE], cgroup[UT_NAMESIZE];
+ char lscmd[32], lrcmd[32];
+ char stime_str[32], rtime_str[32], ctime_str[32];
+
+ user_from_uid (user, sizeof (user), uid);
+ group_from_gid (group, sizeof (group), gid);
+ user_from_uid (cuser, sizeof (cuser), cuid);
+ group_from_gid (cgroup, sizeof (cgroup), cgid);
+
+ command_from_pid (lscmd, sizeof (lscmd), lspid);
+ command_from_pid (lrcmd, sizeof (lrcmd), lrpid);
+
+ time_from_time_t (stime_str, sizeof (stime_str), stime);
+ time_from_time_t (rtime_str, sizeof (rtime_str), rtime);
+ time_from_time_t (ctime_str, sizeof (ctime_str), ctime);
+
+ buffer_xml_printf (
+ &buffer,
+ "<item>"
+ "<column name=\"key\">%d</column>"
+ "<column name=\"msqid\">%d</column>"
+ "<column name=\"permissions\">%o</column>"
+ "<column name=\"num used bytes\">%u</column>"
+ "<column name=\"num messages\">%u</column>"
+ "<column name=\"last msgsnd() command\">%s</column>"
+ "<column name=\"last msgrcv() command\">%s</column>"
+ "<column name=\"user\">%s</column>"
+ "<column name=\"group\">%s</column>"
+ "<column name=\"creator user\">%s</column>"
+ "<column name=\"creator group\">%s</column>"
+ "<column name=\"last msgsnd() time\">%s</column>"
+ "<column name=\"last msgrcv() time\">%s</column>"
+ "<column name=\"last msgctl() time\">%s</column>"
+ "</item>",
+ key,
+ msqid,
+ perms,
+ cbytes,
+ qnum,
+ lscmd,
+ lrcmd,
+ user,
+ group,
+ cuser,
+ cgroup,
+ stime_str,
+ rtime_str,
+ ctime_str);
+ }
+ }
+ }
+ while (!feof (fp));
+
+ fclose (fp);
+ }
+
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the buffer. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+}
+
+/* Collect data about loaded kernel modules and write it into
+ READBUF. */
+
+static LONGEST
+linux_xfer_osdata_modules (gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len)
+{
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct buffer buffer;
+
+ if (offset == 0)
+ {
+ FILE *fp;
+
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"modules\">\n");
+
+ fp = gdb_fopen_cloexec ("/proc/modules", "r");
+ if (fp)
+ {
+ char buf[8192];
+
+ do
+ {
+ if (fgets (buf, sizeof (buf), fp))
+ {
+ char *name, *dependencies, *status, *tmp;
+ unsigned int size;
+ unsigned long long address;
+ int uses;
+
+ name = strtok (buf, " ");
+ if (name == NULL)
+ continue;
+
+ tmp = strtok (NULL, " ");
+ if (tmp == NULL)
+ continue;
+ if (sscanf (tmp, "%u", &size) != 1)
+ continue;
+
+ tmp = strtok (NULL, " ");
+ if (tmp == NULL)
+ continue;
+ if (sscanf (tmp, "%d", &uses) != 1)
+ continue;
+
+ dependencies = strtok (NULL, " ");
+ if (dependencies == NULL)
+ continue;
+
+ status = strtok (NULL, " ");
+ if (status == NULL)
+ continue;
+
+ tmp = strtok (NULL, "\n");
+ if (tmp == NULL)
+ continue;
+ if (sscanf (tmp, "%llx", &address) != 1)
+ continue;
+
+ buffer_xml_printf (
+ &buffer,
+ "<item>"
+ "<column name=\"name\">%s</column>"
+ "<column name=\"size\">%u</column>"
+ "<column name=\"num uses\">%d</column>"
+ "<column name=\"dependencies\">%s</column>"
+ "<column name=\"status\">%s</column>"
+ "<column name=\"address\">%llx</column>"
+ "</item>",
+ name,
+ size,
+ uses,
+ dependencies,
+ status,
+ address);
+ }
+ }
+ while (!feof (fp));
+
+ fclose (fp);
+ }
+
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the buffer. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+}
+
+struct osdata_type {
+ char *type;
+ char *title;
+ char *description;
+ LONGEST (*getter) (gdb_byte *readbuf, ULONGEST offset, ULONGEST len);
+} osdata_table[] = {
+ { "processes", "Processes", "Listing of all processes",
+ linux_xfer_osdata_processes },
+ { "procgroups", "Process groups", "Listing of all process groups",
+ linux_xfer_osdata_processgroups },
+ { "threads", "Threads", "Listing of all threads",
+ linux_xfer_osdata_threads },
+ { "files", "File descriptors", "Listing of all file descriptors",
+ linux_xfer_osdata_fds },
+ { "sockets", "Sockets", "Listing of all internet-domain sockets",
+ linux_xfer_osdata_isockets },
+ { "shm", "Shared-memory regions", "Listing of all shared-memory regions",
+ linux_xfer_osdata_shm },
+ { "semaphores", "Semaphores", "Listing of all semaphores",
+ linux_xfer_osdata_sem },
+ { "msg", "Message queues", "Listing of all message queues",
+ linux_xfer_osdata_msg },
+ { "modules", "Kernel modules", "Listing of all loaded kernel modules",
+ linux_xfer_osdata_modules },
+ { NULL, NULL, NULL }
+};
+
+LONGEST
+linux_common_xfer_osdata (const char *annex, gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len)
+{
+ if (!annex || *annex == '\0')
+ {
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct buffer buffer;
+
+ if (offset == 0)
+ {
+ int i;
+
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"types\">\n");
+
+ for (i = 0; osdata_table[i].type; ++i)
+ buffer_xml_printf (
+ &buffer,
+ "<item>"
+ "<column name=\"Type\">%s</column>"
+ "<column name=\"Description\">%s</column>"
+ "<column name=\"Title\">%s</column>"
+ "</item>",
+ osdata_table[i].type,
+ osdata_table[i].description,
+ osdata_table[i].title);
+
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the buffer. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+ }
+ else
+ {
+ int i;
+
+ for (i = 0; osdata_table[i].type; ++i)
+ {
+ if (strcmp (annex, osdata_table[i].type) == 0)
+ {
+ gdb_assert (readbuf);
+
+ return (osdata_table[i].getter) (readbuf, offset, len);
+ }
+ }
+
+ return 0;
+ }
+}
+
diff --git a/gdb/nat/linux-osdata.h b/gdb/nat/linux-osdata.h
new file mode 100644
index 0000000..e9d4f3c
--- /dev/null
+++ b/gdb/nat/linux-osdata.h
@@ -0,0 +1,29 @@
+/* Linux-specific functions to retrieve OS data.
+
+ Copyright (C) 2009-2014 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ 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 COMMON_LINUX_OSDATA_H
+#define COMMON_LINUX_OSDATA_H
+
+#include "ptid.h"
+
+extern int linux_common_core_of_thread (ptid_t ptid);
+extern LONGEST linux_common_xfer_osdata (const char *annex, gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len);
+
+#endif
diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c
new file mode 100644
index 0000000..1443a88
--- /dev/null
+++ b/gdb/nat/linux-procfs.c
@@ -0,0 +1,121 @@
+/* Linux-specific PROCFS manipulation routines.
+ Copyright (C) 2009-2014 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ 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/>. */
+
+#ifdef GDBSERVER
+#include "server.h"
+#else
+#include "defs.h"
+#include <string.h>
+#endif
+
+#include "linux-procfs.h"
+#include "filestuff.h"
+
+/* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
+ found. */
+
+static int
+linux_proc_get_int (pid_t lwpid, const char *field)
+{
+ size_t field_len = strlen (field);
+ FILE *status_file;
+ char buf[100];
+ int retval = -1;
+
+ snprintf (buf, sizeof (buf), "/proc/%d/status", (int) lwpid);
+ status_file = gdb_fopen_cloexec (buf, "r");
+ if (status_file == NULL)
+ {
+ warning (_("unable to open /proc file '%s'"), buf);
+ return -1;
+ }
+
+ while (fgets (buf, sizeof (buf), status_file))
+ if (strncmp (buf, field, field_len) == 0 && buf[field_len] == ':')
+ {
+ retval = strtol (&buf[field_len + 1], NULL, 10);
+ break;
+ }
+
+ fclose (status_file);
+ return retval;
+}
+
+/* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
+ found. */
+
+int
+linux_proc_get_tgid (pid_t lwpid)
+{
+ return linux_proc_get_int (lwpid, "Tgid");
+}
+
+/* See linux-procfs.h. */
+
+pid_t
+linux_proc_get_tracerpid (pid_t lwpid)
+{
+ return linux_proc_get_int (lwpid, "TracerPid");
+}
+
+/* Return non-zero if 'State' of /proc/PID/status contains STATE. */
+
+static int
+linux_proc_pid_has_state (pid_t pid, const char *state)
+{
+ char buffer[100];
+ FILE *procfile;
+ int retval;
+ int have_state;
+
+ xsnprintf (buffer, sizeof (buffer), "/proc/%d/status", (int) pid);
+ procfile = gdb_fopen_cloexec (buffer, "r");
+ if (procfile == NULL)
+ {
+ warning (_("unable to open /proc file '%s'"), buffer);
+ return 0;
+ }
+
+ have_state = 0;
+ while (fgets (buffer, sizeof (buffer), procfile) != NULL)
+ if (strncmp (buffer, "State:", 6) == 0)
+ {
+ have_state = 1;
+ break;
+ }
+ retval = (have_state && strstr (buffer, state) != NULL);
+ fclose (procfile);
+ return retval;
+}
+
+/* Detect `T (stopped)' in `/proc/PID/status'.
+ Other states including `T (tracing stop)' are reported as false. */
+
+int
+linux_proc_pid_is_stopped (pid_t pid)
+{
+ return linux_proc_pid_has_state (pid, "T (stopped)");
+}
+
+/* See linux-procfs.h declaration. */
+
+int
+linux_proc_pid_is_zombie (pid_t pid)
+{
+ return linux_proc_pid_has_state (pid, "Z (zombie)");
+}
diff --git a/gdb/nat/linux-procfs.h b/gdb/nat/linux-procfs.h
new file mode 100644
index 0000000..d13fff7
--- /dev/null
+++ b/gdb/nat/linux-procfs.h
@@ -0,0 +1,43 @@
+/* Linux-specific PROCFS manipulation routines.
+ Copyright (C) 2011-2014 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ 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 COMMON_LINUX_PROCFS_H
+#define COMMON_LINUX_PROCFS_H
+
+#include <unistd.h>
+
+/* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
+ found. */
+
+extern int linux_proc_get_tgid (pid_t lwpid);
+
+/* Return the TracerPid of LWPID from /proc/pid/status. Returns -1 if not
+ found. */
+
+extern pid_t linux_proc_get_tracerpid (pid_t lwpid);
+
+/* Detect `T (stopped)' in `/proc/PID/status'.
+ Other states including `T (tracing stop)' are reported as false. */
+
+extern int linux_proc_pid_is_stopped (pid_t pid);
+
+/* Return non-zero if PID is a zombie. */
+
+extern int linux_proc_pid_is_zombie (pid_t pid);
+
+#endif /* COMMON_LINUX_PROCFS_H */
diff --git a/gdb/nat/linux-ptrace.c b/gdb/nat/linux-ptrace.c
new file mode 100644
index 0000000..3ad2113
--- /dev/null
+++ b/gdb/nat/linux-ptrace.c
@@ -0,0 +1,553 @@
+/* Linux-specific ptrace manipulation routines.
+ Copyright (C) 2012-2014 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ 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/>. */
+
+#ifdef GDBSERVER
+#include "server.h"
+#else
+#include "defs.h"
+#include <string.h>
+#endif
+
+#include "linux-ptrace.h"
+#include "linux-procfs.h"
+#include "linux-waitpid.h"
+#include "buffer.h"
+#include "gdb_assert.h"
+#include "gdb_wait.h"
+
+#include <stdint.h>
+
+/* Stores the currently supported ptrace options. A value of
+ -1 means we did not check for features yet. A value of 0 means
+ there are no supported features. */
+static int current_ptrace_options = -1;
+
+/* Find all possible reasons we could fail to attach PID and append
+ these as strings to the already initialized BUFFER. '\0'
+ termination of BUFFER must be done by the caller. */
+
+void
+linux_ptrace_attach_fail_reason (pid_t pid, struct buffer *buffer)
+{
+ pid_t tracerpid;
+
+ tracerpid = linux_proc_get_tracerpid (pid);
+ if (tracerpid > 0)
+ buffer_xml_printf (buffer, _("process %d is already traced "
+ "by process %d"),
+ (int) pid, (int) tracerpid);
+
+ if (linux_proc_pid_is_zombie (pid))
+ buffer_xml_printf (buffer, _("process %d is a zombie "
+ "- the process has already terminated"),
+ (int) pid);
+}
+
+#if defined __i386__ || defined __x86_64__
+
+/* Address of the 'ret' instruction in asm code block below. */
+extern void (linux_ptrace_test_ret_to_nx_instr) (void);
+
+#include <sys/reg.h>
+#include <sys/mman.h>
+#include <signal.h>
+
+#endif /* defined __i386__ || defined __x86_64__ */
+
+/* Test broken off-trunk Linux kernel patchset for NX support on i386. It was
+ removed in Fedora kernel 88fa1f0332d188795ed73d7ac2b1564e11a0b4cd.
+
+ Test also x86_64 arch for PaX support. */
+
+static void
+linux_ptrace_test_ret_to_nx (void)
+{
+#if defined __i386__ || defined __x86_64__
+ pid_t child, got_pid;
+ gdb_byte *return_address, *pc;
+ long l;
+ int status, kill_status;
+
+ return_address = mmap (NULL, 2, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (return_address == MAP_FAILED)
+ {
+ warning (_("linux_ptrace_test_ret_to_nx: Cannot mmap: %s"),
+ strerror (errno));
+ return;
+ }
+
+ /* Put there 'int3'. */
+ *return_address = 0xcc;
+
+ child = fork ();
+ switch (child)
+ {
+ case -1:
+ warning (_("linux_ptrace_test_ret_to_nx: Cannot fork: %s"),
+ strerror (errno));
+ return;
+
+ case 0:
+ l = ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) NULL,
+ (PTRACE_TYPE_ARG4) NULL);
+ if (l != 0)
+ warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME: %s"),
+ strerror (errno));
+ else
+ {
+#if defined __i386__
+ asm volatile ("pushl %0;"
+ ".globl linux_ptrace_test_ret_to_nx_instr;"
+ "linux_ptrace_test_ret_to_nx_instr:"
+ "ret"
+ : : "r" (return_address) : "%esp", "memory");
+#elif defined __x86_64__
+ asm volatile ("pushq %0;"
+ ".globl linux_ptrace_test_ret_to_nx_instr;"
+ "linux_ptrace_test_ret_to_nx_instr:"
+ "ret"
+ : : "r" ((uint64_t) (uintptr_t) return_address)
+ : "%rsp", "memory");
+#else
+# error "!__i386__ && !__x86_64__"
+#endif
+ gdb_assert_not_reached ("asm block did not terminate");
+ }
+
+ _exit (1);
+ }
+
+ errno = 0;
+ got_pid = waitpid (child, &status, 0);
+ if (got_pid != child)
+ {
+ warning (_("linux_ptrace_test_ret_to_nx: waitpid returned %ld: %s"),
+ (long) got_pid, strerror (errno));
+ return;
+ }
+
+ if (WIFSIGNALED (status))
+ {
+ if (WTERMSIG (status) != SIGKILL)
+ warning (_("linux_ptrace_test_ret_to_nx: WTERMSIG %d is not SIGKILL!"),
+ (int) WTERMSIG (status));
+ else
+ warning (_("Cannot call inferior functions, Linux kernel PaX "
+ "protection forbids return to non-executable pages!"));
+ return;
+ }
+
+ if (!WIFSTOPPED (status))
+ {
+ warning (_("linux_ptrace_test_ret_to_nx: status %d is not WIFSTOPPED!"),
+ status);
+ return;
+ }
+
+ /* We may get SIGSEGV due to missing PROT_EXEC of the return_address. */
+ if (WSTOPSIG (status) != SIGTRAP && WSTOPSIG (status) != SIGSEGV)
+ {
+ warning (_("linux_ptrace_test_ret_to_nx: "
+ "WSTOPSIG %d is neither SIGTRAP nor SIGSEGV!"),
+ (int) WSTOPSIG (status));
+ return;
+ }
+
+ errno = 0;
+#if defined __i386__
+ l = ptrace (PTRACE_PEEKUSER, child, (PTRACE_TYPE_ARG3) (uintptr_t) (EIP * 4),
+ (PTRACE_TYPE_ARG4) NULL);
+#elif defined __x86_64__
+ l = ptrace (PTRACE_PEEKUSER, child, (PTRACE_TYPE_ARG3) (uintptr_t) (RIP * 8),
+ (PTRACE_TYPE_ARG4) NULL);
+#else
+# error "!__i386__ && !__x86_64__"
+#endif
+ if (errno != 0)
+ {
+ warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_PEEKUSER: %s"),
+ strerror (errno));
+ return;
+ }
+ pc = (void *) (uintptr_t) l;
+
+ kill (child, SIGKILL);
+ ptrace (PTRACE_KILL, child, (PTRACE_TYPE_ARG3) NULL,
+ (PTRACE_TYPE_ARG4) NULL);
+
+ errno = 0;
+ got_pid = waitpid (child, &kill_status, 0);
+ if (got_pid != child)
+ {
+ warning (_("linux_ptrace_test_ret_to_nx: "
+ "PTRACE_KILL waitpid returned %ld: %s"),
+ (long) got_pid, strerror (errno));
+ return;
+ }
+ if (!WIFSIGNALED (kill_status))
+ {
+ warning (_("linux_ptrace_test_ret_to_nx: "
+ "PTRACE_KILL status %d is not WIFSIGNALED!"),
+ status);
+ return;
+ }
+
+ /* + 1 is there as x86* stops after the 'int3' instruction. */
+ if (WSTOPSIG (status) == SIGTRAP && pc == return_address + 1)
+ {
+ /* PASS */
+ return;
+ }
+
+ /* We may get SIGSEGV due to missing PROT_EXEC of the RETURN_ADDRESS page. */
+ if (WSTOPSIG (status) == SIGSEGV && pc == return_address)
+ {
+ /* PASS */
+ return;
+ }
+
+ if ((void (*) (void)) pc != &linux_ptrace_test_ret_to_nx_instr)
+ warning (_("linux_ptrace_test_ret_to_nx: PC %p is neither near return "
+ "address %p nor is the return instruction %p!"),
+ pc, return_address, &linux_ptrace_test_ret_to_nx_instr);
+ else
+ warning (_("Cannot call inferior functions on this system - "
+ "Linux kernel with broken i386 NX (non-executable pages) "
+ "support detected!"));
+#endif /* defined __i386__ || defined __x86_64__ */
+}
+
+/* Helper function to fork a process and make the child process call
+ the function FUNCTION, passing CHILD_STACK as parameter.
+
+ For MMU-less targets, clone is used instead of fork, and
+ CHILD_STACK is used as stack space for the cloned child. If NULL,
+ stack space is allocated via malloc (and subsequently passed to
+ FUNCTION). For MMU targets, CHILD_STACK is ignored. */
+
+static int
+linux_fork_to_function (gdb_byte *child_stack, void (*function) (gdb_byte *))
+{
+ int child_pid;
+
+ /* Sanity check the function pointer. */
+ gdb_assert (function != NULL);
+
+#if defined(__UCLIBC__) && defined(HAS_NOMMU)
+#define STACK_SIZE 4096
+
+ if (child_stack == NULL)
+ child_stack = xmalloc (STACK_SIZE * 4);
+
+ /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
+#ifdef __ia64__
+ child_pid = __clone2 (function, child_stack, STACK_SIZE,
+ CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2);
+#else /* !__ia64__ */
+ child_pid = clone (function, child_stack + STACK_SIZE,
+ CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2);
+#endif /* !__ia64__ */
+#else /* !defined(__UCLIBC) && defined(HAS_NOMMU) */
+ child_pid = fork ();
+
+ if (child_pid == 0)
+ function (NULL);
+#endif /* defined(__UCLIBC) && defined(HAS_NOMMU) */
+
+ if (child_pid == -1)
+ perror_with_name (("fork"));
+
+ return child_pid;
+}
+
+/* A helper function for linux_check_ptrace_features, called after
+ the child forks a grandchild. */
+
+static void
+linux_grandchild_function (gdb_byte *child_stack)
+{
+ /* Free any allocated stack. */
+ xfree (child_stack);
+
+ /* This code is only reacheable by the grandchild (child's child)
+ process. */
+ _exit (0);
+}
+
+/* A helper function for linux_check_ptrace_features, called after
+ the parent process forks a child. The child allows itself to
+ be traced by its parent. */
+
+static void
+linux_child_function (gdb_byte *child_stack)
+{
+ ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
+ kill (getpid (), SIGSTOP);
+
+ /* Fork a grandchild. */
+ linux_fork_to_function (child_stack, linux_grandchild_function);
+
+ /* This code is only reacheable by the child (grandchild's parent)
+ process. */
+ _exit (0);
+}
+
+static void linux_test_for_tracesysgood (int child_pid);
+static void linux_test_for_tracefork (int child_pid);
+
+/* Determine ptrace features available on this target. */
+
+static void
+linux_check_ptrace_features (void)
+{
+ int child_pid, ret, status;
+
+ /* Initialize the options. */
+ current_ptrace_options = 0;
+
+ /* Fork a child so we can do some testing. The child will call
+ linux_child_function and will get traced. The child will
+ eventually fork a grandchild so we can test fork event
+ reporting. */
+ child_pid = linux_fork_to_function (NULL, linux_child_function);
+
+ ret = my_waitpid (child_pid, &status, 0);
+ if (ret == -1)
+ perror_with_name (("waitpid"));
+ else if (ret != child_pid)
+ error (_("linux_check_ptrace_features: waitpid: unexpected result %d."),
+ ret);
+ if (! WIFSTOPPED (status))
+ error (_("linux_check_ptrace_features: waitpid: unexpected status %d."),
+ status);
+
+ linux_test_for_tracesysgood (child_pid);
+
+ linux_test_for_tracefork (child_pid);
+
+ /* Clean things up and kill any pending children. */
+ do
+ {
+ ret = ptrace (PTRACE_KILL, child_pid, (PTRACE_TYPE_ARG3) 0,
+ (PTRACE_TYPE_ARG4) 0);
+ if (ret != 0)
+ warning (_("linux_check_ptrace_features: failed to kill child"));
+ my_waitpid (child_pid, &status, 0);
+ }
+ while (WIFSTOPPED (status));
+}
+
+/* Determine if PTRACE_O_TRACESYSGOOD can be used to catch
+ syscalls. */
+
+static void
+linux_test_for_tracesysgood (int child_pid)
+{
+#ifdef GDBSERVER
+ /* gdbserver does not support PTRACE_O_TRACESYSGOOD. */
+#else
+ int ret;
+
+ ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
+ (PTRACE_TYPE_ARG4) PTRACE_O_TRACESYSGOOD);
+ if (ret == 0)
+ current_ptrace_options |= PTRACE_O_TRACESYSGOOD;
+#endif
+}
+
+/* Determine if PTRACE_O_TRACEFORK can be used to follow fork
+ events. */
+
+static void
+linux_test_for_tracefork (int child_pid)
+{
+ int ret, status;
+ long second_pid;
+
+ /* First, set the PTRACE_O_TRACEFORK option. If this fails, we
+ know for sure that it is not supported. */
+ ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
+ (PTRACE_TYPE_ARG4) PTRACE_O_TRACEFORK);
+
+ if (ret != 0)
+ return;
+
+#ifdef GDBSERVER
+ /* gdbserver does not support PTRACE_O_TRACEVFORKDONE yet. */
+#else
+ /* Check if the target supports PTRACE_O_TRACEVFORKDONE. */
+ ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
+ (PTRACE_TYPE_ARG4) (PTRACE_O_TRACEFORK
+ | PTRACE_O_TRACEVFORKDONE));
+ if (ret == 0)
+ current_ptrace_options |= PTRACE_O_TRACEVFORKDONE;
+#endif
+
+ /* Setting PTRACE_O_TRACEFORK did not cause an error, however we
+ don't know for sure that the feature is available; old
+ versions of PTRACE_SETOPTIONS ignored unknown options.
+ Therefore, we attach to the child process, use PTRACE_SETOPTIONS
+ to enable fork tracing, and let it fork. If the process exits,
+ we assume that we can't use PTRACE_O_TRACEFORK; if we get the
+ fork notification, and we can extract the new child's PID, then
+ we assume that we can.
+
+ We do not explicitly check for vfork tracing here. It is
+ assumed that vfork tracing is available whenever fork tracing
+ is available. */
+ ret = ptrace (PTRACE_CONT, child_pid, (PTRACE_TYPE_ARG3) 0,
+ (PTRACE_TYPE_ARG4) 0);
+ if (ret != 0)
+ warning (_("linux_test_for_tracefork: failed to resume child"));
+
+ ret = my_waitpid (child_pid, &status, 0);
+
+ /* Check if we received a fork event notification. */
+ if (ret == child_pid && WIFSTOPPED (status)
+ && status >> 16 == PTRACE_EVENT_FORK)
+ {
+ /* We did receive a fork event notification. Make sure its PID
+ is reported. */
+ second_pid = 0;
+ ret = ptrace (PTRACE_GETEVENTMSG, child_pid, (PTRACE_TYPE_ARG3) 0,
+ (PTRACE_TYPE_ARG4) &second_pid);
+ if (ret == 0 && second_pid != 0)
+ {
+ int second_status;
+
+ /* We got the PID from the grandchild, which means fork
+ tracing is supported. */
+#ifdef GDBSERVER
+ /* Do not enable all the options for now since gdbserver does not
+ properly support them. This restriction will be lifted when
+ gdbserver is augmented to support them. */
+ current_ptrace_options |= PTRACE_O_TRACECLONE;
+#else
+ current_ptrace_options |= PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK
+ | PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXEC;
+
+ /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to
+ support read-only process state. */
+#endif
+
+ /* Do some cleanup and kill the grandchild. */
+ my_waitpid (second_pid, &second_status, 0);
+ ret = ptrace (PTRACE_KILL, second_pid, (PTRACE_TYPE_ARG3) 0,
+ (PTRACE_TYPE_ARG4) 0);
+ if (ret != 0)
+ warning (_("linux_test_for_tracefork: "
+ "failed to kill second child"));
+ my_waitpid (second_pid, &status, 0);
+ }
+ }
+ else
+ warning (_("linux_test_for_tracefork: unexpected result from waitpid "
+ "(%d, status 0x%x)"), ret, status);
+}
+
+/* Enable reporting of all currently supported ptrace events. */
+
+void
+linux_enable_event_reporting (pid_t pid)
+{
+ /* Check if we have initialized the ptrace features for this
+ target. If not, do it now. */
+ if (current_ptrace_options == -1)
+ linux_check_ptrace_features ();
+
+ /* Set the options. */
+ ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0,
+ (PTRACE_TYPE_ARG4) (uintptr_t) current_ptrace_options);
+}
+
+/* Disable reporting of all currently supported ptrace events. */
+
+void
+linux_disable_event_reporting (pid_t pid)
+{
+ /* Set the options. */
+ ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0, 0);
+}
+
+/* Returns non-zero if PTRACE_OPTIONS is contained within
+ CURRENT_PTRACE_OPTIONS, therefore supported. Returns 0
+ otherwise. */
+
+static int
+ptrace_supports_feature (int ptrace_options)
+{
+ gdb_assert (current_ptrace_options >= 0);
+
+ return ((current_ptrace_options & ptrace_options) == ptrace_options);
+}
+
+/* Returns non-zero if PTRACE_EVENT_FORK is supported by ptrace,
+ 0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is
+ PTRACE_EVENT_CLONE, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
+ since they were all added to the kernel at the same time. */
+
+int
+linux_supports_tracefork (void)
+{
+ return ptrace_supports_feature (PTRACE_O_TRACEFORK);
+}
+
+/* Returns non-zero if PTRACE_EVENT_CLONE is supported by ptrace,
+ 0 otherwise. Note that if PTRACE_EVENT_CLONE is supported so is
+ PTRACE_EVENT_FORK, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
+ since they were all added to the kernel at the same time. */
+
+int
+linux_supports_traceclone (void)
+{
+ return ptrace_supports_feature (PTRACE_O_TRACECLONE);
+}
+
+/* Returns non-zero if PTRACE_O_TRACEVFORKDONE is supported by
+ ptrace, 0 otherwise. */
+
+int
+linux_supports_tracevforkdone (void)
+{
+ return ptrace_supports_feature (PTRACE_O_TRACEVFORKDONE);
+}
+
+/* Returns non-zero if PTRACE_O_TRACESYSGOOD is supported by ptrace,
+ 0 otherwise. */
+
+int
+linux_supports_tracesysgood (void)
+{
+ return ptrace_supports_feature (PTRACE_O_TRACESYSGOOD);
+}
+
+/* Display possible problems on this system. Display them only once per GDB
+ execution. */
+
+void
+linux_ptrace_init_warnings (void)
+{
+ static int warned = 0;
+
+ if (warned)
+ return;
+ warned = 1;
+
+ linux_ptrace_test_ret_to_nx ();
+}
diff --git a/gdb/nat/linux-ptrace.h b/gdb/nat/linux-ptrace.h
new file mode 100644
index 0000000..cffb5ce
--- /dev/null
+++ b/gdb/nat/linux-ptrace.h
@@ -0,0 +1,95 @@
+/* Copyright (C) 2011-2014 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ 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 COMMON_LINUX_PTRACE_H
+#define COMMON_LINUX_PTRACE_H
+
+struct buffer;
+
+#include <sys/ptrace.h>
+
+#ifdef __UCLIBC__
+#if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
+/* PTRACE_TEXT_ADDR and friends. */
+#include <asm/ptrace.h>
+#define HAS_NOMMU
+#endif
+#endif
+
+#if !defined(PTRACE_TYPE_ARG3)
+#define PTRACE_TYPE_ARG3 void *
+#endif
+
+#if !defined(PTRACE_TYPE_ARG4)
+#define PTRACE_TYPE_ARG4 void *
+#endif
+
+#ifndef PTRACE_GETSIGINFO
+# define PTRACE_GETSIGINFO 0x4202
+# define PTRACE_SETSIGINFO 0x4203
+#endif /* PTRACE_GETSIGINF */
+
+/* If the system headers did not provide the constants, hard-code the normal
+ values. */
+#ifndef PTRACE_EVENT_FORK
+
+#define PTRACE_SETOPTIONS 0x4200
+#define PTRACE_GETEVENTMSG 0x4201
+
+/* options set using PTRACE_SETOPTIONS */
+#define PTRACE_O_TRACESYSGOOD 0x00000001
+#define PTRACE_O_TRACEFORK 0x00000002
+#define PTRACE_O_TRACEVFORK 0x00000004
+#define PTRACE_O_TRACECLONE 0x00000008
+#define PTRACE_O_TRACEEXEC 0x00000010
+#define PTRACE_O_TRACEVFORKDONE 0x00000020
+#define PTRACE_O_TRACEEXIT 0x00000040
+
+/* Wait extended result codes for the above trace options. */
+#define PTRACE_EVENT_FORK 1
+#define PTRACE_EVENT_VFORK 2
+#define PTRACE_EVENT_CLONE 3
+#define PTRACE_EVENT_EXEC 4
+#define PTRACE_EVENT_VFORK_DONE 5
+#define PTRACE_EVENT_EXIT 6
+
+#endif /* PTRACE_EVENT_FORK */
+
+#if (defined __bfin__ || defined __frv__ || defined __sh__) \
+ && !defined PTRACE_GETFDPIC
+#define PTRACE_GETFDPIC 31
+#define PTRACE_GETFDPIC_EXEC 0
+#define PTRACE_GETFDPIC_INTERP 1
+#endif
+
+/* We can't always assume that this flag is available, but all systems
+ with the ptrace event handlers also have __WALL, so it's safe to use
+ in some contexts. */
+#ifndef __WALL
+#define __WALL 0x40000000 /* Wait for any child. */
+#endif
+
+extern void linux_ptrace_attach_fail_reason (pid_t pid, struct buffer *buffer);
+extern void linux_ptrace_init_warnings (void);
+extern void linux_enable_event_reporting (pid_t pid);
+extern void linux_disable_event_reporting (pid_t pid);
+extern int linux_supports_tracefork (void);
+extern int linux_supports_traceclone (void);
+extern int linux_supports_tracevforkdone (void);
+extern int linux_supports_tracesysgood (void);
+
+#endif /* COMMON_LINUX_PTRACE_H */
diff --git a/gdb/nat/linux-waitpid.c b/gdb/nat/linux-waitpid.c
index 4693120..5159f03 100644
--- a/gdb/nat/linux-waitpid.c
+++ b/gdb/nat/linux-waitpid.c
@@ -24,8 +24,8 @@
#include "signal.h"
#endif
-#include "nat/linux-nat.h"
-#include "nat/linux-waitpid.h"
+#include "linux-nat.h"
+#include "linux-waitpid.h"
#include "gdb_wait.h"
#include <string.h>
diff --git a/gdb/nat/mips-linux-watch.c b/gdb/nat/mips-linux-watch.c
new file mode 100644
index 0000000..acfc7f4
--- /dev/null
+++ b/gdb/nat/mips-linux-watch.c
@@ -0,0 +1,349 @@
+/* Copyright (C) 2009-2014 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ 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/>. */
+
+#include <sys/ptrace.h>
+#include "mips-linux-watch.h"
+#include "gdb_assert.h"
+
+/* Assuming usable watch registers REGS, return the irw_mask of
+ register N. */
+
+uint32_t
+mips_linux_watch_get_irw_mask (struct pt_watch_regs *regs, int n)
+{
+ switch (regs->style)
+ {
+ case pt_watch_style_mips32:
+ return regs->mips32.watch_masks[n] & IRW_MASK;
+ case pt_watch_style_mips64:
+ return regs->mips64.watch_masks[n] & IRW_MASK;
+ default:
+ internal_error (__FILE__, __LINE__,
+ _("Unrecognized watch register style"));
+ }
+}
+
+/* Assuming usable watch registers REGS, return the reg_mask of
+ register N. */
+
+static uint32_t
+get_reg_mask (struct pt_watch_regs *regs, int n)
+{
+ switch (regs->style)
+ {
+ case pt_watch_style_mips32:
+ return regs->mips32.watch_masks[n] & ~IRW_MASK;
+ case pt_watch_style_mips64:
+ return regs->mips64.watch_masks[n] & ~IRW_MASK;
+ default:
+ internal_error (__FILE__, __LINE__,
+ _("Unrecognized watch register style"));
+ }
+}
+
+/* Assuming usable watch registers REGS, return the num_valid. */
+
+uint32_t
+mips_linux_watch_get_num_valid (struct pt_watch_regs *regs)
+{
+ switch (regs->style)
+ {
+ case pt_watch_style_mips32:
+ return regs->mips32.num_valid;
+ case pt_watch_style_mips64:
+ return regs->mips64.num_valid;
+ default:
+ internal_error (__FILE__, __LINE__,
+ _("Unrecognized watch register style"));
+ }
+}
+
+/* Assuming usable watch registers REGS, return the watchlo of
+ register N. */
+
+CORE_ADDR
+mips_linux_watch_get_watchlo (struct pt_watch_regs *regs, int n)
+{
+ switch (regs->style)
+ {
+ case pt_watch_style_mips32:
+ return regs->mips32.watchlo[n];
+ case pt_watch_style_mips64:
+ return regs->mips64.watchlo[n];
+ default:
+ internal_error (__FILE__, __LINE__,
+ _("Unrecognized watch register style"));
+ }
+}
+
+/* Assuming usable watch registers REGS, set watchlo of register N to
+ VALUE. */
+
+void
+mips_linux_watch_set_watchlo (struct pt_watch_regs *regs, int n,
+ CORE_ADDR value)
+{
+ switch (regs->style)
+ {
+ case pt_watch_style_mips32:
+ /* The cast will never throw away bits as 64 bit addresses can
+ never be used on a 32 bit kernel. */
+ regs->mips32.watchlo[n] = (uint32_t) value;
+ break;
+ case pt_watch_style_mips64:
+ regs->mips64.watchlo[n] = value;
+ break;
+ default:
+ internal_error (__FILE__, __LINE__,
+ _("Unrecognized watch register style"));
+ }
+}
+
+/* Assuming usable watch registers REGS, return the watchhi of
+ register N. */
+
+uint32_t
+mips_linux_watch_get_watchhi (struct pt_watch_regs *regs, int n)
+{
+ switch (regs->style)
+ {
+ case pt_watch_style_mips32:
+ return regs->mips32.watchhi[n];
+ case pt_watch_style_mips64:
+ return regs->mips64.watchhi[n];
+ default:
+ internal_error (__FILE__, __LINE__,
+ _("Unrecognized watch register style"));
+ }
+}
+
+/* Assuming usable watch registers REGS, set watchhi of register N to
+ VALUE. */
+
+void
+mips_linux_watch_set_watchhi (struct pt_watch_regs *regs, int n,
+ uint16_t value)
+{
+ switch (regs->style)
+ {
+ case pt_watch_style_mips32:
+ regs->mips32.watchhi[n] = value;
+ break;
+ case pt_watch_style_mips64:
+ regs->mips64.watchhi[n] = value;
+ break;
+ default:
+ internal_error (__FILE__, __LINE__,
+ _("Unrecognized watch register style"));
+ }
+}
+
+/* Read the watch registers of process LWPID and store it in
+ WATCH_READBACK. Save true to *WATCH_READBACK_VALID if watch
+ registers are valid. Return 1 if watch registers are usable.
+ Cached information is used unless FORCE is true. */
+
+int
+mips_linux_read_watch_registers (long lwpid,
+ struct pt_watch_regs *watch_readback,
+ int *watch_readback_valid, int force)
+{
+ if (force || *watch_readback_valid == 0)
+ {
+ if (ptrace (PTRACE_GET_WATCH_REGS, lwpid, watch_readback) == -1)
+ {
+ *watch_readback_valid = -1;
+ return 0;
+ }
+ switch (watch_readback->style)
+ {
+ case pt_watch_style_mips32:
+ if (watch_readback->mips32.num_valid == 0)
+ {
+ *watch_readback_valid = -1;
+ return 0;
+ }
+ break;
+ case pt_watch_style_mips64:
+ if (watch_readback->mips64.num_valid == 0)
+ {
+ *watch_readback_valid = -1;
+ return 0;
+ }
+ break;
+ default:
+ *watch_readback_valid = -1;
+ return 0;
+ }
+ /* Watch registers appear to be usable. */
+ *watch_readback_valid = 1;
+ }
+ return (*watch_readback_valid == 1) ? 1 : 0;
+}
+
+/* Convert GDB's TYPE to an IRW mask. */
+
+uint32_t
+mips_linux_watch_type_to_irw (int type)
+{
+ switch (type)
+ {
+ case hw_write:
+ return W_MASK;
+ case hw_read:
+ return R_MASK;
+ case hw_access:
+ return (W_MASK | R_MASK);
+ default:
+ return 0;
+ }
+}
+
+/* Set any low order bits in MASK that are not set. */
+
+static CORE_ADDR
+fill_mask (CORE_ADDR mask)
+{
+ CORE_ADDR f = 1;
+
+ while (f && f < mask)
+ {
+ mask |= f;
+ f <<= 1;
+ }
+ return mask;
+}
+
+/* Try to add a single watch to the specified registers REGS. The
+ address of added watch is ADDR, the length is LEN, and the mask
+ is IRW. Return 1 on success, 0 on failure. */
+
+int
+mips_linux_watch_try_one_watch (struct pt_watch_regs *regs,
+ CORE_ADDR addr, int len, uint32_t irw)
+{
+ CORE_ADDR base_addr, last_byte, break_addr, segment_len;
+ CORE_ADDR mask_bits, t_low;
+ uint16_t t_hi;
+ int i, free_watches;
+ struct pt_watch_regs regs_copy;
+
+ if (len <= 0)
+ return 0;
+
+ last_byte = addr + len - 1;
+ mask_bits = fill_mask (addr ^ last_byte) | IRW_MASK;
+ base_addr = addr & ~mask_bits;
+
+ /* Check to see if it is covered by current registers. */
+ for (i = 0; i < mips_linux_watch_get_num_valid (regs); i++)
+ {
+ t_low = mips_linux_watch_get_watchlo (regs, i);
+ if (t_low != 0 && irw == ((uint32_t) t_low & irw))
+ {
+ t_hi = mips_linux_watch_get_watchhi (regs, i) | IRW_MASK;
+ t_low &= ~(CORE_ADDR) t_hi;
+ if (addr >= t_low && last_byte <= (t_low + t_hi))
+ return 1;
+ }
+ }
+ /* Try to find an empty register. */
+ free_watches = 0;
+ for (i = 0; i < mips_linux_watch_get_num_valid (regs); i++)
+ {
+ t_low = mips_linux_watch_get_watchlo (regs, i);
+ if (t_low == 0
+ && irw == (mips_linux_watch_get_irw_mask (regs, i) & irw))
+ {
+ if (mask_bits <= (get_reg_mask (regs, i) | IRW_MASK))
+ {
+ /* It fits, we'll take it. */
+ mips_linux_watch_set_watchlo (regs, i, base_addr | irw);
+ mips_linux_watch_set_watchhi (regs, i, mask_bits & ~IRW_MASK);
+ return 1;
+ }
+ else
+ {
+ /* It doesn't fit, but has the proper IRW capabilities. */
+ free_watches++;
+ }
+ }
+ }
+ if (free_watches > 1)
+ {
+ /* Try to split it across several registers. */
+ regs_copy = *regs;
+ for (i = 0; i < mips_linux_watch_get_num_valid (&regs_copy); i++)
+ {
+ t_low = mips_linux_watch_get_watchlo (&regs_copy, i);
+ t_hi = get_reg_mask (&regs_copy, i) | IRW_MASK;
+ if (t_low == 0 && irw == (t_hi & irw))
+ {
+ t_low = addr & ~(CORE_ADDR) t_hi;
+ break_addr = t_low + t_hi + 1;
+ if (break_addr >= addr + len)
+ segment_len = len;
+ else
+ segment_len = break_addr - addr;
+ mask_bits = fill_mask (addr ^ (addr + segment_len - 1));
+ mips_linux_watch_set_watchlo (&regs_copy, i,
+ (addr & ~mask_bits) | irw);
+ mips_linux_watch_set_watchhi (&regs_copy, i,
+ mask_bits & ~IRW_MASK);
+ if (break_addr >= addr + len)
+ {
+ *regs = regs_copy;
+ return 1;
+ }
+ len = addr + len - break_addr;
+ addr = break_addr;
+ }
+ }
+ }
+ /* It didn't fit anywhere, we failed. */
+ return 0;
+}
+
+/* Fill in the watch registers REGS with the currently cached
+ watches CURRENT_WATCHES. */
+
+void
+mips_linux_watch_populate_regs (struct mips_watchpoint *current_watches,
+ struct pt_watch_regs *regs)
+{
+ struct mips_watchpoint *w;
+ int i;
+
+ /* Clear them out. */
+ for (i = 0; i < mips_linux_watch_get_num_valid (regs); i++)
+ {
+ mips_linux_watch_set_watchlo (regs, i, 0);
+ mips_linux_watch_set_watchhi (regs, i, 0);
+ }
+
+ w = current_watches;
+ while (w)
+ {
+ uint32_t irw = mips_linux_watch_type_to_irw (w->type);
+
+ i = mips_linux_watch_try_one_watch (regs, w->addr, w->len, irw);
+ /* They must all fit, because we previously calculated that they
+ would. */
+ gdb_assert (i);
+ w = w->next;
+ }
+}
diff --git a/gdb/nat/mips-linux-watch.h b/gdb/nat/mips-linux-watch.h
new file mode 100644
index 0000000..c9f6932
--- /dev/null
+++ b/gdb/nat/mips-linux-watch.h
@@ -0,0 +1,126 @@
+/* Copyright (C) 2009-2014 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ 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 MIPS_LINUX_WATCH_H
+#define MIPS_LINUX_WATCH_H 1
+
+#ifdef GDBSERVER
+#include "server.h"
+#else
+#include "defs.h"
+#endif
+
+#include <asm/ptrace.h>
+#include <stdint.h>
+
+#include "break-common.h"
+
+#define MAX_DEBUG_REGISTER 8
+
+/* If macro PTRACE_GET_WATCH_REGS is not defined, kernel header doesn't
+ have hardware watchpoint-related structures. Define them below. */
+
+#ifndef PTRACE_GET_WATCH_REGS
+# define PTRACE_GET_WATCH_REGS 0xd0
+# define PTRACE_SET_WATCH_REGS 0xd1
+
+enum pt_watch_style {
+ pt_watch_style_mips32,
+ pt_watch_style_mips64
+};
+
+/* A value of zero in a watchlo indicates that it is available. */
+
+struct mips32_watch_regs
+{
+ uint32_t watchlo[MAX_DEBUG_REGISTER];
+ /* Lower 16 bits of watchhi. */
+ uint16_t watchhi[MAX_DEBUG_REGISTER];
+ /* Valid mask and I R W bits.
+ * bit 0 -- 1 if W bit is usable.
+ * bit 1 -- 1 if R bit is usable.
+ * bit 2 -- 1 if I bit is usable.
+ * bits 3 - 11 -- Valid watchhi mask bits.
+ */
+ uint16_t watch_masks[MAX_DEBUG_REGISTER];
+ /* The number of valid watch register pairs. */
+ uint32_t num_valid;
+ /* There is confusion across gcc versions about structure alignment,
+ so we force 8 byte alignment for these structures so they match
+ the kernel even if it was build with a different gcc version. */
+} __attribute__ ((aligned (8)));
+
+struct mips64_watch_regs
+{
+ uint64_t watchlo[MAX_DEBUG_REGISTER];
+ uint16_t watchhi[MAX_DEBUG_REGISTER];
+ uint16_t watch_masks[MAX_DEBUG_REGISTER];
+ uint32_t num_valid;
+} __attribute__ ((aligned (8)));
+
+struct pt_watch_regs
+{
+ enum pt_watch_style style;
+ union
+ {
+ struct mips32_watch_regs mips32;
+ struct mips64_watch_regs mips64;
+ };
+};
+
+#endif /* !PTRACE_GET_WATCH_REGS */
+
+#define W_BIT 0
+#define R_BIT 1
+#define I_BIT 2
+
+#define W_MASK (1 << W_BIT)
+#define R_MASK (1 << R_BIT)
+#define I_MASK (1 << I_BIT)
+
+#define IRW_MASK (I_MASK | R_MASK | W_MASK)
+
+/* We keep list of all watchpoints we should install and calculate the
+ watch register values each time the list changes. This allows for
+ easy sharing of watch registers for more than one watchpoint. */
+
+struct mips_watchpoint
+{
+ CORE_ADDR addr;
+ int len;
+ int type;
+ struct mips_watchpoint *next;
+};
+
+uint32_t mips_linux_watch_get_num_valid (struct pt_watch_regs *regs);
+uint32_t mips_linux_watch_get_irw_mask (struct pt_watch_regs *regs, int n);
+CORE_ADDR mips_linux_watch_get_watchlo (struct pt_watch_regs *regs, int n);
+void mips_linux_watch_set_watchlo (struct pt_watch_regs *regs, int n,
+ CORE_ADDR value);
+uint32_t mips_linux_watch_get_watchhi (struct pt_watch_regs *regs, int n);
+void mips_linux_watch_set_watchhi (struct pt_watch_regs *regs, int n,
+ uint16_t value);
+int mips_linux_watch_try_one_watch (struct pt_watch_regs *regs,
+ CORE_ADDR addr, int len, uint32_t irw);
+void mips_linux_watch_populate_regs (struct mips_watchpoint *current_watches,
+ struct pt_watch_regs *regs);
+uint32_t mips_linux_watch_type_to_irw (int type);
+
+int mips_linux_read_watch_registers (long lwpid,
+ struct pt_watch_regs *watch_readback,
+ int *watch_readback_valid, int force);
+#endif