1/* Thread command's finish-state machine, for GDB, the GNU debugger.
2   Copyright (C) 2015-2020 Free Software Foundation, Inc.
3
4   This file is part of GDB.
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 3 of the License, or
9   (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19#ifndef THREAD_FSM_H
20#define THREAD_FSM_H
21
22#include "mi/mi-common.h" /* For enum async_reply_reason.  */
23
24struct return_value_info;
25struct thread_fsm_ops;
26
27/* A thread finite-state machine structure contains the necessary info
28   and callbacks to manage the state machine protocol of a thread's
29   execution command.  */
30
31struct thread_fsm
32{
33  explicit thread_fsm (struct interp *cmd_interp)
34    : command_interp (cmd_interp)
35  {
36  }
37
38  /* The destructor.  This should simply free heap allocated data
39     structures.  Cleaning up target resources (like, e.g.,
40     breakpoints) should be done in the clean_up method.  */
41  virtual ~thread_fsm () = default;
42
43  DISABLE_COPY_AND_ASSIGN (thread_fsm);
44
45  /* Called to clean up target resources after the FSM.  E.g., if the
46     FSM created internal breakpoints, this is where they should be
47     deleted.  */
48  virtual void clean_up (struct thread_info *thread)
49  {
50  }
51
52  /* Called after handle_inferior_event decides the target is done
53     (that is, after stop_waiting).  The FSM is given a chance to
54     decide whether the command is done and thus the target should
55     stop, or whether there's still more to do and thus the thread
56     should be re-resumed.  This is a good place to cache target data
57     too.  For example, the "finish" command saves the just-finished
58     function's return value here.  */
59  virtual bool should_stop (struct thread_info *thread) = 0;
60
61  /* If this FSM saved a function's return value, you can use this
62     method to retrieve it.  Otherwise, this returns NULL.  */
63  virtual struct return_value_info *return_value ()
64  {
65    return nullptr;
66  }
67
68  enum async_reply_reason async_reply_reason ()
69  {
70    /* If we didn't finish, then the stop reason must come from
71       elsewhere.  E.g., a breakpoint hit or a signal intercepted.  */
72    gdb_assert (finished_p ());
73    return do_async_reply_reason ();
74  }
75
76  /* Whether the stop should be notified to the user/frontend.  */
77  virtual bool should_notify_stop ()
78  {
79    return true;
80  }
81
82  void set_finished ()
83  {
84    finished = true;
85  }
86
87  bool finished_p () const
88  {
89    return finished;
90  }
91
92  /* The interpreter that issued the execution command that caused
93     this thread to resume.  If the top level interpreter is MI/async,
94     and the execution command was a CLI command (next/step/etc.),
95     we'll want to print stop event output to the MI console channel
96     (the stepped-to line, etc.), as if the user entered the execution
97     command on a real GDB console.  */
98  struct interp *command_interp = nullptr;
99
100protected:
101
102  /* Whether the FSM is done successfully.  */
103  bool finished = false;
104
105  /* The async_reply_reason that is broadcast to MI clients if this
106     FSM finishes successfully.  */
107  virtual enum async_reply_reason do_async_reply_reason ()
108  {
109    gdb_assert_not_reached (_("should not call async_reply_reason here"));
110  }
111};
112
113#endif /* THREAD_FSM_H */
114