Lluís Vilanova | dd03a39 | 2012-04-03 20:48:01 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | """ |
| 5 | Simple built-in backend. |
| 6 | """ |
| 7 | |
| 8 | __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>" |
| 9 | __copyright__ = "Copyright 2012, Lluís Vilanova <vilanova@ac.upc.edu>" |
| 10 | __license__ = "GPL version 2 or (at your option) any later version" |
| 11 | |
| 12 | __maintainer__ = "Stefan Hajnoczi" |
| 13 | __email__ = "stefanha@linux.vnet.ibm.com" |
| 14 | |
| 15 | |
| 16 | from tracetool import out |
| 17 | |
Lluís Vilanova | 93fba16 | 2013-03-05 14:47:26 +0100 | [diff] [blame] | 18 | |
| 19 | PUBLIC = True |
| 20 | |
| 21 | |
Harsh Prateek Bora | 62bab73 | 2012-07-18 15:15:59 +0530 | [diff] [blame] | 22 | def is_string(arg): |
| 23 | strtype = ('const char*', 'char*', 'const char *', 'char *') |
| 24 | if arg.lstrip().startswith(strtype): |
| 25 | return True |
| 26 | else: |
| 27 | return False |
Lluís Vilanova | dd03a39 | 2012-04-03 20:48:01 +0200 | [diff] [blame] | 28 | |
| 29 | def c(events): |
| 30 | out('#include "trace.h"', |
Lluís Vilanova | 60481e2 | 2013-03-05 14:47:55 +0100 | [diff] [blame] | 31 | '#include "trace/control.h"', |
Harsh Prateek Bora | 62bab73 | 2012-07-18 15:15:59 +0530 | [diff] [blame] | 32 | '#include "trace/simple.h"', |
Lluís Vilanova | dd03a39 | 2012-04-03 20:48:01 +0200 | [diff] [blame] | 33 | '', |
Lluís Vilanova | 60481e2 | 2013-03-05 14:47:55 +0100 | [diff] [blame] | 34 | ) |
Harsh Prateek Bora | 62bab73 | 2012-07-18 15:15:59 +0530 | [diff] [blame] | 35 | |
| 36 | for num, event in enumerate(events): |
| 37 | out('void trace_%(name)s(%(args)s)', |
| 38 | '{', |
| 39 | ' TraceBufferRecord rec;', |
| 40 | name = event.name, |
| 41 | args = event.args, |
| 42 | ) |
| 43 | sizes = [] |
| 44 | for type_, name in event.args: |
| 45 | if is_string(type_): |
| 46 | out(' size_t arg%(name)s_len = %(name)s ? MIN(strlen(%(name)s), MAX_TRACE_STRLEN) : 0;', |
| 47 | name = name, |
| 48 | ) |
| 49 | strsizeinfo = "4 + arg%s_len" % name |
| 50 | sizes.append(strsizeinfo) |
| 51 | else: |
| 52 | sizes.append("8") |
| 53 | sizestr = " + ".join(sizes) |
| 54 | if len(event.args) == 0: |
| 55 | sizestr = '0' |
| 56 | |
| 57 | |
| 58 | out('', |
Lluís Vilanova | 60481e2 | 2013-03-05 14:47:55 +0100 | [diff] [blame] | 59 | ' TraceEvent *eventp = trace_event_id(%(event_id)s);', |
| 60 | ' bool _state = trace_event_get_state_dynamic(eventp);', |
| 61 | ' if (!_state) {', |
Harsh Prateek Bora | 62bab73 | 2012-07-18 15:15:59 +0530 | [diff] [blame] | 62 | ' return;', |
| 63 | ' }', |
| 64 | '', |
| 65 | ' if (trace_record_start(&rec, %(event_id)s, %(size_str)s)) {', |
| 66 | ' return; /* Trace Buffer Full, Event Dropped ! */', |
| 67 | ' }', |
| 68 | event_id = num, |
| 69 | size_str = sizestr, |
| 70 | ) |
| 71 | |
| 72 | if len(event.args) > 0: |
| 73 | for type_, name in event.args: |
| 74 | # string |
| 75 | if is_string(type_): |
| 76 | out(' trace_record_write_str(&rec, %(name)s, arg%(name)s_len);', |
| 77 | name = name, |
| 78 | ) |
| 79 | # pointer var (not string) |
| 80 | elif type_.endswith('*'): |
Stefan Weil | 964d0a7 | 2012-08-13 21:50:56 +0200 | [diff] [blame] | 81 | out(' trace_record_write_u64(&rec, (uintptr_t)(uint64_t *)%(name)s);', |
Harsh Prateek Bora | 62bab73 | 2012-07-18 15:15:59 +0530 | [diff] [blame] | 82 | name = name, |
| 83 | ) |
| 84 | # primitive data type |
| 85 | else: |
| 86 | out(' trace_record_write_u64(&rec, (uint64_t)%(name)s);', |
| 87 | name = name, |
| 88 | ) |
| 89 | |
| 90 | out(' trace_record_finish(&rec);', |
| 91 | '}', |
| 92 | '') |
| 93 | |
Lluís Vilanova | dd03a39 | 2012-04-03 20:48:01 +0200 | [diff] [blame] | 94 | |
| 95 | def h(events): |
| 96 | out('#include "trace/simple.h"', |
| 97 | '') |
| 98 | |
Harsh Prateek Bora | 62bab73 | 2012-07-18 15:15:59 +0530 | [diff] [blame] | 99 | for event in events: |
| 100 | out('void trace_%(name)s(%(args)s);', |
| 101 | name = event.name, |
| 102 | args = event.args, |
Lluís Vilanova | dd03a39 | 2012-04-03 20:48:01 +0200 | [diff] [blame] | 103 | ) |