Commit | Line | Data |
---|---|---|
236ef034 PA |
1 | /* RAII type to create a temporary mock context. |
2 | ||
3 | Copyright (C) 2020 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #ifndef SCOPED_MOCK_CONTEXT_H | |
21 | #define SCOPED_MOCK_CONTEXT_H | |
22 | ||
23 | #include "inferior.h" | |
24 | #include "gdbthread.h" | |
25 | #include "progspace.h" | |
26 | #include "progspace-and-thread.h" | |
27 | ||
28 | #if GDB_SELF_TEST | |
29 | namespace selftests { | |
30 | ||
31 | /* RAII type to create (and switch to) a temporary mock context. An | |
32 | inferior with a thread, with a process_stratum target pushed. */ | |
33 | ||
34 | template<typename Target> | |
35 | struct scoped_mock_context | |
36 | { | |
37 | /* Order here is important. */ | |
38 | ||
39 | Target mock_target; | |
40 | ptid_t mock_ptid {1, 1}; | |
41 | program_space mock_pspace {new_address_space ()}; | |
42 | inferior mock_inferior {mock_ptid.pid ()}; | |
43 | thread_info mock_thread {&mock_inferior, mock_ptid}; | |
44 | ||
45 | scoped_restore_current_pspace_and_thread restore_pspace_thread; | |
46 | ||
47 | scoped_restore_tmpl<thread_info *> restore_thread_list | |
48 | {&mock_inferior.thread_list, &mock_thread}; | |
49 | ||
50 | /* Add the mock inferior to the inferior list so that look ups by | |
51 | target+ptid can find it. */ | |
52 | scoped_restore_tmpl<inferior *> restore_inferior_list | |
53 | {&inferior_list, &mock_inferior}; | |
54 | ||
55 | explicit scoped_mock_context (gdbarch *gdbarch) | |
56 | { | |
57 | mock_inferior.gdbarch = gdbarch; | |
58 | mock_inferior.aspace = mock_pspace.aspace; | |
59 | mock_inferior.pspace = &mock_pspace; | |
60 | ||
61 | /* Switch to the mock inferior. */ | |
62 | switch_to_inferior_no_thread (&mock_inferior); | |
63 | ||
64 | /* Push the process_stratum target so we can mock accessing | |
65 | registers. */ | |
66 | gdb_assert (mock_target.stratum () == process_stratum); | |
67 | push_target (&mock_target); | |
68 | ||
69 | /* Switch to the mock thread. */ | |
70 | switch_to_thread (&mock_thread); | |
71 | } | |
72 | ||
73 | ~scoped_mock_context () | |
74 | { | |
75 | pop_all_targets_at_and_above (process_stratum); | |
76 | } | |
77 | }; | |
78 | ||
79 | } // namespace selftests | |
80 | #endif /* GDB_SELF_TEST */ | |
81 | ||
82 | #endif /* !defined (SCOPED_MOCK_CONTEXT_H) */ |