From d4effb01208269dfeaaf7085567344326b7f7aa5 Mon Sep 17 00:00:00 2001 From: Packit Service Date: Dec 09 2020 11:53:34 +0000 Subject: crash-ptdump-command-1.0.7 base --- diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..813f10b --- /dev/null +++ b/COPYING @@ -0,0 +1,348 @@ + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. + + + + + + + diff --git a/ptdump.c b/ptdump.c new file mode 100644 index 0000000..f5eb110 --- /dev/null +++ b/ptdump.c @@ -0,0 +1,599 @@ +/* + * Extension module to dump log buffer of Intel(R) Processor Trace + * + * Copyright (C) 2016 FUJITSU LIMITED + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#define _GNU_SOURCE +#include + +#include "defs.h" + +#ifdef DEBUG +#define dbgprintf(...) fprintf(__VA_ARGS__) +#else +#define dbgprintf(...) {} +#endif + +#define TOPA_SHIFT 12 + +extern int fastdecode(char *, char *); + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +struct topa_entry { + u64 end : 1; + u64 rsvd0 : 1; + u64 intr : 1; + u64 rsvd1 : 1; + u64 stop : 1; + u64 rsvd2 : 1; + u64 size : 4; + u64 rsvd3 : 2; + u64 base : 36; + u64 rsvd4 : 16; +}; + +struct pt_info { + ulong aux_pages; + int aux_nr_pages; + ulong pt_buffer; + + ulong topa_base; + uint topa_idx; + ulong output_off; + + ulong *buffer_ptr; + int curr_buf_idx; + + ulong *pt_caps; + u32 *cap_regs; +} *pt_info_list; + +static inline int +get_member(ulong addr, char *name, char *member, void* buf) +{ + ulong offset, size; + + offset = MEMBER_OFFSET(name, member); + size = MEMBER_SIZE(name, member); + + + if (!readmem(addr + offset, KVADDR, buf, size, name, RETURN_ON_ERROR)) + return FALSE; + + return TRUE; +} + +int init_pt_info(int cpu) +{ + + ulong struct_pt, struct_handle, struct_ring_buffer; + ulong aux_pages, aux_priv; + int aux_nr_pages, buf_len; + int i, current_buf_idx; + struct topa_entry topa; + ulong topa_base, output_off, output_base; + uint topa_idx; + struct pt_info *pt_info_ptr = pt_info_list + cpu; + + /* Get pointer to struct pt */ + if (!symbol_exists("pt_ctx")) { + fprintf(fp, "[%d] symbol not found: pt_ctx\n", cpu); + return FALSE; + } + struct_pt = symbol_value("pt_ctx") + kt->__per_cpu_offset[cpu]; + + /* Get pointer to struct perf_output_handle, struct ring_buffer */ + struct_handle = struct_pt + MEMBER_OFFSET("pt", "handle"); + if(!get_member(struct_handle, "perf_output_handle", "rb", + &struct_ring_buffer)) + return FALSE; + if (!struct_ring_buffer) { + fprintf(fp, "[%d] ring buffer is zero\n", cpu); + return FALSE; + } + + dbgprintf(fp, "[%d] struct pt=0x%016lx\n", cpu, struct_pt); + dbgprintf(fp, "[%d] struct perf_output_handle=0x%016lx\n", cpu, + struct_handle); + dbgprintf(fp, "[%d] struct ring_buffer=0x%016lx\n", cpu, + struct_ring_buffer); + + /* symbol access check */ + if (STRUCT_EXISTS("ring_buffer") && + !MEMBER_EXISTS("ring_buffer", "aux_pages")) { + fprintf(fp, "[%d] invalid ring_buffer\n", cpu); + return FALSE; + } + + /* array of struct pages for pt buffer */ + if(!get_member(struct_ring_buffer, "ring_buffer", "aux_pages", + &aux_pages)) + return FALSE; + + /* number of pages */ + if(!get_member(struct_ring_buffer, "ring_buffer", "aux_nr_pages", + &aux_nr_pages)) + return FALSE; + + /* private data (struct pt_buffer) */ + if(!get_member(struct_ring_buffer, "ring_buffer", "aux_priv", + &aux_priv)) + return FALSE; + + if (!aux_nr_pages) { + fprintf(fp, "No aux pages\n"); + return FALSE; + } + + pt_info_ptr->aux_pages = aux_pages; + pt_info_ptr->aux_nr_pages = aux_nr_pages; + pt_info_ptr->pt_buffer = aux_priv; + + dbgprintf(fp, "[%d] rb.aux_pages=0x%016lx\n", cpu, aux_pages); + dbgprintf(fp, "[%d] rb.aux_nr_pages=0x%d\n", cpu, aux_nr_pages); + dbgprintf(fp, "[%d] rb.aux_priv=0x%016lx\n", cpu, aux_priv); + + /* Get address of pt buffer */ + buf_len = sizeof(void*)*aux_nr_pages; + pt_info_ptr->buffer_ptr = (ulong *)malloc(buf_len); + if (pt_info_ptr->buffer_ptr == NULL) { + fprintf(fp, "malloc failed\n"); + return FALSE; + } + memset(pt_info_ptr->buffer_ptr, 0, buf_len); + + for (i=0; ibuffer_ptr[i] = page; + + if (!i) + dbgprintf(fp, "[%d] Dump aux pages\n", cpu); + dbgprintf(fp, " %d: 0x%016lx\n", i, page); + } + + /* Get pt registes saved on panic */ + if(!get_member(pt_info_ptr->pt_buffer, "pt_buffer", "cur", + &topa_base)) + goto out_error; + if(!get_member(pt_info_ptr->pt_buffer, "pt_buffer", "cur_idx", + &topa_idx)) + goto out_error; + if(!get_member(pt_info_ptr->pt_buffer, "pt_buffer", "output_off", + &output_off)) + goto out_error; + + dbgprintf(fp, "[%d] buf.cur=0x%016lx\n", cpu, topa_base); + dbgprintf(fp, "[%d] buf.cur_idx=0x%08x\n", cpu, topa_idx); + dbgprintf(fp, "[%d] buf.output_off=0x%016lx\n", cpu, output_off); + + pt_info_ptr->topa_base = topa_base; + pt_info_ptr->topa_idx = topa_idx; + pt_info_ptr->output_off = output_off; + + /* Read topa entry */ + if (!readmem((topa_base) + topa_idx*(sizeof(struct topa_entry)), + KVADDR, &topa, sizeof(topa), + "struct topa_entry", RETURN_ON_ERROR)) { + fprintf(fp, "Cannot read topa table\n"); + goto out_error; + } + + dbgprintf(fp, "[%d] topa.end=%d\n", cpu, topa.end); + dbgprintf(fp, "[%d] topa.intr=%d\n", cpu, topa.intr); + dbgprintf(fp, "[%d] topa.stop=%d\n", cpu, topa.stop); + dbgprintf(fp, "[%d] topa.size=%d\n", cpu, topa.size); + dbgprintf(fp, "[%d] topa.base=0x%016lx\n", cpu, (ulong)topa.base); + + /* + * Find buffer page which is currently used. + */ + output_base = (u64)(topa.base)<buffer_ptr[i]) == output_base) { + current_buf_idx = i; + break; + } + } + + if (current_buf_idx == -1) { + fprintf(fp, "current buffer not found\n"); + goto out_error; + } + + pt_info_ptr->curr_buf_idx = current_buf_idx; + dbgprintf(fp, "[%d] current bufidx=%d\n", cpu, current_buf_idx); + + return TRUE; + +out_error: + if (pt_info_ptr->buffer_ptr != NULL) + free(pt_info_ptr->buffer_ptr); + + return FALSE; +} + +static inline int is_zero_page(ulong page, int offset) +{ + ulong read_addr = page + offset; + ulong read_size = PAGESIZE() - offset; + char *buf = malloc(PAGESIZE()); + int i; + + if (buf == NULL) { + fprintf(fp, "malloc failed\n"); + return FALSE; + } + + memset(buf, 0, PAGESIZE()); + dbgprintf(fp, "zero page chk: 0x%016lx, %lu\n", read_addr, read_size); + if (!readmem(read_addr, KVADDR, buf, read_size, "zero page check", + RETURN_ON_ERROR)) { + free(buf); + return FALSE; + } + + for (i = 0; i < PAGESIZE() - offset; i++) { + if (buf[i]) { + free(buf); + return FALSE; + } + } + + free(buf); + return TRUE; +} + +int check_wrap_around(int cpu) +{ + struct pt_info *pt_info_ptr = pt_info_list + cpu; + int wrapped = 0, i, page_idx; + ulong offset, mask, page; + + mask = (((ulong)1)<output_off & mask; + page_idx = pt_info_ptr->curr_buf_idx + + (pt_info_ptr->output_off >> PAGESHIFT()); + + dbgprintf(fp, "[%d] buf: mask=0x%lx\n", cpu, mask); + dbgprintf(fp, "[%d] buf: offset=0x%lx\n", cpu, offset); + dbgprintf(fp, "[%d] buf: page_idx=%d\n", cpu, page_idx); + + for (i=page_idx; iaux_nr_pages; i++) { + page = pt_info_ptr->buffer_ptr[i]; + + if (!is_zero_page(page, offset)) { + wrapped = 1; + break; + } + + offset = 0; + } + + return wrapped; +} + +int write_buffer_wrapped(int cpu, FILE *out_fp) +{ + struct pt_info *pt_info_ptr = pt_info_list + cpu; + int start_idx, idx, len, ret; + ulong mask, offset, page; + char *buf = malloc(PAGESIZE()); + + if (buf == NULL) { + fprintf(fp, "malloc failed\n"); + return FALSE; + } + + mask = (((ulong)1)<output_off & mask; + + start_idx = pt_info_ptr->curr_buf_idx + + (pt_info_ptr->output_off >> PAGESHIFT()); + + for (idx = start_idx; idxaux_nr_pages; idx++) { + page = pt_info_ptr->buffer_ptr[idx]; + len = PAGESIZE() - offset; + + if (!readmem(page + offset, KVADDR, buf, len, "read page for write", + RETURN_ON_ERROR)) { + free(buf); + return FALSE; + } + + dbgprintf(fp, "[%d] R/W1 buff: p=0x%lx, i=%d, o=%lu, l=%d\n", + cpu, page + offset, idx, offset, len); + + ret = fwrite(buf, len, 1, out_fp); + if (!ret) { + fprintf(fp, "[%d] Cannot write file\n", cpu); + free(buf); + return FALSE; + } + + offset = 0; + } + + for (idx = 0; idx < start_idx; idx++) { + page = pt_info_ptr->buffer_ptr[idx]; + len = PAGESIZE() - offset; + + if (!readmem(page + offset, KVADDR, buf, len, "read page for write", + RETURN_ON_ERROR)) { + free(buf); + return FALSE; + } + + dbgprintf(fp, "[%d] R/W2 buff: p=0x%lx, i=%d, o=%lu, l=%d\n", + cpu, page + offset, idx, offset, len); + + ret = fwrite(buf, len, 1, out_fp); + if (!ret) { + fprintf(fp, "[%d] Cannot write file\n", cpu); + free(buf); + return FALSE; + } + } + + idx = start_idx; + page = pt_info_ptr->buffer_ptr[idx]; + offset = pt_info_ptr->output_off & mask; + len = offset; + + if (!len) + goto done; + + if (!readmem(page, KVADDR, buf, len, "read page for write", + RETURN_ON_ERROR)) { + free(buf); + return FALSE; + } + + dbgprintf(fp, "[%d] R/W3 buff: p=0x%lx, i=%d, o=%lu, l=%d\n", cpu, + page, idx, offset, len); + + ret = fwrite(buf, len, 1, out_fp); + if (!ret) { + fprintf(fp, "[%d] Cannot write file\n", cpu); + free(buf); + return FALSE; + } + +done: + free(buf); + return TRUE; +} + +int write_buffer_nowrapped(int cpu, FILE *out_fp) +{ + struct pt_info *pt_info_ptr = pt_info_list + cpu; + int last_idx, idx, len, ret; + ulong mask, page; + char *buf = malloc(PAGESIZE()); + + if (buf == NULL) { + fprintf(fp, "malloc failed\n"); + return FALSE; + } + + mask = (((ulong)1)<curr_buf_idx + + (pt_info_ptr->output_off >> PAGESHIFT()); + + for (idx = 0; idx < last_idx; idx++) { + page = pt_info_ptr->buffer_ptr[idx]; + len = PAGESIZE(); + + if (!readmem(page, KVADDR, buf, len, "read page for write", + RETURN_ON_ERROR)) { + free(buf); + return FALSE; + } + + dbgprintf(fp, "[%d] R/W1 buff: p=0x%lx, i=%d, o=%lu, l=%d\n", + cpu, page, idx, (ulong)0, len); + + ret = fwrite(buf, len, 1, out_fp); + if (!ret) { + fprintf(fp, "[%d] Cannot write file\n", cpu); + free(buf); + return FALSE; + } + } + + idx = last_idx; + page = pt_info_ptr->buffer_ptr[idx]; + len = pt_info_ptr->output_off & mask; + + if (!len) + goto done; + + if (!readmem(page, KVADDR, buf, len, "read page for write", + RETURN_ON_ERROR)) { + free(buf); + return FALSE; + } + + dbgprintf(fp, "[%d] R/W2 buff: p=0x%lx, i=%d, o=%lu, l=%d\n", cpu, + page, idx, (ulong)0, len); + + ret = fwrite(buf, len, 1, out_fp); + if (!ret) { + fprintf(fp, "[%d] Cannot write file\n", cpu); + free(buf); + return FALSE; + } + +done: + free(buf); + return TRUE; +} + +int write_pt_log_buffer_cpu(int cpu, char *fname) +{ + int wrapped, ret; + FILE *out_fp; + + wrapped = check_wrap_around(cpu); + + if ((out_fp = fopen(fname, "w")) == NULL) { + fprintf(fp, "[%d] Cannot open file: %s\n", cpu, fname); + return FALSE; + } + dbgprintf(fp, "[%d] Open file: %s\n", cpu, fname); + + /* + * Write buffer to file + * + * Case 1: Not wrapped around + * + * start end + * | | + * v v + * +------+ +------+ +------+ +------+ + * |buffer| |buffer| ... |buffer| |buffer| + * +------+ +------+ +------+ +------+ + * + * In this case, just write data between 'start' and 'end' + * + * Case 2: Wrapped around + * + * end start + * | | + * v v + * +------+ +------+ +------+ +------+ + * |buffer| |buffer| ... |buffer| |buffer| + * +------+ +------+ +------+ +------+ + * + * In this case, at first write data between 'start' and end of last + * buffer, and then write data between beginning of first buffer and + * 'end'. + */ + if (wrapped) { + dbgprintf(fp, "[%d] wrap around: true\n", cpu); + ret = write_buffer_wrapped(cpu, out_fp); + } else { + dbgprintf(fp, "[%d] wrap around: false\n", cpu); + ret = write_buffer_nowrapped(cpu, out_fp); + } + + fclose(out_fp); + return ret; +} + +void +cmd_ptdump(void) +{ + int i, ret, list_len; + int online_cpus; + char* outdir; + char dumpfile[16]; + char decodefile[16]; + mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | + S_IRGRP | S_IXGRP | + S_IROTH | S_IXOTH; /* 0755 */ + struct pt_info *pt_info_ptr; + + if (argcnt != 2) + cmd_usage(pc->curcmd, SYNOPSIS); + + if (ACTIVE()) + error(FATAL, "not supported on a live system\n"); + + outdir = args[1]; + if ((ret = mkdir(outdir, mode))) { + fprintf(fp, "Cannot create directory %s: %d\n", outdir, ret); + return; + } + + if ((ret = chdir(outdir))) { + fprintf(fp, "Cannot chdir %s: %d\n", outdir, ret); + return; + } + + /* + * Set the gdb scope to ensure that the appropriate ring_buffer + * structure is selected. + */ + if (kernel_symbol_exists("perf_mmap_to_page")) + gdb_set_crash_scope(symbol_value("perf_mmap_to_page"), + "perf_mmap_to_page"); + + online_cpus = get_cpus_online(); + list_len = sizeof(struct pt_info)*kt->cpus; + pt_info_list = malloc(list_len); + if (pt_info_list == NULL) { + fprintf(fp, "Cannot alloc pt_info_list\n"); + return; + } + memset(pt_info_list, 0, list_len); + + for (i = 0; online_cpus > 0; i++) { + if (!in_cpu_map(ONLINE_MAP, i)) + continue; + + online_cpus--; + + if (!init_pt_info(i)) + continue; + + sprintf(dumpfile, "dump.%d", i); + if (write_pt_log_buffer_cpu(i, dumpfile)) + fprintf(fp, "[%d] buffer dump: %s\n", i, dumpfile); + + sprintf(decodefile, "decode.%d", i); + if (fastdecode(dumpfile, decodefile)) + fprintf(fp, "[%d] packet decode: %s\n", i, decodefile); + + pt_info_ptr = pt_info_list + i; + if (pt_info_ptr->buffer_ptr != NULL) + free(pt_info_ptr->buffer_ptr); + } + free(pt_info_list); + chdir(".."); +} + +char *help_ptdump[] = { + "ptdump", + "Dump log buffer of Intel(R) Processor Trace", + "", + "This command extracts log buffer of PT to the directory", + "specified by ", + NULL +}; + +static struct command_table_entry command_table[] = { + { "ptdump", cmd_ptdump, help_ptdump, 0}, + { NULL }, +}; + +void __attribute__((constructor)) +ptdump_init(void) +{ + register_extension(command_table); +} + +void __attribute__((destructor)) +ptdump_fini(void) { } + diff --git a/ptdump.mk b/ptdump.mk new file mode 100644 index 0000000..7d3333f --- /dev/null +++ b/ptdump.mk @@ -0,0 +1,62 @@ +# +# Copyright (C) 2016 FUJITSU LIMITED +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# + +VERSION=1.0.7 +NAME=ptdump +ARCH=UNSUPPORTED + +ifeq ($(shell arch), x86_64) + TARGET=X86_64 + TARGET_CFLAGS= + ARCH=SUPPORTED +endif + +ifeq ($(shell /bin/ls /usr/include/crash/defs.h 2>/dev/null), /usr/include/crash/defs.h) + INCDIR=/usr/include/crash +endif +ifeq ($(shell /bin/ls ./defs.h 2> /dev/null), ./defs.h) + INCDIR=. +endif +ifeq ($(shell /bin/ls ../defs.h 2> /dev/null), ../defs.h) + INCDIR=.. +endif + +SUBDIR=ptdump +TARGET_CFILES=$(SUBDIR)/fastdecode.c $(SUBDIR)/map.c ptdump.c + +COMMON_CFLAGS=-Wall -I$(INCDIR) -fPIC -D$(TARGET) + +all: ptdump.so + +ptdump.so: $(TARGET_CFILES) $(INCDIR)/defs.h $(SUBDIR)/map.h +ifeq ($(ARCH),UNSUPPORTED) + @echo "ptdump: architecture not supported" +else + gcc $(RPM_OPT_FLAGS) $(CFLAGS) $(TARGET_CFLAGS) $(COMMON_CFLAGS) -nostartfiles -shared -rdynamic -o $@ $(TARGET_CFILES) +endif + +debug: COMMON_CFLAGS+=-DDEBUG +debug: all + +create-archive: + @git archive --format=tar --prefix=$(NAME)-$(VERSION)/ HEAD | \ + gzip > ./$(NAME)-$(VERSION).tar.gz + +tag: + @git tag $(VERSION) + @echo tag:$(VERSION) + +clean: + rm -f *.so *.o $(SUBDIR)/*.so $(SUBDIR)/*.o + diff --git a/ptdump/fastdecode.c b/ptdump/fastdecode.c new file mode 100644 index 0000000..b26b769 --- /dev/null +++ b/ptdump/fastdecode.c @@ -0,0 +1,382 @@ +/* + * This is modified version of Simple PT dumper by Andi Kleen + * https://github.com/andikleen/simple-pt + */ + +/* + * Copyright (c) 2015, Intel Corporation + * Author: Andi Kleen + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#define _GNU_SOURCE 1 +#include +#include +#include +#include +#include +#include +#include +#include + +#include "defs.h" +#include "map.h" + +#define BIT(x) (1U << (x)) + +typedef unsigned long long u64; + +static FILE *out_fp; + +static char psb[16] = { + 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, + 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82 +}; + +#define LEFT(x) ((end - p) >= (x)) + +/* Caller must have checked length */ +static u64 get_ip_val(unsigned char **pp, unsigned char *end, int len, uint64_t *last_ip) +{ + unsigned char *p = *pp; + u64 v = *last_ip; + int i; + unsigned shift = 0; + + if (len == 0) { + *last_ip = 0; + return 0; /* out of context */ + } + if (len < 4) { + if (!LEFT(len)) { + *last_ip = 0; + return 0; /* XXX error */ + } + for (i = 0; i < len; i++, shift += 16, p += 2) { + uint64_t b = *(uint16_t *)p; + v = (v & ~(0xffffULL << shift)) | (b << shift); + } + v = ((int64_t)(v << (64 - 48))) >> (64 - 48); /* sign extension */ + } else { + return 0; /* XXX error */ + } + *pp = p; + *last_ip = v; + return v; +} + +static char *get_ip_symbol(u64 ip, char *buf, int bufsize) +{ + struct syment *sm; + ulong offset; + + sm = value_search(ip, &offset); + + if (sm) { + if (!offset) + sprintf(buf, "%s", sm->name); + else + sprintf(buf, "%s+%lu", sm->name, offset); + + return buf; + } + + return NULL; +} + +/* Caller must have checked length */ +static u64 get_val(unsigned char **pp, int len) +{ + unsigned char *p = *pp; + u64 v = 0; + int i; + unsigned shift = 0; + + for (i = 0; i < len; i++, shift += 8) + v |= ((uint64_t)(*p++)) << shift; + *pp = p; + return v; +} + +static void print_unknown(unsigned char *p, unsigned char *end, unsigned char *map) +{ + fprintf(out_fp, "unknown packet: "); + unsigned len = end - p; + int i; + if (len > 16) + len = 16; + for (i = 0; i < len; i++) + fprintf(out_fp, "%02x ", p[i]); + fprintf(out_fp, "\n"); +} + +static void print_tnt_byte(unsigned char v, int max) +{ + int i; + for (i = max - 1; i >= 0; i--) + if (v & BIT(i)) + fprintf(out_fp, "T"); + else + fprintf(out_fp, "N"); +} + +static void print_tnt_stop(unsigned char v) +{ + int j; + for (j = 7; j >= 0; j--) { + if (v & BIT(j)) + break; + } + print_tnt_byte(v, j); +} + +static void print_multi_tnt(unsigned char *p, int len) +{ + int i; + + for (i = len - 1; i >= 0 && p[i] == 0; i--) + ; + if (i >= 0) { + print_tnt_stop(p[i]); + i--; + } else { + fprintf(out_fp, "??? no stop bit"); + return; + } + for (; i >= 0; i--) + print_tnt_byte(p[i], 8); +} + +void decode_buffer(unsigned char *map, size_t len) +{ + unsigned char *end = map + len; + unsigned char *p; + size_t skipped = 0; + size_t overflow = 0; + uint64_t last_ip = 0; + u64 ip; + char buf[64]; + + for (p = map; p < end; ) { + unsigned char *prev = p; + /* look for PSB */ + p = memmem(p, end - p, psb, 16); + if (!p) { + p = end; + break; + } + skipped += p - prev; + while (p < end) { + fprintf(out_fp, "%lx\t", p - map); + + if (*p == 2 && LEFT(2)) { + if (p[1] == 0xa3 && LEFT(8)) { /* long TNT */ + fprintf(out_fp, "tnt64 "); + print_multi_tnt(p + 2, 6); + fprintf(out_fp, "\n"); + p += 8; + continue; + } + if (p[1] == 0x43 && LEFT(8)) { /* PIP */ + p += 2; + fprintf(out_fp, "pip\t%llx\n", (get_val(&p, 6) >> 1) << 5); + continue; + } + if (p[1] == 3 && LEFT(4) && p[3] == 0) { /* CBR */ + fprintf(out_fp, "cbr\t%u\n", p[2]); + p += 4; + continue; + } + if (p[1] == 0b10000011) { + fprintf(out_fp, "tracestop\n"); + p += 2; + continue; + } + if (p[1] == 0b11110011 && LEFT(8)) { /* OVF */ + fprintf(out_fp, "ovf\n"); + p += 8; + overflow++; + continue; + } + if (p[1] == 0x82 && LEFT(16) && !memcmp(p, psb, 16)) { /* PSB */ + fprintf(out_fp, "psb\n"); + p += 16; + continue; + } + if (p[1] == 0b100011) { /* PSBEND */ + fprintf(out_fp, "psbend\n"); + p += 2; + continue; + } + /* MNT */ + if (p[1] == 0b11000011 && LEFT(11) && p[2] == 0b10001000) { + fprintf(out_fp, "mnt\t%llx\n", + p[3] | + ((u64)p[4] << 8) | + ((u64)p[5] << 16) | + ((u64)p[6] << 24) | + ((u64)p[7] << 32) | + ((u64)p[8] << 40) | + ((u64)p[9] << 48) | + ((u64)p[10] << 56)); + p += 10; + continue; + } + /* TMA */ + if (p[1] == 0b01110011 && LEFT(7)) { + fprintf(out_fp, "tma\tctc=%u fc=%u\n", + p[2] | (p[3] << 8), + p[5] | ((p[6] & 1) << 8)); + p += 7; + continue; + } + /* VMCS */ + if (p[1] == 0b11001000 && LEFT(7)) { + fprintf(out_fp, "vmcs\t%llx\n", + ((u64)p[2] << 12) | + ((u64)p[3] << 20) | + ((u64)p[4] << 28) | + ((u64)p[5] << 36) | + ((u64)p[6] << 44)); + continue; + } + } + + if ((*p & BIT(0)) == 0) { + if (*p == 0) { /* PAD */ + fprintf(out_fp, "pad\n"); + p++; + continue; + } + fprintf(out_fp, "tnt8 "); + print_tnt_stop(*p >> 1); + fprintf(out_fp, "\n"); + p++; + continue; + } + + char *name = NULL; + switch (*p & 0x1f) { + case 0xd: + name = "tip"; + break; + case 0x11: + name = "tip.pge"; + break; + case 0x1: + name = "tip.pgd"; + break; + case 0x1d: + name = "fup"; + break; + } + if (name) { + int ipl = *p >> 5; + p++; + ip = get_ip_val(&p, end, ipl, &last_ip); + fprintf(out_fp, "%s\t%d: %llx", name, ipl, ip); + if (get_ip_symbol(ip, buf, sizeof(buf))) + fprintf(out_fp, " <%s>\n", buf); + else + fprintf(out_fp, "\n"); + continue; + } + if (*p == 0x99 && LEFT(2)) { /* MODE */ + if ((p[1] >> 5) == 1) { + fprintf(out_fp, "mode.tsx"); + if (p[1] & BIT(0)) + fprintf(out_fp, " intx"); + if (p[1] & BIT(1)) + fprintf(out_fp, " txabort"); + fprintf(out_fp, "\n"); + p += 2; + continue; + } else if ((p[1] >> 5) == 0) { + fprintf(out_fp, "mode.exec"); + fprintf(out_fp, " lma=%d", (p[1] & BIT(0))); + fprintf(out_fp, " cs.d=%d", !!(p[1] & BIT(1))); + fprintf(out_fp, "\n"); + p += 2; + continue; + } + } + + if (*p == 0x19 && LEFT(8)) { /* TSC */ + p++; + fprintf(out_fp, "tsc\t%llu\n", get_val(&p, 7)); + continue; + } + if (*p == 0b01011001 && LEFT(2)) { /* MTC */ + fprintf(out_fp, "mtc\t%u\n", p[1]); + p += 2; + continue; + } + if ((*p & 3) == 3) { /* CYC */ + u64 cyc = *p >> 2; + unsigned shift = 4; + if ((*p & 4) && LEFT(1)) { + do { + p++; + cyc |= (*p >> 1) << shift; + shift += 7; + } while ((*p & 1) && LEFT(1)); + } + fprintf(out_fp, "cyc\t%llu\n", cyc); + p++; + continue; + } + + print_unknown(p, end, map); + break; + } + } + if (p < end || skipped) + fprintf(out_fp, "%lu bytes undecoded\n", (end - p) + skipped); + if (overflow) + fprintf(out_fp, "%lu overflows\n", overflow); +} + +int fastdecode(char *in, char *out) +{ + size_t len; + unsigned char *map = mapfile(in, &len); + if (!map) { + fprintf(fp, "Cannot map file: %s\n", in); + return FALSE; + } + + if ((out_fp = fopen(out, "w")) == NULL) { + fprintf(fp, "Cannot open file: %s\n", out); + return FALSE; + } + + decode_buffer(map, len); + unmapfile(map, len); + fclose(out_fp); + + return TRUE; +} + diff --git a/ptdump/map.c b/ptdump/map.c new file mode 100644 index 0000000..52211ca --- /dev/null +++ b/ptdump/map.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +#define round_up(x, y) (((x) + (y) - 1) & ~((y) - 1)) + +static int pagesize; + +static void __attribute__((constructor)) init_ps(void) +{ + pagesize = sysconf(_SC_PAGESIZE); +} + +void *mapfile(char *fn, size_t *size) +{ + int fd = open(fn, O_RDWR); + if (fd < 0) + return NULL; + struct stat st; + void *map = (void *)-1L; + if (fstat(fd, &st) >= 0) { + *size = st.st_size; + map = mmap(NULL, round_up(st.st_size, pagesize), + PROT_READ|PROT_WRITE, + MAP_PRIVATE, fd, 0); + } + close(fd); + return map != (void *)-1L ? map : NULL; +} + +void unmapfile(void *map, size_t size) +{ + munmap(map, round_up(size, pagesize)); +} diff --git a/ptdump/map.h b/ptdump/map.h new file mode 100644 index 0000000..e4b6d10 --- /dev/null +++ b/ptdump/map.h @@ -0,0 +1,2 @@ +void *mapfile(char *fn, size_t *size); +void unmapfile(void *map, size_t size);