为什么gdb显示/stdlib/strtol_l.c:没有这样的文件或目录?我是不是缺少了什么要安装的东西?

k5ifujac  于 2023-06-28  发布在  其他
关注(0)|答案(2)|浏览(424)

我尝试用-g编译,然后运行gdb来查找导致分段错误的行,但错误消息让我感到困惑。

  1. Program received signal SIGSEGV, Segmentation fault.
  2. __GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0, base=base@entry=10, group=group@entry=0, loc=0x7ffff7fb04a0 <_nl_global_locale>)
  3. at ../stdlib/strtol_l.c:292
  4. 292 ../stdlib/strtol_l.c: No such file or directory.

我尝试重新安装gdb以使其再次工作,但失败了。它仍然显示相同的错误消息。我后来自己发现了这个问题,并在下面的代码中标记了它。我只是好奇为什么有时候在调试字符串函数时会发生这样的事情?例如strdupstrtokstrtol等。我是不是缺少了什么要安装的东西?我希望我能彻底解决这个问题。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. char buff[255];
  6. #define NUM_BUCKETS 32
  7. typedef struct Customer {
  8. char* email;
  9. char* name;
  10. int shoesize;
  11. char* food;
  12. struct Customer* next;
  13. } Customer ;
  14. unsigned long hash(char *str) {
  15. unsigned long hash = 0;
  16. int c;
  17. while (*str != '\0') {
  18. c = *str;
  19. hash = ((hash << 5) + hash) + (unsigned char)c;
  20. str++;
  21. }
  22. return hash;
  23. }
  24. Customer *add_friend_to_list(char *email, char *name, int shoesize, char *food, Customer *bucket) {
  25. Customer* customer;
  26. customer = malloc(sizeof(Customer));
  27. customer->name = strdup(name);
  28. customer->food = strdup(food);
  29. customer->shoesize = shoesize;
  30. customer->email = strdup(email);
  31. customer->next = bucket;
  32. return customer;
  33. }
  34. void add_consumer_to_hashtable(char *name, char *food, char *email, int shoesize, Customer **buckets, size_t num_buckets) {
  35. size_t which_bucket = hash(name) % num_buckets;
  36. buckets[which_bucket] = add_friend_to_list(email, name, shoesize, food, buckets[which_bucket]);
  37. }
  38. int main() {
  39. Customer* buckets[NUM_BUCKETS] = {NULL};
  40. int ittime = 0;
  41. FILE *fp = NULL;
  42. fp = fopen("customers.tsv", "r");
  43. while (true) {
  44. fgets(buff, 255, fp);
  45. if (feof(fp)) {
  46. break;
  47. }
  48. ittime++;
  49. }
  50. fclose(fp);
  51. fp = NULL;
  52. char *email = (char *)malloc(5 * sizeof(char));
  53. char *name = (char *)malloc(5 * sizeof(char));
  54. int shoesize;
  55. char *food = (char *)malloc(5 * sizeof(char));
  56. const char s[2] = "\t";
  57. fp = fopen("customers.tsv", "r");
  58. for (int i = 0; i < ittime + 1; i++) { //This line cause the Segmentation Fault
  59. fgets(buff, 255, fp);
  60. char *token;
  61. token = strtok(buff, s);
  62. email = token;
  63. token = strtok(NULL, s);
  64. name = token;
  65. token = strtok(NULL, s);
  66. shoesize = atoi(token);
  67. token = strtok(NULL, s);
  68. food = token;
  69. add_consumer_to_hashtable(name, food, email, shoesize, buckets, NUM_BUCKETS);
  70. }
  71. fclose(fp);
  72. while (true) {
  73. char *cmd = (char *)malloc(5 * sizeof(char));
  74. printf("command: ");
  75. scanf("%s", cmd);
  76. if (strcmp(cmd, "add") == 0) {
  77. char *email1 = (char *)malloc(5 * sizeof(char));
  78. char *name1 = (char *)malloc(5 * sizeof(char));
  79. int shoesize1;
  80. char *food1 = (char *)malloc(5 * sizeof(char));
  81. printf("email address? ");
  82. scanf("%s", email1);
  83. printf("name? ");
  84. scanf(" %[^\n]", name1);
  85. printf("shoe size? ");
  86. scanf("%d", &shoesize1);
  87. printf("favorite food? ");
  88. scanf("%s", food1);
  89. add_consumer_to_hashtable(name1, food1, email1, shoesize1, buckets, NUM_BUCKETS);
  90. free(name1);
  91. free(food1);
  92. free(email1);
  93. } else if (strcmp(cmd, "lookup") == 0) {
  94. char *Email = (char *)malloc(5 * sizeof(char));
  95. printf("email address? ");
  96. scanf("%s", Email);
  97. bool exist = false;
  98. for (int i = 0; i < 32; i++) {
  99. Customer *cus = buckets[i];
  100. if (buckets[i] == NULL) {
  101. continue;
  102. }
  103. while ((cus != NULL)) {
  104. if (cus->shoesize == EOF) {
  105. break;
  106. }
  107. if (strcmp(cus->email, Email) == 0) {
  108. printf("email: %s\n", cus->email);
  109. printf("name: %s\n", cus->name);
  110. printf("shoesize: %d\n", cus->shoesize);
  111. printf("food: %s\n", cus->food);
  112. exist = true;
  113. break;
  114. }
  115. if (cus->next != NULL) {
  116. cus = cus->next;
  117. } else {
  118. break;
  119. }
  120. }
  121. }
  122. if (exist == false) {
  123. printf("user not found!\n");
  124. }
  125. } else if (strcmp(cmd, "delete") == 0) {
  126. char *Email = (char *)malloc(5 * sizeof(char));
  127. printf("email address? ");
  128. scanf("%s", Email);
  129. bool exist = false;
  130. for (int i = 0; i < 32; i++) {
  131. Customer *cus = buckets[i];
  132. if (buckets[i] == NULL) {
  133. continue;
  134. }
  135. while ((cus != NULL)) {
  136. if (cus->shoesize == EOF) {
  137. break;
  138. }
  139. if (strcmp(cus->email, Email) == 0) {
  140. free(cus->email);
  141. free(cus->food);
  142. free(cus->name);
  143. free(cus);
  144. cus->shoesize = EOF;
  145. cus = NULL;
  146. exist = true;
  147. break;
  148. }
  149. if (cus->next != NULL) {
  150. cus = cus->next;
  151. } else {
  152. break;
  153. }
  154. }
  155. }
  156. if (exist == false) {
  157. printf("user not found!\n");
  158. }
  159. } else if (strcmp(cmd, "list") == 0) {
  160. for (int i = 0; i < 32; i++) {
  161. Customer *cus = buckets[i];
  162. if (buckets[i] == NULL) {
  163. continue;
  164. }
  165. while ((cus != NULL) && ((cus->shoesize) != EOF)) {
  166. printf("email: %s\n", cus->email);
  167. printf("name: %s\n", cus->name);
  168. printf("shoesize: %d\n", cus->shoesize);
  169. printf("food: %s\n", cus->food);
  170. if (cus->next != NULL) {
  171. cus = cus->next;
  172. printf("\n");
  173. } else {
  174. break;
  175. }
  176. }
  177. }
  178. } else if (strcmp(cmd, "quit") == 0) {
  179. break;
  180. } else if (strcmp(cmd, "save") == 0) {
  181. fp = fopen("customers.tsv", "w");
  182. for (int i = 0; i < 32; i++) {
  183. Customer *cus = buckets[i];
  184. if (buckets[i] == NULL) {
  185. continue;
  186. }
  187. while ((cus != NULL) && ((cus->shoesize) != EOF)) {
  188. fprintf(fp, "%s\t%s\t%d\t%s", cus->email, cus->name, cus->shoesize, cus->food);
  189. if (cus->next != NULL) {
  190. cus = cus->next;
  191. fprintf(fp, "\n");
  192. } else {
  193. break;
  194. }
  195. }
  196. }
  197. fclose(fp);
  198. } else {
  199. printf("unknown command\n");
  200. }
  201. }
  202. for (int i = 0; i < 32; i++) {
  203. Customer *tmp;
  204. Customer *cus = buckets[i];
  205. if (cus == NULL) {
  206. continue;
  207. }
  208. if (cus->next != NULL) {
  209. tmp = cus;
  210. cus = cus->next;
  211. } else {
  212. break;
  213. }
  214. while ((tmp != NULL)) {
  215. if (tmp->shoesize != EOF) {
  216. free(tmp->email);
  217. free(tmp->food);
  218. free(tmp->name);
  219. free(tmp);
  220. }
  221. cus->shoesize = EOF;
  222. cus = NULL;
  223. }
  224. if (tmp != NULL) {
  225. free(tmp);
  226. }
  227. if (cus != NULL) {
  228. free(cus);
  229. }
  230. }
  231. return 0;
  232. }
mkh04yzy

mkh04yzy1#

我尝试用-g编译,然后运行gdb来查找导致分段错误的行,但错误消息让我感到困惑。
错误消息表示:

  • GLIBC strtol_l_internal()函数内部发生崩溃
  • GDB无法显示该函数的源代码,因为没有安装libc6-src(或类似的)包。

现在,查看strtol_l_internal()的源代码是没有帮助的--问题的根本原因是使用了不正确的参数调用它。
您应该读取man strtol并验证是否满足其先决条件。
看起来你调用了strtol(NULL, NULL, ...),这不是一个有效的操作。你可以使用(gdb) up命令来找出错误的调用来自哪里,并修复调用者。

nkoocmlb

nkoocmlb2#

我遇到了这个错误,结果是命令行参数没有被gdb传入。查找使用gdb调用程序时从命令行传入参数的规则。

相关问题