4 author: José Bollo <jose.bollo@iot.bzh>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
19 #include "afb-debug.h"
21 #if defined(AFB_INSERT_DEBUG_FEATURES)
30 static char key_env_break[] = "AFB_DEBUG_BREAK";
31 static char key_env_wait[] = "AFB_DEBUG_WAIT";
32 static char separs[] = ", \t\n";
34 static int has_key(const char *key, const char *list)
37 list += strspn(list, separs);
39 size_t s = strcspn(list, separs);
40 if (!strncasecmp(list, key, s) && !key[s])
43 list += strspn(list, separs);
49 static void handler(int signum)
53 void afb_debug(const char *key)
55 enum { None, Break, Wait } action;
57 if (has_key(key, secure_getenv(key_env_break)))
59 else if (has_key(key, secure_getenv(key_env_wait)))
65 const char *a = action == Break ? "BREAK" : "WAIT";
66 struct sigaction sa, psa;
69 NOTICE("DEBUG %s before %s", a, key);
70 memset(&sa, 0, sizeof sa);
71 sa.sa_handler = handler;
72 sigaction(SIGINT, &sa, &psa);
73 if (action == Break) {
77 sigaddset(&ss, SIGINT);
78 sigwaitinfo(&ss, NULL);
80 sigaction(SIGINT, &psa, NULL);
81 NOTICE("DEBUG %s after %s", a, key);