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)
29 #include <sys/types.h>
33 #if !defined(NO_CALL_PERSONALITY)
34 #include <sys/personality.h>
39 static char key_env_break[] = "AFB_DEBUG_BREAK";
40 static char key_env_wait[] = "AFB_DEBUG_WAIT";
41 static char separs[] = ", \t\n";
44 * Checks whether the 'key' is in the 'list'
45 * Return 1 if it is in or 0 otherwise
47 static int has_key(const char *key, const char *list)
50 list += strspn(list, separs);
52 size_t s = strcspn(list, separs);
53 if (!strncasecmp(list, key, s) && !key[s])
56 list += strspn(list, separs);
62 static void indicate(const char *key)
64 #if !defined(NO_AFB_DEBUG_FILE_INDICATION)
68 snprintf(filename, sizeof filename, "/tmp/afb-debug-%ld", (long)getpid());
70 fd = creat(filename, 0644);
71 write(fd, key, strlen(key));
79 static void handler(int signum)
83 void afb_debug_wait(const char *key)
85 struct sigaction sa, psa;
89 NOTICE("DEBUG WAIT before %s", key);
91 sigdelset(&ss, SIGINT);
92 sigprocmask(SIG_SETMASK, &ss, &oss);
94 sigaddset(&ss, SIGINT);
95 memset(&sa, 0, sizeof sa);
96 sa.sa_handler = handler;
97 sigaction(SIGINT, &sa, &psa);
99 sigwaitinfo(&ss, NULL);
100 sigaction(SIGINT, &psa, NULL);
102 sigprocmask(SIG_SETMASK, &oss, NULL);
103 NOTICE("DEBUG WAIT after %s", key);
104 #if !defined(NO_CALL_PERSONALITY)
105 personality((unsigned long)-1L);
109 void afb_debug_break(const char *key)
111 struct sigaction sa, psa;
114 NOTICE("DEBUG BREAK before %s", key);
115 memset(&sa, 0, sizeof sa);
116 sa.sa_handler = handler;
117 sigaction(SIGINT, &sa, &psa);
119 sigaction(SIGINT, &psa, NULL);
120 NOTICE("DEBUG BREAK after %s", key);
123 void afb_debug(const char *key)
125 if (has_key(key, secure_getenv(key_env_wait)))
127 if (has_key(key, secure_getenv(key_env_break)))
128 afb_debug_break(key);