• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 version, 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 /*!
291  * Add a memory access callback function. The decoder will call the function for opcode addresses in the
292  * address range supplied for the memory spaces covered.
293  *
294  * @param handle : Handle to decode tree.
295  * @param st_address :  Start address of memory area covered by the callback.
296  * @param en_address :  End address of the memory area covered by the callback. (inclusive)
297  * @param mem_space : Memory space(s) covered by the callback.
298  * @param p_cb_func : Callback function - Signature for CB with Trace ID passed to client.
299  * @param p_context : opaque context pointer value used in callback function.
300  *
301  * @return OCSD_C_API ocsd_err_t  : Library error code -  RCDTL_OK if successful.
302  */
303 OCSD_C_API ocsd_err_t ocsd_dt_add_callback_trcid_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_MemAccID_CB p_cb_func, const void *p_context);
304 
305 
306 /*!
307  * Remove a memory accessor by address and memory space.
308  *
309  * @param handle : Handle to decode tree.
310  * @param st_address : Start address of memory accessor.
311  * @param mem_space : Memory space(s) covered by the accessor.
312  *
313  * @return OCSD_C_API ocsd_err_t  : Library error code -  RCDTL_OK if successful.
314  */
315 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);
316 
317 /*
318  *  Print the mapped memory accessor ranges to the configured logger.
319  *
320  * @param handle : Handle to decode tree.
321  */
322 OCSD_C_API void ocsd_tl_log_mapped_mem_ranges(const dcd_tree_handle_t handle);
323 
324 /** @}*/
325 
326 /** @name Library Default Error Log Object API
327     @brief Configure the default error logging object in the library.
328 
329     Objects created by the decode trees will use this error logger. Configure for
330     desired error severity, and to enable print or logfile output.
331 
332 @{*/
333 
334 /*---------------------- Library Logging and debug ----------------------------------------------------------------------------------*/
335 /*!
336  * Initialise the library error logger.
337  *
338  * Choose severity of errors logger, and if the errors will be logged to screen and / or logfile.
339  *
340  * @param verbosity : Severity of errors that will be logged.
341  * @param create_output_logger : Set to none-zero to create an output printer.
342  *
343  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
344  */
345 OCSD_C_API ocsd_err_t ocsd_def_errlog_init(const ocsd_err_severity_t verbosity, const int create_output_logger);
346 
347 /*!
348  * Configure the output logger. Choose STDOUT, STDERR and/or log to file.
349  * Optionally provide a log file name.
350  *
351  * @param output_flags : OR combination of required  C_API_MSGLOGOUT_FLG_* flags.
352  * @param *log_file_name : optional filename if logging to file. Set to NULL if not needed.
353  *
354  * @return OCSD_C_API ocsd_err_t  : Library error code -  RCDTL_OK if successful.
355  */
356 OCSD_C_API ocsd_err_t ocsd_def_errlog_config_output(const int output_flags, const char *log_file_name);
357 
358 /*!
359  * Configure the library default error logger to send all strings it is outputting back to the client
360  * to allow printing within the client application. This is in additional to any other log destinations
361  * set in ocsd_def_errlog_init().
362  *
363  * @param *p_context : opaque context pointer
364  * @param p_str_print_cb : client callback function to "print" logstring.
365  */
366 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);
367 
368 /*!
369  * Print a message via the library output printer - if enabled.
370  *
371  * @param *msg : Message to output.
372  *
373  */
374 OCSD_C_API void ocsd_def_errlog_msgout(const char *msg);
375 
376 
377 /** @}*/
378 
379 /** @name Packet to string interface
380 
381 @{*/
382 
383 /*!
384  * Take a packet structure and render a string representation of the packet data.
385  *
386  * Returns a '0' terminated string of (buffer_size - 1) length or less.
387  *
388  * @param pkt_protocol : Packet protocol type - used to interpret the packet pointer
389  * @param *p_pkt : pointer to a valid packet structure of protocol type. cast to void *.
390  * @param *buffer : character buffer for string.
391  * @param buffer_size : size of character buffer.
392  *
393  * @return  ocsd_err_t  : Library error code -  RCDTL_OK if successful.
394  */
395 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);
396 
397 /*!
398  * Get a string representation of the generic trace element.
399  *
400  * @param *p_pkt : pointer to valid generic element structure.
401  * @param *buffer : character buffer for string.
402  * @param buffer_size : size of character buffer.
403  *
404  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
405  */
406 OCSD_C_API ocsd_err_t ocsd_gen_elem_str(const ocsd_generic_trace_elem *p_pkt, char *buffer, const int buffer_size);
407 
408 
409 /*!
410  * Init a generic element with type, clearing any flags etc.
411  */
412 OCSD_C_API void ocsd_gen_elem_init(ocsd_generic_trace_elem *p_pkt, const ocsd_gen_trc_elem_t elem_type);
413 
414 /** @}*/
415 
416 /** @name Library packet and data printer control API
417     @brief Allows client to use libraries packet and data printers to log packets etc rather than attach callbacks
418     to packet output and use packet to string calls.
419 @{*/
420 
421 /*!
422  * Set a raw frame printer on the trace frame demuxer. Allows inspection of raw trace data frames for debug.
423  * Prints via the library default error logging mechanisms.
424  *
425  * The flags input determines the data printed. OR combination of one or both of:
426  * OCSD_DFRMTR_PACKED_RAW_OUT   : Output the undemuxed raw data frames.
427  * OCSD_DFRMTR_UNPACKED_RAW_OUT : Output the raw data by trace ID after unpacking the frame.
428  *
429  * @param handle : Handle to decode tree.
430  * @param flags : indicates type of raw frames to print.
431  *
432  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
433  */
434 OCSD_C_API ocsd_err_t ocsd_dt_set_raw_frame_printer(const dcd_tree_handle_t handle, int flags);
435 
436 /*!
437  * Set a library printer on the generic element output of a full decoder.
438  *
439  * @param handle : Handle to decode tree.
440  *
441  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
442  */
443 OCSD_C_API ocsd_err_t ocsd_dt_set_gen_elem_printer(const dcd_tree_handle_t handle);
444 
445 /*!
446  * Attach a library printer to the packet processor. May be attached to the main packet output, or the monitor
447  * output if the main packet output is to be attached to a packet decoder in the datapath.
448  *
449  * @param handle : Handle to decode tree.
450  * @param cs_id  : Coresight trace ID for stream to print.
451  * @param monitor: 0 to attach printer directly to datapath packet output, 1 to attach to packet monitor output
452  *
453  * @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
454  */
455 OCSD_C_API ocsd_err_t ocsd_dt_set_pkt_protocol_printer(const dcd_tree_handle_t handle, uint8_t cs_id, int monitor);
456 
457 /** @}*/
458 
459 
460 /** @name Custom Decoder API functions
461 
462 @{*/
463 
464 /** Register a custom decoder with the library
465 
466     @param *name : Name under which to register the decoder.
467     @param *p_dcd_fact : Custom decoder factory structure.
468 
469     @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
470 */
471 OCSD_C_API ocsd_err_t ocsd_register_custom_decoder(const char *name, ocsd_extern_dcd_fact_t *p_dcd_fact);
472 
473 /** Clear all registered decoders - library cleanup
474 
475     @return ocsd_err_t  : Library error code -  RCDTL_OK if successful.
476 */
477 OCSD_C_API ocsd_err_t ocsd_deregister_decoders(void);
478 
479 /** Get a string representation of a custom protocol packet.
480 
481     Specific function to extract the packet string for a custom protocol ID only. Custom IDs are allocated to decoder factories
482     during the ocsd_register_custom_decoder() process.
483 
484     This function is called by ocsd_pkt_str() when the incoming protocol is a custom ID.
485 
486     @param pkt_protocol : Packet protocol type - must be in the custom ID range ( >= OCSD_PROTOCOL_CUSTOM_0, < OCSD_PROTOCOL_END)
487     @param *p_pkt : pointer to a valid packet structure of protocol type. cast to void *.
488     @param *buffer : character buffer for string.
489     @param buffer_size : size of character buffer.
490 
491     @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.
492 */
493 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);
494 
495 /** @}*/
496 
497 
498 /** @}*/
499 
500 #endif // ARM_OPENCSD_C_API_H_INCLUDED
501 
502 /* End of File opencsd_c_api.h */
503