Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usdt_dof.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2012, Chris Andrews. All rights reserved.
  3. */
  4. #include "usdt_internal.h"
  5. int
  6. usdt_dof_section_add_data(usdt_dof_section_t *section, void *data, size_t length)
  7. {
  8. int newlen = section->size + length;
  9. if ((section->data = realloc((char *)section->data, newlen)) == NULL)
  10. return (-1);
  11. memcpy(section->data + section->size, data, length);
  12. section->size = newlen;
  13. return (0);
  14. }
  15. size_t
  16. usdt_provider_dof_size(usdt_provider_t *provider, usdt_strtab_t *strtab)
  17. {
  18. uint8_t i, j;
  19. int args = 0;
  20. int probes = 0;
  21. size_t size = 0;
  22. usdt_probedef_t *pd;
  23. size_t sections[8];
  24. for (pd = provider->probedefs; pd != NULL; pd = pd->next) {
  25. args += pd->argc;
  26. probes++;
  27. }
  28. sections[0] = sizeof(dof_hdr_t);
  29. sections[1] = sizeof(dof_sec_t) * 6;
  30. sections[2] = strtab->size;
  31. sections[3] = sizeof(dof_probe_t) * probes;
  32. sections[4] = sizeof(uint8_t) * args;
  33. sections[5] = sizeof(uint32_t) * probes;
  34. sections[6] = sizeof(uint32_t) * probes;
  35. sections[7] = sizeof(dof_provider_t);
  36. for (i = 0; i < 8; i++) {
  37. size += sections[i];
  38. j = size % 8;
  39. if (j > 0)
  40. size += (8 - j);
  41. }
  42. return size;
  43. }
  44. int
  45. usdt_dof_section_init(usdt_dof_section_t *section, uint32_t type, dof_secidx_t index)
  46. {
  47. section->type = type;
  48. section->index = index;
  49. section->flags = DOF_SECF_LOAD;
  50. section->offset = 0;
  51. section->size = 0;
  52. section->entsize = 0;
  53. section->pad = 0;
  54. section->next = NULL;
  55. if ((section->data = malloc(1)) == NULL)
  56. return (-1);
  57. switch(type) {
  58. case DOF_SECT_PROBES: section->align = 8; break;
  59. case DOF_SECT_PRARGS: section->align = 1; break;
  60. case DOF_SECT_PROFFS: section->align = 4; break;
  61. case DOF_SECT_PRENOFFS: section->align = 4; break;
  62. case DOF_SECT_PROVIDER: section->align = 4; break;
  63. }
  64. return (0);
  65. }
  66. void
  67. usdt_dof_section_free(usdt_dof_section_t *section)
  68. {
  69. free(section->data);
  70. }
  71. int
  72. usdt_strtab_init(usdt_strtab_t *strtab, dof_secidx_t index)
  73. {
  74. strtab->type = DOF_SECT_STRTAB;;
  75. strtab->index = index;
  76. strtab->flags = DOF_SECF_LOAD;
  77. strtab->offset = 0;
  78. strtab->size = 0;
  79. strtab->entsize = 0;
  80. strtab->pad = 0;
  81. strtab->data = NULL;
  82. strtab->align = 1;
  83. strtab->strindex = 1;
  84. if ((strtab->data = (char *) malloc(1)) == NULL)
  85. return (-1);
  86. *strtab->data = '\0';
  87. return (0);
  88. }
  89. dof_stridx_t
  90. usdt_strtab_add(usdt_strtab_t *strtab, const char *string)
  91. {
  92. size_t length;
  93. int index;
  94. length = strlen(string);
  95. index = strtab->strindex;
  96. strtab->strindex += (length + 1);
  97. if ((strtab->data = realloc(strtab->data, strtab->strindex)) == NULL)
  98. return (0);
  99. memcpy((char *) (strtab->data + index), (char *)string, length + 1);
  100. strtab->size = index + length + 1;
  101. return (index);
  102. }