debug.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * S/390 debug facility
  4. *
  5. * Copyright IBM Corp. 1999, 2012
  6. *
  7. * Author(s): Michael Holzheu (holzheu@de.ibm.com),
  8. * Holger Smolinski (Holger.Smolinski@de.ibm.com)
  9. *
  10. * Bugreports to: <Linux390@de.ibm.com>
  11. */
  12. #define KMSG_COMPONENT "s390dbf"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <linux/stddef.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/slab.h>
  18. #include <linux/ctype.h>
  19. #include <linux/string.h>
  20. #include <linux/sysctl.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/export.h>
  23. #include <linux/init.h>
  24. #include <linux/fs.h>
  25. #include <linux/debugfs.h>
  26. #include <asm/debug.h>
  27. #define DEBUG_PROLOG_ENTRY -1
  28. #define ALL_AREAS 0 /* copy all debug areas */
  29. #define NO_AREAS 1 /* copy no debug areas */
  30. /* typedefs */
  31. typedef struct file_private_info {
  32. loff_t offset; /* offset of last read in file */
  33. int act_area; /* number of last formated area */
  34. int act_page; /* act page in given area */
  35. int act_entry; /* last formated entry (offset */
  36. /* relative to beginning of last */
  37. /* formated page) */
  38. size_t act_entry_offset; /* up to this offset we copied */
  39. /* in last read the last formated */
  40. /* entry to userland */
  41. char temp_buf[2048]; /* buffer for output */
  42. debug_info_t *debug_info_org; /* original debug information */
  43. debug_info_t *debug_info_snap; /* snapshot of debug information */
  44. struct debug_view *view; /* used view of debug info */
  45. } file_private_info_t;
  46. typedef struct {
  47. char *string;
  48. /*
  49. * This assumes that all args are converted into longs
  50. * on L/390 this is the case for all types of parameter
  51. * except of floats, and long long (32 bit)
  52. *
  53. */
  54. long args[0];
  55. } debug_sprintf_entry_t;
  56. /* internal function prototyes */
  57. static int debug_init(void);
  58. static ssize_t debug_output(struct file *file, char __user *user_buf,
  59. size_t user_len, loff_t *offset);
  60. static ssize_t debug_input(struct file *file, const char __user *user_buf,
  61. size_t user_len, loff_t *offset);
  62. static int debug_open(struct inode *inode, struct file *file);
  63. static int debug_close(struct inode *inode, struct file *file);
  64. static debug_info_t *debug_info_create(const char *name, int pages_per_area,
  65. int nr_areas, int buf_size, umode_t mode);
  66. static void debug_info_get(debug_info_t *);
  67. static void debug_info_put(debug_info_t *);
  68. static int debug_prolog_level_fn(debug_info_t *id,
  69. struct debug_view *view, char *out_buf);
  70. static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
  71. struct file *file, const char __user *user_buf,
  72. size_t user_buf_size, loff_t *offset);
  73. static int debug_prolog_pages_fn(debug_info_t *id,
  74. struct debug_view *view, char *out_buf);
  75. static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
  76. struct file *file, const char __user *user_buf,
  77. size_t user_buf_size, loff_t *offset);
  78. static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
  79. struct file *file, const char __user *user_buf,
  80. size_t user_buf_size, loff_t *offset);
  81. static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
  82. char *out_buf, const char *in_buf);
  83. static int debug_raw_format_fn(debug_info_t *id,
  84. struct debug_view *view, char *out_buf,
  85. const char *in_buf);
  86. static int debug_raw_header_fn(debug_info_t *id, struct debug_view *view,
  87. int area, debug_entry_t *entry, char *out_buf);
  88. static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
  89. char *out_buf, debug_sprintf_entry_t *curr_event);
  90. /* globals */
  91. struct debug_view debug_raw_view = {
  92. "raw",
  93. NULL,
  94. &debug_raw_header_fn,
  95. &debug_raw_format_fn,
  96. NULL,
  97. NULL
  98. };
  99. EXPORT_SYMBOL(debug_raw_view);
  100. struct debug_view debug_hex_ascii_view = {
  101. "hex_ascii",
  102. NULL,
  103. &debug_dflt_header_fn,
  104. &debug_hex_ascii_format_fn,
  105. NULL,
  106. NULL
  107. };
  108. EXPORT_SYMBOL(debug_hex_ascii_view);
  109. static struct debug_view debug_level_view = {
  110. "level",
  111. &debug_prolog_level_fn,
  112. NULL,
  113. NULL,
  114. &debug_input_level_fn,
  115. NULL
  116. };
  117. static struct debug_view debug_pages_view = {
  118. "pages",
  119. &debug_prolog_pages_fn,
  120. NULL,
  121. NULL,
  122. &debug_input_pages_fn,
  123. NULL
  124. };
  125. static struct debug_view debug_flush_view = {
  126. "flush",
  127. NULL,
  128. NULL,
  129. NULL,
  130. &debug_input_flush_fn,
  131. NULL
  132. };
  133. struct debug_view debug_sprintf_view = {
  134. "sprintf",
  135. NULL,
  136. &debug_dflt_header_fn,
  137. (debug_format_proc_t *)&debug_sprintf_format_fn,
  138. NULL,
  139. NULL
  140. };
  141. EXPORT_SYMBOL(debug_sprintf_view);
  142. /* used by dump analysis tools to determine version of debug feature */
  143. static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
  144. /* static globals */
  145. static debug_info_t *debug_area_first;
  146. static debug_info_t *debug_area_last;
  147. static DEFINE_MUTEX(debug_mutex);
  148. static int initialized;
  149. static int debug_critical;
  150. static const struct file_operations debug_file_ops = {
  151. .owner = THIS_MODULE,
  152. .read = debug_output,
  153. .write = debug_input,
  154. .open = debug_open,
  155. .release = debug_close,
  156. .llseek = no_llseek,
  157. };
  158. static struct dentry *debug_debugfs_root_entry;
  159. /* functions */
  160. /*
  161. * debug_areas_alloc
  162. * - Debug areas are implemented as a threedimensonal array:
  163. * areas[areanumber][pagenumber][pageoffset]
  164. */
  165. static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas)
  166. {
  167. debug_entry_t ***areas;
  168. int i, j;
  169. areas = kmalloc_array(nr_areas, sizeof(debug_entry_t **), GFP_KERNEL);
  170. if (!areas)
  171. goto fail_malloc_areas;
  172. for (i = 0; i < nr_areas; i++) {
  173. areas[i] = kmalloc_array(pages_per_area,
  174. sizeof(debug_entry_t *),
  175. GFP_KERNEL);
  176. if (!areas[i])
  177. goto fail_malloc_areas2;
  178. for (j = 0; j < pages_per_area; j++) {
  179. areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
  180. if (!areas[i][j]) {
  181. for (j--; j >= 0 ; j--)
  182. kfree(areas[i][j]);
  183. kfree(areas[i]);
  184. goto fail_malloc_areas2;
  185. }
  186. }
  187. }
  188. return areas;
  189. fail_malloc_areas2:
  190. for (i--; i >= 0; i--) {
  191. for (j = 0; j < pages_per_area; j++)
  192. kfree(areas[i][j]);
  193. kfree(areas[i]);
  194. }
  195. kfree(areas);
  196. fail_malloc_areas:
  197. return NULL;
  198. }
  199. /*
  200. * debug_info_alloc
  201. * - alloc new debug-info
  202. */
  203. static debug_info_t *debug_info_alloc(const char *name, int pages_per_area,
  204. int nr_areas, int buf_size, int level,
  205. int mode)
  206. {
  207. debug_info_t *rc;
  208. /* alloc everything */
  209. rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
  210. if (!rc)
  211. goto fail_malloc_rc;
  212. rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
  213. if (!rc->active_entries)
  214. goto fail_malloc_active_entries;
  215. rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
  216. if (!rc->active_pages)
  217. goto fail_malloc_active_pages;
  218. if ((mode == ALL_AREAS) && (pages_per_area != 0)) {
  219. rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
  220. if (!rc->areas)
  221. goto fail_malloc_areas;
  222. } else {
  223. rc->areas = NULL;
  224. }
  225. /* initialize members */
  226. spin_lock_init(&rc->lock);
  227. rc->pages_per_area = pages_per_area;
  228. rc->nr_areas = nr_areas;
  229. rc->active_area = 0;
  230. rc->level = level;
  231. rc->buf_size = buf_size;
  232. rc->entry_size = sizeof(debug_entry_t) + buf_size;
  233. strlcpy(rc->name, name, sizeof(rc->name));
  234. memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
  235. memset(rc->debugfs_entries, 0, DEBUG_MAX_VIEWS * sizeof(struct dentry *));
  236. refcount_set(&(rc->ref_count), 0);
  237. return rc;
  238. fail_malloc_areas:
  239. kfree(rc->active_pages);
  240. fail_malloc_active_pages:
  241. kfree(rc->active_entries);
  242. fail_malloc_active_entries:
  243. kfree(rc);
  244. fail_malloc_rc:
  245. return NULL;
  246. }
  247. /*
  248. * debug_areas_free
  249. * - free all debug areas
  250. */
  251. static void debug_areas_free(debug_info_t *db_info)
  252. {
  253. int i, j;
  254. if (!db_info->areas)
  255. return;
  256. for (i = 0; i < db_info->nr_areas; i++) {
  257. for (j = 0; j < db_info->pages_per_area; j++)
  258. kfree(db_info->areas[i][j]);
  259. kfree(db_info->areas[i]);
  260. }
  261. kfree(db_info->areas);
  262. db_info->areas = NULL;
  263. }
  264. /*
  265. * debug_info_free
  266. * - free memory debug-info
  267. */
  268. static void debug_info_free(debug_info_t *db_info)
  269. {
  270. debug_areas_free(db_info);
  271. kfree(db_info->active_entries);
  272. kfree(db_info->active_pages);
  273. kfree(db_info);
  274. }
  275. /*
  276. * debug_info_create
  277. * - create new debug-info
  278. */
  279. static debug_info_t *debug_info_create(const char *name, int pages_per_area,
  280. int nr_areas, int buf_size, umode_t mode)
  281. {
  282. debug_info_t *rc;
  283. rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
  284. DEBUG_DEFAULT_LEVEL, ALL_AREAS);
  285. if (!rc)
  286. goto out;
  287. rc->mode = mode & ~S_IFMT;
  288. /* create root directory */
  289. rc->debugfs_root_entry = debugfs_create_dir(rc->name,
  290. debug_debugfs_root_entry);
  291. /* append new element to linked list */
  292. if (!debug_area_first) {
  293. /* first element in list */
  294. debug_area_first = rc;
  295. rc->prev = NULL;
  296. } else {
  297. /* append element to end of list */
  298. debug_area_last->next = rc;
  299. rc->prev = debug_area_last;
  300. }
  301. debug_area_last = rc;
  302. rc->next = NULL;
  303. refcount_set(&rc->ref_count, 1);
  304. out:
  305. return rc;
  306. }
  307. /*
  308. * debug_info_copy
  309. * - copy debug-info
  310. */
  311. static debug_info_t *debug_info_copy(debug_info_t *in, int mode)
  312. {
  313. unsigned long flags;
  314. debug_info_t *rc;
  315. int i, j;
  316. /* get a consistent copy of the debug areas */
  317. do {
  318. rc = debug_info_alloc(in->name, in->pages_per_area,
  319. in->nr_areas, in->buf_size, in->level, mode);
  320. spin_lock_irqsave(&in->lock, flags);
  321. if (!rc)
  322. goto out;
  323. /* has something changed in the meantime ? */
  324. if ((rc->pages_per_area == in->pages_per_area) &&
  325. (rc->nr_areas == in->nr_areas)) {
  326. break;
  327. }
  328. spin_unlock_irqrestore(&in->lock, flags);
  329. debug_info_free(rc);
  330. } while (1);
  331. if (mode == NO_AREAS)
  332. goto out;
  333. for (i = 0; i < in->nr_areas; i++) {
  334. for (j = 0; j < in->pages_per_area; j++)
  335. memcpy(rc->areas[i][j], in->areas[i][j], PAGE_SIZE);
  336. }
  337. out:
  338. spin_unlock_irqrestore(&in->lock, flags);
  339. return rc;
  340. }
  341. /*
  342. * debug_info_get
  343. * - increments reference count for debug-info
  344. */
  345. static void debug_info_get(debug_info_t *db_info)
  346. {
  347. if (db_info)
  348. refcount_inc(&db_info->ref_count);
  349. }
  350. /*
  351. * debug_info_put:
  352. * - decreases reference count for debug-info and frees it if necessary
  353. */
  354. static void debug_info_put(debug_info_t *db_info)
  355. {
  356. int i;
  357. if (!db_info)
  358. return;
  359. if (refcount_dec_and_test(&db_info->ref_count)) {
  360. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  361. if (!db_info->views[i])
  362. continue;
  363. debugfs_remove(db_info->debugfs_entries[i]);
  364. }
  365. debugfs_remove(db_info->debugfs_root_entry);
  366. if (db_info == debug_area_first)
  367. debug_area_first = db_info->next;
  368. if (db_info == debug_area_last)
  369. debug_area_last = db_info->prev;
  370. if (db_info->prev)
  371. db_info->prev->next = db_info->next;
  372. if (db_info->next)
  373. db_info->next->prev = db_info->prev;
  374. debug_info_free(db_info);
  375. }
  376. }
  377. /*
  378. * debug_format_entry:
  379. * - format one debug entry and return size of formated data
  380. */
  381. static int debug_format_entry(file_private_info_t *p_info)
  382. {
  383. debug_info_t *id_snap = p_info->debug_info_snap;
  384. struct debug_view *view = p_info->view;
  385. debug_entry_t *act_entry;
  386. size_t len = 0;
  387. if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
  388. /* print prolog */
  389. if (view->prolog_proc)
  390. len += view->prolog_proc(id_snap, view, p_info->temp_buf);
  391. goto out;
  392. }
  393. if (!id_snap->areas) /* this is true, if we have a prolog only view */
  394. goto out; /* or if 'pages_per_area' is 0 */
  395. act_entry = (debug_entry_t *) ((char *)id_snap->areas[p_info->act_area]
  396. [p_info->act_page] + p_info->act_entry);
  397. if (act_entry->id.stck == 0LL)
  398. goto out; /* empty entry */
  399. if (view->header_proc)
  400. len += view->header_proc(id_snap, view, p_info->act_area,
  401. act_entry, p_info->temp_buf + len);
  402. if (view->format_proc)
  403. len += view->format_proc(id_snap, view, p_info->temp_buf + len,
  404. DEBUG_DATA(act_entry));
  405. out:
  406. return len;
  407. }
  408. /*
  409. * debug_next_entry:
  410. * - goto next entry in p_info
  411. */
  412. static inline int debug_next_entry(file_private_info_t *p_info)
  413. {
  414. debug_info_t *id;
  415. id = p_info->debug_info_snap;
  416. if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
  417. p_info->act_entry = 0;
  418. p_info->act_page = 0;
  419. goto out;
  420. }
  421. if (!id->areas)
  422. return 1;
  423. p_info->act_entry += id->entry_size;
  424. /* switch to next page, if we reached the end of the page */
  425. if (p_info->act_entry > (PAGE_SIZE - id->entry_size)) {
  426. /* next page */
  427. p_info->act_entry = 0;
  428. p_info->act_page += 1;
  429. if ((p_info->act_page % id->pages_per_area) == 0) {
  430. /* next area */
  431. p_info->act_area++;
  432. p_info->act_page = 0;
  433. }
  434. if (p_info->act_area >= id->nr_areas)
  435. return 1;
  436. }
  437. out:
  438. return 0;
  439. }
  440. /*
  441. * debug_output:
  442. * - called for user read()
  443. * - copies formated debug entries to the user buffer
  444. */
  445. static ssize_t debug_output(struct file *file, /* file descriptor */
  446. char __user *user_buf, /* user buffer */
  447. size_t len, /* length of buffer */
  448. loff_t *offset) /* offset in the file */
  449. {
  450. size_t count = 0;
  451. size_t entry_offset;
  452. file_private_info_t *p_info;
  453. p_info = (file_private_info_t *) file->private_data;
  454. if (*offset != p_info->offset)
  455. return -EPIPE;
  456. if (p_info->act_area >= p_info->debug_info_snap->nr_areas)
  457. return 0;
  458. entry_offset = p_info->act_entry_offset;
  459. while (count < len) {
  460. int formatted_line_residue;
  461. int formatted_line_size;
  462. int user_buf_residue;
  463. size_t copy_size;
  464. formatted_line_size = debug_format_entry(p_info);
  465. formatted_line_residue = formatted_line_size - entry_offset;
  466. user_buf_residue = len-count;
  467. copy_size = min(user_buf_residue, formatted_line_residue);
  468. if (copy_size) {
  469. if (copy_to_user(user_buf + count, p_info->temp_buf
  470. + entry_offset, copy_size))
  471. return -EFAULT;
  472. count += copy_size;
  473. entry_offset += copy_size;
  474. }
  475. if (copy_size == formatted_line_residue) {
  476. entry_offset = 0;
  477. if (debug_next_entry(p_info))
  478. goto out;
  479. }
  480. }
  481. out:
  482. p_info->offset = *offset + count;
  483. p_info->act_entry_offset = entry_offset;
  484. *offset = p_info->offset;
  485. return count;
  486. }
  487. /*
  488. * debug_input:
  489. * - called for user write()
  490. * - calls input function of view
  491. */
  492. static ssize_t debug_input(struct file *file, const char __user *user_buf,
  493. size_t length, loff_t *offset)
  494. {
  495. file_private_info_t *p_info;
  496. int rc = 0;
  497. mutex_lock(&debug_mutex);
  498. p_info = ((file_private_info_t *) file->private_data);
  499. if (p_info->view->input_proc) {
  500. rc = p_info->view->input_proc(p_info->debug_info_org,
  501. p_info->view, file, user_buf,
  502. length, offset);
  503. } else {
  504. rc = -EPERM;
  505. }
  506. mutex_unlock(&debug_mutex);
  507. return rc; /* number of input characters */
  508. }
  509. /*
  510. * debug_open:
  511. * - called for user open()
  512. * - copies formated output to private_data area of the file
  513. * handle
  514. */
  515. static int debug_open(struct inode *inode, struct file *file)
  516. {
  517. debug_info_t *debug_info, *debug_info_snapshot;
  518. file_private_info_t *p_info;
  519. int i, rc = 0;
  520. mutex_lock(&debug_mutex);
  521. debug_info = file_inode(file)->i_private;
  522. /* find debug view */
  523. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  524. if (!debug_info->views[i])
  525. continue;
  526. else if (debug_info->debugfs_entries[i] == file->f_path.dentry)
  527. goto found; /* found view ! */
  528. }
  529. /* no entry found */
  530. rc = -EINVAL;
  531. goto out;
  532. found:
  533. /* Make snapshot of current debug areas to get it consistent. */
  534. /* To copy all the areas is only needed, if we have a view which */
  535. /* formats the debug areas. */
  536. if (!debug_info->views[i]->format_proc && !debug_info->views[i]->header_proc)
  537. debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
  538. else
  539. debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
  540. if (!debug_info_snapshot) {
  541. rc = -ENOMEM;
  542. goto out;
  543. }
  544. p_info = kmalloc(sizeof(file_private_info_t), GFP_KERNEL);
  545. if (!p_info) {
  546. debug_info_free(debug_info_snapshot);
  547. rc = -ENOMEM;
  548. goto out;
  549. }
  550. p_info->offset = 0;
  551. p_info->debug_info_snap = debug_info_snapshot;
  552. p_info->debug_info_org = debug_info;
  553. p_info->view = debug_info->views[i];
  554. p_info->act_area = 0;
  555. p_info->act_page = 0;
  556. p_info->act_entry = DEBUG_PROLOG_ENTRY;
  557. p_info->act_entry_offset = 0;
  558. file->private_data = p_info;
  559. debug_info_get(debug_info);
  560. nonseekable_open(inode, file);
  561. out:
  562. mutex_unlock(&debug_mutex);
  563. return rc;
  564. }
  565. /*
  566. * debug_close:
  567. * - called for user close()
  568. * - deletes private_data area of the file handle
  569. */
  570. static int debug_close(struct inode *inode, struct file *file)
  571. {
  572. file_private_info_t *p_info;
  573. p_info = (file_private_info_t *) file->private_data;
  574. if (p_info->debug_info_snap)
  575. debug_info_free(p_info->debug_info_snap);
  576. debug_info_put(p_info->debug_info_org);
  577. kfree(file->private_data);
  578. return 0; /* success */
  579. }
  580. /*
  581. * debug_register_mode:
  582. * - Creates and initializes debug area for the caller
  583. * The mode parameter allows to specify access rights for the s390dbf files
  584. * - Returns handle for debug area
  585. */
  586. debug_info_t *debug_register_mode(const char *name, int pages_per_area,
  587. int nr_areas, int buf_size, umode_t mode,
  588. uid_t uid, gid_t gid)
  589. {
  590. debug_info_t *rc = NULL;
  591. /* Since debugfs currently does not support uid/gid other than root, */
  592. /* we do not allow gid/uid != 0 until we get support for that. */
  593. if ((uid != 0) || (gid != 0))
  594. pr_warn("Root becomes the owner of all s390dbf files in sysfs\n");
  595. BUG_ON(!initialized);
  596. mutex_lock(&debug_mutex);
  597. /* create new debug_info */
  598. rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
  599. if (!rc)
  600. goto out;
  601. debug_register_view(rc, &debug_level_view);
  602. debug_register_view(rc, &debug_flush_view);
  603. debug_register_view(rc, &debug_pages_view);
  604. out:
  605. if (!rc)
  606. pr_err("Registering debug feature %s failed\n", name);
  607. mutex_unlock(&debug_mutex);
  608. return rc;
  609. }
  610. EXPORT_SYMBOL(debug_register_mode);
  611. /*
  612. * debug_register:
  613. * - creates and initializes debug area for the caller
  614. * - returns handle for debug area
  615. */
  616. debug_info_t *debug_register(const char *name, int pages_per_area,
  617. int nr_areas, int buf_size)
  618. {
  619. return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
  620. S_IRUSR | S_IWUSR, 0, 0);
  621. }
  622. EXPORT_SYMBOL(debug_register);
  623. /*
  624. * debug_unregister:
  625. * - give back debug area
  626. */
  627. void debug_unregister(debug_info_t *id)
  628. {
  629. if (!id)
  630. return;
  631. mutex_lock(&debug_mutex);
  632. debug_info_put(id);
  633. mutex_unlock(&debug_mutex);
  634. }
  635. EXPORT_SYMBOL(debug_unregister);
  636. /*
  637. * debug_set_size:
  638. * - set area size (number of pages) and number of areas
  639. */
  640. static int debug_set_size(debug_info_t *id, int nr_areas, int pages_per_area)
  641. {
  642. debug_entry_t ***new_areas;
  643. unsigned long flags;
  644. int rc = 0;
  645. if (!id || (nr_areas <= 0) || (pages_per_area < 0))
  646. return -EINVAL;
  647. if (pages_per_area > 0) {
  648. new_areas = debug_areas_alloc(pages_per_area, nr_areas);
  649. if (!new_areas) {
  650. pr_info("Allocating memory for %i pages failed\n",
  651. pages_per_area);
  652. rc = -ENOMEM;
  653. goto out;
  654. }
  655. } else {
  656. new_areas = NULL;
  657. }
  658. spin_lock_irqsave(&id->lock, flags);
  659. debug_areas_free(id);
  660. id->areas = new_areas;
  661. id->nr_areas = nr_areas;
  662. id->pages_per_area = pages_per_area;
  663. id->active_area = 0;
  664. memset(id->active_entries, 0, sizeof(int)*id->nr_areas);
  665. memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
  666. spin_unlock_irqrestore(&id->lock, flags);
  667. pr_info("%s: set new size (%i pages)\n", id->name, pages_per_area);
  668. out:
  669. return rc;
  670. }
  671. /*
  672. * debug_set_level:
  673. * - set actual debug level
  674. */
  675. void debug_set_level(debug_info_t *id, int new_level)
  676. {
  677. unsigned long flags;
  678. if (!id)
  679. return;
  680. spin_lock_irqsave(&id->lock, flags);
  681. if (new_level == DEBUG_OFF_LEVEL) {
  682. id->level = DEBUG_OFF_LEVEL;
  683. pr_info("%s: switched off\n", id->name);
  684. } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
  685. pr_info("%s: level %i is out of range (%i - %i)\n",
  686. id->name, new_level, 0, DEBUG_MAX_LEVEL);
  687. } else {
  688. id->level = new_level;
  689. }
  690. spin_unlock_irqrestore(&id->lock, flags);
  691. }
  692. EXPORT_SYMBOL(debug_set_level);
  693. /*
  694. * proceed_active_entry:
  695. * - set active entry to next in the ring buffer
  696. */
  697. static inline void proceed_active_entry(debug_info_t *id)
  698. {
  699. if ((id->active_entries[id->active_area] += id->entry_size)
  700. > (PAGE_SIZE - id->entry_size)) {
  701. id->active_entries[id->active_area] = 0;
  702. id->active_pages[id->active_area] =
  703. (id->active_pages[id->active_area] + 1) %
  704. id->pages_per_area;
  705. }
  706. }
  707. /*
  708. * proceed_active_area:
  709. * - set active area to next in the ring buffer
  710. */
  711. static inline void proceed_active_area(debug_info_t *id)
  712. {
  713. id->active_area++;
  714. id->active_area = id->active_area % id->nr_areas;
  715. }
  716. /*
  717. * get_active_entry:
  718. */
  719. static inline debug_entry_t *get_active_entry(debug_info_t *id)
  720. {
  721. return (debug_entry_t *) (((char *) id->areas[id->active_area]
  722. [id->active_pages[id->active_area]]) +
  723. id->active_entries[id->active_area]);
  724. }
  725. /*
  726. * debug_finish_entry:
  727. * - set timestamp, caller address, cpu number etc.
  728. */
  729. static inline void debug_finish_entry(debug_info_t *id, debug_entry_t *active,
  730. int level, int exception)
  731. {
  732. active->id.stck = get_tod_clock_fast() -
  733. *(unsigned long long *) &tod_clock_base[1];
  734. active->id.fields.cpuid = smp_processor_id();
  735. active->caller = __builtin_return_address(0);
  736. active->id.fields.exception = exception;
  737. active->id.fields.level = level;
  738. proceed_active_entry(id);
  739. if (exception)
  740. proceed_active_area(id);
  741. }
  742. static int debug_stoppable = 1;
  743. static int debug_active = 1;
  744. #define CTL_S390DBF_STOPPABLE 5678
  745. #define CTL_S390DBF_ACTIVE 5679
  746. /*
  747. * proc handler for the running debug_active sysctl
  748. * always allow read, allow write only if debug_stoppable is set or
  749. * if debug_active is already off
  750. */
  751. static int s390dbf_procactive(struct ctl_table *table, int write,
  752. void __user *buffer, size_t *lenp, loff_t *ppos)
  753. {
  754. if (!write || debug_stoppable || !debug_active)
  755. return proc_dointvec(table, write, buffer, lenp, ppos);
  756. else
  757. return 0;
  758. }
  759. static struct ctl_table s390dbf_table[] = {
  760. {
  761. .procname = "debug_stoppable",
  762. .data = &debug_stoppable,
  763. .maxlen = sizeof(int),
  764. .mode = S_IRUGO | S_IWUSR,
  765. .proc_handler = proc_dointvec,
  766. },
  767. {
  768. .procname = "debug_active",
  769. .data = &debug_active,
  770. .maxlen = sizeof(int),
  771. .mode = S_IRUGO | S_IWUSR,
  772. .proc_handler = s390dbf_procactive,
  773. },
  774. { }
  775. };
  776. static struct ctl_table s390dbf_dir_table[] = {
  777. {
  778. .procname = "s390dbf",
  779. .maxlen = 0,
  780. .mode = S_IRUGO | S_IXUGO,
  781. .child = s390dbf_table,
  782. },
  783. { }
  784. };
  785. static struct ctl_table_header *s390dbf_sysctl_header;
  786. void debug_stop_all(void)
  787. {
  788. if (debug_stoppable)
  789. debug_active = 0;
  790. }
  791. EXPORT_SYMBOL(debug_stop_all);
  792. void debug_set_critical(void)
  793. {
  794. debug_critical = 1;
  795. }
  796. /*
  797. * debug_event_common:
  798. * - write debug entry with given size
  799. */
  800. debug_entry_t *debug_event_common(debug_info_t *id, int level, const void *buf,
  801. int len)
  802. {
  803. debug_entry_t *active;
  804. unsigned long flags;
  805. if (!debug_active || !id->areas)
  806. return NULL;
  807. if (debug_critical) {
  808. if (!spin_trylock_irqsave(&id->lock, flags))
  809. return NULL;
  810. } else {
  811. spin_lock_irqsave(&id->lock, flags);
  812. }
  813. do {
  814. active = get_active_entry(id);
  815. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  816. if (len < id->buf_size)
  817. memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
  818. debug_finish_entry(id, active, level, 0);
  819. len -= id->buf_size;
  820. buf += id->buf_size;
  821. } while (len > 0);
  822. spin_unlock_irqrestore(&id->lock, flags);
  823. return active;
  824. }
  825. EXPORT_SYMBOL(debug_event_common);
  826. /*
  827. * debug_exception_common:
  828. * - write debug entry with given size and switch to next debug area
  829. */
  830. debug_entry_t *debug_exception_common(debug_info_t *id, int level,
  831. const void *buf, int len)
  832. {
  833. debug_entry_t *active;
  834. unsigned long flags;
  835. if (!debug_active || !id->areas)
  836. return NULL;
  837. if (debug_critical) {
  838. if (!spin_trylock_irqsave(&id->lock, flags))
  839. return NULL;
  840. } else {
  841. spin_lock_irqsave(&id->lock, flags);
  842. }
  843. do {
  844. active = get_active_entry(id);
  845. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  846. if (len < id->buf_size)
  847. memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
  848. debug_finish_entry(id, active, level, len <= id->buf_size);
  849. len -= id->buf_size;
  850. buf += id->buf_size;
  851. } while (len > 0);
  852. spin_unlock_irqrestore(&id->lock, flags);
  853. return active;
  854. }
  855. EXPORT_SYMBOL(debug_exception_common);
  856. /*
  857. * counts arguments in format string for sprintf view
  858. */
  859. static inline int debug_count_numargs(char *string)
  860. {
  861. int numargs = 0;
  862. while (*string) {
  863. if (*string++ == '%')
  864. numargs++;
  865. }
  866. return numargs;
  867. }
  868. /*
  869. * debug_sprintf_event:
  870. */
  871. debug_entry_t *__debug_sprintf_event(debug_info_t *id, int level, char *string, ...)
  872. {
  873. debug_sprintf_entry_t *curr_event;
  874. debug_entry_t *active;
  875. unsigned long flags;
  876. int numargs, idx;
  877. va_list ap;
  878. if (!debug_active || !id->areas)
  879. return NULL;
  880. numargs = debug_count_numargs(string);
  881. if (debug_critical) {
  882. if (!spin_trylock_irqsave(&id->lock, flags))
  883. return NULL;
  884. } else {
  885. spin_lock_irqsave(&id->lock, flags);
  886. }
  887. active = get_active_entry(id);
  888. curr_event = (debug_sprintf_entry_t *) DEBUG_DATA(active);
  889. va_start(ap, string);
  890. curr_event->string = string;
  891. for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
  892. curr_event->args[idx] = va_arg(ap, long);
  893. va_end(ap);
  894. debug_finish_entry(id, active, level, 0);
  895. spin_unlock_irqrestore(&id->lock, flags);
  896. return active;
  897. }
  898. EXPORT_SYMBOL(__debug_sprintf_event);
  899. /*
  900. * debug_sprintf_exception:
  901. */
  902. debug_entry_t *__debug_sprintf_exception(debug_info_t *id, int level, char *string, ...)
  903. {
  904. debug_sprintf_entry_t *curr_event;
  905. debug_entry_t *active;
  906. unsigned long flags;
  907. int numargs, idx;
  908. va_list ap;
  909. if (!debug_active || !id->areas)
  910. return NULL;
  911. numargs = debug_count_numargs(string);
  912. if (debug_critical) {
  913. if (!spin_trylock_irqsave(&id->lock, flags))
  914. return NULL;
  915. } else {
  916. spin_lock_irqsave(&id->lock, flags);
  917. }
  918. active = get_active_entry(id);
  919. curr_event = (debug_sprintf_entry_t *)DEBUG_DATA(active);
  920. va_start(ap, string);
  921. curr_event->string = string;
  922. for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
  923. curr_event->args[idx] = va_arg(ap, long);
  924. va_end(ap);
  925. debug_finish_entry(id, active, level, 1);
  926. spin_unlock_irqrestore(&id->lock, flags);
  927. return active;
  928. }
  929. EXPORT_SYMBOL(__debug_sprintf_exception);
  930. /*
  931. * debug_register_view:
  932. */
  933. int debug_register_view(debug_info_t *id, struct debug_view *view)
  934. {
  935. unsigned long flags;
  936. struct dentry *pde;
  937. umode_t mode;
  938. int rc = 0;
  939. int i;
  940. if (!id)
  941. goto out;
  942. mode = (id->mode | S_IFREG) & ~S_IXUGO;
  943. if (!(view->prolog_proc || view->format_proc || view->header_proc))
  944. mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
  945. if (!view->input_proc)
  946. mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
  947. pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
  948. id, &debug_file_ops);
  949. if (!pde) {
  950. pr_err("Registering view %s/%s failed due to out of "
  951. "memory\n", id->name, view->name);
  952. rc = -1;
  953. goto out;
  954. }
  955. spin_lock_irqsave(&id->lock, flags);
  956. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  957. if (!id->views[i])
  958. break;
  959. }
  960. if (i == DEBUG_MAX_VIEWS) {
  961. pr_err("Registering view %s/%s would exceed the maximum "
  962. "number of views %i\n", id->name, view->name, i);
  963. rc = -1;
  964. } else {
  965. id->views[i] = view;
  966. id->debugfs_entries[i] = pde;
  967. }
  968. spin_unlock_irqrestore(&id->lock, flags);
  969. if (rc)
  970. debugfs_remove(pde);
  971. out:
  972. return rc;
  973. }
  974. EXPORT_SYMBOL(debug_register_view);
  975. /*
  976. * debug_unregister_view:
  977. */
  978. int debug_unregister_view(debug_info_t *id, struct debug_view *view)
  979. {
  980. struct dentry *dentry = NULL;
  981. unsigned long flags;
  982. int i, rc = 0;
  983. if (!id)
  984. goto out;
  985. spin_lock_irqsave(&id->lock, flags);
  986. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  987. if (id->views[i] == view)
  988. break;
  989. }
  990. if (i == DEBUG_MAX_VIEWS) {
  991. rc = -1;
  992. } else {
  993. dentry = id->debugfs_entries[i];
  994. id->views[i] = NULL;
  995. id->debugfs_entries[i] = NULL;
  996. }
  997. spin_unlock_irqrestore(&id->lock, flags);
  998. debugfs_remove(dentry);
  999. out:
  1000. return rc;
  1001. }
  1002. EXPORT_SYMBOL(debug_unregister_view);
  1003. static inline char *debug_get_user_string(const char __user *user_buf,
  1004. size_t user_len)
  1005. {
  1006. char *buffer;
  1007. buffer = kmalloc(user_len + 1, GFP_KERNEL);
  1008. if (!buffer)
  1009. return ERR_PTR(-ENOMEM);
  1010. if (copy_from_user(buffer, user_buf, user_len) != 0) {
  1011. kfree(buffer);
  1012. return ERR_PTR(-EFAULT);
  1013. }
  1014. /* got the string, now strip linefeed. */
  1015. if (buffer[user_len - 1] == '\n')
  1016. buffer[user_len - 1] = 0;
  1017. else
  1018. buffer[user_len] = 0;
  1019. return buffer;
  1020. }
  1021. static inline int debug_get_uint(char *buf)
  1022. {
  1023. int rc;
  1024. buf = skip_spaces(buf);
  1025. rc = simple_strtoul(buf, &buf, 10);
  1026. if (*buf)
  1027. rc = -EINVAL;
  1028. return rc;
  1029. }
  1030. /*
  1031. * functions for debug-views
  1032. ***********************************
  1033. */
  1034. /*
  1035. * prints out actual debug level
  1036. */
  1037. static int debug_prolog_pages_fn(debug_info_t *id, struct debug_view *view,
  1038. char *out_buf)
  1039. {
  1040. return sprintf(out_buf, "%i\n", id->pages_per_area);
  1041. }
  1042. /*
  1043. * reads new size (number of pages per debug area)
  1044. */
  1045. static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
  1046. struct file *file, const char __user *user_buf,
  1047. size_t user_len, loff_t *offset)
  1048. {
  1049. int rc, new_pages;
  1050. char *str;
  1051. if (user_len > 0x10000)
  1052. user_len = 0x10000;
  1053. if (*offset != 0) {
  1054. rc = -EPIPE;
  1055. goto out;
  1056. }
  1057. str = debug_get_user_string(user_buf, user_len);
  1058. if (IS_ERR(str)) {
  1059. rc = PTR_ERR(str);
  1060. goto out;
  1061. }
  1062. new_pages = debug_get_uint(str);
  1063. if (new_pages < 0) {
  1064. rc = -EINVAL;
  1065. goto free_str;
  1066. }
  1067. rc = debug_set_size(id, id->nr_areas, new_pages);
  1068. if (rc != 0) {
  1069. rc = -EINVAL;
  1070. goto free_str;
  1071. }
  1072. rc = user_len;
  1073. free_str:
  1074. kfree(str);
  1075. out:
  1076. *offset += user_len;
  1077. return rc; /* number of input characters */
  1078. }
  1079. /*
  1080. * prints out actual debug level
  1081. */
  1082. static int debug_prolog_level_fn(debug_info_t *id, struct debug_view *view,
  1083. char *out_buf)
  1084. {
  1085. int rc = 0;
  1086. if (id->level == DEBUG_OFF_LEVEL)
  1087. rc = sprintf(out_buf, "-\n");
  1088. else
  1089. rc = sprintf(out_buf, "%i\n", id->level);
  1090. return rc;
  1091. }
  1092. /*
  1093. * reads new debug level
  1094. */
  1095. static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
  1096. struct file *file, const char __user *user_buf,
  1097. size_t user_len, loff_t *offset)
  1098. {
  1099. int rc, new_level;
  1100. char *str;
  1101. if (user_len > 0x10000)
  1102. user_len = 0x10000;
  1103. if (*offset != 0) {
  1104. rc = -EPIPE;
  1105. goto out;
  1106. }
  1107. str = debug_get_user_string(user_buf, user_len);
  1108. if (IS_ERR(str)) {
  1109. rc = PTR_ERR(str);
  1110. goto out;
  1111. }
  1112. if (str[0] == '-') {
  1113. debug_set_level(id, DEBUG_OFF_LEVEL);
  1114. rc = user_len;
  1115. goto free_str;
  1116. } else {
  1117. new_level = debug_get_uint(str);
  1118. }
  1119. if (new_level < 0) {
  1120. pr_warn("%s is not a valid level for a debug feature\n", str);
  1121. rc = -EINVAL;
  1122. } else {
  1123. debug_set_level(id, new_level);
  1124. rc = user_len;
  1125. }
  1126. free_str:
  1127. kfree(str);
  1128. out:
  1129. *offset += user_len;
  1130. return rc; /* number of input characters */
  1131. }
  1132. /*
  1133. * flushes debug areas
  1134. */
  1135. static void debug_flush(debug_info_t *id, int area)
  1136. {
  1137. unsigned long flags;
  1138. int i, j;
  1139. if (!id || !id->areas)
  1140. return;
  1141. spin_lock_irqsave(&id->lock, flags);
  1142. if (area == DEBUG_FLUSH_ALL) {
  1143. id->active_area = 0;
  1144. memset(id->active_entries, 0, id->nr_areas * sizeof(int));
  1145. for (i = 0; i < id->nr_areas; i++) {
  1146. id->active_pages[i] = 0;
  1147. for (j = 0; j < id->pages_per_area; j++)
  1148. memset(id->areas[i][j], 0, PAGE_SIZE);
  1149. }
  1150. } else if (area >= 0 && area < id->nr_areas) {
  1151. id->active_entries[area] = 0;
  1152. id->active_pages[area] = 0;
  1153. for (i = 0; i < id->pages_per_area; i++)
  1154. memset(id->areas[area][i], 0, PAGE_SIZE);
  1155. }
  1156. spin_unlock_irqrestore(&id->lock, flags);
  1157. }
  1158. /*
  1159. * view function: flushes debug areas
  1160. */
  1161. static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
  1162. struct file *file, const char __user *user_buf,
  1163. size_t user_len, loff_t *offset)
  1164. {
  1165. char input_buf[1];
  1166. int rc = user_len;
  1167. if (user_len > 0x10000)
  1168. user_len = 0x10000;
  1169. if (*offset != 0) {
  1170. rc = -EPIPE;
  1171. goto out;
  1172. }
  1173. if (copy_from_user(input_buf, user_buf, 1)) {
  1174. rc = -EFAULT;
  1175. goto out;
  1176. }
  1177. if (input_buf[0] == '-') {
  1178. debug_flush(id, DEBUG_FLUSH_ALL);
  1179. goto out;
  1180. }
  1181. if (isdigit(input_buf[0])) {
  1182. int area = ((int) input_buf[0] - (int) '0');
  1183. debug_flush(id, area);
  1184. goto out;
  1185. }
  1186. pr_info("Flushing debug data failed because %c is not a valid "
  1187. "area\n", input_buf[0]);
  1188. out:
  1189. *offset += user_len;
  1190. return rc; /* number of input characters */
  1191. }
  1192. /*
  1193. * prints debug header in raw format
  1194. */
  1195. static int debug_raw_header_fn(debug_info_t *id, struct debug_view *view,
  1196. int area, debug_entry_t *entry, char *out_buf)
  1197. {
  1198. int rc;
  1199. rc = sizeof(debug_entry_t);
  1200. memcpy(out_buf, entry, sizeof(debug_entry_t));
  1201. return rc;
  1202. }
  1203. /*
  1204. * prints debug data in raw format
  1205. */
  1206. static int debug_raw_format_fn(debug_info_t *id, struct debug_view *view,
  1207. char *out_buf, const char *in_buf)
  1208. {
  1209. int rc;
  1210. rc = id->buf_size;
  1211. memcpy(out_buf, in_buf, id->buf_size);
  1212. return rc;
  1213. }
  1214. /*
  1215. * prints debug data in hex/ascii format
  1216. */
  1217. static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
  1218. char *out_buf, const char *in_buf)
  1219. {
  1220. int i, rc = 0;
  1221. for (i = 0; i < id->buf_size; i++)
  1222. rc += sprintf(out_buf + rc, "%02x ", ((unsigned char *) in_buf)[i]);
  1223. rc += sprintf(out_buf + rc, "| ");
  1224. for (i = 0; i < id->buf_size; i++) {
  1225. unsigned char c = in_buf[i];
  1226. if (isascii(c) && isprint(c))
  1227. rc += sprintf(out_buf + rc, "%c", c);
  1228. else
  1229. rc += sprintf(out_buf + rc, ".");
  1230. }
  1231. rc += sprintf(out_buf + rc, "\n");
  1232. return rc;
  1233. }
  1234. /*
  1235. * prints header for debug entry
  1236. */
  1237. int debug_dflt_header_fn(debug_info_t *id, struct debug_view *view,
  1238. int area, debug_entry_t *entry, char *out_buf)
  1239. {
  1240. unsigned long base, sec, usec;
  1241. unsigned long caller;
  1242. unsigned int level;
  1243. char *except_str;
  1244. int rc = 0;
  1245. level = entry->id.fields.level;
  1246. base = (*(unsigned long *) &tod_clock_base[0]) >> 4;
  1247. sec = (entry->id.stck >> 12) + base - (TOD_UNIX_EPOCH >> 12);
  1248. usec = do_div(sec, USEC_PER_SEC);
  1249. if (entry->id.fields.exception)
  1250. except_str = "*";
  1251. else
  1252. except_str = "-";
  1253. caller = (unsigned long) entry->caller;
  1254. rc += sprintf(out_buf, "%02i %011ld:%06lu %1u %1s %02i %pK ",
  1255. area, sec, usec, level, except_str,
  1256. entry->id.fields.cpuid, (void *)caller);
  1257. return rc;
  1258. }
  1259. EXPORT_SYMBOL(debug_dflt_header_fn);
  1260. /*
  1261. * prints debug data sprintf-formated:
  1262. * debug_sprinf_event/exception calls must be used together with this view
  1263. */
  1264. #define DEBUG_SPRINTF_MAX_ARGS 10
  1265. static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
  1266. char *out_buf, debug_sprintf_entry_t *curr_event)
  1267. {
  1268. int num_longs, num_used_args = 0, i, rc = 0;
  1269. int index[DEBUG_SPRINTF_MAX_ARGS];
  1270. /* count of longs fit into one entry */
  1271. num_longs = id->buf_size / sizeof(long);
  1272. if (num_longs < 1)
  1273. goto out; /* bufsize of entry too small */
  1274. if (num_longs == 1) {
  1275. /* no args, we use only the string */
  1276. strcpy(out_buf, curr_event->string);
  1277. rc = strlen(curr_event->string);
  1278. goto out;
  1279. }
  1280. /* number of arguments used for sprintf (without the format string) */
  1281. num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
  1282. memset(index, 0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
  1283. for (i = 0; i < num_used_args; i++)
  1284. index[i] = i;
  1285. rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
  1286. curr_event->args[index[1]], curr_event->args[index[2]],
  1287. curr_event->args[index[3]], curr_event->args[index[4]],
  1288. curr_event->args[index[5]], curr_event->args[index[6]],
  1289. curr_event->args[index[7]], curr_event->args[index[8]],
  1290. curr_event->args[index[9]]);
  1291. out:
  1292. return rc;
  1293. }
  1294. /*
  1295. * debug_init:
  1296. * - is called exactly once to initialize the debug feature
  1297. */
  1298. static int __init debug_init(void)
  1299. {
  1300. s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
  1301. mutex_lock(&debug_mutex);
  1302. debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT, NULL);
  1303. initialized = 1;
  1304. mutex_unlock(&debug_mutex);
  1305. return 0;
  1306. }
  1307. postcore_initcall(debug_init);