ソースを参照

Add function and line number for internal use of mg_cry

Improve runtime tracability (see also #560)
bel2125 7 年 前
コミット
3b9519a3e7
4 ファイル変更142 行追加87 行削除
  1. 47 17
      src/civetweb.c
  2. 55 39
      src/handle_form.inl
  3. 3 3
      src/mod_duktape.inl
  4. 37 28
      src/mod_lua.inl

+ 47 - 17
src/civetweb.c

@@ -2547,6 +2547,16 @@ static int is_websocket_protocol(const struct mg_connection *conn);
 #endif
 
 
+#define mg_cry_internal(conn, fmt, ...)                                        \
+	mg_cry_internal_wrap(conn, __func__, __LINE__, fmt, __VA_ARGS__)
+
+static void mg_cry_internal_wrap(const struct mg_connection *conn,
+                                 const char *func,
+                                 unsigned line,
+                                 const char *fmt,
+                                 ...) PRINTF_ARGS(4, 5);
+
+
 #if !defined(NO_THREAD_NAME)
 #if defined(_WIN32) && defined(_MSC_VER)
 /* Set the thread name for debugging purposes in Visual Studio
@@ -3174,10 +3184,10 @@ mg_vsnprintf(const struct mg_connection *conn,
 		if (truncated) {
 			*truncated = 1;
 		}
-		mg_cry_internal_internal(conn,
-		                         "truncating vsnprintf buffer: [%.*s]",
-		                         (int)((buflen > 200) ? 200 : (buflen - 1)),
-		                         buf);
+		mg_cry_internal(conn,
+		                "truncating vsnprintf buffer: [%.*s]",
+		                (int)((buflen > 200) ? 200 : (buflen - 1)),
+		                buf);
 		n = (int)buflen - 1;
 	}
 	buf[n] = '\0';
@@ -3401,11 +3411,15 @@ mg_cry_internal_impl(const struct mg_connection *conn,
 	struct mg_file fi;
 	time_t timestamp;
 
+	/* Unused, in the RELEASE build */
+	(void)func;
+	(void)line;
+
 	IGNORE_UNUSED_RESULT(vsnprintf_impl(buf, sizeof(buf), fmt, ap));
 
 	buf[sizeof(buf) - 1] = 0;
 
-    DEBUG_TRACE("mg_cry called from %s:%u: %s", func, line, buf);
+	DEBUG_TRACE("mg_cry called from %s:%u: %s", func, line, buf);
 
 	if (!conn) {
 		puts(buf);
@@ -3458,8 +3472,6 @@ mg_cry_internal_impl(const struct mg_connection *conn,
 	}
 }
 
-#define mg_cry_internal(conn, fmt, ...)                                        \
-	mg_cry_internal_wrap(conn, __func__, __LINE__, fmt, __VA_ARGS__)
 
 static void
 mg_cry_internal_wrap(const struct mg_connection *conn,
@@ -3474,15 +3486,17 @@ mg_cry_internal_wrap(const struct mg_connection *conn,
 	va_end(ap);
 }
 
+
 void
 mg_cry(const struct mg_connection *conn, const char *fmt, ...)
 {
 	va_list ap;
 	va_start(ap, fmt);
-	mg_cry_internal_impl(conn, "user", "0", fmt, ap);
+	mg_cry_internal_impl(conn, "user", 0, fmt, ap);
 	va_end(ap);
 }
 
+
 #define mg_cry DO_NOT_USE_THIS_FUNCTION__USE_mg_cry_internal
 
 
@@ -11498,7 +11512,9 @@ read_websocket(struct mg_connection *conn,
 				if (data_len > (uint64_t)0x7FFF0000ul) {
 					/* no can do */
 					mg_cry_internal(
-					    conn, "websocket out of memory; closing connection");
+					    conn,
+					    "%s",
+					    "websocket out of memory; closing connection");
 					break;
 				}
 			}
@@ -11515,7 +11531,9 @@ read_websocket(struct mg_connection *conn,
 					/* Allocation failed, exit the loop and then close the
 					 * connection */
 					mg_cry_internal(
-					    conn, "websocket out of memory; closing connection");
+					    conn,
+					    "%s",
+					    "websocket out of memory; closing connection");
 					break;
 				}
 			}
@@ -11554,7 +11572,9 @@ read_websocket(struct mg_connection *conn,
 				}
 				if (error) {
 					mg_cry_internal(
-					    conn, "Websocket pull failed; closing connection");
+					    conn,
+					    "%s",
+					    "Websocket pull failed; closing connection");
 					if (data != mem) {
 						mg_free(data);
 					}
@@ -11767,6 +11787,7 @@ mg_websocket_client_write(struct mg_connection *conn,
 	if (masked_data == NULL) {
 		/* Return -1 in an error case */
 		mg_cry_internal(conn,
+		                "%s",
 		                "Cannot allocate buffer for masked websocket response: "
 		                "Out of memory");
 		return -1;
@@ -12774,6 +12795,7 @@ handle_request(struct mg_connection *conn)
 			                   "%s",
 			                   "Error: SSL forward not configured properly");
 			mg_cry_internal(conn,
+			                "%s",
 			                "Can not redirect to SSL, no SSL port available");
 		}
 		return;
@@ -14252,6 +14274,7 @@ ssl_get_client_cert_info(struct mg_connection *conn)
 			    mg_strdup_ctx(str_finger, conn->phys_ctx);
 		} else {
 			mg_cry_internal(conn,
+			                "%s",
 			                "Out of memory: Cannot allocate memory for client "
 			                "certificate");
 		}
@@ -17328,6 +17351,7 @@ mg_start(const struct mg_callbacks *callbacks,
 		/* Fatal error - abort start. However, this situation should never
 		 * occur in practice. */
 		mg_cry_internal(fc(ctx),
+		                "%s",
 		                "Cannot initialize thread synchronization objects");
 		mg_free(ctx);
 		pthread_setspecific(sTlsKey, NULL);
@@ -17379,7 +17403,7 @@ mg_start(const struct mg_callbacks *callbacks,
 	/* Request size option */
 	itmp = atoi(ctx->dd.config[MAX_REQUEST_SIZE]);
 	if (itmp < 1024) {
-		mg_cry_internal(fc(ctx), "max_request_size too small");
+		mg_cry_internal(fc(ctx), "%s", "max_request_size too small");
 		free_context(ctx);
 		pthread_setspecific(sTlsKey, NULL);
 		return NULL;
@@ -17390,14 +17414,14 @@ mg_start(const struct mg_callbacks *callbacks,
 	workerthreadcount = atoi(ctx->dd.config[NUM_THREADS]);
 
 	if (workerthreadcount > MAX_WORKER_THREADS) {
-		mg_cry_internal(fc(ctx), "Too many worker threads");
+		mg_cry_internal(fc(ctx), "%s", "Too many worker threads");
 		free_context(ctx);
 		pthread_setspecific(sTlsKey, NULL);
 		return NULL;
 	}
 
 	if (workerthreadcount <= 0) {
-		mg_cry_internal(fc(ctx), "Invalid number of worker threads");
+		mg_cry_internal(fc(ctx), "%s", "Invalid number of worker threads");
 		free_context(ctx);
 		pthread_setspecific(sTlsKey, NULL);
 		return NULL;
@@ -17473,6 +17497,7 @@ mg_start(const struct mg_callbacks *callbacks,
 
 	if (ctx->worker_threadids == NULL) {
 		mg_cry_internal(fc(ctx),
+		                "%s",
 		                "Not enough memory for worker thread ID array");
 		free_context(ctx);
 		pthread_setspecific(sTlsKey, NULL);
@@ -17484,6 +17509,7 @@ mg_start(const struct mg_callbacks *callbacks,
 	                                          ctx);
 	if (ctx->worker_connections == NULL) {
 		mg_cry_internal(fc(ctx),
+		                "%s",
 		                "Not enough memory for worker thread connection array");
 		free_context(ctx);
 		pthread_setspecific(sTlsKey, NULL);
@@ -17497,7 +17523,9 @@ mg_start(const struct mg_callbacks *callbacks,
 	                           ctx->cfg_worker_threads,
 	                           ctx);
 	if (ctx->client_wait_events == NULL) {
-		mg_cry_internal(fc(ctx), "Not enough memory for worker event array");
+		mg_cry_internal(fc(ctx),
+		                "%s",
+		                "Not enough memory for worker event array");
 		mg_free(ctx->worker_threadids);
 		free_context(ctx);
 		pthread_setspecific(sTlsKey, NULL);
@@ -17509,7 +17537,9 @@ mg_start(const struct mg_callbacks *callbacks,
 	                                   ctx->cfg_worker_threads,
 	                                   ctx);
 	if (ctx->client_wait_events == NULL) {
-		mg_cry_internal(fc(ctx), "Not enough memory for worker socket array");
+		mg_cry_internal(fc(ctx),
+		                "%s",
+		                "Not enough memory for worker socket array");
 		mg_free(ctx->client_socks);
 		mg_free(ctx->worker_threadids);
 		free_context(ctx);
@@ -17537,7 +17567,7 @@ mg_start(const struct mg_callbacks *callbacks,
 
 #if defined(USE_TIMERS)
 	if (timers_init(ctx) != 0) {
-		mg_cry_internal(fc(ctx), "Error creating timers");
+		mg_cry_internal(fc(ctx), "%s", "Error creating timers");
 		free_context(ctx);
 		pthread_setspecific(sTlsKey, NULL);
 		return NULL;

+ 55 - 39
src/handle_form.inl

@@ -53,7 +53,7 @@ url_encoded_field_found(const struct mg_connection *conn,
 		if (((size_t)filename_dec_len >= (size_t)sizeof(filename_dec))
 		    || (filename_dec_len < 0)) {
 			/* Log error message and skip this field. */
-			mg_cry(conn, "%s: Cannot decode filename", __func__);
+			mg_cry_internal(conn, "%s: Cannot decode filename", __func__);
 			return MG_FORM_FIELD_STORAGE_SKIP;
 		}
 	} else {
@@ -65,13 +65,17 @@ url_encoded_field_found(const struct mg_connection *conn,
 
 	if ((ret & 0xF) == MG_FORM_FIELD_STORAGE_GET) {
 		if (fdh->field_get == NULL) {
-			mg_cry(conn, "%s: Function \"Get\" not available", __func__);
+			mg_cry_internal(conn,
+			                "%s: Function \"Get\" not available",
+			                __func__);
 			return MG_FORM_FIELD_STORAGE_SKIP;
 		}
 	}
 	if ((ret & 0xF) == MG_FORM_FIELD_STORAGE_STORE) {
 		if (fdh->field_store == NULL) {
-			mg_cry(conn, "%s: Function \"Store\" not available", __func__);
+			mg_cry_internal(conn,
+			                "%s: Function \"Store\" not available",
+			                __func__);
 			return MG_FORM_FIELD_STORAGE_SKIP;
 		}
 	}
@@ -95,10 +99,10 @@ url_encoded_field_get(const struct mg_connection *conn,
 
 	if (!value_dec) {
 		/* Log error message and stop parsing the form data. */
-		mg_cry(conn,
-		       "%s: Not enough memory (required: %lu)",
-		       __func__,
-		       (unsigned long)(value_len + 1));
+		mg_cry_internal(conn,
+		                "%s: Not enough memory (required: %lu)",
+		                __func__,
+		                (unsigned long)(value_len + 1));
 		return MG_FORM_FIELD_STORAGE_ABORT;
 	}
 
@@ -279,10 +283,10 @@ mg_handle_form_request(struct mg_connection *conn,
 					size_t n = (size_t)
 					    fwrite(val, 1, (size_t)vallen, fstore.access.fp);
 					if ((n != (size_t)vallen) || (ferror(fstore.access.fp))) {
-						mg_cry(conn,
-						       "%s: Cannot write file %s",
-						       __func__,
-						       path);
+						mg_cry_internal(conn,
+						                "%s: Cannot write file %s",
+						                __func__,
+						                path);
 						(void)mg_fclose(&fstore.access);
 						remove_bad_file(conn, path);
 					}
@@ -294,17 +298,20 @@ mg_handle_form_request(struct mg_connection *conn,
 							/* stored successfully */
 							field_stored(conn, path, file_size, fdh);
 						} else {
-							mg_cry(conn,
-							       "%s: Error saving file %s",
-							       __func__,
-							       path);
+							mg_cry_internal(conn,
+							                "%s: Error saving file %s",
+							                __func__,
+							                path);
 							remove_bad_file(conn, path);
 						}
 						fstore.access.fp = NULL;
 					}
 
 				} else {
-					mg_cry(conn, "%s: Cannot create file %s", __func__, path);
+					mg_cry_internal(conn,
+					                "%s: Cannot create file %s",
+					                __func__,
+					                path);
 				}
 			}
 
@@ -411,7 +418,10 @@ mg_handle_form_request(struct mg_connection *conn,
 				}
 				file_size = 0;
 				if (!fstore.access.fp) {
-					mg_cry(conn, "%s: Cannot create file %s", __func__, path);
+					mg_cry_internal(conn,
+					                "%s: Cannot create file %s",
+					                __func__,
+					                path);
 				}
 			}
 
@@ -451,10 +461,10 @@ mg_handle_form_request(struct mg_connection *conn,
 					size_t n = (size_t)
 					    fwrite(val, 1, (size_t)vallen, fstore.access.fp);
 					if ((n != (size_t)vallen) || (ferror(fstore.access.fp))) {
-						mg_cry(conn,
-						       "%s: Cannot write file %s",
-						       __func__,
-						       path);
+						mg_cry_internal(conn,
+						                "%s: Cannot write file %s",
+						                __func__,
+						                path);
 						mg_fclose(&fstore.access);
 						remove_bad_file(conn, path);
 					}
@@ -504,7 +514,10 @@ mg_handle_form_request(struct mg_connection *conn,
 					/* stored successfully */
 					field_stored(conn, path, file_size, fdh);
 				} else {
-					mg_cry(conn, "%s: Error saving file %s", __func__, path);
+					mg_cry_internal(conn,
+					                "%s: Error saving file %s",
+					                __func__,
+					                path);
 					remove_bad_file(conn, path);
 				}
 				fstore.access.fp = NULL;
@@ -552,10 +565,10 @@ mg_handle_form_request(struct mg_connection *conn,
 		boundary = (char *)mg_malloc(bl + 1);
 		if (!boundary) {
 			/* Out of memory */
-			mg_cry(conn,
-			       "%s: Cannot allocate memory for boundary [%lu]",
-			       __func__,
-			       (unsigned long)bl);
+			mg_cry_internal(conn,
+			                "%s: Cannot allocate memory for boundary [%lu]",
+			                __func__,
+			                (unsigned long)bl);
 			return -1;
 		}
 		memcpy(boundary, fbeg, bl);
@@ -820,7 +833,10 @@ mg_handle_form_request(struct mg_connection *conn,
 				file_size = 0;
 
 				if (!fstore.access.fp) {
-					mg_cry(conn, "%s: Cannot create file %s", __func__, path);
+					mg_cry_internal(conn,
+					                "%s: Cannot create file %s",
+					                __func__,
+					                path);
 				}
 			}
 
@@ -852,10 +868,10 @@ mg_handle_form_request(struct mg_connection *conn,
 						/* Store the content of the buffer. */
 						n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
 						if ((n != towrite) || (ferror(fstore.access.fp))) {
-							mg_cry(conn,
-							       "%s: Cannot write file %s",
-							       __func__,
-							       path);
+							mg_cry_internal(conn,
+							                "%s: Cannot write file %s",
+							                __func__,
+							                path);
 							mg_fclose(&fstore.access);
 							remove_bad_file(conn, path);
 						}
@@ -906,10 +922,10 @@ mg_handle_form_request(struct mg_connection *conn,
 				if (fstore.access.fp) {
 					n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
 					if ((n != towrite) || (ferror(fstore.access.fp))) {
-						mg_cry(conn,
-						       "%s: Cannot write file %s",
-						       __func__,
-						       path);
+						mg_cry_internal(conn,
+						                "%s: Cannot write file %s",
+						                __func__,
+						                path);
 						mg_fclose(&fstore.access);
 						remove_bad_file(conn, path);
 					} else {
@@ -919,10 +935,10 @@ mg_handle_form_request(struct mg_connection *conn,
 							/* stored successfully */
 							field_stored(conn, path, file_size, fdh);
 						} else {
-							mg_cry(conn,
-							       "%s: Error saving file %s",
-							       __func__,
-							       path);
+							mg_cry_internal(conn,
+							                "%s: Error saving file %s",
+							                __func__,
+							                path);
 							remove_bad_file(conn, path);
 						}
 					}

+ 3 - 3
src/mod_duktape.inl

@@ -53,7 +53,7 @@ mg_duk_fatal_handler(duk_context *duk_ctx, duk_errcode_t code, const char *msg)
 	duk_get_prop_string(duk_ctx, -1, civetweb_conn_id);
 	conn = (struct mg_connection *)duk_to_pointer(duk_ctx, -1);
 
-	mg_cry(conn, "JavaScript fatal (%u): %s", (unsigned)code, msg);
+	mg_cry_internal(conn, "JavaScript fatal (%u): %s", (unsigned)code, msg);
 }
 
 
@@ -167,7 +167,7 @@ mg_exec_duktape_script(struct mg_connection *conn, const char *script_name)
 	                          (void *)conn->phys_ctx,
 	                          mg_duk_fatal_handler);
 	if (!duk_ctx) {
-		mg_cry(conn, "Failed to create a Duktape heap.");
+		mg_cry_internal(conn, "%s", "Failed to create a Duktape heap.");
 		goto exec_duktape_finished;
 	}
 
@@ -255,7 +255,7 @@ mg_exec_duktape_script(struct mg_connection *conn, const char *script_name)
 	duk_put_prop_string(duk_ctx, -2, civetweb_conn_id);
 
 	if (duk_peval_file(duk_ctx, script_name) != 0) {
-		mg_cry(conn, "%s", duk_safe_to_string(duk_ctx, -1));
+		mg_cry_internal(conn, "%s", duk_safe_to_string(duk_ctx, -1));
 		goto exec_duktape_finished;
 	}
 	duk_pop(duk_ctx); /* ignore result */

+ 37 - 28
src/mod_lua.inl

@@ -149,37 +149,41 @@ lua_cry(struct mg_connection *conn,
 	case LUA_YIELD:
 		break;
 	case LUA_ERRRUN:
-		mg_cry(conn,
-		       "%s: %s failed: runtime error: %s",
-		       lua_title,
-		       lua_operation,
-		       lua_tostring(L, -1));
+		mg_cry_internal(conn,
+		                "%s: %s failed: runtime error: %s",
+		                lua_title,
+		                lua_operation,
+		                lua_tostring(L, -1));
 		break;
 	case LUA_ERRSYNTAX:
-		mg_cry(conn,
-		       "%s: %s failed: syntax error: %s",
-		       lua_title,
-		       lua_operation,
-		       lua_tostring(L, -1));
+		mg_cry_internal(conn,
+		                "%s: %s failed: syntax error: %s",
+		                lua_title,
+		                lua_operation,
+		                lua_tostring(L, -1));
 		break;
 	case LUA_ERRMEM:
-		mg_cry(conn, "%s: %s failed: out of memory", lua_title, lua_operation);
+		mg_cry_internal(conn,
+		                "%s: %s failed: out of memory",
+		                lua_title,
+		                lua_operation);
 		break;
 	case LUA_ERRGCMM:
-		mg_cry(conn,
-		       "%s: %s failed: error during garbage collection",
-		       lua_title,
-		       lua_operation);
+		mg_cry_internal(conn,
+		                "%s: %s failed: error during garbage collection",
+		                lua_title,
+		                lua_operation);
 		break;
 	case LUA_ERRERR:
-		mg_cry(conn,
-		       "%s: %s failed: error in error handling: %s",
-		       lua_title,
-		       lua_operation,
-		       lua_tostring(L, -1));
+		mg_cry_internal(conn,
+		                "%s: %s failed: error in error handling: %s",
+		                lua_title,
+		                lua_operation,
+		                lua_tostring(L, -1));
 		break;
 	default:
-		mg_cry(conn, "%s: %s failed: error %i", lua_title, lua_operation, err);
+		mg_cry_internal(
+		    conn, "%s: %s failed: error %i", lua_title, lua_operation, err);
 		break;
 	}
 }
@@ -567,10 +571,11 @@ lsp_include(lua_State *L)
 		include_history = (struct lsp_include_history *)lua_touserdata(L, -1);
 
 		if (include_history->depth >= ((int)(LSP_INCLUDE_MAX_DEPTH))) {
-			mg_cry(conn,
-			       "lsp max include depth of %i reached while including %s",
-			       (int)(LSP_INCLUDE_MAX_DEPTH),
-			       file_name);
+			mg_cry_internal(
+			    conn,
+			    "lsp max include depth of %i reached while including %s",
+			    (int)(LSP_INCLUDE_MAX_DEPTH),
+			    file_name);
 		} else {
 			char file_name_path[512];
 			char *p;
@@ -660,7 +665,7 @@ lsp_cry(lua_State *L)
 	const char *text = (num_args == 1) ? lua_tostring(L, 1) : NULL;
 
 	if (text) {
-		mg_cry(conn, "%s", lua_tostring(L, -1));
+		mg_cry_internal(conn, "%s", lua_tostring(L, -1));
 	} else {
 		/* Syntax error */
 		return luaL_error(L, "invalid cry() call");
@@ -2092,7 +2097,9 @@ lua_websocket_new(const char *script, struct mg_connection *conn)
 		if (*shared_websock_list == NULL) {
 			conn->must_close = 1;
 			mg_unlock_context(conn->phys_ctx);
-			mg_cry(conn, "Cannot create shared websocket struct, OOM");
+			mg_cry_internal(conn,
+			                "%s",
+			                "Cannot create shared websocket struct, OOM");
 			return NULL;
 		}
 		/* init ws list element */
@@ -2102,7 +2109,9 @@ lua_websocket_new(const char *script, struct mg_connection *conn)
 		if (!ws->script) {
 			conn->must_close = 1;
 			mg_unlock_context(conn->phys_ctx);
-			mg_cry(conn, "Cannot create shared websocket script, OOM");
+			mg_cry_internal(conn,
+			                "%s",
+			                "Cannot create shared websocket script, OOM");
 			return NULL;
 		}
 		pthread_mutex_init(&(ws->ws_mutex), &pthread_mutex_attr);