debug.c 35 KB

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