25 #include "dbus-internals.h"
26 #include "dbus-protocol.h"
27 #include "dbus-marshal-basic.h"
28 #include "dbus-test.h"
29 #include "dbus-valgrind-internal.h"
34 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
201 fatal_warnings =
FALSE;
202 fatal_warnings_on_check_failed =
FALSE;
206 fatal_warnings =
TRUE;
207 fatal_warnings_on_check_failed =
TRUE;
211 fprintf(stderr,
"DBUS_FATAL_WARNINGS should be set to 0 or 1 if set, not '%s'",
233 DBusSystemLogSeverity severity = DBUS_SYSTEM_LOG_WARNING;
240 severity = DBUS_SYSTEM_LOG_ERROR;
242 va_start (args, format);
265 DBusSystemLogSeverity severity = DBUS_SYSTEM_LOG_WARNING;
271 if (fatal_warnings_on_check_failed)
272 severity = DBUS_SYSTEM_LOG_ERROR;
274 va_start (args, format);
278 if (fatal_warnings_on_check_failed)
285 #ifdef DBUS_ENABLE_VERBOSE_MODE
290 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
291 static char module_name[1024];
295 _dbus_verbose_init (
void)
297 if (!verbose_initted)
300 verbose = p !=
NULL && *p ==
'1';
301 verbose_initted =
TRUE;
302 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
304 char *last_period, *last_slash;
305 GetModuleFileName(0,module_name,
sizeof(module_name)-1);
306 last_period = _mbsrchr(module_name,
'.');
309 last_slash = _mbsrchr(module_name,
'\\');
311 strcpy(module_name,last_slash+1);
312 strcat(module_name,
": ");
324 #define DBUS_IS_DIR_SEPARATOR(c) (c == '\\' || c == '/')
326 #define DBUS_IS_DIR_SEPARATOR(c) (c == '/')
333 static char *_dbus_file_path_extract_elements_from_tail(
const char *file,
int level)
336 char *p = (
char *)file + strlen(file);
341 if (DBUS_IS_DIR_SEPARATOR(*p))
351 return (
char *)file+prefix;
360 _dbus_is_verbose_real (
void)
362 _dbus_verbose_init ();
386 #ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
389 const char *
function,
403 if (!_dbus_is_verbose_real())
406 #ifndef DBUS_USE_OUTPUT_DEBUG_STRING
410 _dbus_print_thread ();
413 fprintf (stderr,
"%ld.%06ld ", sec, usec);
417 len = strlen (format);
418 if (format[len-1] ==
'\n')
423 va_start (args, format);
424 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
427 strcpy(buf,module_name);
428 #ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
429 sprintf (buf+strlen(buf),
"[%s(%d):%s] ",_dbus_file_path_extract_elements_from_tail(file,2),line,
function);
431 vsprintf (buf+strlen(buf),format, args);
433 OutputDebugStringA(buf);
436 #ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
437 fprintf (stderr,
"[%s(%d):%s] ",_dbus_file_path_extract_elements_from_tail(file,2),line,
function);
440 vfprintf (stderr, format, args);
454 _dbus_verbose_reset_real (
void)
456 verbose_initted =
FALSE;
460 _dbus_trace_ref (
const char *obj_name,
473 if (old_refcount == -1)
501 _dbus_warn (
"%s should be 0 or 1 if set, not '%s'", env_var, s);
507 if (old_refcount == -1)
509 VALGRIND_PRINTF_BACKTRACE (
"%s %p ref stolen (%s)",
511 _dbus_verbose (
"%s %p ref stolen (%s)\n",
516 VALGRIND_PRINTF_BACKTRACE (
"%s %p %d -> %d refs (%s)",
518 old_refcount, new_refcount, why);
519 _dbus_verbose (
"%s %p %d -> %d refs (%s)\n",
520 obj_name, obj, old_refcount, new_refcount, why);
550 memcpy (copy, str, len + 1);
573 memcpy (copy, mem, n_bytes);
596 for (len = 0; array[len] !=
NULL; ++len)
633 while (array[i] !=
NULL)
635 if (strcmp (array[i], str) == 0)
653 for (i = 0; array[i]; i++) {}
680 uuid->
as_uint32s[DBUS_UUID_LENGTH_WORDS - 1] = DBUS_UINT32_TO_BE (now);
683 DBUS_UUID_LENGTH_BYTES - 4,
687 "Failed to generate UUID: %s", rand_error.
message);
712 _dbus_read_uuid_file_without_creating (
const DBusString *filename,
722 _DBUS_SET_OOM (error);
729 _DBUS_SET_OOM (error);
741 "UUID file '%s' should contain a hex string of length %d, not length %d, with no other text",
743 DBUS_UUID_LENGTH_HEX,
750 _DBUS_SET_OOM (error);
757 "UUID file '%s' contains invalid hex data",
765 "UUID file '%s' contains %d bytes of hex-encoded data instead of %d",
768 DBUS_UUID_LENGTH_BYTES);
777 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
782 _DBUS_ASSERT_ERROR_IS_SET (error);
805 _DBUS_SET_OOM (error);
811 _DBUS_SET_OOM (error);
817 _DBUS_SET_OOM (error);
826 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
830 _DBUS_ASSERT_ERROR_IS_SET (error);
853 if (_dbus_read_uuid_file_without_creating (filename, uuid, &read_error))
856 if (!create_if_not_found)
883 static int machine_uuid_initialized_generation = 0;
905 _DBUS_SET_OOM (error);
920 _DBUS_SET_OOM (error);
929 #ifndef DBUS_DISABLE_CHECKS
931 _dbus_warn_return_if_fail (
const char *
function,
932 const char *assertion,
937 "arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n"
938 "This is normally a bug in some application using the D-Bus library.\n",
939 function, assertion, file, line);
943 #ifndef DBUS_DISABLE_ASSERT
958 const char *condition_text,
963 if (_DBUS_UNLIKELY (!condition))
965 _dbus_warn (
"assertion failed \"%s\" file \"%s\" line %d function %s",
966 condition_text, file, line, func);
986 _dbus_warn (
"File \"%s\" line %d should not have been reached: %s",
987 file, line, explanation);
992 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
994 run_failing_each_malloc (
int n_mallocs,
995 const char *description,
996 DBusTestMemoryFunction func,
1001 while (n_mallocs >= 0)
1003 _dbus_set_fail_alloc_counter (n_mallocs);
1005 _dbus_verbose (
"\n===\n%s: (will fail malloc %d with %d failures)\n===\n",
1006 description, n_mallocs,
1007 _dbus_get_fail_alloc_failures ());
1009 if (!(* func) (data))
1034 _dbus_test_oom_handling (
const char *description,
1035 DBusTestMemoryFunction func,
1039 const char *setting;
1040 int max_failures_to_try;
1047 _dbus_verbose (
"Running once to count mallocs\n");
1049 if (!(* func) (data))
1052 approx_mallocs =
_DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
1054 _dbus_verbose (
"\n=================\n%s: about %d mallocs total\n=================\n",
1055 description, approx_mallocs);
1058 if (setting !=
NULL)
1065 _dbus_warn (
"couldn't parse '%s' as integer\n", setting);
1066 max_failures_to_try = v;
1070 max_failures_to_try = 4;
1073 if (max_failures_to_try < 1)
1075 _dbus_verbose (
"not testing OOM handling\n");
1079 i = setting ? max_failures_to_try - 1 : 1;
1080 while (i < max_failures_to_try)
1082 _dbus_set_fail_alloc_failures (i);
1083 if (!run_failing_each_malloc (approx_mallocs, description, func, data))
1088 _dbus_verbose (
"\n=================\n%s: all iterations passed\n=================\n",