<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 22 Nov 2017, at 02:32, comic fans <<a href="mailto:comicfans44@gmail.com" class="">comicfans44@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">with some dirty hack , I've made xray runtime  'built' on windows ,<br class=""></div></div></blockquote><div><br class=""></div>\o/</div><div><br class=""><blockquote type="cite" class=""><div class=""><div class="">but unfortunately I haven't  enough knowledge about linker and the<br class="">runtime, and finally built executable didn't run.  I'd like to share<br class="">my changes here , hopes somebody help me to make it run on windows.<br class=""></div></div></blockquote><div><br class=""></div><div>Thanks for working on this!</div><div><br class=""></div><div>If you're alright with it, maybe you can send some patches to review, preferably through the LLVM Phabricator instance? You can have me or Reid (who knows more about COFF and the Windows stuff) as reviewers.</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">in AsmPrinter, copy/paster xray for coff target<br class=""><br class="">InstMap = OutContext.getCOFFSection("xray_instr_map", 0,<br class="">SectionKind::getReadOnlyWithRel());<br class="">FnSledIndex = OutContext.getCOFFSection("xray_fn_idx",<br class="">0,SectionKind::getReadOnlyWithRel());<br class=""><br class="">in XRayArgs , allow windows platform to use xray args. with this,<br class="">generated code seems have sled and xray parts.<br class=""><br class=""></div></div></blockquote><div><br class=""></div><div>Nice, I suspect we can make this change with tests as well, which we can build on incrementally.</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">in xray runtime,<br class=""> bool atomic_compare_exchange_strong(volatile atomic_sint32_t *a,<br class="">                                           s32 *cmp,<br class="">                                           s32 xchg,<br class="">                                           memory_order mo)<br class="">is missed for MSVC , I take atomic_uint32_t implementation<br class=""><br class=""></div></div></blockquote><div><br class=""></div><div>This is in compiler-rt/lib/sanitizer_common/... right?</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">msvc 14.1 treats BufferQueue::Buffer::Buffer as constructor instead of<br class="">data member, Buf.Buffer=>Buf.Data<br class=""><br class=""></div></div></blockquote><div><br class=""></div><div>Interesting. That's an easy patch to merge. :)</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">FunctionRecord pack , __attribute__((packed)) =>  #pragma<br class="">pack(push,1),  msvc also requires bitfields to be same type to pack<br class="">them together( all types => uint32_t)<br class=""><br class=""></div></div></blockquote><div><br class=""></div><div>Are you able to test this on other platforms?</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""> FD  int => HANDLE, most code logic still valid (-1 as invalid value),<br class="">r/w API replaced with windows<br class=""><br class="">mprotect => VirtualProtect<br class=""><br class="">readTSC in xray_x86_64.inc also works for windows<br class=""><br class="">replace read tsc from proc with QueryPerformanceFrequency<br class=""><br class="">msvc can not compile such code<br class="">void setupNewBuffer(int (*wall_clock_reader)(clockid_t,<br class="">                                                    struct timespec *));<br class=""><br class="">must use typedef first . xray use clock_gettime as default<br class="">implementation , which is not friendly for windows .create a fake one<br class="">based on chrono system_clock(ignore clockid_t)<br class=""><br class=""></div></div></blockquote><div><br class=""></div><div>This one is definitely something to do, even for potentially supporting XRay on Darwin where older versions of the SDK (10.11 and lower) don't define clock_gettime. Probably can be split off as a thing that can be reviewed and merged regardless.</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">for tls destructor part, I've just commented them out.(but<br class=""><a href="https://www.codeproject.com/Articles/8113/Thread-Local-Storage-The-C-Way" class="">https://www.codeproject.com/Articles/8113/Thread-Local-Storage-The-C-Way</a><br class="">gives a thread exit callback way for coff)<br class=""><br class=""></div></div></blockquote><div><br class=""></div><div>Interesting, thanks! This one is something that could be abstracted away on a per-platform basis.</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">and last thing , which I don't understand is the weak symbol for<br class="">__start_xray_instr_map[]<br class=""> __stop_xray_instr_map[]<br class=""> __start_xray_fn_idx[]<br class=""> __stop_xray_fn_idx[]<br class=""><br class="">I replace them with  __declspec(selectany) , but I'm not sure they<br class="">have same meanings.<br class=""><br class=""></div></div></blockquote><div><br class=""></div><div>The __{start, stop}_xray_{instr_map,fn_idx}[] arrays are usually generated by the linker on ELF and ELF-like platforms. I'm not aware what the MSVC COFF linkers do, probably something others who know better can answer.</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">some random generated code:<br class="">    .text<br class="">    .intel_syntax noprefix<br class="">    .def     call;<br class="">    .scl    2;<br class="">    .type    32;<br class="">    .endef<br class="">    .globl    call                    # -- Begin function call<br class="">    .p2align    4, 0x90<br class="">call:                                   # @call<br class="">.seh_proc call<br class=""># BB#0:                                 # %entry<br class="">    .p2align    1, 0x90<br class="">.Lxray_sled_0:<br class="">    .ascii    "\353\t"<br class="">    nop    word ptr [rax + rax + 512]<br class="">    sub    rsp, 16<br class="">    .seh_stackalloc 16<br class="">    .seh_endprologue<br class="">    mov    dword ptr [rsp + 12], ecx<br class="">    mov    dword ptr [rsp + 8], 0<br class="">    mov    dword ptr [rsp + 4], 0<br class="">.LBB0_1:                                # %for.cond<br class="">                                        # =>This Inner Loop Header: Depth=1<br class="">    mov    eax, dword ptr [rsp + 4]<br class="">    cmp    eax, dword ptr [rsp + 12]<br class="">    jge    .LBB0_4<br class=""># BB#2:                                 # %for.body<br class="">                                        #   in Loop: Header=BB0_1 Depth=1<br class="">    mov    eax, dword ptr [rsp + 4]<br class="">    add    eax, dword ptr [rsp + 8]<br class="">    mov    dword ptr [rsp + 8], eax<br class=""># BB#3:                                 # %for.inc<br class="">                                        #   in Loop: Header=BB0_1 Depth=1<br class="">    mov    eax, dword ptr [rsp + 4]<br class="">    add    eax, 1<br class="">    mov    dword ptr [rsp + 4], eax<br class="">    jmp    .LBB0_1<br class="">.LBB0_4:                                # %for.end<br class="">    mov    eax, dword ptr [rsp + 8]<br class="">    add    rsp, 16<br class="">    .p2align    1, 0x90<br class="">.Lxray_sled_1:<br class="">    ret<br class="">    nop    word ptr cs:[rax + rax + 512]<br class="">    .seh_handlerdata<br class="">    .text<br class="">    .seh_endproc<br class="">                                        # -- End function<br class="">    .section    xray_instr_map,"y"<br class="">.Lxray_sleds_start0:<br class="">    .quad    .Lxray_sled_0<br class="">    .quad    call<br class="">    .byte    0x00<br class="">    .byte    0x00<br class="">    .byte    0x00<br class="">    .zero    13<br class="">    .quad    .Lxray_sled_1<br class="">    .quad    call<br class="">    .byte    0x01<br class="">    .byte    0x00<br class="">    .byte    0x00<br class="">    .zero    13<br class="">.Lxray_sleds_end0:<br class="">    .section    xray_fn_idx,"y"<br class="">    .p2align    4, 0x90<br class="">    .quad    .Lxray_sleds_start0<br class="">    .quad    .Lxray_sleds_end0<br class="">    .text<br class=""><br class="">and parts of obj dump:<br class=""><br class=""><br class="">SECTION HEADER #5<br class="">     /16 name (xray_instr_map)<br class="">       0 physical address<br class="">       0 virtual address<br class="">      40 size of raw data<br class="">     198 file pointer to raw data (00000198 to 000001D7)<br class="">     1D8 file pointer to relocation table<br class="">       0 file pointer to line numbers<br class="">       4 number of relocations<br class="">       0 number of line numbers<br class="">  100000 flags<br class="">         1 byte align<br class=""><br class="">RAW DATA #5<br class="">  00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................<br class="">  00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................<br class="">  00000020: 56 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  V...............<br class="">  00000030: 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................<br class=""><br class=""><br class="">RELOCATIONS #5<br class="">                                                Symbol    Symbol<br class=""> Offset    Type              Applied To         Index     Name<br class=""> --------  ----------------  -----------------  --------  ------<br class=""> 00000000  ADDR64            00000000 00000000         0  .text<br class=""> 00000008  ADDR64            00000000 00000000         E  call<br class=""> 00000020  ADDR64            00000000 00000056         0  .text<br class=""> 00000028  ADDR64            00000000 00000000         E  call<br class=""><br class="">SECTION HEADER #6<br class="">      /4 name (xray_fn_idx)<br class="">       0 physical address<br class="">       0 virtual address<br class="">      10 size of raw data<br class="">     200 file pointer to raw data (00000200 to 0000020F)<br class="">     210 file pointer to relocation table<br class="">       0 file pointer to line numbers<br class="">       2 number of relocations<br class="">       0 number of line numbers<br class="">  500000 flags<br class="">         16 byte align<br class=""><br class="">RAW DATA #6<br class="">  00000000: 00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00  ........@.......<br class=""><br class="">RELOCATIONS #6<br class="">                                                Symbol    Symbol<br class=""> Offset    Type              Applied To         Index     Name<br class=""> --------  ----------------  -----------------  --------  ------<br class=""> 00000000  ADDR64            00000000 00000000         8  xray_instr_map<br class=""> 00000008  ADDR64            00000000 00000040         8  xray_instr_map<br class=""><br class=""></div></div></blockquote><div><br class=""></div><div>This looks like it's actually worked, at least at CodeGen time.</div><div><br class=""></div><div>Thanks again for sharing your experience, it'd be really great if you can have patches that we can review and land to potentially get XRay working on Windows!</div><div><br class=""></div><div>Cheers</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">On Tue, Nov 21, 2017 at 7:46 PM, Dean Michael Berris<br class=""><<a href="mailto:dean.berris@gmail.com" class="">dean.berris@gmail.com</a>> wrote:<br class=""><blockquote type="cite" class=""><br class="">On 17 Nov 2017, at 00:44, comic fans via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>><br class="">wrote:<br class=""><br class="">I'm learning the xray library and try if it can be built on windows,  in<br class="">xray_fdr_logging_impl.h<br class=""><br class="">line 152  , comment written as<br class="">// Using pthread_once(...) to initialize the thread-local data structures<br class=""><br class=""><br class="">but at line 175, 183, code written as<br class=""><br class="">thread_local pthread_key_t key;<br class=""><br class=""> // Ensure that we only actually ever do the pthread initialization once.<br class=""> thread_local bool UNUSED Unused = [] {<br class="">   new (&TLSBuffer) ThreadLocalData();<br class="">   auto result = pthread_key_create(&key, +[](void *) {<br class="">     auto &TLD = *reinterpret_cast<ThreadLocalData *>(&TLSBuffer);<br class=""><br class=""><br class="">I'm confused that pthread_key_t and Unused are both thread_local<br class="">variable, doesn't it mean the following lambda will run for each<br class="">thread , and create one pthread_key_t for only one tls data(instead of<br class="">only one pthread_key_t for all thread) ? also what does the '+' before<br class="">lambda expression mean ?  this may be stupid questions, could somebody<br class="">kindly  helped ?<br class=""><br class=""><br class="">Yeah, that comment is out-of-date (and the implementation is buggy) -- which<br class="">is a shame really. :/<br class=""><br class="">But, the good news, is I think we've fixed this now in the top-of-trunk with<br class=""><a href="https://reviews.llvm.org/D39526" class="">https://reviews.llvm.org/D39526</a> and <a href="https://reviews.llvm.org/D40164" class="">https://reviews.llvm.org/D40164</a>.<br class=""><br class="">Curiously though, how far did your exploration into getting XRay to build on<br class="">Windows go?<br class=""><br class="">Cheers<br class=""><br class="">-- Dean<br class=""><br class=""></blockquote></div></div></blockquote></div><br class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">-- Dean</div></div>

</div>
<br class=""></body></html>