GDB (API)
/home/stan/gdb/src/gdb/gdbthread.h
Go to the documentation of this file.
00001 /* Multi-process/thread control defs for GDB, the GNU debugger.
00002    Copyright (C) 1987-2013 Free Software Foundation, Inc.
00003    Contributed by Lynx Real-Time Systems, Inc.  Los Gatos, CA.
00004    
00005 
00006    This file is part of GDB.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 3 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00020 
00021 #ifndef GDBTHREAD_H
00022 #define GDBTHREAD_H
00023 
00024 struct symtab;
00025 
00026 #include "breakpoint.h"
00027 #include "frame.h"
00028 #include "ui-out.h"
00029 #include "inferior.h"
00030 #include "btrace.h"
00031 
00032 /* Frontend view of the thread state.  Possible extensions: stepping,
00033    finishing, until(ling),...  */
00034 enum thread_state
00035 {
00036   THREAD_STOPPED,
00037   THREAD_RUNNING,
00038   THREAD_EXITED,
00039 };
00040 
00041 /* Inferior thread specific part of `struct infcall_control_state'.
00042 
00043    Inferior process counterpart is `struct inferior_control_state'.  */
00044 
00045 struct thread_control_state
00046 {
00047   /* User/external stepping state.  */
00048 
00049   /* Step-resume or longjmp-resume breakpoint.  */
00050   struct breakpoint *step_resume_breakpoint;
00051 
00052   /* Exception-resume breakpoint.  */
00053   struct breakpoint *exception_resume_breakpoint;
00054 
00055   /* Range to single step within.
00056 
00057      If this is nonzero, respond to a single-step signal by continuing
00058      to step if the pc is in this range.
00059 
00060      If step_range_start and step_range_end are both 1, it means to
00061      step for a single instruction (FIXME: it might clean up
00062      wait_for_inferior in a minor way if this were changed to the
00063      address of the instruction and that address plus one.  But maybe
00064      not).  */
00065   CORE_ADDR step_range_start;   /* Inclusive */
00066   CORE_ADDR step_range_end;     /* Exclusive */
00067 
00068   /* If GDB issues a target step request, and this is nonzero, the
00069      target should single-step this thread once, and then continue
00070      single-stepping it without GDB core involvement as long as the
00071      thread stops in the step range above.  If this is zero, the
00072      target should ignore the step range, and only issue one single
00073      step.  */
00074   int may_range_step;
00075 
00076   /* Stack frame address as of when stepping command was issued.
00077      This is how we know when we step into a subroutine call, and how
00078      to set the frame for the breakpoint used to step out.  */
00079   struct frame_id step_frame_id;
00080 
00081   /* Similarly, the frame ID of the underlying stack frame (skipping
00082      any inlined frames).  */
00083   struct frame_id step_stack_frame_id;
00084 
00085   /* Nonzero if we are presently stepping over a breakpoint.
00086 
00087      If we hit a breakpoint or watchpoint, and then continue, we need
00088      to single step the current thread with breakpoints disabled, to
00089      avoid hitting the same breakpoint or watchpoint again.  And we
00090      should step just a single thread and keep other threads stopped,
00091      so that other threads don't miss breakpoints while they are
00092      removed.
00093 
00094      So, this variable simultaneously means that we need to single
00095      step the current thread, keep other threads stopped, and that
00096      breakpoints should be removed while we step.
00097 
00098      This variable is set either:
00099      - in proceed, when we resume inferior on user's explicit request
00100      - in keep_going, if handle_inferior_event decides we need to
00101      step over breakpoint.
00102 
00103      The variable is cleared in normal_stop.  The proceed calls
00104      wait_for_inferior, which calls handle_inferior_event in a loop,
00105      and until wait_for_inferior exits, this variable is changed only
00106      by keep_going.  */
00107   int trap_expected;
00108 
00109   /* Nonzero if the thread is being proceeded for a "finish" command
00110      or a similar situation when stop_registers should be saved.  */
00111   int proceed_to_finish;
00112 
00113   /* Nonzero if the thread is being proceeded for an inferior function
00114      call.  */
00115   int in_infcall;
00116 
00117   enum step_over_calls_kind step_over_calls;
00118 
00119   /* Nonzero if stopped due to a step command.  */
00120   int stop_step;
00121 
00122   /* Chain containing status of breakpoint(s) the thread stopped
00123      at.  */
00124   bpstat stop_bpstat;
00125 };
00126 
00127 /* Inferior thread specific part of `struct infcall_suspend_state'.
00128 
00129    Inferior process counterpart is `struct inferior_suspend_state'.  */
00130 
00131 struct thread_suspend_state
00132 {
00133   /* Last signal that the inferior received (why it stopped).  */
00134   enum gdb_signal stop_signal;
00135 };
00136 
00137 struct thread_info
00138 {
00139   struct thread_info *next;
00140   ptid_t ptid;                  /* "Actual process id";
00141                                     In fact, this may be overloaded with 
00142                                     kernel thread id, etc.  */
00143   int num;                      /* Convenient handle (GDB thread id) */
00144 
00145   /* The name of the thread, as specified by the user.  This is NULL
00146      if the thread does not have a user-given name.  */
00147   char *name;
00148 
00149   /* Non-zero means the thread is executing.  Note: this is different
00150      from saying that there is an active target and we are stopped at
00151      a breakpoint, for instance.  This is a real indicator whether the
00152      thread is off and running.  */
00153   int executing;
00154 
00155   /* Frontend view of the thread state.  Note that the RUNNING/STOPPED
00156      states are different from EXECUTING.  When the thread is stopped
00157      internally while handling an internal event, like a software
00158      single-step breakpoint, EXECUTING will be false, but running will
00159      still be true.  As a possible future extension, this could turn
00160      into enum { stopped, exited, stepping, finishing, until(ling),
00161      running ... }  */
00162   int state;
00163 
00164   /* If this is > 0, then it means there's code out there that relies
00165      on this thread being listed.  Don't delete it from the lists even
00166      if we detect it exiting.  */
00167   int refcount;
00168 
00169   /* State of GDB control of inferior thread execution.
00170      See `struct thread_control_state'.  */
00171   struct thread_control_state control;
00172 
00173   /* State of inferior thread to restore after GDB is done with an inferior
00174      call.  See `struct thread_suspend_state'.  */
00175   struct thread_suspend_state suspend;
00176 
00177   int current_line;
00178   struct symtab *current_symtab;
00179 
00180   /* Internal stepping state.  */
00181 
00182   /* Record the pc of the thread the last time it stopped.  This is
00183      maintained by proceed and keep_going, and used in
00184      adjust_pc_after_break to distinguish a hardware single-step
00185      SIGTRAP from a breakpoint SIGTRAP.  */
00186   CORE_ADDR prev_pc;
00187 
00188   /* Should we step over breakpoint next time keep_going is called?  */
00189   int stepping_over_breakpoint;
00190 
00191   /* Set to TRUE if we should finish single-stepping over a breakpoint
00192      after hitting the current step-resume breakpoint.  The context here
00193      is that GDB is to do `next' or `step' while signal arrives.
00194      When stepping over a breakpoint and signal arrives, GDB will attempt
00195      to skip signal handler, so it inserts a step_resume_breakpoint at the
00196      signal return address, and resume inferior.
00197      step_after_step_resume_breakpoint is set to TRUE at this moment in
00198      order to keep GDB in mind that there is still a breakpoint to step over
00199      when GDB gets back SIGTRAP from step_resume_breakpoint.  */
00200   int step_after_step_resume_breakpoint;
00201 
00202   /* Per-thread command support.  */
00203 
00204   /* Pointer to what is left to do for an execution command after the
00205      target stops.  Used only in asynchronous mode, by targets that
00206      support async execution.  Several execution commands use it.  */
00207   struct continuation *continuations;
00208 
00209   /* Similar to the above, but used when a single execution command
00210      requires several resume/stop iterations.  Used by the step
00211      command.  */
00212   struct continuation *intermediate_continuations;
00213 
00214   /* If stepping, nonzero means step count is > 1 so don't print frame
00215      next time inferior stops if it stops due to stepping.  */
00216   int step_multi;
00217 
00218   /* This is used to remember when a fork or vfork event was caught by
00219      a catchpoint, and thus the event is to be followed at the next
00220      resume of the thread, and not immediately.  */
00221   struct target_waitstatus pending_follow;
00222 
00223   /* True if this thread has been explicitly requested to stop.  */
00224   int stop_requested;
00225 
00226   /* The initiating frame of a nexting operation, used for deciding
00227      which exceptions to intercept.  If it is null_frame_id no
00228      bp_longjmp or bp_exception but longjmp has been caught just for
00229      bp_longjmp_call_dummy.  */
00230   struct frame_id initiating_frame;
00231 
00232   /* Private data used by the target vector implementation.  */
00233   struct private_thread_info *private;
00234 
00235   /* Function that is called to free PRIVATE.  If this is NULL, then
00236      xfree will be called on PRIVATE.  */
00237   void (*private_dtor) (struct private_thread_info *);
00238 
00239   /* Branch trace information for this thread.  */
00240   struct btrace_thread_info btrace;
00241 };
00242 
00243 /* Create an empty thread list, or empty the existing one.  */
00244 extern void init_thread_list (void);
00245 
00246 /* Add a thread to the thread list, print a message
00247    that a new thread is found, and return the pointer to
00248    the new thread.  Caller my use this pointer to 
00249    initialize the private thread data.  */
00250 extern struct thread_info *add_thread (ptid_t ptid);
00251 
00252 /* Same as add_thread, but does not print a message
00253    about new thread.  */
00254 extern struct thread_info *add_thread_silent (ptid_t ptid);
00255 
00256 /* Same as add_thread, and sets the private info.  */
00257 extern struct thread_info *add_thread_with_info (ptid_t ptid,
00258                                                  struct private_thread_info *);
00259 
00260 /* Delete an existing thread list entry.  */
00261 extern void delete_thread (ptid_t);
00262 
00263 /* Delete an existing thread list entry, and be quiet about it.  Used
00264    after the process this thread having belonged to having already
00265    exited, for example.  */
00266 extern void delete_thread_silent (ptid_t);
00267 
00268 /* Delete a step_resume_breakpoint from the thread database.  */
00269 extern void delete_step_resume_breakpoint (struct thread_info *);
00270 
00271 /* Delete an exception_resume_breakpoint from the thread database.  */
00272 extern void delete_exception_resume_breakpoint (struct thread_info *);
00273 
00274 /* Translate the integer thread id (GDB's homegrown id, not the system's)
00275    into a "pid" (which may be overloaded with extra thread information).  */
00276 extern ptid_t thread_id_to_pid (int);
00277 
00278 /* Translate a 'pid' (which may be overloaded with extra thread information) 
00279    into the integer thread id (GDB's homegrown id, not the system's).  */
00280 extern int pid_to_thread_id (ptid_t ptid);
00281 
00282 /* Boolean test for an already-known pid (which may be overloaded with
00283    extra thread information).  */
00284 extern int in_thread_list (ptid_t ptid);
00285 
00286 /* Boolean test for an already-known thread id (GDB's homegrown id, 
00287    not the system's).  */
00288 extern int valid_thread_id (int thread);
00289 
00290 /* Search function to lookup a thread by 'pid'.  */
00291 extern struct thread_info *find_thread_ptid (ptid_t ptid);
00292 
00293 /* Find thread by GDB user-visible thread number.  */
00294 struct thread_info *find_thread_id (int num);
00295 
00296 /* Finds the first thread of the inferior given by PID.  If PID is -1,
00297    returns the first thread in the list.  */
00298 struct thread_info *first_thread_of_process (int pid);
00299 
00300 /* Returns any thread of process PID.  */
00301 extern struct thread_info *any_thread_of_process (int pid);
00302 
00303 /* Returns any non-exited thread of process PID, giving preference for
00304    not executing threads.  */
00305 extern struct thread_info *any_live_thread_of_process (int pid);
00306 
00307 /* Change the ptid of thread OLD_PTID to NEW_PTID.  */
00308 void thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid);
00309 
00310 /* Iterator function to call a user-provided callback function
00311    once for each known thread.  */
00312 typedef int (*thread_callback_func) (struct thread_info *, void *);
00313 extern struct thread_info *iterate_over_threads (thread_callback_func, void *);
00314 
00315 /* Traverse all threads.  */
00316 
00317 #define ALL_THREADS(T)                          \
00318   for (T = thread_list; T; T = T->next)
00319 
00320 extern int thread_count (void);
00321 
00322 /* Switch from one thread to another.  */
00323 extern void switch_to_thread (ptid_t ptid);
00324 
00325 /* Marks thread PTID is running, or stopped. 
00326    If ptid_get_pid (PTID) is -1, marks all threads.  */
00327 extern void set_running (ptid_t ptid, int running);
00328 
00329 /* Marks or clears thread(s) PTID as having been requested to stop.
00330    If PTID is MINUS_ONE_PTID, applies to all threads.  If
00331    ptid_is_pid(PTID) is true, applies to all threads of the process
00332    pointed at by PTID.  If STOP, then the THREAD_STOP_REQUESTED
00333    observer is called with PTID as argument.  */
00334 extern void set_stop_requested (ptid_t ptid, int stop);
00335 
00336 /* NOTE: Since the thread state is not a boolean, most times, you do
00337    not want to check it with negation.  If you really want to check if
00338    the thread is stopped,
00339 
00340     use (good):
00341 
00342      if (is_stopped (ptid))
00343 
00344     instead of (bad):
00345 
00346      if (!is_running (ptid))
00347 
00348    The latter also returns true on exited threads, most likelly not
00349    what you want.  */
00350 
00351 /* Reports if in the frontend's perpective, thread PTID is running.  */
00352 extern int is_running (ptid_t ptid);
00353 
00354 /* Is this thread listed, but known to have exited?  We keep it listed
00355    (but not visible) until it's safe to delete.  */
00356 extern int is_exited (ptid_t ptid);
00357 
00358 /* In the frontend's perpective, is this thread stopped?  */
00359 extern int is_stopped (ptid_t ptid);
00360 
00361 /* In the frontend's perpective is there any thread running?  */
00362 extern int any_running (void);
00363 
00364 /* Marks thread PTID as executing, or not.  If ptid_get_pid (PTID) is -1,
00365    marks all threads.
00366 
00367    Note that this is different from the running state.  See the
00368    description of state and executing fields of struct
00369    thread_info.  */
00370 extern void set_executing (ptid_t ptid, int executing);
00371 
00372 /* Reports if thread PTID is executing.  */
00373 extern int is_executing (ptid_t ptid);
00374 
00375 /* Merge the executing property of thread PTID over to its thread
00376    state property (frontend running/stopped view).
00377 
00378    "not executing" -> "stopped"
00379    "executing"     -> "running"
00380    "exited"        -> "exited"
00381 
00382    If ptid_get_pid (PTID) is -1, go over all threads.
00383 
00384    Notifications are only emitted if the thread state did change.  */
00385 extern void finish_thread_state (ptid_t ptid);
00386 
00387 /* Same as FINISH_THREAD_STATE, but with an interface suitable to be
00388    registered as a cleanup.  PTID_P points to the ptid_t that is
00389    passed to FINISH_THREAD_STATE.  */
00390 extern void finish_thread_state_cleanup (void *ptid_p);
00391 
00392 /* Commands with a prefix of `thread'.  */
00393 extern struct cmd_list_element *thread_cmd_list;
00394 
00395 /* Print notices on thread events (attach, detach, etc.), set with
00396    `set print thread-events'.  */
00397 extern int print_thread_events;
00398 
00399 extern void print_thread_info (struct ui_out *uiout, char *threads,
00400                                int pid);
00401 
00402 extern struct cleanup *make_cleanup_restore_current_thread (void);
00403 
00404 /* Returns a pointer into the thread_info corresponding to
00405    INFERIOR_PTID.  INFERIOR_PTID *must* be in the thread list.  */
00406 extern struct thread_info* inferior_thread (void);
00407 
00408 extern void update_thread_list (void);
00409 
00410 /* Return true if PC is in the stepping range of THREAD.  */
00411 
00412 int pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread);
00413 
00414 extern struct thread_info *thread_list;
00415 
00416 #endif /* GDBTHREAD_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines