aboutsummaryrefslogtreecommitdiffstats
path: root/src/chdebug.c
blob: 9a8120b2995325b1e8ee5051b7651ff75a99c819 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
    ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file chdebug.c
 * @brief ChibiOS/RT Debug code.
 * @addtogroup Debug
 * @{
 */

#include <ch.h>

#if CH_DBG_ENABLE_TRACE
/**
 * @brief Public trace buffer.
 */
TraceBuffer trace_buffer;

/**
 * @brief Trace circular buffer subsystem initialization.
 */
void trace_init(void) {

  trace_buffer.tb_size = TRACE_BUFFER_SIZE;
  trace_buffer.tb_ptr = &trace_buffer.tb_buffer[0];
}

/**
 * @brief Inserts in the circular debug trace buffer a context switch record.
 *
 * @param[in] otp the thread being switched out
 * @param[in] ntp the thread to be switched in
 */
void chDbgTrace(Thread *otp, Thread *ntp) {

  trace_buffer.tb_ptr->cse_wtobjp = otp->p_wtobjp;
  trace_buffer.tb_ptr->cse_time = chTimeNow();
  trace_buffer.tb_ptr->cse_state = otp->p_state;
  trace_buffer.tb_ptr->cse_tid = (unsigned)ntp >> 4;
  if (++trace_buffer.tb_ptr >= &trace_buffer.tb_buffer[TRACE_BUFFER_SIZE])
    trace_buffer.tb_ptr = &trace_buffer.tb_buffer[0];
}
#endif /* CH_DBG_ENABLE_TRACE */

#if CH_DBG_ENABLE_ASSERTS || CH_DBG_ENABLE_CHECKS || CH_DBG_ENABLE_STACK_CHECK
/**
 * @brief Pointer to the panic message.
 * @details This pointer is meant to be accessed through the debugger, it is
 *          written once and then the system is halted.
 */
char *panic_msg;

/**
 * @brief Prints a panic message on the console and then halts the system.
 *
 * @param[in] msg the pointer to the panic message string
 */
void chDbgPanic(char *msg) {

  panic_msg = msg;
  chSysHalt();
}
#endif /* CH_DBG_ENABLE_ASSERTS || CH_DBG_ENABLE_CHECKS || CH_DBG_ENABLE_STACK_CHECK */

/** @} */
span>EOne) == "<UnscopedEnum.EOne: 1>" assert repr(m.UnscopedEnum.ETwo) == "<UnscopedEnum.ETwo: 2>" assert repr(m.EOne) == "<UnscopedEnum.EOne: 1>" # name property assert m.UnscopedEnum.EOne.name == "EOne" assert m.UnscopedEnum.ETwo.name == "ETwo" assert m.EOne.name == "EOne" # name readonly with pytest.raises(AttributeError): m.UnscopedEnum.EOne.name = "" # name returns a copy foo = m.UnscopedEnum.EOne.name foo = "bar" assert m.UnscopedEnum.EOne.name == "EOne" # __members__ property assert m.UnscopedEnum.__members__ == { "EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo, "EThree": m.UnscopedEnum.EThree, } # __members__ readonly with pytest.raises(AttributeError): m.UnscopedEnum.__members__ = {} # __members__ returns a copy foo = m.UnscopedEnum.__members__ foo["bar"] = "baz" assert m.UnscopedEnum.__members__ == { "EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo, "EThree": m.UnscopedEnum.EThree, } for docstring_line in """An unscoped enumeration Members: EOne : Docstring for EOne ETwo : Docstring for ETwo EThree : Docstring for EThree""".split( "\n" ): assert docstring_line in m.UnscopedEnum.__doc__ # Unscoped enums will accept ==/!= int comparisons y = m.UnscopedEnum.ETwo assert y == 2 assert 2 == y assert y != 3 assert 3 != y # Compare with None assert y != None # noqa: E711 assert not (y == None) # noqa: E711 # Compare with an object assert y != object() assert not (y == object()) # Compare with string assert y != "2" assert "2" != y assert not ("2" == y) assert not (y == "2") with pytest.raises(TypeError): y < object() with pytest.raises(TypeError): y <= object() with pytest.raises(TypeError): y > object() with pytest.raises(TypeError): y >= object() with pytest.raises(TypeError): y | object() with pytest.raises(TypeError): y & object() with pytest.raises(TypeError): y ^ object() assert int(m.UnscopedEnum.ETwo) == 2 assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo" # order assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo assert m.UnscopedEnum.EOne < 2 assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne assert m.UnscopedEnum.ETwo > 1 assert m.UnscopedEnum.ETwo <= 2 assert m.UnscopedEnum.ETwo >= 2 assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo assert m.UnscopedEnum.EOne <= 2 assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne assert m.UnscopedEnum.ETwo >= 1 assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne) assert not (2 < m.UnscopedEnum.EOne) # arithmetic assert m.UnscopedEnum.EOne & m.UnscopedEnum.EThree == m.UnscopedEnum.EOne assert m.UnscopedEnum.EOne | m.UnscopedEnum.ETwo == m.UnscopedEnum.EThree assert m.UnscopedEnum.EOne ^ m.UnscopedEnum.EThree == m.UnscopedEnum.ETwo def test_scoped_enum(): assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three" z = m.ScopedEnum.Two assert m.test_scoped_enum(z) == "ScopedEnum::Two" # Scoped enums will *NOT* accept ==/!= int comparisons (Will always return False) assert not z == 3 assert not 3 == z assert z != 3 assert 3 != z # Compare with None assert z != None # noqa: E711 assert not (z == None) # noqa: E711 # Compare with an object assert z != object() assert not (z == object()) # Scoped enums will *NOT* accept >, <, >= and <= int comparisons (Will throw exceptions) with pytest.raises(TypeError): z > 3 with pytest.raises(TypeError): z < 3 with pytest.raises(TypeError): z >= 3 with pytest.raises(TypeError): z <= 3 # order assert m.ScopedEnum.Two < m.ScopedEnum.Three assert m.ScopedEnum.Three > m.ScopedEnum.Two assert m.ScopedEnum.Two <= m.ScopedEnum.Three assert m.ScopedEnum.Two <= m.ScopedEnum.Two assert m.ScopedEnum.Two >= m.ScopedEnum.Two assert m.ScopedEnum.Three >= m.ScopedEnum.Two def test_implicit_conversion(): assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode" assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode" assert repr(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "<EMode.EFirstMode: 1>" assert repr(m.ClassWithUnscopedEnum.EFirstMode) == "<EMode.EFirstMode: 1>" f = m.ClassWithUnscopedEnum.test_function first = m.ClassWithUnscopedEnum.EFirstMode second = m.ClassWithUnscopedEnum.ESecondMode assert f(first) == 1 assert f(first) == f(first) assert not f(first) != f(first) assert f(first) != f(second) assert not f(first) == f(second) assert f(first) == int(f(first)) assert not f(first) != int(f(first)) assert f(first) != int(f(second)) assert not f(first) == int(f(second)) # noinspection PyDictCreation x = {f(first): 1, f(second): 2} x[f(first)] = 3 x[f(second)] = 4 # Hashing test assert repr(x) == "{<EMode.EFirstMode: 1>: 3, <EMode.ESecondMode: 2>: 4}" def test_binary_operators(): assert int(m.Flags.Read) == 4 assert int(m.Flags.Write) == 2 assert int(m.Flags.Execute) == 1 assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7 assert int(m.Flags.Read | m.Flags.Write) == 6 assert int(m.Flags.Read | m.Flags.Execute) == 5 assert int(m.Flags.Write | m.Flags.Execute) == 3 assert int(m.Flags.Write | 1) == 3 assert ~m.Flags.Write == -3 state = m.Flags.Read | m.Flags.Write assert (state & m.Flags.Read) != 0 assert (state & m.Flags.Write) != 0 assert (state & m.Flags.Execute) == 0 assert (state & 1) == 0 state2 = ~state assert state2 == -7 assert int(state ^ state2) == -1 def test_enum_to_int(): m.test_enum_to_int(m.Flags.Read) m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode) m.test_enum_to_uint(m.Flags.Read) m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode) m.test_enum_to_long_long(m.Flags.Read) m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode) def test_duplicate_enum_name(): with pytest.raises(ValueError) as excinfo: m.register_bad_enum() assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!' def test_docstring_signatures(): for enum_type in [m.ScopedEnum, m.UnscopedEnum]: for attr in enum_type.__dict__.values(): # Issue #2623/PR #2637: Add argument names to enum_ methods assert "arg0" not in (attr.__doc__ or "")