1 <!-- Copyright (C) 2003 Red Hat, Inc. -->
2 <!-- This material may be distributed only subject to the terms -->
3 <!-- and conditions set forth in the Open Publication License, v1.0 -->
4 <!-- or later (the latest version is presently available at -->
5 <!-- http://www.opencontent.org/openpub/). -->
6 <!-- Distribution of the work or derivative of the work in any -->
7 <!-- standard (paper) book form is prohibited unless prior -->
8 <!-- permission is obtained from the copyright holder. -->
12 >Kernel Instrumentation</TITLE
13 ><meta name="MSSmartTagsPreventParsing" content="TRUE">
16 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
19 TITLE="eCos User Guide"
20 HREF="ecos-user-guide.html"><LINK
22 TITLE="Debugging Techniques"
23 HREF="debugging-techniques.html"><LINK
25 TITLE="Debugging Techniques"
26 HREF="debugging-techniques.html"><LINK
28 TITLE="Configuration and the Package Repository"
29 HREF="configuration-and-the-package-repository.html"></HEAD
40 SUMMARY="Header navigation table"
57 HREF="debugging-techniques.html"
65 >Chapter 27. Debugging Techniques</TD
71 HREF="configuration-and-the-package-repository.html"
85 NAME="KERNEL-INSTRUMENTATION">Kernel Instrumentation</H1
89 >Instrument buffers</I
91 find out how many events of a given type happened in the
92 kernel during execution of a program.</P
94 >You can monitor a class of several types of events, or
95 you can just look at individual events. </P
108 >scheduler events </P
112 >thread operations</P
120 >mutex operations </P
124 >binary semaphore operations </P
128 >counting semaphore operations </P
132 >clock ticks and interrupts </P
136 >Examples of fine-grained scheduler event types are: </P
158 >Information about the events is stored in an
162 >. The structure that
163 defines this record has type <SPAN
166 Instrument_Record</SPAN
169 >The list of records is stored in an array called <SPAN
171 >instrument_buffer</SPAN
173 which you can let the kernel provide or you can provide yourself
174 by setting the configuration option <TT
176 >CYGVAR_KERNEL_INSTRUMENT_EXTERNAL_BUFFER</TT
179 >To write a program that examines the instrumentation
187 >Enable instrumentation buffers in the <SPAN
190 > kernel configuration.
191 The component macro is <TT
193 >CYGPKG_KERNEL_INSTRUMENT</TT
198 >To allocate the buffers yourself, enable the configuration
201 >CYGVAR_KERNEL_INSTRUMENT_EXTERNAL_BUFFER</TT
206 >Include the header file
209 >cyg/kernel/instrmnt.h</TT
219 CLASS="PROGRAMLISTING"
220 >#include <cyg/kernel/instrmnt.h></PRE
230 >Instrumentation_Record</SPAN
232 is not published in the kernel header file. In the future there
233 will be a cleaner mechanism to access it, but for now you should
234 paste into your code in the following lines:
243 CLASS="PROGRAMLISTING"
244 >struct Instrument_Record
246 CYG_WORD16 type; // record type
247 CYG_WORD16 thread; // current thread id
248 CYG_WORD timestamp; // 32 bit timestamp
249 CYG_WORD arg1; // first arg
250 CYG_WORD arg2; // second arg
258 >Enable the events you want to record using
261 >cyg_instrument_enable()</TT
263 , and disable them later. Look at
266 >cyg/kernel/instrmnt.h</TT
268 and the examples below to see what events can be enabled. </P
272 >Place the code you want to debug between the matching
276 >cyg_instrument_enable()</TT
281 >cyg_instrument_disable()</TT
287 >Examine the buffer. For now you need to look at the data
288 in there (the example program below shows how to do that), and future
292 > will include a host-side tool to help you understand
301 >Example 27-2. Using instrument buffers</B
304 >This program is also provided in the
317 CLASS="PROGRAMLISTING"
318 >/* this is a program which uses <SPAN
321 > instrumentation buffers; it needs
322 to be linked with a kernel which was compiled with support for
325 #include <stdio.h>
326 #include <pkgconf/kernel.h>
327 #include <cyg/kernel/instrmnt.h>
328 #include <cyg/kernel/kapi.h>
330 #ifndef CYGVAR_KERNEL_INSTRUMENT_EXTERNAL_BUFFER
331 # error You must configure eCos with CYGVAR_KERNEL_INSTRUMENT_EXTERNAL_BUFFER
334 struct Instrument_Record
336 CYG_WORD16 type; // record type
337 CYG_WORD16 thread; // current thread id
338 CYG_WORD timestamp; // 32 bit timestamp
339 CYG_WORD arg1; // first arg
340 CYG_WORD arg2; // second arg
343 struct Instrument_Record instrument_buffer[20];
344 cyg_uint32 instrument_buffer_size = 20;
350 cyg_instrument_enable(CYG_INSTRUMENT_CLASS_CLOCK, 0);
351 cyg_instrument_enable(CYG_INSTRUMENT_CLASS_THREAD, 0);
352 cyg_instrument_enable(CYG_INSTRUMENT_CLASS_ALARM, 0);
354 printf("Program to play with instrumentation buffer\n");
358 cyg_instrument_disable(CYG_INSTRUMENT_CLASS_CLOCK, 0);
359 cyg_instrument_disable(CYG_INSTRUMENT_CLASS_THREAD, 0);
360 cyg_instrument_disable(CYG_INSTRUMENT_CLASS_ALARM, 0);
362 for (i = 0; i < instrument_buffer_size; ++i) {
363 printf("Record %02d: type 0x%04x, thread %d, ",
364 i, instrument_buffer[i].type, instrument_buffer[i].thread);
365 printf("time %5d, arg1 0x%08x, arg2 0x%08x\n",
366 instrument_buffer[i].timestamp, instrument_buffer[i].arg1,
367 instrument_buffer[i].arg2);
376 >Here is how you could compile and run this program in the <TT
380 using (for example) the MN10300 simulator target: </P
389 >$ make XCC=mn10300-elf-gcc INSTALL_DIR=/tmp/ecos-work-mn10300/install instrument-test
390 mn10300-elf-gcc -c -o instrument-test.o -g -Wall -I/tmp/ecos-work-mn10300/install/include \
391 -ffunction-sections -fdata-sections instrument-test.c
392 mn10300-elf-gcc -nostartfiles -L/tmp/ecos-work-mn10300/install/lib -W1,--gc-sections -o \
393 instrument-test instrument-test.o -Ttarget.ld -nostdlib
394 $ mn10300-elf-run --board=stdeval1 instrument-test</PRE
403 >Example 27-3. Instrument buffer output</B
406 >Here is the output of the
410 > program. Notice that in
411 little over 2 seconds, and with very little activity, and
412 with few event types enabled, it gathered 17 records. In
413 larger programs it will be necessary to select very few
414 event types for debugging. </P
422 CLASS="PROGRAMLISTING"
423 >Program to play with instrumentation buffer
424 Record 00: type 0x0207, thread 2, time 6057, arg1 0x48001cd8, arg2 0x00000002
425 Record 01: type 0x0202, thread 2, time 6153, arg1 0x48001cd8, arg2 0x00000000
426 Record 02: type 0x0904, thread 2, time 6358, arg1 0x48001d24, arg2 0x00000000
427 Record 03: type 0x0905, thread 2, time 6424, arg1 0x00000002, arg2 0x00000000
428 Record 04: type 0x0906, thread 2, time 6490, arg1 0x00000000, arg2 0x00000000
429 Record 05: type 0x0901, thread 2, time 6608, arg1 0x48009d74, arg2 0x48001d24
430 Record 06: type 0x0201, thread 2, time 6804, arg1 0x48001cd8, arg2 0x480013e0
431 Record 07: type 0x0803, thread 1, time 94, arg1 0x00000000, arg2 0x00000000
432 Record 08: type 0x0801, thread 1, time 361, arg1 0x00000000, arg2 0x00000000
433 Record 09: type 0x0802, thread 1, time 548, arg1 0x00000001, arg2 0x00000000
434 Record 10: type 0x0803, thread 1, time 94, arg1 0x00000000, arg2 0x00000000
435 Record 11: type 0x0801, thread 1, time 361, arg1 0x00000001, arg2 0x00000000
436 Record 12: type 0x0903, thread 1, time 513, arg1 0x48009d74, arg2 0x48001d24
437 Record 13: type 0x0208, thread 1, time 588, arg1 0x00000000, arg2 0x00000000
438 Record 14: type 0x0203, thread 1, time 697, arg1 0x48001cd8, arg2 0x480013e0
439 Record 15: type 0x0802, thread 1, time 946, arg1 0x00000002, arg2 0x00000000
440 Record 16: type 0x0201, thread 1, time 1083, arg1 0x480013e0, arg2 0x48001cd8
441 Record 17: type 0x0000, thread 0, time 0, arg1 0x00000000, arg2 0x00000000
442 Record 18: type 0x0000, thread 0, time 0, arg1 0x00000000, arg2 0x00000000
443 Record 19: type 0x0000, thread 0, time 0, arg1 0x00000000, arg2 0x00000000</PRE
454 SUMMARY="Footer navigation table"
465 HREF="debugging-techniques.html"
474 HREF="ecos-user-guide.html"
483 HREF="configuration-and-the-package-repository.html"
493 >Debugging Techniques</TD
499 HREF="debugging-techniques.html"
507 >Configuration and the Package Repository</TD