printStackTrace.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2006 Michael Eddington
  3. * Copyright (c) 2001 Jani Kajala
  4. *
  5. * Permission to use, copy, modify, distribute and sell this
  6. * software and its documentation for any purpose is hereby
  7. * granted without fee, provided that the above copyright notice
  8. * appear in all copies and that both that copyright notice and
  9. * this permission notice appear in supporting documentation.
  10. * Jani Kajala makes no representations about the suitability
  11. * of this software for any purpose. It is provided "as is"
  12. * without express or implied warranty.
  13. */
  14. // $Id$
  15. #include "printStackTrace.h"
  16. #include "StackTrace.h"
  17. #include "MapFile.h"
  18. #include <stdio.h>
  19. #include <string.h>
  20. //-----------------------------------------------------------------------------
  21. using namespace dev;
  22. //-----------------------------------------------------------------------------
  23. // We will keep the parsed map file around
  24. // after we create it.
  25. static MapFile* map = NULL;
  26. /* Function: StackTraceCleanup
  27. *
  28. * Cleanup memory used by stacktrace library.
  29. *
  30. * See Also:
  31. *
  32. * <printStackTrace>
  33. */
  34. extern "C" void StackTraceCleanup()
  35. {
  36. if(map)
  37. delete map;
  38. map = NULL;
  39. }
  40. /* Function: printStackTrace
  41. *
  42. * Prints stack trace to user defined buffer.
  43. * Always terminates the buffer with 0.
  44. *
  45. * Must call <StackTraceCleanup> to free used memory.
  46. *
  47. * GitHub #81: Support for multiple map files in func def.
  48. * already supported by core code.
  49. *
  50. * Parameters:
  51. *
  52. * buffer - Buffer to recieve stacktrace, at least 600 bytes
  53. * bufferSize - Size of buffer
  54. * mapFilename - [Optional] Location of map file to use
  55. *
  56. * See Also:
  57. *
  58. * <StackTraceCleanup>
  59. */
  60. extern "C" void printStackTrace( char* buffer, int bufferSize, char* mapFilename )
  61. {
  62. if(map == NULL)
  63. {
  64. // find out map file name
  65. char modname[500];
  66. if(mapFilename == NULL)
  67. MapFile::getModuleMapFilename( modname, sizeof(modname) );
  68. else
  69. strncpy(modname, mapFilename, sizeof(modname)/sizeof(char));
  70. // parse map file
  71. map = new MapFile( modname );
  72. switch ( map->error() )
  73. {
  74. case MapFile::ERROR_OPEN: sprintf( buffer, "Failed to open map file %s\n", modname ); break;
  75. case MapFile::ERROR_READ: sprintf( buffer, "Error while reading map file %s(%i)\n", modname, map->line() ); break;
  76. case MapFile::ERROR_PARSE: sprintf( buffer, "Parse error in map file %s(%i)\n", modname, map->line() ); break;
  77. default: break;
  78. }
  79. }
  80. // print stack trace to buffer
  81. if ( !map->error() )
  82. {
  83. MapFile* maps[] = {map};
  84. StackTrace::printStackTrace( maps, 1, 1, 16, buffer, bufferSize);
  85. }
  86. }