debug.c 36 KB

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