Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(437)

Side by Side Diff: Source/platform/TraceEvent.h

Issue 342513003: Don't allow direct const char* trace value (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 // TRACE_EVENT_INSTANT0("SUBSYSTEM", str); // BAD! 118 // TRACE_EVENT_INSTANT0("SUBSYSTEM", str); // BAD!
119 // free(str); // Trace system now has dangling pointer 119 // free(str); // Trace system now has dangling pointer
120 // 120 //
121 // To avoid this issue with the |name| and |arg_name| parameters, use the 121 // To avoid this issue with the |name| and |arg_name| parameters, use the
122 // TRACE_EVENT_COPY_XXX overloads of the macros at additional runtime overhead. 122 // TRACE_EVENT_COPY_XXX overloads of the macros at additional runtime overhead.
123 // Notes: The category must always be in a long-lived char* (i.e. static const). 123 // Notes: The category must always be in a long-lived char* (i.e. static const).
124 // The |arg_values|, when used, are always deep copied with the _COPY 124 // The |arg_values|, when used, are always deep copied with the _COPY
125 // macros. 125 // macros.
126 // 126 //
127 // When are string argument values copied: 127 // When are string argument values copied:
128 // const char* arg_values are only referenced by default: 128 // - Literal string is always allowed:
129 // TRACE_EVENT1("category", "name", "arg1", "literal string");
130 // - Use TRACE_STR_COPY to force copying of a const char*:
129 // TRACE_EVENT1("category", "name", 131 // TRACE_EVENT1("category", "name",
130 // "arg1", "literal string is only referenced"); 132 // "arg1", TRACE_STR_COPY(string_will_be_copied));
131 // Use TRACE_STR_COPY to force copying of a const char*: 133 // - Use TRACE_STR_NO_COPY to explicitly pass a const char* that is ensured
134 // long-lived without copy:
132 // TRACE_EVENT1("category", "name", 135 // TRACE_EVENT1("category", "name",
133 // "arg1", TRACE_STR_COPY("string will be copied")); 136 // "arg1", TRACE_STR_NO_COPY(long_lived_string));
134 // std::string arg_values are always copied: 137 // - std::string arg_values are always copied:
135 // TRACE_EVENT1("category", "name", 138 // TRACE_EVENT1("category", "name",
136 // "arg1", std::string("string will be copied")); 139 // "arg1", std::string(string_will_be_copied));
137 // 140 //
138 // 141 //
139 // Thread Safety: 142 // Thread Safety:
140 // A thread safe singleton and mutex are used for thread safety. Category 143 // A thread safe singleton and mutex are used for thread safety. Category
141 // enabled flags are used to limit the performance impact when the system 144 // enabled flags are used to limit the performance impact when the system
142 // is not enabled. 145 // is not enabled.
143 // 146 //
144 // TRACE_EVENT macros first cache a pointer to a category. The categories are 147 // TRACE_EVENT macros first cache a pointer to a category. The categories are
145 // statically allocated and safe at all times, even after exit. Fetching a 148 // statically allocated and safe at all times, even after exit. Fetching a
146 // category is protected by the TraceLog::lock_. Multiple threads initializing 149 // category is protected by the TraceLog::lock_. Multiple threads initializing
(...skipping 15 matching lines...) Expand all
162 165
163 #ifndef TraceEvent_h 166 #ifndef TraceEvent_h
164 #define TraceEvent_h 167 #define TraceEvent_h
165 168
166 #include "platform/EventTracer.h" 169 #include "platform/EventTracer.h"
167 170
168 #include "wtf/DynamicAnnotations.h" 171 #include "wtf/DynamicAnnotations.h"
169 #include "wtf/PassRefPtr.h" 172 #include "wtf/PassRefPtr.h"
170 #include "wtf/text/CString.h" 173 #include "wtf/text/CString.h"
171 174
172 // By default, const char* argument values are assumed to have long-lived scope 175 // Non-literal const char* argument values are not allowed.
173 // and will not be copied. Use this macro to force a const char* to be copied. 176 // Use this macro to force a const char* to be copied.
174 #define TRACE_STR_COPY(str) \ 177 #define TRACE_STR_COPY(str) \
175 WebCore::TraceEvent::TraceStringWithCopy(str) 178 WebCore::TraceEvent::TraceStringWrapper<true>(str)
179 // And use this macro to explicitly pass a long-lived const char*.
180 #define TRACE_STR_NO_COPY(str) \
181 WebCore::TraceEvent::TraceStringWrapper<false>(str)
176 182
177 // By default, uint64 ID argument values are not mangled with the Process ID in 183 // By default, uint64 ID argument values are not mangled with the Process ID in
178 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling. 184 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling.
179 #define TRACE_ID_MANGLE(id) \ 185 #define TRACE_ID_MANGLE(id) \
180 WebCore::TraceEvent::TraceID::ForceMangle(id) 186 WebCore::TraceEvent::TraceID::ForceMangle(id)
181 187
182 // By default, pointers are mangled with the Process ID in TRACE_EVENT_ASYNC 188 // By default, pointers are mangled with the Process ID in TRACE_EVENT_ASYNC
183 // macros. Use this macro to prevent Process ID mangling. 189 // macros. Use this macro to prevent Process ID mangling.
184 #define TRACE_ID_DONT_MANGLE(id) \ 190 #define TRACE_ID_DONT_MANGLE(id) \
185 WebCore::TraceEvent::TraceID::DontMangle(id) 191 WebCore::TraceEvent::TraceID::DontMangle(id)
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 // Simple union to store various types as unsigned long long. 733 // Simple union to store various types as unsigned long long.
728 union TraceValueUnion { 734 union TraceValueUnion {
729 bool m_bool; 735 bool m_bool;
730 unsigned long long m_uint; 736 unsigned long long m_uint;
731 long long m_int; 737 long long m_int;
732 double m_double; 738 double m_double;
733 const void* m_pointer; 739 const void* m_pointer;
734 const char* m_string; 740 const char* m_string;
735 }; 741 };
736 742
737 // Simple container for const char* that should be copied instead of retained. 743 // Simple container for const char*. The string will be copied or retained
738 class TraceStringWithCopy { 744 // according to the Copy template parameter.
745 template <bool Copy>
dsinclair 2014/06/17 20:47:16 Can we make the template parameter an enum? CopySt
Xianzhu 2014/06/17 21:27:35 Done with a slightly different method using TraceS
746 class TraceStringWrapper {
739 public: 747 public:
740 explicit TraceStringWithCopy(const char* str) : m_str(str) { } 748 explicit TraceStringWrapper(const char* str) : m_str(str) { }
741 operator const char* () const { return m_str; } 749 const char* str() const { return m_str; }
742 private: 750 private:
743 const char* m_str; 751 const char* m_str;
744 }; 752 };
745 753
746 // Define setTraceValue for each allowed type. It stores the type and 754 // Define setTraceValue for each allowed type. It stores the type and
747 // value in the return arguments. This allows this API to avoid declaring any 755 // value in the return arguments. This allows this API to avoid declaring any
748 // structures so that it is portable to third_party libraries. 756 // structures so that it is portable to third_party libraries.
749 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \ 757 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actualType, argExpression, unionMember, valueTypeId) \
750 union_member, \ 758 static inline void setTraceValue(actualType arg, unsigned char* type, unsign ed long long* value) { \
751 value_type_id) \
752 static inline void setTraceValue(actual_type arg, \
753 unsigned char* type, \
754 unsigned long long* value) { \
755 TraceValueUnion typeValue; \ 759 TraceValueUnion typeValue; \
756 typeValue.union_member = arg; \ 760 typeValue.unionMember = argExpression; \
757 *type = value_type_id; \ 761 *type = valueTypeId; \
758 *value = typeValue.m_uint; \ 762 *value = typeValue.m_uint; \
759 } 763 }
760 // Simpler form for int types that can be safely casted. 764 // Simpler form for int types that can be safely casted.
761 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \ 765 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actualType, valueTypeId) \
762 value_type_id) \ 766 static inline void setTraceValue(actualType arg, unsigned char* type, unsign ed long long* value) { \
763 static inline void setTraceValue(actual_type arg, \ 767 *type = valueTypeId; \
764 unsigned char* type, \
765 unsigned long long* value) { \
766 *type = value_type_id; \
767 *value = static_cast<unsigned long long>(arg); \ 768 *value = static_cast<unsigned long long>(arg); \
768 } 769 }
769 770
770 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT) 771 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
771 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned, TRACE_VALUE_TYPE_UINT) 772 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned, TRACE_VALUE_TYPE_UINT)
772 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT) 773 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
773 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT) 774 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
774 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT) 775 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
775 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT) 776 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
776 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT) 777 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
777 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT) 778 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
778 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, m_bool, TRACE_VALUE_TYPE_BOOL) 779 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, arg, m_bool, TRACE_VALUE_TYPE_BOOL)
779 INTERNAL_DECLARE_SET_TRACE_VALUE(double, m_double, TRACE_VALUE_TYPE_DOUBLE) 780 INTERNAL_DECLARE_SET_TRACE_VALUE(double, arg, m_double, TRACE_VALUE_TYPE_DOUBLE)
780 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, m_pointer, 781 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWrapper<false>&, arg.str(), m_ string, TRACE_VALUE_TYPE_STRING)
781 TRACE_VALUE_TYPE_POINTER) 782 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWrapper<true>&, arg.str(), m_s tring, TRACE_VALUE_TYPE_COPY_STRING)
782 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, m_string,
783 TRACE_VALUE_TYPE_STRING)
784 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, m_string,
785 TRACE_VALUE_TYPE_COPY_STRING)
786 783
787 #undef INTERNAL_DECLARE_SET_TRACE_VALUE 784 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
788 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT 785 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
789 786
790 // WTF::String version of setTraceValue so that trace arguments can be strings. 787 template <typename T>
788 struct TraceValuePointerTypeAllowed {
789 static const bool allowed = true;
790 };
791
792 template <>
793 struct TraceValuePointerTypeAllowed<char> {
794 static const bool allowed = false;
795 };
796
797 // Pointer version of SetTraceValue, but const char* is not allowed.
798 template <typename T>
799 static inline void setTraceValue(const T* arg, unsigned char* type, unsigned lon g long* value)
800 {
801 COMPILE_ASSERT(TraceValuePointerTypeAllowed<T>::allowed, invalid_pointer_typ e);
802 TraceValueUnion typeValue;
803 typeValue.m_pointer = arg;
804 *type = TRACE_VALUE_TYPE_POINTER;
805 *value = typeValue.m_uint;
806 }
807
808 // Allow literal string.
809 // FIXME: This will also allow passing local char arrays. Is it possible to avoi d?
810 template <size_t N>
811 static inline void setTraceValue(const char (&arg)[N], unsigned char* type, unsi gned long long* value)
dsinclair 2014/06/17 20:47:16 This is going to end up stamping out a function fo
Xianzhu 2014/06/17 21:27:35 There should be no difference before and after the
812 {
813 setTraceValue(TRACE_STR_NO_COPY(arg), type, value);
814 }
815
816 // WTF::CString version of setTraceValue so that trace arguments can be strings.
791 static inline void setTraceValue(const WTF::CString& arg, unsigned char* type, u nsigned long long* value) 817 static inline void setTraceValue(const WTF::CString& arg, unsigned char* type, u nsigned long long* value)
792 { 818 {
793 TraceValueUnion typeValue; 819 TraceValueUnion typeValue;
794 typeValue.m_string = arg.data(); 820 typeValue.m_string = arg.data();
795 *type = TRACE_VALUE_TYPE_COPY_STRING; 821 *type = TRACE_VALUE_TYPE_COPY_STRING;
796 *value = typeValue.m_uint; 822 *value = typeValue.m_uint;
797 } 823 }
798 824
799 static inline void setTraceValue(ConvertableToTraceFormat*, unsigned char* type, unsigned long long*) 825 static inline void setTraceValue(ConvertableToTraceFormat*, unsigned char* type, unsigned long long*)
800 { 826 {
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1009 const char* m_categoryGroup; 1035 const char* m_categoryGroup;
1010 const char* m_name; 1036 const char* m_name;
1011 IDType m_id; 1037 IDType m_id;
1012 }; 1038 };
1013 1039
1014 } // namespace TraceEvent 1040 } // namespace TraceEvent
1015 1041
1016 } // namespace WebCore 1042 } // namespace WebCore
1017 1043
1018 #endif 1044 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698