Merge pull request #474 from kanoi/master cgsem - increase information on failure
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
diff --git a/logging.h b/logging.h
index 0a9b464..fa2a747 100644
--- a/logging.h
+++ b/logging.h
@@ -30,6 +30,8 @@ extern int opt_log_level;
extern void _applog(int prio, const char *str);
+#define IN_FMT_FFL " in %s %s():%d"
+
#define applog(prio, fmt, ...) do { \
if (opt_debug || prio != LOG_DEBUG) { \
if (use_syslog || opt_log_output || prio <= opt_log_level) { \
@@ -49,6 +51,26 @@ extern void _applog(int prio, const char *str);
_quit(status); \
} while (0)
+#define quithere(status, fmt, ...) do { \
+ if (fmt) { \
+ char tmp42[LOGBUFSIZ]; \
+ snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
+ ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
+ _applog(LOG_ERR, tmp42); \
+ } \
+ _quit(status); \
+} while (0)
+
+#define quitfrom(status, _file, _func, _line, fmt, ...) do { \
+ if (fmt) { \
+ char tmp42[LOGBUFSIZ]; \
+ snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
+ ##__VA_ARGS__, _file, _func, _line); \
+ _applog(LOG_ERR, tmp42); \
+ } \
+ _quit(status); \
+} while (0)
+
#ifdef HAVE_CURSES
#define wlog(fmt, ...) do { \
diff --git a/miner.h b/miner.h
index 2bc7ba7..500ecb9 100644
--- a/miner.h
+++ b/miner.h
@@ -715,16 +715,24 @@ endian_flip128(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
extern void _quit(int status);
-static inline void mutex_lock(pthread_mutex_t *lock)
+#define mutex_lock(_lock) _mutex_lock(_lock, __FILE__, __func__, __LINE__)
+#define mutex_unlock_noyield(_lock) _mutex_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
+#define wr_lock(_lock) _wr_lock(_lock, __FILE__, __func__, __LINE__)
+#define rd_lock(_lock) _rd_lock(_lock, __FILE__, __func__, __LINE__)
+#define rw_unlock(_lock) _rw_unlock(_lock, __FILE__, __func__, __LINE__)
+#define mutex_init(_lock) _mutex_init(_lock, __FILE__, __func__, __LINE__)
+#define rwlock_init(_lock) _rwlock_init(_lock, __FILE__, __func__, __LINE__)
+
+static inline void _mutex_lock(pthread_mutex_t *lock, const char *file, const char *func, const int line)
{
if (unlikely(pthread_mutex_lock(lock)))
- quit(1, "WTF MUTEX ERROR ON LOCK!");
+ quitfrom(1, file, func, line, "WTF MUTEX ERROR ON LOCK! errno=%d", errno);
}
-static inline void mutex_unlock_noyield(pthread_mutex_t *lock)
+static inline void _mutex_unlock_noyield(pthread_mutex_t *lock, const char *file, const char *func, const int line)
{
if (unlikely(pthread_mutex_unlock(lock)))
- quit(1, "WTF MUTEX ERROR ON UNLOCK!");
+ quitfrom(1, file, func, line, "WTF MUTEX ERROR ON UNLOCK! errno=%d", errno);
}
static inline void mutex_unlock(pthread_mutex_t *lock)
@@ -738,22 +746,22 @@ static inline int mutex_trylock(pthread_mutex_t *lock)
return pthread_mutex_trylock(lock);
}
-static inline void wr_lock(pthread_rwlock_t *lock)
+static inline void _wr_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
{
if (unlikely(pthread_rwlock_wrlock(lock)))
- quit(1, "WTF WRLOCK ERROR ON LOCK!");
+ quitfrom(1, file, func, line, "WTF WRLOCK ERROR ON LOCK! errno=%d", errno);
}
-static inline void rd_lock(pthread_rwlock_t *lock)
+static inline void _rd_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
{
if (unlikely(pthread_rwlock_rdlock(lock)))
- quit(1, "WTF RDLOCK ERROR ON LOCK!");
+ quitfrom(1, file, func, line, "WTF RDLOCK ERROR ON LOCK! errno=%d", errno);
}
-static inline void rw_unlock(pthread_rwlock_t *lock)
+static inline void _rw_unlock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
{
if (unlikely(pthread_rwlock_unlock(lock)))
- quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
+ quitfrom(1, file, func, line, "WTF RWLOCK ERROR ON UNLOCK! errno=%d", errno);
}
static inline void rd_unlock_noyield(pthread_rwlock_t *lock)
@@ -778,16 +786,16 @@ static inline void wr_unlock(pthread_rwlock_t *lock)
sched_yield();
}
-static inline void mutex_init(pthread_mutex_t *lock)
+static inline void _mutex_init(pthread_mutex_t *lock, const char *file, const char *func, const int line)
{
if (unlikely(pthread_mutex_init(lock, NULL)))
- quit(1, "Failed to pthread_mutex_init");
+ quitfrom(1, file, func, line, "Failed to pthread_mutex_init errno=%d", errno);
}
-static inline void rwlock_init(pthread_rwlock_t *lock)
+static inline void _rwlock_init(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
{
if (unlikely(pthread_rwlock_init(lock, NULL)))
- quit(1, "Failed to pthread_rwlock_init");
+ quitfrom(1, file, func, line, "Failed to pthread_rwlock_init errno=%d", errno);
}
/* cgminer locks, a write biased variant of rwlocks */
diff --git a/util.c b/util.c
index ef05881..81008f3 100644
--- a/util.c
+++ b/util.c
@@ -564,7 +564,7 @@ char *get_proxy(char *url, struct pool *pool)
len = split - url;
pool->rpc_proxy = malloc(1 + len - plen);
if (!(pool->rpc_proxy))
- quit(1, "Failed to malloc rpc_proxy");
+ quithere(1, "Failed to malloc rpc_proxy");
strcpy(pool->rpc_proxy, url + plen);
pool->rpc_proxytype = proxynames[i].proxytype;
@@ -590,7 +590,7 @@ char *bin2hex(const unsigned char *p, size_t len)
slen += 4 - (slen % 4);
s = calloc(slen, 1);
if (unlikely(!s))
- quit(1, "Failed to calloc in bin2hex");
+ quithere(1, "Failed to calloc");
for (i = 0; i < len; i++)
sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);
@@ -1090,7 +1090,7 @@ static void recalloc_sock(struct pool *pool, size_t len)
// applog(LOG_DEBUG, "Recallocing pool sockbuf to %d", new);
pool->sockbuf = realloc(pool->sockbuf, new);
if (!pool->sockbuf)
- quit(1, "Failed to realloc pool sockbuf in recalloc_sock");
+ quithere(1, "Failed to realloc pool sockbuf");
memset(pool->sockbuf + old, 0, new - old);
pool->sockbuf_size = new;
}
@@ -1282,11 +1282,11 @@ static bool parse_notify(struct pool *pool, json_t *val)
free(pool->swork.cb2);
pool->swork.cb1 = calloc(pool->swork.cb1_len, 1);
if (unlikely(!pool->swork.cb1))
- quit(1, "Failed to calloc swork cb1 in parse_notify");
+ quithere(1, "Failed to calloc swork cb1");
hex2bin(pool->swork.cb1, pool->swork.coinbase1, pool->swork.cb1_len);
pool->swork.cb2 = calloc(pool->swork.cb2_len, 1);
if (unlikely(!pool->swork.cb2))
- quit(1, "Failed to calloc swork cb2 in parse_notify");
+ quithere(1, "Failed to calloc swork cb2");
hex2bin(pool->swork.cb2, pool->swork.coinbase2, pool->swork.cb2_len);
cg_wunlock(&pool->data_lock);
@@ -1578,7 +1578,7 @@ static bool setup_stratum_socket(struct pool *pool)
if (!pool->sockbuf) {
pool->sockbuf = calloc(RBUFSIZE, 1);
if (!pool->sockbuf)
- quit(1, "Failed to calloc pool sockbuf in initiate_stratum");
+ quithere(1, "Failed to calloc pool sockbuf");
pool->sockbuf_size = RBUFSIZE;
}
@@ -1721,7 +1721,7 @@ resend:
free(pool->nonce1bin);
pool->nonce1bin = calloc(pool->n1_len, 1);
if (unlikely(!pool->nonce1bin))
- quit(1, "Failed to calloc pool->nonce1bin in initiate_stratum");
+ quithere(1, "Failed to calloc pool->nonce1bin");
hex2bin(pool->nonce1bin, pool->nonce1, pool->n1_len);
pool->n2size = n2size;
cg_wunlock(&pool->data_lock);
@@ -1830,7 +1830,7 @@ void *realloc_strcat(char *ptr, char *s)
ret = malloc(len);
if (unlikely(!ret))
- quit(1, "Failed to malloc in realloc_strcat");
+ quithere(1, "Failed to malloc");
sprintf(ret, "%s%s", ptr, s);
free(ptr);
@@ -1849,14 +1849,14 @@ void *str_text(char *ptr)
ret = strdup("(null)");
if (unlikely(!ret))
- quit(1, "Failed to malloc in text_str null");
+ quithere(1, "Failed to malloc null");
}
uptr = (unsigned char *)ptr;
ret = txt = malloc(strlen(ptr)*4+5); // Guaranteed >= needed
if (unlikely(!txt))
- quit(1, "Failed to malloc in text_str txt");
+ quithere(1, "Failed to malloc txt");
do {
if (*uptr < ' ' || *uptr > '~') {
@@ -1890,12 +1890,12 @@ void RenameThread(const char* name)
* that support them and for apple which does not. We use a single byte across
* a pipe to emulate semaphore behaviour there. */
#ifdef __APPLE__
-void cgsem_init(cgsem_t *cgsem)
+void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line)
{
int flags, fd, i;
if (pipe(cgsem->pipefd) == -1)
- quit(1, "Failed pipe in cgsem_init");
+ quitfrom(1, file, func, line, "Failed pipe errno=%d", errno);
/* Make the pipes FD_CLOEXEC to allow them to close should we call
* execv on restart. */
@@ -1904,55 +1904,56 @@ void cgsem_init(cgsem_t *cgsem)
flags = fcntl(fd, F_GETFD, 0);
flags |= FD_CLOEXEC;
if (fcntl(fd, F_SETFD, flags) == -1)
- quit(1, "Failed to fcntl in cgsem_init");
+ quitfrom(1, file, func, line, "Failed to fcntl errno=%d", errno);
}
}
-void cgsem_post(cgsem_t *cgsem)
+void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line)
{
const char buf = 1;
int ret;
ret = write(cgsem->pipefd[1], &buf, 1);
if (unlikely(ret == 0))
- applog(LOG_WARNING, "Failed to write in cgsem_post");
+ applog(LOG_WARNING, "Failed to write errno=%d" IN_FMT_FFL, errno, file, func, line);
}
-void cgsem_wait(cgsem_t *cgsem)
+void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int line)
{
char buf;
int ret;
ret = read(cgsem->pipefd[0], &buf, 1);
if (unlikely(ret == 0))
- applog(LOG_WARNING, "Failed to read in cgsem_wait");
+ applog(LOG_WARNING, "Failed to read errno=%d" IN_FMT_FFL, errno, file, func, line);
}
-void cgsem_destroy(cgsem_t *cgsem)
+void _cgsem_destroy(cgsem_t *cgsem)
{
close(cgsem->pipefd[1]);
close(cgsem->pipefd[0]);
}
#else
-void cgsem_init(cgsem_t *cgsem)
+void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line)
{
- if (sem_init(cgsem, 0, 0))
- quit(1, "Failed to sem_init in cgsem_init");
+ int ret;
+ if ((ret = sem_init(cgsem, 0, 0)))
+ quitfrom(1, file, func, line, "Failed to sem_init ret=%d errno=%d", ret, errno);
}
-void cgsem_post(cgsem_t *cgsem)
+void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line)
{
if (unlikely(sem_post(cgsem)))
- quit(1, "Failed to sem_post in cgsem_post");
+ quitfrom(1, file, func, line, "Failed to sem_post errno=%d cgsem=0x%p", errno, cgsem);
}
-void cgsem_wait(cgsem_t *cgsem)
+void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int line)
{
if (unlikely(sem_wait(cgsem)))
- quit(1, "Failed to sem_wait in cgsem_wait");
+ quitfrom(1, file, func, line, "Failed to sem_wait errno=%d cgsem=0x%p", errno, cgsem);
}
-void cgsem_destroy(cgsem_t *cgsem)
+void _cgsem_destroy(cgsem_t *cgsem)
{
sem_destroy(cgsem);
}
diff --git a/util.h b/util.h
index c95f7f4..dd17931 100644
--- a/util.h
+++ b/util.h
@@ -93,10 +93,15 @@ void dev_error(struct cgpu_info *dev, enum dev_reason reason);
void *realloc_strcat(char *ptr, char *s);
void *str_text(char *ptr);
void RenameThread(const char* name);
-void cgsem_init(cgsem_t *cgsem);
-void cgsem_post(cgsem_t *cgsem);
-void cgsem_wait(cgsem_t *cgsem);
-void cgsem_destroy(cgsem_t *cgsem);
+void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line);
+void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line);
+void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int line);
+void _cgsem_destroy(cgsem_t *cgsem);
+
+#define cgsem_init(_sem) _cgsem_init(_sem, __FILE__, __func__, __LINE__)
+#define cgsem_post(_sem) _cgsem_post(_sem, __FILE__, __func__, __LINE__)
+#define cgsem_wait(_sem) _cgsem_wait(_sem, __FILE__, __func__, __LINE__)
+#define cgsem_destroy(_sem) _cgsem_destroy(_sem)
/* Align a size_t to 4 byte boundaries for fussy arches */
static inline void align_len(size_t *len)