|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/debug/debug.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <stdio.h> |
| 26 | +#include <stdbool.h> |
| 27 | + |
| 28 | +#include <nuttx/gdbstub.h> |
| 29 | +#include <nuttx/compiler.h> |
| 30 | + |
| 31 | +/**************************************************************************** |
| 32 | + * Pre-processor Definitions |
| 33 | + ****************************************************************************/ |
| 34 | + |
| 35 | +/**************************************************************************** |
| 36 | + * Private Data |
| 37 | + ****************************************************************************/ |
| 38 | + |
| 39 | +static uint8_t g_test_data[8]; |
| 40 | +static const char g_test_rodata[] = "This is a read-only string"; |
| 41 | + |
| 42 | +/**************************************************************************** |
| 43 | + * Private Functions |
| 44 | + ****************************************************************************/ |
| 45 | + |
| 46 | +/**************************************************************************** |
| 47 | + * Name: trigger_func1 |
| 48 | + ****************************************************************************/ |
| 49 | + |
| 50 | +static void test_trigger(void) |
| 51 | +{ |
| 52 | + printf("Calling test_trigger\n"); |
| 53 | +} |
| 54 | + |
| 55 | +/**************************************************************************** |
| 56 | + * Name: debug_callback |
| 57 | + ****************************************************************************/ |
| 58 | + |
| 59 | +static void debug_callback(int type, FAR void *addr, size_t size, |
| 60 | + FAR void *arg) |
| 61 | +{ |
| 62 | + *(bool *)arg = true; |
| 63 | + gdb_debugpoint_remove(type, addr, size); |
| 64 | +} |
| 65 | + |
| 66 | +/**************************************************************************** |
| 67 | + * Name: test_function |
| 68 | + ****************************************************************************/ |
| 69 | + |
| 70 | +nooptimiziation_function static void test_function(void (*func)(void)) |
| 71 | +{ |
| 72 | + int ret = 0; |
| 73 | + bool triggered; |
| 74 | + printf("==== Calling test_function ====\n"); |
| 75 | + |
| 76 | + printf("Add breakpoint at %p\n", func); |
| 77 | + ret = gdb_debugpoint_add(GDB_STOPREASON_BREAKPOINT, func, 0, |
| 78 | + debug_callback, &triggered); |
| 79 | + |
| 80 | + if (ret != OK) |
| 81 | + { |
| 82 | + printf("ERROR: Registering breakpoint at %p failed with %d\n", |
| 83 | + func, ret); |
| 84 | + } |
| 85 | + |
| 86 | + /* Trigger the breakpoint */ |
| 87 | + |
| 88 | + triggered = false; |
| 89 | + func(); |
| 90 | + |
| 91 | + if (triggered) |
| 92 | + { |
| 93 | + printf("Pass: Breakpoint at %p triggered\n", func); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + gdb_debugpoint_remove(GDB_STOPREASON_BREAKPOINT, func, 0); |
| 98 | + printf("ERROR: Breakpoint %p not triggered\n", func); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/**************************************************************************** |
| 103 | + * Name: test_data |
| 104 | + ****************************************************************************/ |
| 105 | + |
| 106 | +nooptimiziation_function static void test_data(uint8_t *addr, |
| 107 | + size_t size, bool r, bool w) |
| 108 | +{ |
| 109 | + int ret = 0; |
| 110 | + int tmp; |
| 111 | + int test_type; |
| 112 | + bool triggered; |
| 113 | + |
| 114 | + printf("==== Calling test_data ====\n"); |
| 115 | + |
| 116 | + if (r && w) |
| 117 | + { |
| 118 | + test_type = GDB_STOPREASON_WATCHPOINT_RW; |
| 119 | + printf("Add read/write watchpoint at %p\n", addr); |
| 120 | + } |
| 121 | + else if (r) |
| 122 | + { |
| 123 | + test_type = GDB_STOPREASON_WATCHPOINT_RO; |
| 124 | + printf("Add read watchpoint at %p\n", addr); |
| 125 | + } |
| 126 | + else if (w) |
| 127 | + { |
| 128 | + test_type = GDB_STOPREASON_WATCHPOINT_WO; |
| 129 | + printf("Add write watchpoint at %p\n", addr); |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + printf("ERROR: Invalid test type\n"); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + ret = gdb_debugpoint_add(test_type, addr, size, |
| 138 | + debug_callback, &triggered); |
| 139 | + |
| 140 | + if (ret != OK) |
| 141 | + { |
| 142 | + printf("ERROR: Registering read watchpoint at %p failed with %d\n", |
| 143 | + addr, ret); |
| 144 | + } |
| 145 | + |
| 146 | + if (r & w) |
| 147 | + { |
| 148 | + /* Trigger the watchpoint by reading the address */ |
| 149 | + |
| 150 | + triggered = false; |
| 151 | + if (addr[0] == 0x55) |
| 152 | + { |
| 153 | + /* Do something to avoid compiler removing the read */ |
| 154 | + |
| 155 | + tmp = 0xaa; |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + tmp = 0x55; |
| 160 | + } |
| 161 | + |
| 162 | + if (triggered) |
| 163 | + { |
| 164 | + printf("Pass: Read watchpoint at %p triggered\n", addr); |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + gdb_debugpoint_remove(test_type, addr, size); |
| 169 | + printf("ERROR: Read watchpoint at %p not triggered\n", addr); |
| 170 | + } |
| 171 | + |
| 172 | + /* Register the watchpoint again to test write watchpoints */ |
| 173 | + |
| 174 | + ret = gdb_debugpoint_add(test_type, addr, size, |
| 175 | + debug_callback, &triggered); |
| 176 | + |
| 177 | + if (ret != OK) |
| 178 | + { |
| 179 | + printf("ERROR: Registering read/write watchpoint at %p" |
| 180 | + " failed with %d\n", addr, ret); |
| 181 | + } |
| 182 | + |
| 183 | + /* Trigger the watchpoint by writing to the address */ |
| 184 | + |
| 185 | + triggered = false; |
| 186 | + addr[0] = tmp; |
| 187 | + |
| 188 | + if (triggered) |
| 189 | + { |
| 190 | + printf("Pass: Write watchpoint at %p triggered\n", addr); |
| 191 | + } |
| 192 | + else |
| 193 | + { |
| 194 | + gdb_debugpoint_remove(test_type, addr, size); |
| 195 | + printf("ERROR: Write watchpoint at %p not triggered\n", addr); |
| 196 | + } |
| 197 | + } |
| 198 | + else if (r) |
| 199 | + { |
| 200 | + /* Trigger the watchpoint by reading the address */ |
| 201 | + |
| 202 | + triggered = false; |
| 203 | + if (addr[0] == 0x55) |
| 204 | + { |
| 205 | + /* Do something to avoid compiler removing the read */ |
| 206 | + |
| 207 | + printf("Reading %p\n", addr); |
| 208 | + } |
| 209 | + |
| 210 | + if (triggered) |
| 211 | + { |
| 212 | + printf("Pass: Read watchpoint at %p triggered\n", addr); |
| 213 | + } |
| 214 | + else |
| 215 | + { |
| 216 | + gdb_debugpoint_remove(test_type, addr, size); |
| 217 | + printf("ERROR: Read watchpoint at %p not triggered\n", addr); |
| 218 | + } |
| 219 | + } |
| 220 | + else if (w) |
| 221 | + { |
| 222 | + /** Trigger the watchpoint by writing to the address */ |
| 223 | + |
| 224 | + triggered = false; |
| 225 | + addr[0] = 0x55; |
| 226 | + |
| 227 | + if (triggered) |
| 228 | + { |
| 229 | + printf("Pass: Write watchpoint at %p triggered\n", addr); |
| 230 | + } |
| 231 | + else |
| 232 | + { |
| 233 | + gdb_debugpoint_remove(test_type, addr, size); |
| 234 | + printf("ERROR: Write watchpoint at %p not triggered\n", addr); |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +/**************************************************************************** |
| 240 | + * Public Functions |
| 241 | + ****************************************************************************/ |
| 242 | + |
| 243 | +/**************************************************************************** |
| 244 | + * Name: main |
| 245 | + ****************************************************************************/ |
| 246 | + |
| 247 | +int main(int argc, FAR char *argv[]) |
| 248 | +{ |
| 249 | + printf("Testing breakpoints\n"); |
| 250 | + |
| 251 | + /* Test breakpoint at a function */ |
| 252 | + |
| 253 | + test_function(test_trigger); |
| 254 | + |
| 255 | + /* Test read watchpoint for rodata */ |
| 256 | + |
| 257 | + test_data((uint8_t *)&g_test_rodata, sizeof(g_test_rodata), true, false); |
| 258 | + |
| 259 | + /* Test read watchpoint for data */ |
| 260 | + |
| 261 | + test_data((uint8_t *)&g_test_data, sizeof(g_test_data), true, false); |
| 262 | + |
| 263 | + /* Test write watchpoint for data */ |
| 264 | + |
| 265 | + test_data((uint8_t *)&g_test_data, sizeof(g_test_data), false, true); |
| 266 | + |
| 267 | + /* Test read/write watchpoint for data */ |
| 268 | + |
| 269 | + test_data((uint8_t *)&g_test_data, sizeof(g_test_data), true, true); |
| 270 | + |
| 271 | + return 0; |
| 272 | +} |
0 commit comments