Basil Eljuse | 4b14afb | 2020-09-30 13:07:23 +0100 | [diff] [blame] | 1 | /*! |
| 2 | ############################################################################## |
| 3 | # Copyright (c) 2020, ARM Limited and Contributors. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | ############################################################################## |
| 7 | */ |
| 8 | |
| 9 | #include "plugin_utils.h" |
| 10 | |
| 11 | // Get a named trace source, create an event class from the named subset of |
| 12 | // event fields, register the event class and look up the field indexes, and |
| 13 | // register a user-provided MTI callback with the trace source. |
| 14 | // Writes to error_ss and returns false if anything fails. |
| 15 | bool RegisterCallbackForComponent(const MTI::ComponentTraceInterface *mti, |
| 16 | const char *trace_source, |
| 17 | ValueBind_t *value_bind, void *this_ptr, |
| 18 | MTI::CallbackT callback, |
| 19 | MTI::EventClass **ptr_event_class, |
| 20 | std::stringstream &error_ss) |
| 21 | { |
| 22 | const MTI::TraceSource *source = mti->GetTraceSource(trace_source); |
| 23 | if (!source) { |
| 24 | error_ss << "Could not find " << trace_source << " source"; |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | MTI::FieldMask mask = 0; |
| 29 | const MTI::EventFieldType *eft; |
| 30 | |
| 31 | for(unsigned i=0; value_bind[i].name != 0; i++) { |
| 32 | if ((eft = source->GetField( value_bind[i].name )) != 0) { |
| 33 | mask |= 1 << eft->GetIndex(); |
| 34 | } else { |
| 35 | error_ss << "No field " << value_bind[i].name << |
| 36 | " found in " << trace_source << " trace source"; |
| 37 | return false; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | MTI::EventClass *event_class = source->CreateEventClass(mask); |
| 42 | if (!event_class) { |
| 43 | error_ss << "Unable to register event class for " << |
| 44 | trace_source << " trace source."; |
| 45 | return false; |
| 46 | } |
| 47 | for(unsigned i=0; value_bind[i].name != 0; i++) |
| 48 | { |
| 49 | MTI::ValueIndex idx = event_class->GetValueIndex(value_bind[i].name); |
| 50 | if (idx != -1) { |
| 51 | *(value_bind[i].index) = idx; |
| 52 | } else { |
| 53 | error_ss << "Unable to GetValueIndex for " << trace_source |
| 54 | << "." << value_bind[i].name << "."; |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | if (callback && |
| 59 | event_class->RegisterCallback(callback, this_ptr) != MTI::MTI_OK) { |
| 60 | error_ss << "RegisterCallback failed for " << trace_source; |
| 61 | return false; |
| 62 | } |
| 63 | *ptr_event_class = event_class; |
| 64 | return true; |
| 65 | } |