source: rtems/cpukit/libmisc/capture/capture.c @ 249730de

5
Last change on this file since 249730de was 249730de, checked in by Sebastian Huber <sebastian.huber@…>, on 12/13/17 at 07:25:43

capture: Include <rtems/captureimpl.h>

Prepare for header file move to common include directory.

Update #3254.

  • Property mode set to 100644
File size: 35.8 KB
Line 
1/*
2  ------------------------------------------------------------------------
3
4  Copyright 2002, 2016 Chris Johns <chrisj@rtems.org>.
5  All rights reserved.
6
7  COPYRIGHT (c) 1989-2014.
8  On-Line Applications Research Corporation (OAR).
9
10  The license and distribution terms for this file may be
11  found in the file LICENSE in this distribution.
12
13  This software with is provided ``as is'' and with NO WARRANTY.
14
15  ------------------------------------------------------------------------
16
17  RTEMS Performance Monitoring and Measurement Framework.
18
19  This is the Capture Engine component.
20
21*/
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <stdlib.h>
28#include <string.h>
29
30#include <rtems/captureimpl.h>
31#include "capture_buffer.h"
32
33/*
34 * These events are always recorded and are not part of the
35 * watch filters.
36 *
37 * This feature has been disabled as it becomes confusing when
38 * setting up filters and some event leak.
39 */
40#if defined (RTEMS_CAPTURE_ENGINE_ALLOW_RELATED_EVENTS)
41#define RTEMS_CAPTURE_RECORD_EVENTS  (RTEMS_CAPTURE_CREATED_BY_EVENT | \
42                                      RTEMS_CAPTURE_CREATED_EVENT | \
43                                      RTEMS_CAPTURE_STARTED_BY_EVENT | \
44                                      RTEMS_CAPTURE_STARTED_EVENT | \
45                                      RTEMS_CAPTURE_RESTARTED_BY_EVENT | \
46                                      RTEMS_CAPTURE_RESTARTED_EVENT | \
47                                      RTEMS_CAPTURE_DELETED_BY_EVENT | \
48                                      RTEMS_CAPTURE_DELETED_EVENT | \
49                                      RTEMS_CAPTURE_BEGIN_EVENT | \
50                                      RTEMS_CAPTURE_EXITTED_EVENT | \
51                                      RTEMS_CAPTURE_TERMINATED_EVENT | \
52                                      RTEMS_CAPTURE_AUTOGEN_ENTRY_EVENT | \
53                                      RTEMS_CAPTURE_AUTOGEN_EXIT_EVENT)
54#else
55#define RTEMS_CAPTURE_RECORD_EVENTS  (0)
56#endif
57
58typedef struct {
59  rtems_capture_buffer records;
60  uint32_t             count;
61  rtems_id             reader;
62  rtems_interrupt_lock lock;
63  uint32_t             flags;
64} rtems_capture_per_cpu_data;
65
66typedef struct {
67  uint32_t                flags;
68  rtems_capture_control*  controls;
69  int                     extension_index;
70  rtems_capture_timestamp timestamp;
71  rtems_task_priority     ceiling;
72  rtems_task_priority     floor;
73  rtems_interrupt_lock    lock;
74} rtems_capture_global_data;
75
76static rtems_capture_per_cpu_data  *capture_per_cpu = NULL;
77
78static rtems_capture_global_data capture_global = {
79  .lock = RTEMS_INTERRUPT_LOCK_INITIALIZER( "Capture" )
80};
81
82/*
83 * RTEMS Capture Data.
84 */
85#define capture_per_cpu_get( _cpu ) \
86   ( &capture_per_cpu[ _cpu ] )
87
88#define capture_records_on_cpu( _cpu ) capture_per_cpu[ _cpu ].records
89#define capture_count_on_cpu( _cpu )   capture_per_cpu[ _cpu ].count
90#define capture_flags_on_cpu( _cpu )   capture_per_cpu[ _cpu ].flags
91#define capture_reader_on_cpu( _cpu )  capture_per_cpu[ _cpu ].reader
92#define capture_lock_on_cpu( _cpu )    capture_per_cpu[ _cpu ].lock
93
94#define capture_flags_global     capture_global.flags
95#define capture_controls         capture_global.controls
96#define capture_extension_index  capture_global.extension_index
97#define capture_timestamp        capture_global.timestamp
98#define capture_ceiling          capture_global.ceiling
99#define capture_floor            capture_global.floor
100#define capture_lock_global      capture_global.lock
101
102/*
103 * RTEMS Event text.
104 */
105static const char * const capture_event_text[] =
106{
107  "CREATED_BY",
108  "CREATED",
109  "STARTED_BY",
110  "STARTED",
111  "RESTARTED_BY",
112  "RESTARTED",
113  "DELETED_BY",
114  "DELETED",
115  "TERMINATED",
116  "BEGIN",
117  "EXITTED",
118  "SWITCHED_OUT",
119  "SWITCHED_IN",
120  "TIMESTAMP"
121};
122
123void rtems_capture_set_extension_index(int index)
124{
125  capture_extension_index = index;
126}
127
128int  rtems_capture_get_extension_index(void)
129{
130  return capture_extension_index;
131}
132
133uint32_t rtems_capture_get_flags(void)
134{
135  return capture_flags_global;
136}
137
138void rtems_capture_set_flags(uint32_t mask)
139{
140  capture_flags_global |= mask;
141}
142
143/*
144 * This function returns the current time. If a handler is provided
145 * by the user get the time from that.
146 */
147void
148rtems_capture_get_time (rtems_capture_time* time)
149{
150  if (capture_timestamp)
151    capture_timestamp (time);
152  else
153  {
154    *time = rtems_clock_get_uptime_nanoseconds ();
155  }
156}
157
158/*
159 * This function compares rtems_names. It protects the
160 * capture engine from a change to the way names are supported
161 * in RTEMS.
162 */
163static inline bool
164rtems_capture_match_names (rtems_name lhs, rtems_name rhs)
165{
166  return lhs == rhs;
167}
168
169/*
170 * This function compares rtems_ids. It protects the
171 * capture engine from a change to the way id are supported
172 * in RTEMS.
173 */
174static inline bool
175rtems_capture_match_ids (rtems_id lhs, rtems_id rhs)
176{
177  return lhs == rhs;
178}
179
180/*
181 * This function matches a name and/or id.
182 */
183static inline bool
184rtems_capture_match_name_id (rtems_name lhs_name,
185                             rtems_id   lhs_id,
186                             rtems_name rhs_name,
187                             rtems_id   rhs_id)
188{
189  bool match_name;
190
191  match_name = ((rtems_capture_task_api(lhs_id) != OBJECTS_POSIX_API) &&
192                (rtems_capture_task_api(rhs_id) != OBJECTS_POSIX_API));
193
194  /*
195   * The left hand side name or id could be 0 which means a wildcard.
196   */
197  if ((lhs_name == 0) && (lhs_id == rhs_id))
198    return true;
199  else if (match_name && ((lhs_id == 0) || (lhs_id == rhs_id)))
200  {
201    if (rtems_capture_match_names (lhs_name, rhs_name))
202      return true;
203  }
204  return false;
205}
206
207/*
208 * This function duplicates an rtems_names. It protects the
209 * capture engine from a change to the way names are supported
210 * in RTEMS.
211 */
212static inline void
213rtems_capture_dup_name (rtems_name* dst, rtems_name src)
214{
215  *dst = src;
216}
217
218/*
219 * This function sees if a BY control is in the BY names. The use
220 * of the valid_mask in this way assumes the number of trigger
221 * tasks is the number of bits in uint32_t.
222 */
223static inline bool
224rtems_capture_by_in_to (uint32_t               events,
225                        rtems_tcb*             by,
226                        rtems_capture_control* to)
227{
228  uint32_t valid_mask = RTEMS_CAPTURE_CONTROL_FROM_MASK (0);
229  uint32_t valid_remainder = 0xffffffff;
230  int      i;
231
232  for (i = 0; i < RTEMS_CAPTURE_TRIGGER_TASKS; i++)
233  {
234    /*
235     * If there are no more valid BY entries then
236     * we are finished.
237     */
238    if ((valid_remainder & to->by_valid) == 0)
239      break;
240
241    /*
242     * Is the froby entry valid and does its name or id match.
243     */
244    if ((valid_mask & to->by_valid) &&
245        (to->by[i].trigger & events))
246    {
247      /*
248       * We have the BY task on the right hand side so we
249       * match with id's first then labels if the id's are
250       * not set.
251       */
252      if (rtems_capture_match_name_id (to->by[i].name, to->by[i].id,
253                                       rtems_capture_task_name( by ),
254                                       by->Object.id))
255        return 1;
256    }
257
258    valid_mask >>= 1;
259    valid_remainder >>= 1;
260  }
261
262  return 0;
263}
264
265/*
266 * This function searches for a trigger given a name.
267 */
268static inline rtems_capture_control*
269rtems_capture_find_control (rtems_name name, rtems_id id)
270{
271  rtems_capture_control* control;
272  for (control = capture_controls; control != NULL; control = control->next)
273    if (rtems_capture_match_name_id (name, id, control->name, control->id))
274      break;
275  return control;
276}
277
278/*
279 * This function checks if a new control structure matches
280 * the given task and sets the control if it does.
281 */
282static bool
283rtems_capture_initialize_control (rtems_tcb *tcb, void *arg)
284{
285  if (tcb->Capture.control == NULL)
286  {
287    rtems_name             name = rtems_build_name(0, 0, 0, 0);
288    rtems_id               id;
289    rtems_capture_control* control;
290
291    /*
292     * We need to scan the default control list to initialise
293     * this control.
294     */
295    id = tcb->Object.id;
296    if (rtems_capture_task_api (id) != OBJECTS_POSIX_API)
297      rtems_object_get_classic_name (id, &name);
298    for (control = capture_controls; control != NULL; control = control->next)
299    {
300      if (rtems_capture_match_name_id (control->name, control->id,
301                                       name, id))
302      {
303        tcb->Capture.control = control;
304        break;
305      }
306    }
307  }
308
309  return false;
310}
311
312static rtems_capture_control*
313rtems_capture_create_control (rtems_name name, rtems_id id)
314{
315  rtems_interrupt_lock_context lock_context;
316  rtems_capture_control*       control;
317
318  if ((name == 0) && (id == 0))
319    return NULL;
320
321  control = rtems_capture_find_control (name, id);
322
323  if (control == NULL)
324  {
325    control = malloc (sizeof (*control));
326
327    if (!control)
328    {
329      capture_flags_global |= RTEMS_CAPTURE_NO_MEMORY;
330      return NULL;
331    }
332
333    control->name          = name;
334    control->id            = id;
335    control->flags         = 0;
336    control->to_triggers   = 0;
337    control->from_triggers = 0;
338    control->by_valid      = 0;
339
340    memset (control->by, 0, sizeof (control->by));
341
342    rtems_interrupt_lock_acquire (&capture_lock_global, &lock_context);
343
344    control->next    = capture_controls;
345    capture_controls = control;
346
347    _Thread_Iterate (rtems_capture_initialize_control, NULL);
348
349    rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
350  }
351
352  return control;
353}
354
355void
356rtems_capture_record_lock (rtems_capture_record_lock_context* context)
357{
358  rtems_capture_per_cpu_data* cpu;
359  cpu = capture_per_cpu_get (rtems_get_current_processor ());
360  rtems_interrupt_lock_interrupt_disable (&context->lock_context);
361  context->lock = &cpu->lock;
362  rtems_interrupt_lock_acquire_isr (&cpu->lock, &context->lock_context);
363}
364
365void
366rtems_capture_record_unlock (rtems_capture_record_lock_context* context)
367{
368  rtems_interrupt_lock_release (context->lock, &context->lock_context);
369}
370
371void*
372rtems_capture_record_open (rtems_tcb*                         tcb,
373                           uint32_t                           events,
374                           size_t                             size,
375                           rtems_capture_record_lock_context* context)
376{
377  rtems_capture_per_cpu_data* cpu;
378  uint8_t*                    ptr;
379
380  size += sizeof (rtems_capture_record);
381
382  cpu = capture_per_cpu_get (rtems_get_current_processor ());
383
384  rtems_capture_record_lock (context);
385
386  ptr = rtems_capture_buffer_allocate (&cpu->records, size);
387  if (ptr != NULL)
388  {
389    rtems_capture_record in;
390
391    ++cpu->count;
392
393    if ((events & RTEMS_CAPTURE_RECORD_EVENTS) == 0)
394      tcb->Capture.flags |= RTEMS_CAPTURE_TRACED;
395
396    /*
397     * Create a local copy then copy. The buffer maybe mis-aligned.
398     */
399    in.size    = size;
400    in.task_id = tcb->Object.id;
401    in.events  = (events |
402                  rtems_capture_task_real_priority (tcb) |
403                  (rtems_capture_task_curr_priority (tcb) << 8));
404
405    rtems_capture_get_time (&in.time);
406
407    ptr = rtems_capture_record_append(ptr, &in, sizeof(in));
408  }
409  else
410    cpu->flags |= RTEMS_CAPTURE_OVERFLOW;
411
412  return ptr;
413}
414
415void
416rtems_capture_record_close (rtems_capture_record_lock_context* context)
417{
418  rtems_capture_record_unlock (context);
419}
420
421void
422rtems_capture_initialize_task( rtems_tcb* tcb )
423{
424  rtems_capture_control*       control;
425  rtems_name                   name = rtems_build_name(0, 0, 0, 0);
426  rtems_id                     id = rtems_capture_task_id (tcb);
427  rtems_interrupt_lock_context lock_context;
428
429  /*
430   * We need to scan the default control list to initialize
431   * this control if it is a new task.
432   */
433  if (rtems_capture_task_api (id) != OBJECTS_POSIX_API)
434    rtems_object_get_classic_name (id, &name);
435
436  rtems_interrupt_lock_acquire (&capture_lock_global, &lock_context);
437
438  if (tcb->Capture.control == NULL) {
439    for (control = capture_controls; control != NULL; control = control->next)
440      if (rtems_capture_match_name_id (control->name, control->id,
441                                       name, id))
442        tcb->Capture.control = control;
443  }
444
445  tcb->Capture.flags |= RTEMS_CAPTURE_INIT_TASK;
446
447  rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
448}
449
450void rtems_capture_record_task (rtems_tcb* tcb)
451{
452  rtems_name                        name = rtems_build_name (0, 0, 0, 0);
453  rtems_id                          id = rtems_capture_task_id (tcb);
454  rtems_capture_task_record         rec;
455  void*                             ptr;
456  rtems_interrupt_lock_context      lock_context;
457  rtems_capture_record_lock_context rec_context;
458
459  if (rtems_capture_task_api (id) != OBJECTS_POSIX_API)
460    rtems_object_get_classic_name (id, &name);
461
462  rec.name = name;
463  rec.stack_size = tcb->Start.Initial_stack.size;
464  rec.start_priority = rtems_capture_task_start_priority (tcb);
465
466  rtems_interrupt_lock_acquire (&capture_lock_global, &lock_context);
467  tcb->Capture.flags |= RTEMS_CAPTURE_RECORD_TASK;
468  rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
469
470  /*
471   *  Log the task information. The first time a task is seen a record is
472   *  logged.  This record can be identified by a 0 in the event identifier.
473   */
474  ptr = rtems_capture_record_open (tcb, 0, sizeof(rec), &rec_context);
475  if (ptr != NULL)
476    rtems_capture_record_append(ptr, &rec, sizeof (rec));
477  rtems_capture_record_close (&rec_context);
478}
479
480/*
481 * This function indicates if data should be filtered from the
482 * log.
483 */
484bool rtems_capture_filter (rtems_tcb*  tcb, uint32_t events)
485{
486  if (tcb &&
487      ((capture_flags_global &
488        (RTEMS_CAPTURE_TRIGGERED | RTEMS_CAPTURE_ONLY_MONITOR)) ==
489       RTEMS_CAPTURE_TRIGGERED))
490  {
491    rtems_capture_control* control;
492
493    control = tcb->Capture.control;
494
495    /*
496     * Capture the record if we have an event that is always
497     * captured, or the task's real priority is greater than the
498     * watch ceiling, and the global watch or task watch is enabled.
499     */
500    if ((events & RTEMS_CAPTURE_RECORD_EVENTS) ||
501        ((rtems_capture_task_real_priority (tcb) >= capture_ceiling) &&
502         (rtems_capture_task_real_priority (tcb) <= capture_floor) &&
503         ((capture_flags_global & RTEMS_CAPTURE_GLOBAL_WATCH) ||
504          (control && (control->flags & RTEMS_CAPTURE_WATCH)))))
505    {
506      return false;
507    }
508  }
509
510  return true;
511}
512
513/*
514 * See if we have triggered and if not see if this event is a
515 * cause of a trigger.
516 */
517bool
518rtems_capture_trigger_fired (rtems_tcb* ft, rtems_tcb* tt, uint32_t events)
519{
520  /*
521   * If we have not triggered then see if this is a trigger condition.
522   */
523  if (!(capture_flags_global & RTEMS_CAPTURE_TRIGGERED))
524  {
525    rtems_capture_control* fc = NULL;
526    rtems_capture_control* tc = NULL;
527    uint32_t               from_events = 0;
528    uint32_t               to_events = 0;
529    uint32_t               from_to_events = 0;
530
531    if (ft)
532    {
533      fc = ft->Capture.control;
534      if (fc)
535        from_events = fc->from_triggers & events;
536    }
537
538    if (tt)
539    {
540      tc = tt->Capture.control;
541      if (tc)
542      {
543        to_events = tc->to_triggers & events;
544        if (ft && tc->by_valid)
545          from_to_events = tc->by_triggers & events;
546      }
547    }
548
549    /*
550     * Check if we have any from or to events. These are the
551     * from any or to any type triggers. All from/to triggers are
552     * listed in the to's control with the from in the from list.
553     *
554     * The masking above means any flag set is a trigger.
555     */
556    if (from_events || to_events)
557    {
558      capture_flags_global |= RTEMS_CAPTURE_TRIGGERED;
559      return true;
560    }
561
562    /*
563     * Check the from->to events.
564     */
565    if (from_to_events)
566    {
567      if (rtems_capture_by_in_to (events, ft, tc))
568      {
569        capture_flags_global |= RTEMS_CAPTURE_TRIGGERED;
570        return true;
571      }
572    }
573
574    return false;
575  }
576
577  return true;
578}
579
580/*
581 * This function initialises the realtime capture engine allocating the trace
582 * buffer. It is assumed we have a working heap at stage of initialization.
583 */
584rtems_status_code
585rtems_capture_open (uint32_t   size, rtems_capture_timestamp timestamp RTEMS_UNUSED)
586{
587  rtems_status_code     sc = RTEMS_SUCCESSFUL;
588  size_t                count;
589  uint32_t              i;
590  rtems_capture_buffer* buff;
591
592  /*
593   * See if the capture engine is already open.
594   */
595
596  if ((capture_flags_global & RTEMS_CAPTURE_INIT) == RTEMS_CAPTURE_INIT) {
597    return RTEMS_RESOURCE_IN_USE;
598  }
599
600  count = rtems_get_processor_count();
601  if (capture_per_cpu == NULL) {
602    capture_per_cpu = calloc( count, sizeof( *capture_per_cpu ) );
603  }
604
605  for (i=0; i<count; i++) {
606    buff = &capture_records_on_cpu(i);
607    rtems_capture_buffer_create( buff, size );
608    if (buff->buffer == NULL) {
609      sc = RTEMS_NO_MEMORY;
610      break;
611    }
612
613    rtems_interrupt_lock_initialize(
614      &capture_lock_on_cpu( i ),
615      "Capture Per-CPU"
616    );
617  }
618
619  capture_flags_global   = 0;
620  capture_ceiling = 0;
621  capture_floor   = 255;
622  if (sc == RTEMS_SUCCESSFUL)
623    sc = rtems_capture_user_extension_open();
624
625  if (sc != RTEMS_SUCCESSFUL)
626  {
627    for (i=0; i<count; i++)
628      rtems_capture_buffer_destroy( &capture_records_on_cpu(i));
629  }  else {
630    capture_flags_global |= RTEMS_CAPTURE_INIT;
631  }
632
633  return sc;
634}
635
636/*
637 * This function shutdowns the capture engine and release any claimed
638 * resources.  Capture control must be disabled prior to calling a close.
639 */
640rtems_status_code
641rtems_capture_close (void)
642{
643  rtems_interrupt_lock_context lock_context;
644  rtems_capture_control*       control;
645  rtems_status_code            sc;
646  uint32_t                     cpu;
647
648  rtems_interrupt_lock_acquire (&capture_lock_global, &lock_context);
649
650  if ((capture_flags_global & RTEMS_CAPTURE_INIT) != RTEMS_CAPTURE_INIT)
651  {
652    rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
653    return RTEMS_SUCCESSFUL;
654  }
655
656  if ( (capture_flags_global & RTEMS_CAPTURE_ON) != 0 )
657  {
658    rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
659    return RTEMS_UNSATISFIED;
660  }
661
662  capture_flags_global &=
663    ~(RTEMS_CAPTURE_ON | RTEMS_CAPTURE_ONLY_MONITOR | RTEMS_CAPTURE_INIT);
664
665  rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
666
667  /*
668   * Delete the extension first. This means we are now able to
669   * release the resources we have without them being used.
670   */
671
672  sc = rtems_capture_user_extension_close();
673
674  if (sc != RTEMS_SUCCESSFUL)
675    return sc;
676
677  control = capture_controls;
678
679  while (control)
680  {
681    rtems_capture_control* delete = control;
682    control = control->next;
683    free (delete);
684  }
685
686  capture_controls = NULL;
687  for (cpu=0; cpu < rtems_get_processor_count(); cpu++) {
688    if (capture_records_on_cpu(cpu).buffer)
689      rtems_capture_buffer_destroy( &capture_records_on_cpu(cpu) );
690
691    rtems_interrupt_lock_destroy( &capture_lock_on_cpu( cpu ) );
692  }
693
694  free( capture_per_cpu );
695  capture_per_cpu = NULL;
696
697  return RTEMS_SUCCESSFUL;
698}
699
700rtems_status_code
701rtems_capture_set_control (bool enable)
702{
703  rtems_interrupt_lock_context lock_context;
704
705  rtems_interrupt_lock_acquire (&capture_lock_global, &lock_context);
706
707  if ((capture_flags_global & RTEMS_CAPTURE_INIT) != RTEMS_CAPTURE_INIT)
708  {
709    rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
710    return RTEMS_UNSATISFIED;
711  }
712
713  if (enable)
714    capture_flags_global |= RTEMS_CAPTURE_ON;
715  else
716    capture_flags_global &= ~RTEMS_CAPTURE_ON;
717
718  rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
719
720  return RTEMS_SUCCESSFUL;
721}
722
723/*
724 * This function enable the monitor mode. When in the monitor mode
725 * the tasks are monitored but no data is saved. This can be used
726 * to profile the load on a system.
727 */
728rtems_status_code
729rtems_capture_set_monitor (bool enable)
730{
731  rtems_interrupt_lock_context lock_context;
732
733  rtems_interrupt_lock_acquire (&capture_lock_global, &lock_context);
734
735  if ((capture_flags_global & RTEMS_CAPTURE_INIT) != RTEMS_CAPTURE_INIT)
736  {
737    rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
738    return RTEMS_UNSATISFIED;
739  }
740
741  if (enable)
742    capture_flags_global |= RTEMS_CAPTURE_ONLY_MONITOR;
743  else
744    capture_flags_global &= ~RTEMS_CAPTURE_ONLY_MONITOR;
745
746  rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
747
748  return RTEMS_SUCCESSFUL;
749}
750
751/*
752 * This function clears the capture trace flag in the tcb.
753 */
754static bool
755rtems_capture_flush_tcb (rtems_tcb *tcb, void *arg)
756{
757  tcb->Capture.flags &= ~RTEMS_CAPTURE_TRACED;
758  return false;
759}
760
761/*
762 * This function flushes the capture buffer. The prime parameter allows the
763 * capture engine to also be primed again.
764 */
765rtems_status_code
766rtems_capture_flush (bool prime)
767{
768  rtems_status_code sc = RTEMS_NOT_CONFIGURED;
769  if (capture_per_cpu != NULL)
770  {
771    rtems_interrupt_lock_context lock_context_global;
772    uint32_t                     cpu;
773
774    rtems_interrupt_lock_acquire (&capture_lock_global, &lock_context_global);
775
776    if ( (capture_flags_global & RTEMS_CAPTURE_ON) != 0 )
777    {
778      rtems_interrupt_lock_release (&capture_lock_global, &lock_context_global);
779      return RTEMS_UNSATISFIED;
780    }
781
782    _Thread_Iterate (rtems_capture_flush_tcb, NULL);
783
784    if (prime)
785      capture_flags_global &= ~(RTEMS_CAPTURE_TRIGGERED | RTEMS_CAPTURE_OVERFLOW);
786    else
787      capture_flags_global &= ~RTEMS_CAPTURE_OVERFLOW;
788
789    for (cpu=0; cpu < rtems_get_processor_count(); cpu++) {
790      RTEMS_INTERRUPT_LOCK_REFERENCE( lock, &(capture_lock_on_cpu( cpu )) )
791      rtems_interrupt_lock_context lock_context_per_cpu;
792
793      rtems_interrupt_lock_acquire (lock, &lock_context_per_cpu);
794      capture_count_on_cpu(cpu) = 0;
795      if (capture_records_on_cpu(cpu).buffer)
796        rtems_capture_buffer_flush( &capture_records_on_cpu(cpu) );
797      rtems_interrupt_lock_release (lock, &lock_context_per_cpu);
798    }
799
800    rtems_interrupt_lock_release (&capture_lock_global, &lock_context_global);
801
802    sc = RTEMS_SUCCESSFUL;
803  }
804
805  return sc;
806}
807
808/*
809 * This function defines a watch for a specific task given a name. A watch
810 * causes it to be traced either in or out of context. The watch can be
811 * optionally enabled or disabled with the set routine. It is disabled by
812 * default.  A watch can only be defined when capture control is disabled
813 */
814rtems_status_code
815rtems_capture_watch_add (rtems_name name, rtems_id id)
816{
817  rtems_capture_control* control;
818
819  if ( (capture_flags_global & RTEMS_CAPTURE_ON) != 0 )
820    return RTEMS_UNSATISFIED;
821
822  if ((name == 0) && (id == 0))
823    return RTEMS_UNSATISFIED;
824
825  control = rtems_capture_find_control (name, id);
826
827  if (control && !id)
828    return RTEMS_TOO_MANY;
829
830  if (!control)
831    control = rtems_capture_create_control (name, id);
832
833  if (!control)
834    return RTEMS_NO_MEMORY;
835
836  return RTEMS_SUCCESSFUL;
837}
838
839/*
840 * This function removes a watch for a specific task given a name. The task
841 * description will still exist if referenced by a trace record in the trace
842 * buffer or a global watch is defined.  A watch can only be deleted when
843 * capture control is disabled.
844 */
845rtems_status_code
846rtems_capture_watch_del (rtems_name name, rtems_id id)
847{
848  rtems_interrupt_lock_context lock_context;
849  rtems_capture_control*       control;
850  rtems_capture_control**      prev_control;
851  bool                         found = false;
852
853  if ( (capture_flags_global & RTEMS_CAPTURE_ON) != 0 )
854    return RTEMS_UNSATISFIED;
855
856  /*
857   * Should this test be for wildcards ?
858   */
859
860  for (prev_control = &capture_controls, control = capture_controls;
861       control != NULL; )
862  {
863    if (rtems_capture_match_name_id (control->name, control->id, name, id))
864    {
865      rtems_interrupt_lock_acquire (&capture_lock_global, &lock_context);
866
867      *prev_control = control->next;
868
869      rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
870
871      free (control);
872
873      control = *prev_control;
874
875      found = true;
876    }
877    else
878    {
879      prev_control = &control->next;
880      control      = control->next;
881    }
882  }
883
884  if (found)
885    return RTEMS_SUCCESSFUL;
886
887  return RTEMS_INVALID_NAME;
888}
889
890/*
891 * This function allows control of a watch. The watch can be enabled or
892 * disabled.
893 */
894rtems_status_code
895rtems_capture_watch_ctrl (rtems_name name, rtems_id id, bool enable)
896{
897  rtems_interrupt_lock_context lock_context;
898  rtems_capture_control*       control;
899  bool                         found = false;
900
901  if ( (capture_flags_global & RTEMS_CAPTURE_ON) != 0 )
902    return RTEMS_UNSATISFIED;
903
904  /*
905   * Find the control and then set the watch. It must exist before it can
906   * be controlled.
907   */
908  for (control = capture_controls; control != NULL; control = control->next)
909  {
910    if (rtems_capture_match_name_id (control->name, control->id, name, id))
911    {
912      rtems_interrupt_lock_acquire (&capture_lock_global, &lock_context);
913
914      if (enable)
915        control->flags |= RTEMS_CAPTURE_WATCH;
916      else
917        control->flags &= ~RTEMS_CAPTURE_WATCH;
918
919      rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
920
921      found = true;
922    }
923  }
924
925  if (found)
926    return RTEMS_SUCCESSFUL;
927
928  return RTEMS_INVALID_NAME;
929}
930
931/*
932 * This function allows control of a global watch. The watch can be enabled or
933 * disabled. A global watch configures all tasks below the ceiling and above
934 * the floor to be traced.
935 */
936rtems_status_code
937rtems_capture_watch_global (bool enable)
938{
939  rtems_interrupt_lock_context lock_context;
940
941  rtems_interrupt_lock_acquire (&capture_lock_global, &lock_context);
942
943  /*
944   * We need to keep specific and global watches separate so
945   * a global enable/disable does not lose a specific watch.
946   */
947  if (enable)
948    capture_flags_global |= RTEMS_CAPTURE_GLOBAL_WATCH;
949  else
950    capture_flags_global &= ~RTEMS_CAPTURE_GLOBAL_WATCH;
951
952  rtems_interrupt_lock_release (&capture_lock_global, &lock_context);
953
954  return RTEMS_SUCCESSFUL;
955}
956
957/*
958 * This function returns the global watch state.
959 */
960bool
961rtems_capture_watch_global_on (void)
962{
963  return capture_flags_global & RTEMS_CAPTURE_GLOBAL_WATCH ? 1 : 0;
964}
965
966/*
967 * This function sets a watch ceiling. Tasks at or greating that the
968 * ceiling priority are not watched. This is a simple way to monitor
969 * an application and exclude system tasks running at a higher
970 * priority level.
971 */
972rtems_status_code
973rtems_capture_watch_ceiling (rtems_task_priority ceiling)
974{
975  capture_ceiling = ceiling;
976  return RTEMS_SUCCESSFUL;
977}
978
979/*
980 * This function gets the watch ceiling.
981 */
982rtems_task_priority
983rtems_capture_watch_get_ceiling (void)
984{
985  return capture_ceiling;
986}
987
988/*
989 * This function sets a watch floor. Tasks at or less that the
990 * floor priority are not watched. This is a simple way to monitor
991 * an application and exclude system tasks running at a lower
992 * priority level.
993 */
994rtems_status_code
995rtems_capture_watch_floor (rtems_task_priority floor)
996{
997  capture_floor = floor;
998  return RTEMS_SUCCESSFUL;
999}
1000
1001/*
1002 * This function gets the watch floor.
1003 */
1004rtems_task_priority
1005rtems_capture_watch_get_floor (void)
1006{
1007  return capture_floor;
1008}
1009
1010/*
1011 * Map the trigger to a bit mask.
1012 */
1013static uint32_t
1014rtems_capture_map_trigger (rtems_capture_trigger trigger)
1015{
1016  /*
1017   * Transform the mode and trigger to a bit map.
1018   */
1019  switch (trigger)
1020  {
1021    case rtems_capture_switch:
1022      return RTEMS_CAPTURE_SWITCH;
1023    case rtems_capture_create:
1024      return RTEMS_CAPTURE_CREATE;
1025    case rtems_capture_start:
1026      return RTEMS_CAPTURE_START;
1027    case rtems_capture_restart:
1028      return RTEMS_CAPTURE_RESTART;
1029    case rtems_capture_delete:
1030      return RTEMS_CAPTURE_DELETE;
1031    case rtems_capture_begin:
1032      return RTEMS_CAPTURE_BEGIN;
1033    case rtems_capture_exitted:
1034      return RTEMS_CAPTURE_EXITTED;
1035    case rtems_capture_terminated:
1036      return RTEMS_CAPTURE_TERMINATED;
1037    default:
1038      break;
1039  }
1040  return 0;
1041}
1042
1043/*
1044 * This function sets a trigger.
1045 *
1046 * This set trigger routine will create a capture control for the
1047 * target task. The task list is searched and any existing tasks
1048 * are linked to the new control.
1049 *
1050 * We can have a number of tasks that have the same name so we
1051 * search using names. This means a number of tasks can be
1052 * linked to single control.
1053 */
1054rtems_status_code
1055rtems_capture_set_trigger (rtems_name                 from_name,
1056                           rtems_id                   from_id,
1057                           rtems_name                 to_name,
1058                           rtems_id                   to_id,
1059                           rtems_capture_trigger_mode mode,
1060                           rtems_capture_trigger      trigger)
1061{
1062  rtems_capture_control* control;
1063  uint32_t               flags;
1064
1065  flags = rtems_capture_map_trigger (trigger);
1066
1067  /*
1068   * The mode sets the opposite type of trigger. For example
1069   * FROM ANY means trigger when the event happens TO this
1070   * task. TO ANY means FROM this task.
1071   */
1072
1073  if (mode == rtems_capture_to_any)
1074  {
1075    control = rtems_capture_create_control (from_name, from_id);
1076    if (control == NULL)
1077      return RTEMS_NO_MEMORY;
1078    control->from_triggers |= flags & RTEMS_CAPTURE_FROM_TRIGS;
1079  }
1080  else
1081  {
1082    control = rtems_capture_create_control (to_name, to_id);
1083    if (control == NULL)
1084      return RTEMS_NO_MEMORY;
1085    if (mode == rtems_capture_from_any)
1086      control->to_triggers |= flags;
1087    else
1088    {
1089      bool done = false;
1090      int  i;
1091
1092      control->by_triggers |= flags;
1093
1094      for (i = 0; i < RTEMS_CAPTURE_TRIGGER_TASKS; i++)
1095      {
1096        if (rtems_capture_control_by_valid (control, i) &&
1097            ((control->by[i].name == from_name) ||
1098             (from_id && (control->by[i].id == from_id))))
1099        {
1100          control->by[i].trigger |= flags;
1101          done = true;
1102          break;
1103        }
1104      }
1105
1106      if (!done)
1107      {
1108        for (i = 0; i < RTEMS_CAPTURE_TRIGGER_TASKS; i++)
1109        {
1110          if (!rtems_capture_control_by_valid (control, i))
1111          {
1112            control->by_valid |= RTEMS_CAPTURE_CONTROL_FROM_MASK (i);
1113            control->by[i].name = from_name;
1114            control->by[i].id = from_id;
1115            control->by[i].trigger = flags;
1116            done = true;
1117            break;
1118          }
1119        }
1120      }
1121
1122      if (!done)
1123        return RTEMS_TOO_MANY;
1124    }
1125  }
1126  return RTEMS_SUCCESSFUL;
1127}
1128
1129/*
1130 * This function clear a trigger.
1131 */
1132rtems_status_code
1133rtems_capture_clear_trigger (rtems_name                 from_name,
1134                             rtems_id                   from_id,
1135                             rtems_name                 to_name,
1136                             rtems_id                   to_id,
1137                             rtems_capture_trigger_mode mode,
1138                             rtems_capture_trigger      trigger)
1139{
1140  rtems_capture_control* control;
1141  uint32_t               flags;
1142
1143  flags = rtems_capture_map_trigger (trigger);
1144
1145  if (mode == rtems_capture_to_any)
1146  {
1147    control = rtems_capture_find_control (from_name, from_id);
1148    if (control == NULL)
1149    {
1150      if (from_id)
1151        return RTEMS_INVALID_ID;
1152      return RTEMS_INVALID_NAME;
1153    }
1154    control->from_triggers &= ~flags;
1155  }
1156  else
1157  {
1158    control = rtems_capture_find_control (to_name, to_id);
1159    if (control == NULL)
1160    {
1161      if (to_id)
1162        return RTEMS_INVALID_ID;
1163      return RTEMS_INVALID_NAME;
1164    }
1165    if (mode == rtems_capture_from_any)
1166      control->to_triggers &= ~flags;
1167    else
1168    {
1169      bool done = false;
1170      int  i;
1171
1172      control->by_triggers &= ~flags;
1173
1174      for (i = 0; i < RTEMS_CAPTURE_TRIGGER_TASKS; i++)
1175      {
1176        if (rtems_capture_control_by_valid (control, i) &&
1177            ((control->by[i].name == from_name) ||
1178             (control->by[i].id == from_id)))
1179        {
1180          control->by[i].trigger &= ~trigger;
1181          if (control->by[i].trigger == 0)
1182            control->by_valid &= ~RTEMS_CAPTURE_CONTROL_FROM_MASK (i);
1183          done = true;
1184          break;
1185        }
1186      }
1187
1188      if (!done)
1189      {
1190        if (from_id)
1191          return RTEMS_INVALID_ID;
1192        return RTEMS_INVALID_NAME;
1193      }
1194    }
1195  }
1196  return RTEMS_SUCCESSFUL;
1197}
1198
1199static inline uint32_t
1200rtems_capture_count_records (const void* records, size_t size)
1201{
1202  const uint8_t* ptr = records;
1203  uint32_t       recs = 0;
1204  size_t         bytes = 0;
1205
1206  while (bytes < size)
1207  {
1208    const rtems_capture_record* rec = (const rtems_capture_record*) ptr;
1209    recs++;
1210    ptr += rec->size;
1211    bytes += rec->size;
1212 }
1213
1214 return recs;
1215}
1216
1217/*
1218 * This function reads a number of records from the capture buffer.
1219 *
1220 * The function returns the number of record that is has that are
1221 * in a continous block of memory. If the number of available records
1222 * wrap then only those records are provided. This removes the need for
1223 * caller to be concerned about buffer wrappings. If the number of
1224 * requested records cannot be met due to the wrapping of the records
1225 * less than the specified number will be returned.
1226 *
1227 * The user must release the records. This is achieved with a call to
1228 * rtems_capture_release. Calls this function without a release will
1229 * result in at least the same number of records being released.
1230 */
1231rtems_status_code
1232rtems_capture_read (uint32_t cpu, size_t* read, const void** recs)
1233{
1234  rtems_status_code sc = RTEMS_NOT_CONFIGURED;
1235  if (capture_per_cpu != NULL)
1236  {
1237    RTEMS_INTERRUPT_LOCK_REFERENCE (lock, &(capture_lock_on_cpu (cpu)))
1238    rtems_interrupt_lock_context lock_context;
1239    size_t                       recs_size = 0;
1240    rtems_capture_buffer*        records;
1241    uint32_t*                    flags;
1242
1243    *read = 0;
1244    *recs = NULL;
1245
1246    records = &(capture_records_on_cpu (cpu));
1247    flags = &(capture_flags_on_cpu (cpu));
1248
1249    rtems_interrupt_lock_acquire (lock, &lock_context);
1250
1251    /*
1252     * Only one reader is allowed.
1253     */
1254
1255    if (*flags & RTEMS_CAPTURE_READER_ACTIVE)
1256    {
1257      rtems_interrupt_lock_release (lock, &lock_context);
1258      return RTEMS_RESOURCE_IN_USE;
1259    }
1260
1261    if ( (capture_flags_global & RTEMS_CAPTURE_ON) != 0 )
1262    {
1263      rtems_interrupt_lock_release (lock, &lock_context);
1264      return RTEMS_UNSATISFIED;
1265    }
1266
1267    *flags |= RTEMS_CAPTURE_READER_ACTIVE;
1268
1269    *recs = rtems_capture_buffer_peek( records, &recs_size );
1270
1271    *read = rtems_capture_count_records( *recs, recs_size );
1272
1273    rtems_interrupt_lock_release (lock, &lock_context);
1274
1275    sc = RTEMS_SUCCESSFUL;
1276  }
1277
1278  return sc;
1279}
1280
1281/*
1282 * This function releases the requested number of record slots back
1283 * to the capture engine. The count must match the number read.
1284 */
1285rtems_status_code
1286rtems_capture_release (uint32_t cpu, uint32_t count)
1287{
1288  rtems_status_code sc = RTEMS_NOT_CONFIGURED;
1289  if (capture_per_cpu != NULL)
1290  {
1291    rtems_interrupt_lock_context lock_context;
1292    uint8_t*                     ptr;
1293    rtems_capture_record*        rec;
1294    uint32_t                     counted;
1295    size_t                       ptr_size = 0;
1296    size_t                       rel_size = 0;
1297    RTEMS_INTERRUPT_LOCK_REFERENCE( lock, &(capture_lock_on_cpu( cpu )) )
1298    rtems_capture_buffer*        records = &(capture_records_on_cpu( cpu ));
1299    uint32_t*                    flags = &(capture_flags_on_cpu( cpu ));
1300    uint32_t*                    total = &(capture_count_on_cpu( cpu ));
1301
1302    sc = RTEMS_SUCCESSFUL;
1303
1304    rtems_interrupt_lock_acquire (lock, &lock_context);
1305
1306    if (count > *total) {
1307      count = *total;
1308    }
1309
1310    if ( (capture_flags_global & RTEMS_CAPTURE_ON) != 0 ) {
1311      rtems_interrupt_lock_release (lock, &lock_context);
1312      return RTEMS_UNSATISFIED;
1313    }
1314
1315    counted = count;
1316
1317    ptr = rtems_capture_buffer_peek( records, &ptr_size );
1318    _Assert(ptr_size >= (count * sizeof(*rec) ));
1319
1320    rel_size = 0;
1321    while (counted--) {
1322      rec = (rtems_capture_record*) ptr;
1323      rel_size += rec->size;
1324      _Assert( rel_size <= ptr_size );
1325      ptr += rec->size;
1326    }
1327
1328    if (rel_size > ptr_size ) {
1329      sc = RTEMS_INVALID_NUMBER;
1330      rel_size = ptr_size;
1331    }
1332
1333    *total -= count;
1334
1335    if (count) {
1336      rtems_capture_buffer_free( records, rel_size );
1337    }
1338
1339    *flags &= ~RTEMS_CAPTURE_READER_ACTIVE;
1340
1341    rtems_interrupt_lock_release (lock, &lock_context);
1342  }
1343
1344  return sc;
1345}
1346
1347/*
1348 * This function returns a string for an event based on the bit in the
1349 * event. The functions takes the bit offset as a number not the bit
1350 * set in a bit map.
1351 */
1352const char*
1353rtems_capture_event_text (int event)
1354{
1355  if ((event < RTEMS_CAPTURE_EVENT_START) || (event > RTEMS_CAPTURE_EVENT_END))
1356    return "invalid event id";
1357  return capture_event_text[event - RTEMS_CAPTURE_EVENT_START];
1358}
1359
1360/*
1361 * This function returns the head of the list of control in the
1362 * capture engine.
1363 */
1364rtems_capture_control*
1365rtems_capture_get_control_list (void)
1366{
1367  return capture_controls;
1368}
Note: See TracBrowser for help on using the repository browser.