The Pedigree Project  0.1
LocksCommand.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef LOCKSCOMMAND_H
21 #define LOCKSCOMMAND_H
22 
23 #include "pedigree/kernel/Atomic.h"
24 #include "pedigree/kernel/debugger/DebuggerCommand.h"
25 #include "pedigree/kernel/debugger/DebuggerIO.h"
26 #include "pedigree/kernel/debugger/Scrollable.h"
27 #include "pedigree/kernel/processor/state_forward.h"
28 #include "pedigree/kernel/processor/types.h"
29 #include "pedigree/kernel/utilities/StaticString.h"
30 
31 class Spinlock;
32 
36 #define NUM_BT_FRAMES 6
37 #define MAX_DESCRIPTORS 50
38 
39 #ifdef TESTSUITE
40 #define LOCKS_COMMAND_NUM_CPU 4
41 #else
42 #ifdef MULTIPROCESSOR
43 #define LOCKS_COMMAND_NUM_CPU 255
44 #else
45 #define LOCKS_COMMAND_NUM_CPU 1
46 #endif
47 #endif
48 
49 #define LOCKS_COMMAND_DO_BACKTRACES 0
50 
54 class LocksCommand : public DebuggerCommand, public Scrollable
55 {
56  friend class Spinlock;
57 
58  public:
62  LocksCommand();
63 
67  virtual ~LocksCommand();
68 
72  void autocomplete(const HugeStaticString &input, HugeStaticString &output);
73 
77  bool execute(
78  const HugeStaticString &input, HugeStaticString &output,
79  InterruptState &state, DebuggerIO *screen);
80 
85  {
86  return NormalStaticString("locks");
87  }
88 
89  void setReady();
90 
95  void setFatal();
96 
97  bool lockAttempted(
98  const Spinlock *pLock, size_t nCpu = ~0U, bool intState = false);
99  bool lockAcquired(
100  const Spinlock *pLock, size_t nCpu = ~0U, bool intState = false);
101  bool lockReleased(const Spinlock *pLock, size_t nCpu = ~0U);
102 
108  bool checkSchedule(size_t nCpu = ~0U);
109 
116  bool checkState(const Spinlock *pLock, size_t nCpu = ~0U);
117 
118  // Scrollable interface.
119  virtual const char *getLine1(
120  size_t index, DebuggerIO::Colour &colour, DebuggerIO::Colour &bgColour);
121  virtual const char *getLine2(
122  size_t index, size_t &colOffset, DebuggerIO::Colour &colour,
123  DebuggerIO::Colour &bgColour);
124  virtual size_t getLineCount();
125 
126  protected:
127  void clearFatal();
128 
129  private:
130  enum State
131  {
133  Inactive = 0,
140  };
141 
142  const char *stateName(State s)
143  {
144  switch (s)
145  {
146  case Attempted:
147  return "attempted";
148  case Acquired:
149  return "acquired";
150  case Checked:
151  return "checked";
152  case Inactive:
153  return "inactive";
154  default:
155  return "unknown";
156  }
157  }
158 
160  {
162  : pLock(0), state(Inactive)
163 #if LOCKS_COMMAND_DO_BACKTRACES
164  ,
165  n(0), ra()
166 #endif
167  {
168  }
169 
170  const Spinlock *pLock;
171  State state;
172 #if LOCKS_COMMAND_DO_BACKTRACES
173  size_t n;
174  uintptr_t ra[NUM_BT_FRAMES];
175 #endif
176  };
177 
178  LockDescriptor m_pDescriptors[LOCKS_COMMAND_NUM_CPU][MAX_DESCRIPTORS];
179 
180  Atomic<bool> m_bAcquiring;
181 #if LOCKS_COMMAND_DO_BACKTRACES
182  Atomic<bool> m_bTracing[LOCKS_COMMAND_NUM_CPU];
183 #endif
184  Atomic<uint8_t> m_NextPosition[LOCKS_COMMAND_NUM_CPU];
186  Atomic<size_t> m_LockIndex;
187 
188  bool m_bFatal;
189 
190  size_t m_SelectedLine;
191 
194 };
195 
196 extern LocksCommand g_LocksCommand;
197 
199 #endif
The lock is about to be attempted.
Definition: LocksCommand.h:135
The lock failed to be acquired, and has been checked once.
Definition: LocksCommand.h:139
const NormalStaticString getString()
Definition: LocksCommand.h:84
bool lockReleased(const Spinlock *pLock, size_t nCpu=~0U)
Atomic< uint8_t > m_NextPosition[LOCKS_COMMAND_NUM_CPU]
Definition: LocksCommand.h:185
LockDescriptor * m_pSelectedLock
Lock we&#39;ve selected for backtracing.
Definition: LocksCommand.h:193
The lock is acquired.
Definition: LocksCommand.h:137
virtual ~LocksCommand()
Definition: LocksCommand.cc:63
bool checkState(const Spinlock *pLock, size_t nCpu=~0U)
bool execute(const HugeStaticString &input, HugeStaticString &output, InterruptState &state, DebuggerIO *screen)
Definition: LocksCommand.cc:72
void autocomplete(const HugeStaticString &input, HugeStaticString &output)
Definition: LocksCommand.cc:67
bool checkSchedule(size_t nCpu=~0U)
This entry is no longer active.
Definition: LocksCommand.h:133