crash_logging.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_DEBUG_CRASH_LOGGING_H_
  5. #define BASE_DEBUG_CRASH_LOGGING_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/base_export.h"
  9. #include "base/basictypes.h"
  10. #include "base/strings/string_piece.h"
  11. // These functions add metadata to the upload payload when sending crash reports
  12. // to the crash server.
  13. //
  14. // IMPORTANT: On OS X and Linux, the key/value pairs are only sent as part of
  15. // the upload and are not included in the minidump!
  16. namespace base {
  17. namespace debug {
  18. class StackTrace;
  19. // Set or clear a specific key-value pair from the crash metadata. Keys and
  20. // values are terminated at the null byte.
  21. BASE_EXPORT void SetCrashKeyValue(const base::StringPiece& key,
  22. const base::StringPiece& value);
  23. BASE_EXPORT void ClearCrashKey(const base::StringPiece& key);
  24. // Records the given StackTrace into a crash key.
  25. BASE_EXPORT void SetCrashKeyToStackTrace(const base::StringPiece& key,
  26. const StackTrace& trace);
  27. // Formats |count| instruction pointers from |addresses| using %p and
  28. // sets the resulting string as a value for crash key |key|. A maximum of 23
  29. // items will be encoded, since breakpad limits values to 255 bytes.
  30. BASE_EXPORT void SetCrashKeyFromAddresses(const base::StringPiece& key,
  31. const void* const* addresses,
  32. size_t count);
  33. // A scoper that sets the specified key to value for the lifetime of the
  34. // object, and clears it on destruction.
  35. class BASE_EXPORT ScopedCrashKey {
  36. public:
  37. ScopedCrashKey(const base::StringPiece& key, const base::StringPiece& value);
  38. ~ScopedCrashKey();
  39. private:
  40. std::string key_;
  41. DISALLOW_COPY_AND_ASSIGN(ScopedCrashKey);
  42. };
  43. // Before setting values for a key, all the keys must be registered.
  44. struct BASE_EXPORT CrashKey {
  45. // The name of the crash key, used in the above functions.
  46. const char* key_name;
  47. // The maximum length for a value. If the value is longer than this, it will
  48. // be truncated. If the value is larger than the |chunk_max_length| passed to
  49. // InitCrashKeys() but less than this value, it will be split into multiple
  50. // numbered chunks.
  51. size_t max_length;
  52. };
  53. // Before the crash key logging mechanism can be used, all crash keys must be
  54. // registered with this function. The function returns the amount of space
  55. // the crash reporting implementation should allocate space for the registered
  56. // crash keys. |chunk_max_length| is the maximum size that a value in a single
  57. // chunk can be.
  58. BASE_EXPORT size_t InitCrashKeys(const CrashKey* const keys, size_t count,
  59. size_t chunk_max_length);
  60. // Returns the correspnding crash key object or NULL for a given key.
  61. BASE_EXPORT const CrashKey* LookupCrashKey(const base::StringPiece& key);
  62. // In the platform crash reporting implementation, these functions set and
  63. // clear the NUL-termianted key-value pairs.
  64. typedef void (*SetCrashKeyValueFuncT)(const base::StringPiece&,
  65. const base::StringPiece&);
  66. typedef void (*ClearCrashKeyValueFuncT)(const base::StringPiece&);
  67. // Sets the function pointers that are used to integrate with the platform-
  68. // specific crash reporting libraries.
  69. BASE_EXPORT void SetCrashKeyReportingFunctions(
  70. SetCrashKeyValueFuncT set_key_func,
  71. ClearCrashKeyValueFuncT clear_key_func);
  72. // Helper function that breaks up a value according to the parameters
  73. // specified by the crash key object.
  74. BASE_EXPORT std::vector<std::string> ChunkCrashKeyValue(
  75. const CrashKey& crash_key,
  76. const base::StringPiece& value,
  77. size_t chunk_max_length);
  78. // Resets the crash key system so it can be reinitialized. For testing only.
  79. BASE_EXPORT void ResetCrashLoggingForTesting();
  80. } // namespace debug
  81. } // namespace base
  82. #endif // BASE_DEBUG_CRASH_LOGGING_H_