| 1 | /* |
| 2 | csdebug.h: |
| 3 | |
| 4 | Copyright (C) 2014 Andres Cabrera |
| 5 | |
| 6 | This file is part of Csound. |
| 7 | |
| 8 | The Csound Library is free software; you can redistribute it |
| 9 | and/or modify it under the terms of the GNU Lesser General Public |
| 10 | License as published by the Free Software Foundation; either |
| 11 | version 2.1 of the License, or (at your option) any later version. |
| 12 | |
| 13 | Csound is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU Lesser General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU Lesser General Public |
| 19 | License along with Csound; if not, write to the Free Software |
| 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | 02110-1301 USA |
| 22 | */ |
| 23 | |
| 24 | #ifndef CSDEBUG_H |
| 25 | #define CSDEBUG_H |
| 26 | |
| 27 | /** |
| 28 | * \file csdebug.h |
| 29 | * |
| 30 | * This header provides the debugger API which is part of libcsound. |
| 31 | * |
| 32 | * Basic usage of the debugger is this: |
| 33 | * |
| 34 | * \code |
| 35 | CSOUND* csound = csoundCreate(NULL); |
| 36 | csoundCompileOrc(csound, " "); |
| 37 | csoundStart(csound); |
| 38 | csoundDebuggerInit(csound); |
| 39 | csoundSetBreakpointCallback(csound, brkpt_cb, &userdata); |
| 40 | csoundSetInstrumentBreakpoint(csound, 1, 5); |
| 41 | |
| 42 | // Run Csound Performance here |
| 43 | // The breakpoint callback will be called when instrument 1 has been |
| 44 | // instantiated and 5 control blocks have been processed. |
| 45 | |
| 46 | csoundDebuggerClean(csound); |
| 47 | csoundDestroy(csound); |
| 48 | * \endcode |
| 49 | * |
| 50 | */ |
| 51 | |
| 52 | #ifdef __BUILDING_LIBCSOUND |
| 53 | #include "csoundCore.h" |
| 54 | #else |
| 55 | #include "csound.h" |
| 56 | #endif |
| 57 | |
| 58 | #include "csound_type_system.h" |
| 59 | |
| 60 | |
| 61 | /** @defgroup DEBUGGER Debugger |
| 62 | * |
| 63 | * @{ */ |
| 64 | |
| 65 | typedef struct debug_instr_s { |
| 66 | CS_VARIABLE *varPoolHead; |
| 67 | MYFLT *lclbas; |
| 68 | void *instrptr; |
| 69 | MYFLT p1, p2, p3; |
| 70 | uint64_t kcounter; |
| 71 | int line; |
| 72 | struct debug_instr_s *next; |
| 73 | } debug_instr_t; |
| 74 | |
| 75 | typedef struct debug_opcode_s { |
| 76 | char opname[16]; |
| 77 | int line; |
| 78 | // TODO: Fill opcode linked list |
| 79 | struct debug_opcode_s *next; |
| 80 | struct debug_opcode_s *prev; |
| 81 | } debug_opcode_t; |
| 82 | |
| 83 | typedef struct debug_variable_s { |
| 84 | const char *name; |
| 85 | const char *typeName; |
| 86 | void *data; |
| 87 | struct debug_variable_s *next; |
| 88 | } debug_variable_t; |
| 89 | |
| 90 | typedef struct { |
| 91 | debug_instr_t *breakpointInstr; |
| 92 | debug_variable_t *instrVarList; |
| 93 | debug_instr_t *instrListHead; |
| 94 | debug_opcode_t *currentOpcode; |
| 95 | } debug_bkpt_info_t; |
| 96 | |
| 97 | |
| 98 | /** \cond DOXYGEN_HIDDEN |
| 99 | * These types should not appear in the Doxygen docs */ |
| 100 | |
| 101 | |
| 102 | typedef enum { |
| 103 | CSDEBUG_BKPT_LINE, |
| 104 | CSDEBUG_BKPT_INSTR, |
| 105 | CSDEBUG_BKPT_DELETE, |
| 106 | CSDEBUG_BKPT_CLEAR_ALL |
| 107 | } bkpt_mode_t; |
| 108 | |
| 109 | typedef enum { |
| 110 | CSDEBUG_STATUS_RUNNING, |
| 111 | CSDEBUG_STATUS_STOPPED, |
| 112 | CSDEBUG_STATUS_NEXT |
| 113 | } debug_status_t; |
| 114 | |
| 115 | typedef struct bkpt_node_s { |
| 116 | int line; /* if line is < 0 breakpoint is for instrument instances */ |
| 117 | MYFLT instr; /* instrument number (including fractional part */ |
| 118 | int skip; /* number of times to skip when arriving at the breakpoint */ |
| 119 | int count; /* current backwards count for skip, when 0 break */ |
| 120 | bkpt_mode_t mode; |
| 121 | struct bkpt_node_s *next; |
| 122 | } bkpt_node_t; |
| 123 | |
| 124 | typedef enum { |
| 125 | CSDEBUG_CMD_NONE, |
| 126 | CSDEBUG_CMD_STEPOVER, |
| 127 | CSDEBUG_CMD_STEPINTO, |
| 128 | CSDEBUG_CMD_NEXT, |
| 129 | CSDEBUG_CMD_CONTINUE, |
| 130 | CSDEBUG_CMD_STOP |
| 131 | } debug_command_t; |
| 132 | |
| 133 | typedef enum { |
| 134 | CSDEBUG_OFF = 0x0, |
| 135 | CSDEBUG_K = 0x01, |
| 136 | CSDEBUG_INIT = 0x02 |
| 137 | } debug_mode_t; |
| 138 | |
| 139 | #ifdef __BUILD_LIBCSOUND |
| 140 | |
| 141 | void csoundDebuggerBreakpointReached(CSOUND *csound); |
| 142 | |
| 143 | #endif |
| 144 | |
| 145 | /** @endcond */ |
| 146 | |
| 147 | #ifdef __cplusplus |
| 148 | extern "C" { |
| 149 | #endif |
| 150 | |
| 151 | |
| 152 | /** Breakpoint callback function type |
| 153 | * |
| 154 | * When a breakpoint is reached, the debugger will call a function of this type |
| 155 | * see csoundSetBreakpointCallback() */ |
| 156 | typedef void (*breakpoint_cb_t) (CSOUND *, debug_bkpt_info_t *, void *userdata); |
| 157 | |
| 158 | typedef struct csdebug_data_s { |
| 159 | void *bkpt_buffer; /* for passing breakpoints to the running engine */ |
| 160 | void *cmd_buffer; /* for passing commands to the running engine */ |
| 161 | debug_status_t status; |
| 162 | bkpt_node_t *bkpt_anchor; /* linked list for breakpoints */ |
| 163 | bkpt_node_t *cur_bkpt; /* current breakpoint where we are stopped */ |
| 164 | breakpoint_cb_t bkpt_cb; |
| 165 | void *cb_data; |
| 166 | void *debug_instr_ptr; /* != NULL when stopped at a breakpoint. |
| 167 | Holds INSDS * */ |
| 168 | void *debug_opcode_ptr; /* != NULL when stopped at a line breakpoint. |
| 169 | Holds OPDS * */ |
| 170 | } csdebug_data_t; |
| 171 | |
| 172 | /** Intialize debugger facilities |
| 173 | * |
| 174 | * This function allocates debugger structures, and enables its usage. |
| 175 | * There is a small performance penalty when using the debugger, so |
| 176 | * be sure to call csoundDebuggerClean() after use. |
| 177 | * |
| 178 | * This call is not thread safe and must be called before performance starts. |
| 179 | * |
| 180 | * @param csound A Csound instance |
| 181 | */ |
| 182 | PUBLIC void csoundDebuggerInit(CSOUND *csound); |
| 183 | |
| 184 | /** Cleanup debugger facilities |
| 185 | * |
| 186 | * @param csound A Csound instance |
| 187 | */ |
| 188 | PUBLIC void csoundDebuggerClean(CSOUND *csound); |
| 189 | |
| 190 | /** Set a breakpoint on a particular line |
| 191 | * |
| 192 | * @param csound A Csound instance @param line The line on which to |
| 193 | * set a breakpoint |
| 194 | * @param instr When set to 0, the line number refers |
| 195 | * to the line number in the score. When a number is given, it should |
| 196 | * refer to an instrument that has been compiled on the fly using |
| 197 | * csoundParseOrc(). |
| 198 | * @param skip number of control blocks to skip |
| 199 | * |
| 200 | */ |
| 201 | PUBLIC void csoundSetBreakpoint(CSOUND *csound, int line, int instr, int skip); |
| 202 | |
| 203 | /** Remove a previously set line breakpoint |
| 204 | * |
| 205 | */ |
| 206 | PUBLIC void csoundRemoveBreakpoint(CSOUND *csound, int line, int instr); |
| 207 | |
| 208 | /** Set a breakpoint for an instrument number |
| 209 | * |
| 210 | * Sets a breakpoint for an instrument number with optional number of skip |
| 211 | * control blocks. You can specify a fractional instrument number to identify |
| 212 | * particular instances. Specifying a value greater than 1 will result in that |
| 213 | * number of control blocks being skipped before this breakpoint is called |
| 214 | * again. A value of 0 and 1 has the same effect. |
| 215 | * |
| 216 | * This call is thread safe, as the breakpoint will be put in a lock free queue |
| 217 | * that is processed as soon as possible in the kperf function. |
| 218 | * |
| 219 | * @param csound a Csound instance |
| 220 | * @param instr instrument number |
| 221 | * @param skip number of control blocks to skip |
| 222 | */ |
| 223 | PUBLIC void csoundSetInstrumentBreakpoint(CSOUND *csound, MYFLT instr, int skip); |
| 224 | |
| 225 | /** Remove instrument breakpoint |
| 226 | * |
| 227 | * Removes an instrument breakpoint from the breakpoint list. Csound will no |
| 228 | * longer break at that instrument |
| 229 | * |
| 230 | * This call is thread safe, as the breakpoint will be put in a lock free queue |
| 231 | * that is processed as soon as possible in the kperf function. |
| 232 | */ |
| 233 | PUBLIC void csoundRemoveInstrumentBreakpoint(CSOUND *csound, MYFLT instr); |
| 234 | |
| 235 | /** Clear all breakpoints |
| 236 | * |
| 237 | * Removes all breakpoints. This call is thread safe, as it will be processed |
| 238 | * as soon as possible in the kperf function. |
| 239 | */ |
| 240 | PUBLIC void csoundClearBreakpoints(CSOUND *csound); |
| 241 | |
| 242 | /** Sets the breakpoint callback function |
| 243 | * |
| 244 | * Sets the function that will be called when a breakpoint is reached. |
| 245 | * |
| 246 | * @param csound Csound instance pointer |
| 247 | * @param bkpt_cb pointer to breakpoint callback function |
| 248 | * @param userdata pointer to user data that will be passed to the callback |
| 249 | * function |
| 250 | */ |
| 251 | PUBLIC void csoundSetBreakpointCallback(CSOUND *csound, |
| 252 | breakpoint_cb_t bkpt_cb, void *userdata); |
| 253 | |
| 254 | /* Not implemented yet, so not exposed in the API |
| 255 | PUBLIC void csoundDebugStepOver(CSOUND *csound); |
| 256 | PUBLIC void csoundDebugStepInto(CSOUND *csound); |
| 257 | */ |
| 258 | |
| 259 | /** Continue execution and break at next instrument instance |
| 260 | * |
| 261 | * Call this function to continue execution but automatically stop at |
| 262 | * next instrument instance. |
| 263 | */ |
| 264 | PUBLIC void csoundDebugNext(CSOUND *csound); |
| 265 | |
| 266 | /** Continue execution from breakpoint |
| 267 | * |
| 268 | * Call this function to continue execution of a Csound instance which is |
| 269 | * stopped because a breakpoint has been reached. This function will continue |
| 270 | * traversing the instrument chain from the instrument instance that |
| 271 | * triggered the break. |
| 272 | */ |
| 273 | PUBLIC void csoundDebugContinue(CSOUND *csound); |
| 274 | |
| 275 | /** Stop Csound rendering and enter the debugger |
| 276 | * |
| 277 | * Calling this function will enter the debugger at the soonest possible point |
| 278 | * as if a breakpoint had been reached. |
| 279 | */ |
| 280 | PUBLIC void csoundDebugStop(CSOUND *csound); |
| 281 | |
| 282 | /** Get a list of active instrument instances |
| 283 | * Returns a linked list of allocated instrument instances |
| 284 | * csoundDebugFreeInstrInstances() must be called on the list once it is no |
| 285 | * longer needed. |
| 286 | * This function is not thread safe and should only be called while the csound |
| 287 | * engine is stopped at a breakpoint. |
| 288 | */ |
| 289 | PUBLIC debug_instr_t *csoundDebugGetInstrInstances(CSOUND *csound); |
| 290 | |
| 291 | /** Free list created by csoundDebugGetCurrentInstrInstance() or |
| 292 | * csoundDebugGetInstrInstances() |
| 293 | */ |
| 294 | PUBLIC void csoundDebugFreeInstrInstances(CSOUND *csound, debug_instr_t *instr); |
| 295 | |
| 296 | /** Get list of variables for instrument */ |
| 297 | PUBLIC debug_variable_t *csoundDebugGetVariables(CSOUND *csound, |
| 298 | debug_instr_t *instr); |
| 299 | |
| 300 | /** Free variable list generated by csoundDebugGetVariables() */ |
| 301 | PUBLIC void csoundDebugFreeVariables(CSOUND *csound, |
| 302 | debug_variable_t *varHead); |
| 303 | |
| 304 | |
| 305 | /** @} */ |
| 306 | |
| 307 | #ifdef __cplusplus |
| 308 | } |
| 309 | #endif |
| 310 | |
| 311 | #endif |