Back to index

glibc  2.9
thread_state.h
Go to the documentation of this file.
00001 /* Generic definitions for dealing with Mach thread states.
00002    Copyright (C) 1994, 1996 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 
00021 /* Everything else is called `thread_state', but CMU's header file is
00022    called `thread_status'.  Oh boy.  */
00023 #include <mach/thread_status.h>
00024 
00025 /* The machine-dependent thread_state.h file can either define these
00026    macros, or just define PC and SP to the register names.  */
00027 
00028 #ifndef MACHINE_THREAD_STATE_SET_PC
00029 #define MACHINE_THREAD_STATE_SET_PC(ts, pc) \
00030   ((ts)->PC = (unsigned long int) (pc))
00031 #endif
00032 #ifndef MACHINE_THREAD_STATE_SET_SP
00033 #ifdef STACK_GROWTH_UP
00034 #define MACHINE_THREAD_STATE_SET_SP(ts, stack, size) \
00035   ((ts)->SP = (unsigned long int) (stack))
00036 #else
00037 #define MACHINE_THREAD_STATE_SET_SP(ts, stack, size) \
00038   ((ts)->SP = (unsigned long int) (stack) + (size))
00039 #endif
00040 #endif
00041 
00042 /* These functions are of use in machine-dependent signal trampoline
00043    implementations.  */
00044 
00045 #include <string.h>         /* size_t, memcpy */
00046 #include <mach/mach_interface.h> /* __thread_get_state */
00047 
00048 static inline int
00049 machine_get_state (thread_t thread, struct machine_thread_all_state *state,
00050                  int flavor, void *stateptr, void *scpptr, size_t size)
00051 {
00052   if (state->set & (1 << flavor))
00053     {
00054       /* Copy the saved state.  */
00055       memcpy (scpptr, stateptr, size);
00056       return 1;
00057     }
00058   else
00059     {
00060       /* No one asked about this flavor of state before; fetch the state
00061         directly from the kernel into the sigcontext.  */
00062       mach_msg_type_number_t got = (size / sizeof (int));
00063       return (! __thread_get_state (thread, flavor, scpptr, &got)
00064              && got == (size / sizeof (int)));
00065     }
00066 }
00067 
00068 static inline int
00069 machine_get_basic_state (thread_t thread,
00070                       struct machine_thread_all_state *state)
00071 {
00072   mach_msg_type_number_t count;
00073 
00074   if (state->set & (1 << MACHINE_THREAD_STATE_FLAVOR))
00075     return 1;
00076 
00077   count = MACHINE_THREAD_STATE_COUNT;
00078   if (__thread_get_state (thread, MACHINE_THREAD_STATE_FLAVOR,
00079                        (natural_t *) &state->basic,
00080                        &count) != KERN_SUCCESS ||
00081       count != MACHINE_THREAD_STATE_COUNT)
00082     /* What kind of thread?? */
00083     return 0;               /* XXX */
00084 
00085   state->set |= 1 << MACHINE_THREAD_STATE_FLAVOR;
00086   return 1;
00087 }