debug.c 36 KB

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