1010
1111#include " include/v8.h"
1212#include " src/api/api-inl.h"
13- #include " src/base/address-region.h"
1413#include " src/base/bits.h"
1514#include " src/base/hashmap.h"
1615#include " src/base/platform/platform.h"
@@ -897,20 +896,17 @@ class CodeDescription {
897896 };
898897#endif
899898
900- CodeDescription (const char * name, base::AddressRegion region,
901- SharedFunctionInfo shared, LineInfo* lineinfo,
902- bool is_function)
903- : name_(name),
904- shared_info_ (shared),
905- lineinfo_(lineinfo),
906- is_function_(is_function),
907- code_region_(region) {}
899+ CodeDescription (const char * name, Code code, SharedFunctionInfo shared,
900+ LineInfo* lineinfo)
901+ : name_(name), code_(code), shared_info_(shared), lineinfo_(lineinfo) {}
908902
909903 const char * name () const { return name_; }
910904
911905 LineInfo* lineinfo () const { return lineinfo_; }
912906
913- bool is_function () const { return is_function_; }
907+ bool is_function () const {
908+ return CodeKindIsOptimizedJSFunction (code_.kind ());
909+ }
914910
915911 bool has_scope_info () const { return !shared_info_.is_null (); }
916912
@@ -919,11 +915,15 @@ class CodeDescription {
919915 return shared_info_.scope_info ();
920916 }
921917
922- uintptr_t CodeStart () const { return code_region_.begin (); }
918+ uintptr_t CodeStart () const {
919+ return static_cast <uintptr_t >(code_.InstructionStart ());
920+ }
923921
924- uintptr_t CodeEnd () const { return code_region_.end (); }
922+ uintptr_t CodeEnd () const {
923+ return static_cast <uintptr_t >(code_.InstructionEnd ());
924+ }
925925
926- uintptr_t CodeSize () const { return code_region_. size (); }
926+ uintptr_t CodeSize () const { return CodeEnd () - CodeStart (); }
927927
928928 bool has_script () {
929929 return !shared_info_.is_null () && shared_info_.script ().IsScript ();
@@ -933,8 +933,6 @@ class CodeDescription {
933933
934934 bool IsLineInfoAvailable () { return lineinfo_ != nullptr ; }
935935
936- base::AddressRegion region () { return code_region_; }
937-
938936#if V8_TARGET_ARCH_X64
939937 uintptr_t GetStackStateStartAddress (StackState state) const {
940938 DCHECK (state < STACK_STATE_MAX );
@@ -967,10 +965,9 @@ class CodeDescription {
967965
968966 private:
969967 const char * name_;
968+ Code code_;
970969 SharedFunctionInfo shared_info_;
971970 LineInfo* lineinfo_;
972- bool is_function_;
973- base::AddressRegion code_region_;
974971#if V8_TARGET_ARCH_X64
975972 uintptr_t stack_state_start_addresses_[STACK_STATE_MAX ];
976973#endif
@@ -1819,17 +1816,26 @@ static JITCodeEntry* CreateELFObject(CodeDescription* desc, Isolate* isolate) {
18191816 return CreateCodeEntry (reinterpret_cast <Address>(w.buffer ()), w.position ());
18201817}
18211818
1822- // Like base::AddressRegion::StartAddressLess but also compares |end| when
1823- // |begin| is equal.
1824- struct AddressRegionLess {
1825- bool operator ()(const base::AddressRegion& a,
1826- const base::AddressRegion& b) const {
1827- if (a.begin () == b.begin ()) return a.end () < b.end ();
1828- return a.begin () < b.begin ();
1819+ struct AddressRange {
1820+ Address start;
1821+ Address end;
1822+ };
1823+
1824+ struct AddressRangeLess {
1825+ bool operator ()(const AddressRange& a, const AddressRange& b) const {
1826+ if (a.start == b.start ) return a.end < b.end ;
1827+ return a.start < b.start ;
18291828 }
18301829};
18311830
1832- using CodeMap = std::map<base::AddressRegion, JITCodeEntry*, AddressRegionLess>;
1831+ struct CodeMapConfig {
1832+ using Key = AddressRange;
1833+ using Value = JITCodeEntry*;
1834+ using Less = AddressRangeLess;
1835+ };
1836+
1837+ using CodeMap =
1838+ std::map<CodeMapConfig::Key, CodeMapConfig::Value, CodeMapConfig::Less>;
18331839
18341840static CodeMap* GetCodeMap () {
18351841 // TODO(jgruber): Don't leak.
@@ -1903,37 +1909,36 @@ static void AddUnwindInfo(CodeDescription* desc) {
19031909
19041910static base::LazyMutex mutex = LAZY_MUTEX_INITIALIZER ;
19051911
1906- // Remove entries from the map that intersect the given address region ,
1912+ // Remove entries from the map that intersect the given address range ,
19071913// and deregister them from GDB.
1908- static void RemoveJITCodeEntries (CodeMap* map,
1909- const base::AddressRegion region) {
1910- DCHECK_LT (region.begin (), region.end ());
1914+ static void RemoveJITCodeEntries (CodeMap* map, const AddressRange& range) {
1915+ DCHECK (range.start < range.end );
19111916
19121917 if (map->empty ()) return ;
19131918
19141919 // Find the first overlapping entry.
19151920
1916- // If successful, points to the first element not less than `region `. The
1921+ // If successful, points to the first element not less than `range `. The
19171922 // returned iterator has the key in `first` and the value in `second`.
1918- auto it = map->lower_bound (region );
1923+ auto it = map->lower_bound (range );
19191924 auto start_it = it;
19201925
19211926 if (it == map->end ()) {
19221927 start_it = map->begin ();
19231928 } else if (it != map->begin ()) {
19241929 for (--it; it != map->begin (); --it) {
1925- if ((*it).first .end () <= region. begin () ) break ;
1930+ if ((*it).first .end <= range. start ) break ;
19261931 start_it = it;
19271932 }
19281933 }
19291934
1930- DCHECK_NE (start_it, map->end ());
1935+ DCHECK (start_it != map->end ());
19311936
1932- // Find the first non-overlapping entry after `region `.
1937+ // Find the first non-overlapping entry after `range `.
19331938
1934- const auto end_it = map->lower_bound ({region .end () , 0 });
1939+ const auto end_it = map->lower_bound ({range .end , 0 });
19351940
1936- // Evict intersecting regions .
1941+ // Evict intersecting ranges .
19371942
19381943 if (std::distance (start_it, end_it) < 1 ) return ; // No overlapping entries.
19391944
@@ -1947,7 +1952,7 @@ static void RemoveJITCodeEntries(CodeMap* map,
19471952}
19481953
19491954// Insert the entry into the map and register it with GDB.
1950- static void AddJITCodeEntry (CodeMap* map, const base::AddressRegion region ,
1955+ static void AddJITCodeEntry (CodeMap* map, const AddressRange& range ,
19511956 JITCodeEntry* entry, bool dump_if_enabled,
19521957 const char * name_hint) {
19531958#if defined(DEBUG) && !V8_OS_WIN
@@ -1964,28 +1969,32 @@ static void AddJITCodeEntry(CodeMap* map, const base::AddressRegion region,
19641969 }
19651970#endif
19661971
1967- auto result = map->emplace (region , entry);
1972+ auto result = map->emplace (range , entry);
19681973 DCHECK (result.second ); // Insertion happened.
19691974 USE (result);
19701975
19711976 RegisterCodeEntry (entry);
19721977}
19731978
1974- static void AddCode (const char * name, base::AddressRegion region,
1975- SharedFunctionInfo shared, LineInfo* lineinfo,
1976- Isolate* isolate, bool is_function) {
1979+ static void AddCode (const char * name, Code code, SharedFunctionInfo shared,
1980+ LineInfo* lineinfo) {
19771981 DisallowGarbageCollection no_gc;
1978- CodeDescription code_desc (name, region, shared, lineinfo, is_function);
19791982
19801983 CodeMap* code_map = GetCodeMap ();
1981- RemoveJITCodeEntries (code_map, region);
1984+ AddressRange range;
1985+ range.start = code.address ();
1986+ range.end = code.address () + code.CodeSize ();
1987+ RemoveJITCodeEntries (code_map, range);
1988+
1989+ CodeDescription code_desc (name, code, shared, lineinfo);
19821990
19831991 if (!FLAG_gdbjit_full && !code_desc.IsLineInfoAvailable ()) {
19841992 delete lineinfo;
19851993 return ;
19861994 }
19871995
19881996 AddUnwindInfo (&code_desc);
1997+ Isolate* isolate = code.GetIsolate ();
19891998 JITCodeEntry* entry = CreateELFObject (&code_desc, isolate);
19901999
19912000 delete lineinfo;
@@ -2001,40 +2010,25 @@ static void AddCode(const char* name, base::AddressRegion region,
20012010 should_dump = (name_hint != nullptr );
20022011 }
20032012 }
2004- AddJITCodeEntry (code_map, region , entry, should_dump, name_hint);
2013+ AddJITCodeEntry (code_map, range , entry, should_dump, name_hint);
20052014}
20062015
20072016void EventHandler (const v8::JitCodeEvent* event) {
20082017 if (!FLAG_gdbjit) return ;
2009- if ((event->code_type != v8::JitCodeEvent::JIT_CODE ) &&
2010- (event->code_type != v8::JitCodeEvent::WASM_CODE )) {
2011- return ;
2012- }
2018+ if (event->code_type != v8::JitCodeEvent::JIT_CODE ) return ;
20132019 base::MutexGuard lock_guard (mutex.Pointer ());
20142020 switch (event->type ) {
20152021 case v8::JitCodeEvent::CODE_ADDED : {
20162022 Address addr = reinterpret_cast <Address>(event->code_start );
2023+ Isolate* isolate = reinterpret_cast <Isolate*>(event->isolate );
2024+ Code code = isolate->heap ()->GcSafeFindCodeForInnerPointer (addr);
20172025 LineInfo* lineinfo = GetLineInfo (addr);
20182026 std::string event_name (event->name .str , event->name .len );
20192027 // It's called UnboundScript in the API but it's a SharedFunctionInfo.
20202028 SharedFunctionInfo shared = event->script .IsEmpty ()
20212029 ? SharedFunctionInfo ()
20222030 : *Utils::OpenHandle (*event->script );
2023- Isolate* isolate = reinterpret_cast <Isolate*>(event->isolate );
2024- bool is_function = false ;
2025- // TODO(zhin): See if we can use event->code_type to determine
2026- // is_function, the difference currently is that JIT_CODE is SparkPlug,
2027- // TurboProp, TurboFan, whereas CodeKindIsOptimizedJSFunction is only
2028- // TurboProp and TurboFan. is_function is used for AddUnwindInfo, and the
2029- // prologue that SP generates probably matches that of TP/TF, so we can
2030- // use event->code_type here instead of finding the Code.
2031- // TODO(zhin): Rename is_function to be more accurate.
2032- if (event->code_type == v8::JitCodeEvent::JIT_CODE ) {
2033- Code code = isolate->heap ()->GcSafeFindCodeForInnerPointer (addr);
2034- is_function = CodeKindIsOptimizedJSFunction (code.kind ());
2035- }
2036- AddCode (event_name.c_str (), {addr, event->code_len }, shared, lineinfo,
2037- isolate, is_function);
2031+ AddCode (event_name.c_str (), code, shared, lineinfo);
20382032 break ;
20392033 }
20402034 case v8::JitCodeEvent::CODE_MOVED :
0 commit comments