-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
163 lines (149 loc) · 4.2 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* Copyright 2012-2018 Dustin M. DeWeese
This file is part of the Startle example program.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "startle/types.h"
#include "startle/macros.h"
#include "startle/support.h"
#include "startle/log.h"
#include "startle/error.h"
#include "startle/test.h"
#include "startle/map.h"
#include "commands.h"
int main(int argc, char **argv) {
seg_t command_name = {NULL, 0};
seg_t command_argv[4];
unsigned int command_argc = 0;
error_t error;
if(catch_error(&error)) {
log_print_all();
return -error.type;
} else {
COUNTUP(i, argc) {
char *arg = argv[i];
int len = strlen(arg);
if(len == 0) continue;
if(arg[0] == '-') {
// run previous command
if(command_name.s) {
run_command(command_name, command_argc, command_argv);
}
command_argc = 0;
command_name = (seg_t) {.s = arg + 1,
.n = len - 1};
} else if(command_argc < LENGTH(command_argv)) {
command_argv[command_argc++] = (seg_t) { .s = arg, .n = len };
}
}
// run previous command
if(command_name.s) {
run_command(command_name, command_argc, command_argv);
}
return 0;
}
}
COMMAND(test, "run tests matching the argument") {
run_test(argc > 0 ? argv[0] : (seg_t){"", 0});
}
COMMAND(log, "print the log") {
log_print_all();
}
/* How to use this:
* 1. Find an interesting tag (last four characters) in the log
* 2. Load the program in a debugger
* 3. Set a breakpoint on the function 'breakpoint'
* 4. Run again passing the log tag with this flag
* 5. Finish the 'breakpoint' function
* 6. Now you are at the log message
*
* With LLDB:
* $ lldb example
* (lldb) b breakpoint
* (lldb) process launch -- -watch mhdc -fib 5
* (lldb) finish
*/
COMMAND(watch, "watch for a log tag") {
if(argc > 0) {
seg_t tag = argv[0];
if(tag.n == sizeof(tag_t) &&
tag.s[0] >= 'a' &&
tag.s[0] <= 'z') {
set_log_watch(tag.s, false);
} else {
printf("invalid log tag\n");
}
}
}
// Naive recursive calculation of the nth Fibonacci number
int fib(int n) {
CONTEXT("Calculating fib(%d)", n);
assert_error(n >= 0, "must be a positive number");
if(n < 2) {
LOG("fib(%d) is obviously 1", n);
return 1;
} else {
assert_counter(10000000);
int a = fib(n - 1);
int b = fib(n - 2);
int r = a + b;
assert_error(r >= 0, "overflow");
LOG("%d + %d = %d", a, b, r);
return r;
}
}
COMMAND(fib, "calculate the Nth Fibonacci number") {
if(argc > 0) {
int x = atoi(argv[0].s);
int r = fib(x);
printf("fib(%d) = %d\n", x, r);
}
}
// Memoized recursive calculation of the nth Fibonacci number
MAP(fib_result_map, 50);
int fib_map(int n) {
CONTEXT("Calculating fib_map(%d)", n);
assert_error(n >= 0, "must be a positive number");
if(n < 2) {
LOG("fib(%d) is obviously 1", n);
return 1;
} else {
assert_counter(10000000);
pair_t *x = map_find(fib_result_map, n);
if(x) {
int r = x->second;
LOG("found fib(%d) = %d", n, r);
return r;
} else {
int a = fib_map(n - 1);
int b = fib_map(n - 2);
int r = a + b;
assert_error(r >= 0, "overflow");
LOG_UNLESS(map_insert(fib_result_map, (pair_t) {n, r}),
NOTE("insertion failed"));
LOG("insert fib(%d) = %d + %d = %d", n, a, b, r);
return r;
}
}
}
COMMAND(fib_map, "calculate the Nth Fibonacci number, with memoization") {
if(argc > 0) {
int x = atoi(argv[0].s);
int r = fib_map(x);
printf("fib_map(%d) = %d\n", x, r);
}
}