call_graph.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* call_graph.c - Create call graphs.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "gprof.h"
  17. #include "search_list.h"
  18. #include "source.h"
  19. #include "symtab.h"
  20. #include "cg_arcs.h"
  21. #include "call_graph.h"
  22. #include "corefile.h"
  23. #include "gmon_io.h"
  24. #include "gmon_out.h"
  25. #include "sym_ids.h"
  26. void
  27. cg_tally (bfd_vma from_pc, bfd_vma self_pc, unsigned long count)
  28. {
  29. Sym *parent;
  30. Sym *child;
  31. parent = sym_lookup (&symtab, from_pc);
  32. child = sym_lookup (&symtab, self_pc);
  33. if (child == NULL || parent == NULL)
  34. return;
  35. /* If we're doing line-by-line profiling, both the parent and the
  36. child will probably point to line symbols instead of function
  37. symbols. For the parent this is fine, since this identifies the
  38. line number in the calling routing, but the child should always
  39. point to a function entry point, so we back up in the symbol
  40. table until we find it.
  41. For normal profiling, is_func will be set on all symbols, so this
  42. code will do nothing. */
  43. while (child >= symtab.base && ! child->is_func)
  44. --child;
  45. if (child < symtab.base)
  46. return;
  47. /* Keep arc if it is on INCL_ARCS table or if the INCL_ARCS table
  48. is empty and it is not in the EXCL_ARCS table. */
  49. if (sym_id_arc_is_present (&syms[INCL_ARCS], parent, child)
  50. || (syms[INCL_ARCS].len == 0
  51. && !sym_id_arc_is_present (&syms[EXCL_ARCS], parent, child)))
  52. {
  53. child->ncalls += count;
  54. DBG (TALLYDEBUG,
  55. printf (_("[cg_tally] arc from %s to %s traversed %lu times\n"),
  56. parent->name, child->name, count));
  57. arc_add (parent, child, count);
  58. }
  59. }
  60. /* Read a record from file IFP describing an arc in the function
  61. call-graph and the count of how many times the arc has been
  62. traversed. FILENAME is the name of file IFP and is provided
  63. for formatting error-messages only. */
  64. void
  65. cg_read_rec (FILE *ifp, const char *filename)
  66. {
  67. bfd_vma from_pc, self_pc;
  68. unsigned int count;
  69. if (gmon_io_read_vma (ifp, &from_pc)
  70. || gmon_io_read_vma (ifp, &self_pc)
  71. || gmon_io_read_32 (ifp, &count))
  72. {
  73. fprintf (stderr, _("%s: %s: unexpected end of file\n"),
  74. whoami, filename);
  75. done (1);
  76. }
  77. DBG (SAMPLEDEBUG,
  78. printf ("[cg_read_rec] frompc 0x%lx selfpc 0x%lx count %lu\n",
  79. (unsigned long) from_pc, (unsigned long) self_pc,
  80. (unsigned long) count));
  81. /* Add this arc: */
  82. cg_tally (from_pc, self_pc, count);
  83. }
  84. /* Write all the arcs in the call-graph to file OFP. FILENAME is
  85. the name of OFP and is provided for formatting error-messages
  86. only. */
  87. void
  88. cg_write_arcs (FILE *ofp, const char *filename)
  89. {
  90. Arc *arc;
  91. Sym *sym;
  92. for (sym = symtab.base; sym < symtab.limit; sym++)
  93. {
  94. for (arc = sym->cg.children; arc; arc = arc->next_child)
  95. {
  96. if (gmon_io_write_8 (ofp, GMON_TAG_CG_ARC)
  97. || gmon_io_write_vma (ofp, arc->parent->addr)
  98. || gmon_io_write_vma (ofp, arc->child->addr)
  99. || gmon_io_write_32 (ofp, arc->count))
  100. {
  101. perror (filename);
  102. done (1);
  103. }
  104. DBG (SAMPLEDEBUG,
  105. printf ("[cg_write_arcs] frompc 0x%lx selfpc 0x%lx count %lu\n",
  106. (unsigned long) arc->parent->addr,
  107. (unsigned long) arc->child->addr, arc->count));
  108. }
  109. }
  110. }