1 /*!
2  * \file       opencsd_c_api.h
3  * \brief      OpenCSD : "C" API
4  *
5  * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
6  */
7 
8 /*
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef ARM_OPENCSD_C_API_H_INCLUDED
36 #define ARM_OPENCSD_C_API_H_INCLUDED
37 
38 /** @defgroup lib_c_api OpenCSD Library : Library "C" API.
39     @brief  "C" API for the OpenCSD Library
40 
41     Set of "C" wrapper functions for the OpenCSD library.
42 
43     Defines API, functions and callback types.
44 @{*/
45 
46 /* ensure C bindings */
47 
48 #if defined(WIN32)  /* windows bindings */
49     /** Building the C-API DLL **/
50     #ifdef _OCSD_C_API_DLL_EXPORT
51         #ifdef __cplusplus
52             #define OCSD_C_API extern "C" __declspec(dllexport)
53         #else
54             #define OCSD_C_API __declspec(dllexport)
55         #endif
56     #else
57         /** building or using the static C-API library **/
58         #if defined(_LIB) || defined(OCSD_USE_STATIC_C_API)
59             #ifdef __cplusplus
60                 #define OCSD_C_API extern "C"
61             #else
62                 #define OCSD_C_API
63             #endif
64         #else
65         /** using the C-API DLL **/
66             #ifdef __cplusplus
67                 #define OCSD_C_API extern "C" __declspec(dllimport)
68             #else
69                 #define OCSD_C_API __declspec(dllimport)
70             #endif
71         #endif
72     #endif
73 #else           /* linux bindings */
74     #ifdef __cplusplus
75         #define OCSD_C_API extern "C"
76     #else
77         #define OCSD_C_API
78     #endif
79 #endif
80 
81 #include "ocsd_c_api_types.h"
82 #include "ocsd_c_api_custom.h"
83 
84 /** @name Library Version API
85 
86 @{*/
87 /** Get Library version. Return a 32 bit version in form MMMMnnpp - MMMM = major verison, nn = minor version, pp = patch version */
88 OCSD_C_API uint32_t ocsd_get_version(void);
89 
90 /** Get library version string */
91 OCSD_C_API const char * ocsd_get_version_str(void);
92 /** @}*/
93 
94 /*---------------------- Trace Decode Tree ----------------------------------------------------------------------------------*/
95 
96 /** @name Library Decode Tree API
97 @{*/
98 
99 /*!
100  * Create a decode tree.
101  *
102  * @param src_type : Type of tree - formatted input, or single source input
103  * @param deformatterCfgFlags : Formatter flags - determine presence of frame syncs etc.
104  *
105  * @return dcd_tree_handle_t  : Handle to the decode tree. Handle value set to 0 if creation failed.
106  */
107 OCSD_C_API dcd_tree_handle_t ocsd_create_dcd_tree(const ocsd_dcd_tree_src_t src_type, const uint32_t deformatterCfgFlags);
108 
109 /*!
110  * Destroy a decode tree.
111  *
112  * Also destroys all the associated processors and decoders for the tree.
113  *
114  * @param handle : Handle for decode tree to destroy.
115  */
116 OCSD_C_API void ocsd_destroy_dcd_tree(const dcd_tree_handle_t handle);
117 
118 /*!
119  * Input trace data into the decoder.
120  *
121  * Large trace source buffers can be broken down into smaller fragments.
122  *
123  * @param handle : Handle to decode tree.
124  * @param op : Datapath operation.
125  * @param index : Trace buffer byte index for the start of the supplied data block.
126  * @param dataBlockSize : Size of data block.
127  * @param *pDataBlock : Pointer to data block.
128  * @param *numBytesProcessed : Number of bytes actually processed by the decoder.
129  *
130  * @return ocsd_datapath_resp_t  : Datapath response code (CONT/WAIT/FATAL)
131  */
132 OCSD_C_API ocsd_datapath_resp_t ocsd_dt_process_data(const dcd_tree_handle_t handle,
133                                             const ocsd_datapath_op_t op,
134                                             const ocsd_trc_index_t index,
135                                             const uint32_t dataBlockSize,
136                                             const uint8_t *pDataBlock,
137                                             uint32_t *numBytesProcessed);
138 
139 
140 /*---------------------- Generic Trace Element Output  --------------------------------------------------------------*/
141 
142 /*!
143  * Set the trace element output callback function.
144  *
145  * This function will be called for each decoded generic trace element generated by
146  * any full trace decoder in the decode tree.
147  *
148  * A single function is used for all trace source IDs in the decode tree.
149  *
150  * @param handle : Handle to decode tree.
151  * @param pFn : Pointer to the callback function.
152  * @param p_context : opaque context pointer value used in callback function.
153  *
154  * @return  ocsd_err_t  : Library error code -  OCSD_OK if successful.
155  */
156 OCSD_C_API ocsd_err_t ocsd_dt_set_gen_elem_outfn(const dcd_tree_handle_t handle, FnTraceElemIn pFn, const void *p_context);
157 
158 /*---------------------- Trace Decoders ----------------------------------------------------------------------------------*/
159 /*!
160 * Creates a decoder that is registered with the library under the supplied name.
161 * Flags determine if a full packet processor / packet decoder pair or
162 * packet processor only is created.
163 * Uses the supplied configuration structure.
164 *
165 * @param handle : Handle to decode tree.
166 * @param *decoder_name : Registered name of the decoder to create.
167 * @param create_flags : Decoder creation options.
168 * @param *decoder_cfg : Pointer to a valid configuration structure for the named decoder.
169 * @param *pCSID : Pointer to location to return the configured CoreSight trace ID for the decoder.
170 *
171 * @return ocsd_err_t  : Library error code -  OCSD_OK if successful.
172 */
173 OCSD_C_API ocsd_err_t ocsd_dt_create_decoder(const dcd_tree_handle_t handle,
174                                              const char *decoder_name,
175                                              const int create_flags,
176                                              const void *decoder_cfg,
177                                              unsigned char *pCSID
178                                              );
179 
180 /*!
181 * Remove a decoder from the tree and destroy it.
182 *
183 * @param handle :  Handle to decode tree.
184 * @param CSID : Configured CoreSight trace ID for the decoder.
185 *
186 * @return ocsd_err_t  : Library error code -  OCSD_OK if successful.
187 */
188 OCSD_C_API ocsd_err_t ocsd_dt_remove_decoder(   const dcd_tree_handle_t handle,
189                                                 const unsigned char CSID);
190 
191 
192 /*!
193 * Attach a callback function to the packet processor.
194 *
195 * The callback_type defines the attachment point, either the main packet output
196 * (only if no decoder attached), or the packet monitor.
197 *
198 * @param handle : Handle to decode tree.
199 * @param CSID : Configured CoreSight trace ID for the decoder.
200 * @param callback_type : Attachment point
201 * @param p_fn_pkt_data_in : Pointer to the callback function.
202 * @param p_context : Opaque context pointer value used in callback function.
203 *
204 * @return ocsd_err_t  : Library error code -  OCSD_OK if successful.
205 */
206 OCSD_C_API ocsd_err_t ocsd_dt_attach_packet_callback(  const dcd_tree_handle_t handle,
207                                                 const unsigned char CSID,
208                                                 const ocsd_c_api_cb_types callback_type,
209                                                 void *p_fn_callback_data,
210                                                 const void *p_context);
211 
212 
213 
214 
215 
216 
217 /** @}*/
218 /*---------------------- Memory Access for traced opcodes ----------------------------------------------------------------------------------*/
219 /** @name Library Memory Accessor configuration on decode tree.
220     @brief Configure the memory regions available for decode.
221 
222     Full decode requires memory regions set up to allow access to the traced
223     opcodes. Add memory buffers or binary file regions to a map of regions.
224 
225 @{*/
226 
227 /*!
228  * Add a binary file based memory range accessor to the decode tree.
229  *
230  * Adds the entire binary file as a memory space to be accessed
231  *
232  * @param handle : Handle to decode tree.
233  * @param address : Start address of memory area.
234  * @param mem_space : Associated memory space.
235  * @param *filepath : Path to binary data file.
236  *
237  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
238  */
239 OCSD_C_API ocsd_err_t ocsd_dt_add_binfile_mem_acc(const dcd_tree_handle_t handle, const ocsd_vaddr_t address, const ocsd_mem_space_acc_t mem_space, const char *filepath);
240 
241 /*!
242  * Add a binary file based memory range accessor to the decode tree.
243  *
244  * Add a binary file that contains multiple regions of memory with differing
245  * offsets wihtin the file.
246  *
247  * A linked list of file_mem_region_t structures is supplied. Each structure contains an
248  * offset into the binary file, the start address for this offset and the size of the region.
249  *
250  * @param handle : Handle to decode tree.
251  * @param region_list : Array of memory regions in the file.
252  * @param num_regions : Size of region array
253  * @param mem_space : Associated memory space.
254  * @param *filepath : Path to binary data file.
255  *
256  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
257  */
258 OCSD_C_API ocsd_err_t ocsd_dt_add_binfile_region_mem_acc(const dcd_tree_handle_t handle, const ocsd_file_mem_region_t *region_array, const int num_regions, const ocsd_mem_space_acc_t mem_space, const char *filepath);
259 
260 /*!
261  * Add a memory buffer based memory range accessor to the decode tree.
262  *
263  * @param handle : Handle to decode tree.
264  * @param address : Start address of memory area.
265  * @param mem_space : Associated memory space.
266  * @param *p_mem_buffer : pointer to memory buffer.
267  * @param mem_length : Size of memory buffer.
268  *
269  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
270  */
271 OCSD_C_API ocsd_err_t ocsd_dt_add_buffer_mem_acc(const dcd_tree_handle_t handle, const ocsd_vaddr_t address, const ocsd_mem_space_acc_t mem_space, const uint8_t *p_mem_buffer, const uint32_t mem_length);
272 
273 
274 /*!
275  * Add a memory access callback function. The decoder will call the function for opcode addresses in the
276  * address range supplied for the memory spaces covered.
277  *
278  * @param handle : Handle to decode tree.
279  * @param st_address :  Start address of memory area covered by the callback.
280  * @param en_address :  End address of the memory area covered by the callback. (inclusive)
281  * @param mem_space : Memory space(s) covered by the callback.
282  * @param p_cb_func : Callback function
283  * @param p_context : opaque context pointer value used in callback function.
284  *
285  * @return OCSD_C_API ocsd_err_t  : Library error code -  RCDTL_OK if successful.
286  */
287 OCSD_C_API ocsd_err_t ocsd_dt_add_callback_mem_acc(const dcd_tree_handle_t handle, const ocsd_vaddr_t st_address, const ocsd_vaddr_t en_address, const ocsd_mem_space_acc_t mem_space, Fn_MemAcc_CB p_cb_func, const void *p_context);
288 
289 /*!
290  * Remove a memory accessor by address and memory space.
291  *
292  * @param handle : Handle to decode tree.
293  * @param st_address : Start address of memory accessor.
294  * @param mem_space : Memory space(s) covered by the accessor.
295  *
296  * @return OCSD_C_API ocsd_err_t  : Library error code -  RCDTL_OK if successful.
297  */
298 OCSD_C_API ocsd_err_t ocsd_dt_remove_mem_acc(const dcd_tree_handle_t handle, const ocsd_vaddr_t st_address, const ocsd_mem_space_acc_t mem_space);
299 
300 /*
301  *  Print the mapped memory accessor ranges to the configured logger.
302  *
303  * @param handle : Handle to decode tree.
304  */
305 OCSD_C_API void ocsd_tl_log_mapped_mem_ranges(const dcd_tree_handle_t handle);
306 
307 /** @}*/
308 
309 /** @name Library Default Error Log Object API
310     @brief Configure the default error logging object in the library.
311 
312     Objects created by the decode trees will use this error logger. Configure for
313     desired error severity, and to enable print or logfile output.
314 
315 @{*/
316 
317 /*---------------------- Library Logging and debug ----------------------------------------------------------------------------------*/
318 /*!
319  * Initialise the library error logger.
320  *
321  * Choose severity of errors logger, and if the errors will be logged to screen and / or logfile.
322  *
323  * @param verbosity : Severity of errors that will be logged.
324  * @param create_output_logger : Set to none-zero to create an output printer.
325  *
326  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
327  */
328 OCSD_C_API ocsd_err_t ocsd_def_errlog_init(const ocsd_err_severity_t verbosity, const int create_output_logger);
329 
330 /*!
331  * Configure the output logger. Choose STDOUT, STDERR and/or log to file.
332  * Optionally provide a log file name.
333  *
334  * @param output_flags : OR combination of required  C_API_MSGLOGOUT_FLG_* flags.
335  * @param *log_file_name : optional filename if logging to file. Set to NULL if not needed.
336  *
337  * @return OCSD_C_API ocsd_err_t  : Library error code -  RCDTL_OK if successful.
338  */
339 OCSD_C_API ocsd_err_t ocsd_def_errlog_config_output(const int output_flags, const char *log_file_name);
340 
341 /*!
342  * Configure the library default error logger to send all strings it is outputting back to the client
343  * to allow printing within the client application. This is in additional to any other log destinations
344  * set in ocsd_def_errlog_init().
345  *
346  * @param *p_context : opaque context pointer
347  * @param p_str_print_cb : client callback function to "print" logstring.
348  */
349 OCSD_C_API ocsd_err_t ocsd_def_errlog_set_strprint_cb(const dcd_tree_handle_t handle, void *p_context, FnDefLoggerPrintStrCB p_str_print_cb);
350 
351 /*!
352  * Print a message via the library output printer - if enabled.
353  *
354  * @param *msg : Message to output.
355  *
356  */
357 OCSD_C_API void ocsd_def_errlog_msgout(const char *msg);
358 
359 
360 /** @}*/
361 
362 /** @name Packet to string interface
363 
364 @{*/
365 
366 /*!
367  * Take a packet structure and render a string representation of the packet data.
368  *
369  * Returns a '0' terminated string of (buffer_size - 1) length or less.
370  *
371  * @param pkt_protocol : Packet protocol type - used to interpret the packet pointer
372  * @param *p_pkt : pointer to a valid packet structure of protocol type. cast to void *.
373  * @param *buffer : character buffer for string.
374  * @param buffer_size : size of character buffer.
375  *
376  * @return  ocsd_err_t  : Library error code -  RCDTL_OK if successful.
377  */
378 OCSD_C_API ocsd_err_t ocsd_pkt_str(const ocsd_trace_protocol_t pkt_protocol, const void *p_pkt, char *buffer, const int buffer_size);
379 
380 /*!
381  * Get a string representation of the generic trace element.
382  *
383  * @param *p_pkt : pointer to valid generic element structure.
384  * @param *buffer : character buffer for string.
385  * @param buffer_size : size of character buffer.
386  *
387  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
388  */
389 OCSD_C_API ocsd_err_t ocsd_gen_elem_str(const ocsd_generic_trace_elem *p_pkt, char *buffer, const int buffer_size);
390 
391 
392 /*!
393  * Init a generic element with type, clearing any flags etc.
394  */
395 OCSD_C_API void ocsd_gen_elem_init(ocsd_generic_trace_elem *p_pkt, const ocsd_gen_trc_elem_t elem_type);
396 
397 /** @}*/
398 
399 /** @name Library packet and data printer control API
400     @brief Allows client to use libraries packet and data printers to log packets etc rather than attach callbacks
401     to packet output and use packet to string calls.
402 @{*/
403 
404 /*!
405  * Set a raw frame printer on the trace frame demuxer. Allows inspection of raw trace data frames for debug.
406  * Prints via the library default error logging mechanisms.
407  *
408  * The flags input determines the data printed. OR combination of one or both of:
409  * OCSD_DFRMTR_PACKED_RAW_OUT   : Output the undemuxed raw data frames.
410  * OCSD_DFRMTR_UNPACKED_RAW_OUT : Output the raw data by trace ID after unpacking the frame.
411  *
412  * @param handle : Handle to decode tree.
413  * @param flags : indicates type of raw frames to print.
414  *
415  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
416  */
417 OCSD_C_API ocsd_err_t ocsd_dt_set_raw_frame_printer(const dcd_tree_handle_t handle, int flags);
418 
419 /*!
420  * Set a library printer on the generic element output of a full decoder.
421  *
422  * @param handle : Handle to decode tree.
423  *
424  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
425  */
426 OCSD_C_API ocsd_err_t ocsd_dt_set_gen_elem_printer(const dcd_tree_handle_t handle);
427 
428 /*!
429  * Attach a library printer to the packet processor. May be attached to the main packet output, or the monitor
430  * output if the main packet output is to be attached to a packet decoder in the datapath.
431  *
432  * @param handle : Handle to decode tree.
433  * @param cs_id  : Coresight trace ID for stream to print.
434  * @param monitor: 0 to attach printer directly to datapath packet output, 1 to attach to packet monitor output
435  *
436  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
437  */
438 OCSD_C_API ocsd_err_t ocsd_dt_set_pkt_protocol_printer(const dcd_tree_handle_t handle, uint8_t cs_id, int monitor);
439 
440 /** @}*/
441 
442 
443 /** @name Custom Decoder API functions
444 
445 @{*/
446 
447 /** Register a custom decoder with the library
448 
449     @param *name : Name under which to register the decoder.
450     @param *p_dcd_fact : Custom decoder factory structure.
451 
452     @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
453 */
454 OCSD_C_API ocsd_err_t ocsd_register_custom_decoder(const char *name, ocsd_extern_dcd_fact_t *p_dcd_fact);
455 
456 /** Clear all registered decoders - library cleanup
457 
458     @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
459 */
460 OCSD_C_API ocsd_err_t ocsd_deregister_decoders(void);
461 
462 /** Get a string representation of a custom protocol packet.
463 
464     Specific function to extract the packet string for a custom protocol ID only. Custom IDs are allocated to decoder factories
465     during the ocsd_register_custom_decoder() process.
466 
467     This function is called by ocsd_pkt_str() when the incoming protocol is a custom ID.
468 
469     @param pkt_protocol : Packet protocol type - must be in the custom ID range ( >= OCSD_PROTOCOL_CUSTOM_0, < OCSD_PROTOCOL_END)
470     @param *p_pkt : pointer to a valid packet structure of protocol type. cast to void *.
471     @param *buffer : character buffer for string.
472     @param buffer_size : size of character buffer.
473 
474     @return ocsd_err_t  : Library error code -  RCDTL_OK if successful, OCSD_ERR_NO_PROTOCOL if input ID not in custom range or not in use.
475 */
476 OCSD_C_API ocsd_err_t ocsd_cust_protocol_to_str(const ocsd_trace_protocol_t pkt_protocol, const void *trc_pkt, char *buffer, const int buflen);
477 
478 /** @}*/
479 
480 
481 /** @}*/
482 
483 #endif // ARM_OPENCSD_C_API_H_INCLUDED
484 
485 /* End of File opencsd_c_api.h */
486